parser.cpp
Go to the documentation of this file.
1 #include "yaml-cpp-pm/parser.h"
4 #include "yaml-cpp-pm/node.h"
5 #include "directives.h"
6 #include "nodebuilder.h"
7 #include "scanner.h"
8 #include "singledocparser.h"
9 #include "tag.h"
10 #include "token.h"
11 #include <sstream>
12 #include <cstdio>
13 
14 namespace YAML_PM
15 {
17  {
18  }
19 
20  Parser::Parser(std::istream& in)
21  {
22  Load(in);
23  }
24 
26  {
27  }
28 
29  Parser::operator bool() const
30  {
31  return m_pScanner.get() && !m_pScanner->empty();
32  }
33 
34  void Parser::Load(std::istream& in)
35  {
36  m_pScanner.reset(new Scanner(in));
37  m_pDirectives.reset(new Directives);
38  }
39 
40  // HandleNextDocument
41  // . Handles the next document
42  // . Throws a ParserException on error.
43  // . Returns false if there are no more documents
45  {
46  if(!m_pScanner.get())
47  return false;
48 
50  if(m_pScanner->empty())
51  return false;
52 
54  sdp.HandleDocument(eventHandler);
55  return true;
56  }
57 
58  // GetNextDocument
59  // . Reads the next document in the queue (of tokens).
60  // . Throws a ParserException on error.
61  bool Parser::GetNextDocument(Node& document)
62  {
63  NodeBuilder builder(document);
64  return HandleNextDocument(builder);
65  }
66 
67  // ParseDirectives
68  // . Reads any directives that are next in the queue.
70  {
71  bool readDirective = false;
72 
73  while(1) {
74  if(m_pScanner->empty())
75  break;
76 
77  Token& token = m_pScanner->peek();
78  if(token.type != Token::DIRECTIVE)
79  break;
80 
81  // we keep the directives from the last document if none are specified;
82  // but if any directives are specific, then we reset them
83  if(!readDirective)
84  m_pDirectives.reset(new Directives);
85 
86  readDirective = true;
87  HandleDirective(token);
88  m_pScanner->pop();
89  }
90  }
91 
92  void Parser::HandleDirective(const Token& token)
93  {
94  if(token.value == "YAML")
95  HandleYamlDirective(token);
96  else if(token.value == "TAG")
97  HandleTagDirective(token);
98  }
99 
100  // HandleYamlDirective
101  // . Should be of the form 'major.minor' (like a version number)
103  {
104  if(token.params.size() != 1)
106 
107  if(!m_pDirectives->version.isDefault)
109 
110  std::stringstream str(token.params[0]);
111  str >> m_pDirectives->version.major;
112  str.get();
113  str >> m_pDirectives->version.minor;
114  if(!str || str.peek() != EOF)
116 
117  if(m_pDirectives->version.major > 1)
119 
120  m_pDirectives->version.isDefault = false;
121  // TODO: warning on major == 1, minor > 2?
122  }
123 
124  // HandleTagDirective
125  // . Should be of the form 'handle prefix', where 'handle' is converted to 'prefix' in the file.
127  {
128  if(token.params.size() != 2)
130 
131  const std::string& handle = token.params[0];
132  const std::string& prefix = token.params[1];
133  if(m_pDirectives->tags.find(handle) != m_pDirectives->tags.end())
135 
136  m_pDirectives->tags[handle] = prefix;
137  }
138 
139  void Parser::PrintTokens(std::ostream& out)
140  {
141  if(!m_pScanner.get())
142  return;
143 
144  while(1) {
145  if(m_pScanner->empty())
146  break;
147 
148  out << m_pScanner->peek() << "\n";
149  m_pScanner->pop();
150  }
151  }
152 }
void HandleTagDirective(const Token &token)
Definition: parser.cpp:126
void ParseDirectives()
Definition: parser.cpp:69
::std::string string
Definition: gtest.h:1979
bool HandleNextDocument(EventHandler &eventHandler)
Definition: parser.cpp:44
void HandleDocument(EventHandler &eventHandler)
void Load(std::istream &in)
Definition: parser.cpp:34
void HandleDirective(const Token &token)
Definition: parser.cpp:92
const char *const YAML_DIRECTIVE_ARGS
Definition: exceptions.h:20
bool GetNextDocument(Node &document)
Definition: parser.cpp:61
const char *const REPEATED_TAG_DIRECTIVE
Definition: exceptions.h:25
const char *const YAML_VERSION
Definition: exceptions.h:21
Mark mark
Definition: token.h:78
const char *const YAML_MAJOR_VERSION
Definition: exceptions.h:22
std::auto_ptr< Scanner > m_pScanner
Definition: parser.h:46
const char *const REPEATED_YAML_DIRECTIVE
Definition: exceptions.h:23
std::auto_ptr< Directives > m_pDirectives
Definition: parser.h:47
std::vector< std::string > params
Definition: token.h:80
void PrintTokens(std::ostream &out)
Definition: parser.cpp:139
void HandleYamlDirective(const Token &token)
Definition: parser.cpp:102
std::string value
Definition: token.h:79
TYPE type
Definition: token.h:77
const char *const TAG_DIRECTIVE_ARGS
Definition: exceptions.h:24


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:38:03