Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 6c569e5

Browse files
committed
Update docs, formatting for Parser.
1 parent 148da47 commit 6c569e5

2 files changed

Lines changed: 65 additions & 26 deletions

File tree

include/yaml-cpp/parser.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,61 @@ class Scanner;
2020
struct Directives;
2121
struct Token;
2222

23+
/**
24+
* A parser turns a stream of bytes into one stream of "events" per YAML
25+
* document in the input stream.
26+
*/
2327
class YAML_CPP_API Parser : private noncopyable {
2428
public:
29+
/** Constructs an empty parser (with no input. */
2530
Parser();
26-
Parser(std::istream& in);
31+
32+
/**
33+
* Constructs a parser from the given input stream. The input stream must
34+
* live as long as the parser.
35+
*/
36+
explicit Parser(std::istream& in);
37+
2738
~Parser();
2839

29-
operator bool() const;
40+
/** Evaluates to true if the parser has some valid input to be read. */
41+
explicit operator bool() const;
3042

43+
/**
44+
* Resets the parser with the given input stream. Any existing state is
45+
* erased.
46+
*/
3147
void Load(std::istream& in);
48+
49+
/**
50+
* Handles the next document by calling events on the {@param eventHandler}.
51+
*
52+
* @throw a ParserException on error.
53+
* @return false if there are no more documents
54+
*/
3255
bool HandleNextDocument(EventHandler& eventHandler);
3356

3457
void PrintTokens(std::ostream& out);
3558

3659
private:
60+
/**
61+
* Reads any directives that are next in the queue, setting the internal
62+
* {@code m_pDirectives} state.
63+
*/
3764
void ParseDirectives();
65+
3866
void HandleDirective(const Token& token);
67+
68+
/**
69+
* Handles a "YAML" directive, which should be of the form 'major.minor' (like
70+
* a version number).
71+
*/
3972
void HandleYamlDirective(const Token& token);
73+
74+
/**
75+
* Handles a "TAG" directive, which should be of the form 'handle prefix',
76+
* where 'handle' is converted to 'prefix' in the file.
77+
*/
4078
void HandleTagDirective(const Token& token);
4179

4280
private:

src/parser.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,38 @@ void Parser::Load(std::istream& in) {
2626
m_pDirectives.reset(new Directives);
2727
}
2828

29-
// HandleNextDocument
30-
// . Handles the next document
31-
// . Throws a ParserException on error.
32-
// . Returns false if there are no more documents
3329
bool Parser::HandleNextDocument(EventHandler& eventHandler) {
3430
if (!m_pScanner.get())
3531
return false;
3632

3733
ParseDirectives();
38-
if (m_pScanner->empty())
34+
if (m_pScanner->empty()) {
3935
return false;
36+
}
4037

4138
SingleDocParser sdp(*m_pScanner, *m_pDirectives);
4239
sdp.HandleDocument(eventHandler);
4340
return true;
4441
}
4542

46-
// ParseDirectives
47-
// . Reads any directives that are next in the queue.
4843
void Parser::ParseDirectives() {
4944
bool readDirective = false;
5045

5146
while (1) {
52-
if (m_pScanner->empty())
47+
if (m_pScanner->empty()) {
5348
break;
49+
}
5450

5551
Token& token = m_pScanner->peek();
56-
if (token.type != Token::DIRECTIVE)
52+
if (token.type != Token::DIRECTIVE) {
5753
break;
54+
}
5855

5956
// we keep the directives from the last document if none are specified;
6057
// but if any directives are specific, then we reset them
61-
if (!readDirective)
58+
if (!readDirective) {
6259
m_pDirectives.reset(new Directives);
60+
}
6361

6462
readDirective = true;
6563
HandleDirective(token);
@@ -68,58 +66,61 @@ void Parser::ParseDirectives() {
6866
}
6967

7068
void Parser::HandleDirective(const Token& token) {
71-
if (token.value == "YAML")
69+
if (token.value == "YAML") {
7270
HandleYamlDirective(token);
73-
else if (token.value == "TAG")
71+
} else if (token.value == "TAG") {
7472
HandleTagDirective(token);
73+
}
7574
}
7675

77-
// HandleYamlDirective
78-
// . Should be of the form 'major.minor' (like a version number)
7976
void Parser::HandleYamlDirective(const Token& token) {
80-
if (token.params.size() != 1)
77+
if (token.params.size() != 1) {
8178
throw ParserException(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS);
79+
}
8280

83-
if (!m_pDirectives->version.isDefault)
81+
if (!m_pDirectives->version.isDefault) {
8482
throw ParserException(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE);
83+
}
8584

8685
std::stringstream str(token.params[0]);
8786
str >> m_pDirectives->version.major;
8887
str.get();
8988
str >> m_pDirectives->version.minor;
90-
if (!str || str.peek() != EOF)
89+
if (!str || str.peek() != EOF) {
9190
throw ParserException(
9291
token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]);
92+
}
9393

94-
if (m_pDirectives->version.major > 1)
94+
if (m_pDirectives->version.major > 1) {
9595
throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION);
96+
}
9697

9798
m_pDirectives->version.isDefault = false;
9899
// TODO: warning on major == 1, minor > 2?
99100
}
100101

101-
// HandleTagDirective
102-
// . Should be of the form 'handle prefix', where 'handle' is converted to
103-
// 'prefix' in the file.
104102
void Parser::HandleTagDirective(const Token& token) {
105103
if (token.params.size() != 2)
106104
throw ParserException(token.mark, ErrorMsg::TAG_DIRECTIVE_ARGS);
107105

108106
const std::string& handle = token.params[0];
109107
const std::string& prefix = token.params[1];
110-
if (m_pDirectives->tags.find(handle) != m_pDirectives->tags.end())
108+
if (m_pDirectives->tags.find(handle) != m_pDirectives->tags.end()) {
111109
throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE);
110+
}
112111

113112
m_pDirectives->tags[handle] = prefix;
114113
}
115114

116115
void Parser::PrintTokens(std::ostream& out) {
117-
if (!m_pScanner.get())
116+
if (!m_pScanner.get()) {
118117
return;
118+
}
119119

120120
while (1) {
121-
if (m_pScanner->empty())
121+
if (m_pScanner->empty()) {
122122
break;
123+
}
123124

124125
out << m_pScanner->peek() << "\n";
125126
m_pScanner->pop();

0 commit comments

Comments
 (0)