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 }
singledocparser.h
exceptions.h
YAML_PM::NodeBuilder
Definition: nodebuilder.h:18
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::Parser::Load
void Load(std::istream &in)
Definition: parser.cpp:34
YAML_PM::Token::mark
Mark mark
Definition: token.h:78
YAML_PM::ErrorMsg::REPEATED_YAML_DIRECTIVE
const char *const REPEATED_YAML_DIRECTIVE
Definition: exceptions.h:23
YAML_PM::Directives
Definition: directives.h:19
YAML_PM::EventHandler
Definition: eventhandler.h:15
YAML_PM::Parser::HandleDirective
void HandleDirective(const Token &token)
Definition: parser.cpp:92
YAML_PM::Token::params
std::vector< std::string > params
Definition: token.h:80
YAML_PM::ErrorMsg::YAML_DIRECTIVE_ARGS
const char *const YAML_DIRECTIVE_ARGS
Definition: exceptions.h:20
YAML_PM::Token::value
std::string value
Definition: token.h:79
eventhandler.h
directives.h
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_PM::ParserException
Definition: exceptions.h:110
YAML_PM::Parser::HandleYamlDirective
void HandleYamlDirective(const Token &token)
Definition: parser.cpp:102
YAML_PM::Parser::~Parser
~Parser()
Definition: parser.cpp:25
node.h
kitti-batch-convert.out
string out
Definition: kitti-batch-convert.py:7
YAML_PM::Parser::Parser
Parser()
Definition: parser.cpp:16
nodebuilder.h
YAML_PM::Parser::GetNextDocument
bool GetNextDocument(Node &document)
Definition: parser.cpp:61
YAML_PM::ErrorMsg::TAG_DIRECTIVE_ARGS
const char *const TAG_DIRECTIVE_ARGS
Definition: exceptions.h:24
YAML_PM::SingleDocParser::HandleDocument
void HandleDocument(EventHandler &eventHandler)
Definition: singledocparser.cpp:26
YAML_PM::ErrorMsg::REPEATED_TAG_DIRECTIVE
const char *const REPEATED_TAG_DIRECTIVE
Definition: exceptions.h:25
YAML_PM::ErrorMsg::YAML_VERSION
const char *const YAML_VERSION
Definition: exceptions.h:21
YAML_PM::Parser::ParseDirectives
void ParseDirectives()
Definition: parser.cpp:69
YAML_PM::Token::DIRECTIVE
@ DIRECTIVE
Definition: token.h:43
YAML_PM::Parser::HandleTagDirective
void HandleTagDirective(const Token &token)
Definition: parser.cpp:126
YAML_PM::Token::type
TYPE type
Definition: token.h:77
YAML_PM::Node
Definition: node.h:33
YAML_PM::Parser::m_pScanner
std::auto_ptr< Scanner > m_pScanner
Definition: parser.h:46
parser.h
YAML_PM::Parser::m_pDirectives
std::auto_ptr< Directives > m_pDirectives
Definition: parser.h:47
YAML_PM::SingleDocParser
Definition: singledocparser.h:25
tag.h
YAML_PM::Token
Definition: token.h:39
scanner.h
YAML_PM::ErrorMsg::YAML_MAJOR_VERSION
const char *const YAML_MAJOR_VERSION
Definition: exceptions.h:22
YAML_PM::Parser::PrintTokens
void PrintTokens(std::ostream &out)
Definition: parser.cpp:139
YAML_PM::Scanner
Definition: scanner.h:24
token.h
YAML_PM::Parser::HandleNextDocument
bool HandleNextDocument(EventHandler &eventHandler)
Definition: parser.cpp:44


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:40