scanner.cpp
Go to the documentation of this file.
1 #include "scanner.h"
2 #include "token.h"
4 #include "exp.h"
5 #include <cassert>
6 #include <memory>
7 
8 namespace YAML_PM
9 {
10  Scanner::Scanner(std::istream& in)
11  : INPUT(in), m_startedStream(false), m_endedStream(false), m_simpleKeyAllowed(false), m_canBeJSONFlow(false)
12  {
13  }
14 
16  {
17  }
18 
19  // empty
20  // . Returns true if there are no more tokens to be read
22  {
24  return m_tokens.empty();
25  }
26 
27  // pop
28  // . Simply removes the next token on the queue.
29  void Scanner::pop()
30  {
32  if(!m_tokens.empty())
33  m_tokens.pop();
34  }
35 
36  // peek
37  // . Returns (but does not remove) the next token on the queue.
39  {
41  assert(!m_tokens.empty()); // should we be asserting here? I mean, we really just be checking
42  // if it's empty before peeking.
43 
44 #if 0
45  static Token *pLast = 0;
46  if(pLast != &m_tokens.front())
47  std::cerr << "peek: " << m_tokens.front() << "\n";
48  pLast = &m_tokens.front();
49 #endif
50 
51  return m_tokens.front();
52  }
53 
54  // EnsureTokensInQueue
55  // . Scan until there's a valid token at the front of the queue,
56  // or we're sure the queue is empty.
58  {
59  while(1) {
60  if(!m_tokens.empty()) {
61  Token& token = m_tokens.front();
62 
63  // if this guy's valid, then we're done
64  if(token.status == Token::VALID)
65  return;
66 
67  // here's where we clean up the impossible tokens
68  if(token.status == Token::INVALID) {
69  m_tokens.pop();
70  continue;
71  }
72 
73  // note: what's left are the unverified tokens
74  }
75 
76  // no token? maybe we've actually finished
77  if(m_endedStream)
78  return;
79 
80  // no? then scan...
81  ScanNextToken();
82  }
83  }
84 
85  // ScanNextToken
86  // . The main scanning function; here we branch out and
87  // scan whatever the next token should be.
89  {
90  if(m_endedStream)
91  return;
92 
93  if(!m_startedStream)
94  return StartStream();
95 
96  // get rid of whitespace, etc. (in between tokens it should be irrelevent)
98 
99  // maybe need to end some blocks
100  PopIndentToHere();
101 
102  // *****
103  // And now branch based on the next few characters!
104  // *****
105 
106  // end of stream
107  if(!INPUT)
108  return EndStream();
109 
110  if(INPUT.column() == 0 && INPUT.peek() == Keys::Directive)
111  return ScanDirective();
112 
113  // document token
114  if(INPUT.column() == 0 && Exp::DocStart().Matches(INPUT))
115  return ScanDocStart();
116 
117  if(INPUT.column() == 0 && Exp::DocEnd().Matches(INPUT))
118  return ScanDocEnd();
119 
120  // flow start/end/entry
122  return ScanFlowStart();
123 
125  return ScanFlowEnd();
126 
127  if(INPUT.peek() == Keys::FlowEntry)
128  return ScanFlowEntry();
129 
130  // block/map stuff
131  if(Exp::BlockEntry().Matches(INPUT))
132  return ScanBlockEntry();
133 
134  if((InBlockContext() ? Exp::Key() : Exp::KeyInFlow()).Matches(INPUT))
135  return ScanKey();
136 
137  if(GetValueRegex().Matches(INPUT))
138  return ScanValue();
139 
140  // alias/anchor
141  if(INPUT.peek() == Keys::Alias || INPUT.peek() == Keys::Anchor)
142  return ScanAnchorOrAlias();
143 
144  // tag
145  if(INPUT.peek() == Keys::Tag)
146  return ScanTag();
147 
148  // special scalars
150  return ScanBlockScalar();
151 
152  if(INPUT.peek() == '\'' || INPUT.peek() == '\"')
153  return ScanQuotedScalar();
154 
155  // plain scalars
157  return ScanPlainScalar();
158 
159  // don't know what it is!
161  }
162 
163  // ScanToNextToken
164  // . Eats input until we reach the next token-like thing.
166  {
167  while(1) {
168  // first eat whitespace
169  while(INPUT && IsWhitespaceToBeEaten(INPUT.peek())) {
170  if(InBlockContext() && Exp::Tab().Matches(INPUT))
171  m_simpleKeyAllowed = false;
172  INPUT.eat(1);
173  }
174 
175  // then eat a comment
176  if(Exp::Comment().Matches(INPUT)) {
177  // eat until line break
178  while(INPUT && !Exp::Break().Matches(INPUT))
179  INPUT.eat(1);
180  }
181 
182  // if it's NOT a line break, then we're done!
183  if(!Exp::Break().Matches(INPUT))
184  break;
185 
186  // otherwise, let's eat the line break and keep going
187  int n = Exp::Break().Match(INPUT);
188  INPUT.eat(n);
189 
190  // oh yeah, and let's get rid of that simple key
192 
193  // new line - we may be able to accept a simple key now
194  if(InBlockContext())
195  m_simpleKeyAllowed = true;
196  }
197  }
198 
200  // Misc. helpers
201 
202  // IsWhitespaceToBeEaten
203  // . We can eat whitespace if it's a space or tab
204  // . Note: originally tabs in block context couldn't be eaten
205  // "where a simple key could be allowed
206  // (i.e., not at the beginning of a line, or following '-', '?', or ':')"
207  // I think this is wrong, since tabs can be non-content whitespace; it's just
208  // that they can't contribute to indentation, so once you've seen a tab in a
209  // line, you can't start a simple key
211  {
212  if(ch == ' ')
213  return true;
214 
215  if(ch == '\t')
216  return true;
217 
218  return false;
219  }
220 
221  // GetValueRegex
222  // . Get the appropriate regex to check if it's a value token
224  {
225  if(InBlockContext())
226  return Exp::Value();
227 
229  }
230 
231  // StartStream
232  // . Set the initial conditions for starting a stream.
234  {
235  m_startedStream = true;
236  m_simpleKeyAllowed = true;
237  std::auto_ptr<IndentMarker> pIndent(new IndentMarker(-1, IndentMarker::NONE));
238  m_indentRefs.push_back(pIndent);
239  m_indents.push(&m_indentRefs.back());
240  }
241 
242  // EndStream
243  // . Close out the stream, finish up, etc.
245  {
246  // force newline
247  if(INPUT.column() > 0)
248  INPUT.ResetColumn();
249 
250  PopAllIndents();
252 
253  m_simpleKeyAllowed = false;
254  m_endedStream = true;
255  }
256 
258  {
259  m_tokens.push(Token(type, INPUT.mark()));
260  return &m_tokens.back();
261  }
262 
264  {
265  switch(type) {
268  case IndentMarker::NONE: assert(false); break;
269  }
270  assert(false);
271  throw std::runtime_error("yaml-cpp: internal error, invalid indent type");
272  }
273 
274  // PushIndentTo
275  // . Pushes an indentation onto the stack, and enqueues the
276  // proper token (sequence start or mapping start).
277  // . Returns the indent marker it generates (if any).
279  {
280  // are we in flow?
281  if(InFlowContext())
282  return 0;
283 
284  std::auto_ptr<IndentMarker> pIndent(new IndentMarker(column, type));
285  IndentMarker& indent = *pIndent;
286  const IndentMarker& lastIndent = *m_indents.top();
287 
288  // is this actually an indentation?
289  if(indent.column < lastIndent.column)
290  return 0;
291  if(indent.column == lastIndent.column && !(indent.type == IndentMarker::SEQ && lastIndent.type == IndentMarker::MAP))
292  return 0;
293 
294  // push a start token
295  indent.pStartToken = PushToken(GetStartTokenFor(type));
296 
297  // and then the indent
298  m_indents.push(&indent);
299  m_indentRefs.push_back(pIndent);
300  return &m_indentRefs.back();
301  }
302 
303  // PopIndentToHere
304  // . Pops indentations off the stack until we reach the current indentation level,
305  // and enqueues the proper token each time.
306  // . Then pops all invalid indentations off.
308  {
309  // are we in flow?
310  if(InFlowContext())
311  return;
312 
313  // now pop away
314  while(!m_indents.empty()) {
315  const IndentMarker& indent = *m_indents.top();
316  if(indent.column < INPUT.column())
317  break;
318  if(indent.column == INPUT.column() && !(indent.type == IndentMarker::SEQ && !Exp::BlockEntry().Matches(INPUT)))
319  break;
320 
321  PopIndent();
322  }
323 
324  while(!m_indents.empty() && m_indents.top()->status == IndentMarker::INVALID)
325  PopIndent();
326  }
327 
328  // PopAllIndents
329  // . Pops all indentations (except for the base empty one) off the stack,
330  // and enqueues the proper token each time.
332  {
333  // are we in flow?
334  if(InFlowContext())
335  return;
336 
337  // now pop away
338  while(!m_indents.empty()) {
339  const IndentMarker& indent = *m_indents.top();
340  if(indent.type == IndentMarker::NONE)
341  break;
342 
343  PopIndent();
344  }
345  }
346 
347  // PopIndent
348  // . Pops a single indent, pushing the proper token
350  {
351  const IndentMarker& indent = *m_indents.top();
352  m_indents.pop();
353 
354  if(indent.status != IndentMarker::VALID) {
356  return;
357  }
358 
359  if(indent.type == IndentMarker::SEQ)
361  else if(indent.type == IndentMarker::MAP)
363  }
364 
365  // GetTopIndent
367  {
368  if(m_indents.empty())
369  return 0;
370  return m_indents.top()->column;
371  }
372 
373  // ThrowParserException
374  // . Throws a ParserException with the current token location
375  // (if available).
376  // . Does not parse any more tokens.
378  {
379  Mark mark = Mark::null();
380  if(!m_tokens.empty()) {
381  const Token& token = m_tokens.front();
382  mark = token.mark;
383  }
384  throw ParserException(mark, msg);
385  }
386 }
387 
YAML_PM::Exp::Tab
const RegEx & Tab()
Definition: exp.h:26
YAML_PM::Token::BLOCK_MAP_START
@ BLOCK_MAP_START
Definition: token.h:47
YAML_PM::Scanner::pop
void pop()
Definition: scanner.cpp:29
YAML_PM::Scanner::INPUT
Stream INPUT
Definition: scanner.h:115
YAML_PM::Scanner::GetStartTokenFor
Token::TYPE GetStartTokenFor(IndentMarker::INDENT_TYPE type) const
Definition: scanner.cpp:263
YAML_PM::Exp::BlockEntry
const RegEx & BlockEntry()
Definition: exp.h:89
YAML_PM::Scanner::ScanFlowEnd
void ScanFlowEnd()
Definition: scantoken.cpp:104
YAML_PM::Stream::peek
char peek() const
Definition: stream.cpp:228
YAML_PM::Scanner::InBlockContext
bool InBlockContext() const
Definition: scanner.h:59
exceptions.h
YAML_PM::Scanner::ScanKey
void ScanKey()
Definition: scantoken.cpp:176
YAML_PM::Stream::column
int column() const
Definition: stream.h:41
YAML_PM::Exp::Break
const RegEx & Break()
Definition: exp.h:34
YAML_PM::Keys::Alias
const char Alias
Definition: exp.h:186
YAML_PM::Scanner::IndentMarker::VALID
@ VALID
Definition: scanner.h:38
YAML_PM::Scanner::PushToken
Token * PushToken(Token::TYPE type)
Definition: scanner.cpp:257
YAML_PM::Scanner::IndentMarker::MAP
@ MAP
Definition: scanner.h:37
YAML_PM::Scanner::IndentMarker::INDENT_TYPE
INDENT_TYPE
Definition: scanner.h:37
YAML_PM::Keys::Anchor
const char Anchor
Definition: exp.h:187
YAML_PM::Scanner::IndentMarker::NONE
@ NONE
Definition: scanner.h:37
YAML_PM::Scanner::ScanPlainScalar
void ScanPlainScalar()
Definition: scantoken.cpp:299
YAML_PM::Token::status
STATUS status
Definition: token.h:76
YAML_PM::Scanner::IndentMarker
Definition: scanner.h:36
YAML_PM::Keys::FlowSeqStart
const char FlowSeqStart
Definition: exp.h:181
YAML_PM::Scanner::EnsureTokensInQueue
void EnsureTokensInQueue()
Definition: scanner.cpp:57
YAML_PM::Token::BLOCK_SEQ_END
@ BLOCK_SEQ_END
Definition: token.h:48
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::Token::mark
Mark mark
Definition: token.h:78
YAML_PM::Mark::null
static const Mark null()
Definition: mark.h:16
YAML_PM::Keys::Tag
const char Tag
Definition: exp.h:188
YAML_PM::Scanner::ScanDirective
void ScanDirective()
Definition: scantoken.cpp:17
YAML_PM::Scanner::IsWhitespaceToBeEaten
bool IsWhitespaceToBeEaten(char ch)
Definition: scanner.cpp:210
YAML_PM::Exp::ValueInFlow
const RegEx & ValueInFlow()
Definition: exp.h:105
YAML_PM::Scanner::PopIndent
void PopIndent()
Definition: scanner.cpp:349
YAML_PM::Scanner::IndentMarker::SEQ
@ SEQ
Definition: scanner.h:37
YAML_PM::Token::BLOCK_SEQ_START
@ BLOCK_SEQ_START
Definition: token.h:46
YAML_PM::Scanner::m_indentRefs
ptr_vector< IndentMarker > m_indentRefs
Definition: scanner.h:126
YAML_PM::Token::TYPE
TYPE
Definition: token.h:42
YAML_PM::Scanner::~Scanner
~Scanner()
Definition: scanner.cpp:15
YAML_PM::Stream::ResetColumn
void ResetColumn()
Definition: stream.h:42
YAML_PM::Keys::FlowSeqEnd
const char FlowSeqEnd
Definition: exp.h:182
exp.h
YAML_PM::Scanner::StartStream
void StartStream()
Definition: scanner.cpp:233
YAML_PM::Scanner::ScanBlockScalar
void ScanBlockScalar()
Definition: scantoken.cpp:377
YAML_PM::Scanner::PushIndentTo
IndentMarker * PushIndentTo(int column, IndentMarker::INDENT_TYPE type)
Definition: scanner.cpp:278
YAML_PM::Exp::Value
const RegEx & Value()
Definition: exp.h:101
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_PM::Token::INVALID
@ INVALID
Definition: token.h:41
YAML_PM::Keys::FlowMapEnd
const char FlowMapEnd
Definition: exp.h:184
YAML_PM::Exp::DocEnd
const RegEx & DocEnd()
Definition: exp.h:81
YAML_PM::Scanner::ScanToNextToken
void ScanToNextToken()
Definition: scanner.cpp:165
YAML_PM::ParserException
Definition: exceptions.h:110
YAML_PM::Exp::PlainScalarInFlow
const RegEx & PlainScalarInFlow()
Definition: exp.h:143
YAML_PM::Exp::Key
const RegEx & Key()
Definition: exp.h:93
YAML_PM::Scanner::ScanValue
void ScanValue()
Definition: scantoken.cpp:196
YAML_PM::Keys::FoldedScalar
const char FoldedScalar
Definition: exp.h:190
YAML_PM::Scanner::IndentMarker::status
STATUS status
Definition: scanner.h:43
YAML_PM::Scanner::IndentMarker::pStartToken
Token * pStartToken
Definition: scanner.h:44
YAML_PM::Scanner::InFlowContext
bool InFlowContext() const
Definition: scanner.h:58
YAML_PM::Scanner::m_simpleKeyAllowed
bool m_simpleKeyAllowed
Definition: scanner.h:122
YAML_PM::Scanner::IndentMarker::INVALID
@ INVALID
Definition: scanner.h:38
YAML_PM::Scanner::Scanner
Scanner(std::istream &in)
Definition: scanner.cpp:10
YAML_PM::Token::VALID
@ VALID
Definition: token.h:41
YAML_PM::Token::BLOCK_MAP_END
@ BLOCK_MAP_END
Definition: token.h:49
YAML_PM::Scanner::GetValueRegex
const RegEx & GetValueRegex() const
Definition: scanner.cpp:223
YAML_PM::Scanner::m_canBeJSONFlow
bool m_canBeJSONFlow
Definition: scanner.h:123
YAML_PM::Scanner::IndentMarker::type
INDENT_TYPE type
Definition: scanner.h:42
YAML_PM::Scanner::ScanNextToken
void ScanNextToken()
Definition: scanner.cpp:88
YAML_PM::Scanner::ScanDocStart
void ScanDocStart()
Definition: scantoken.cpp:59
YAML_PM::Keys::LiteralScalar
const char LiteralScalar
Definition: exp.h:189
YAML_PM::Scanner::ScanTag
void ScanTag()
Definition: scantoken.cpp:259
YAML_PM::Scanner::ScanFlowStart
void ScanFlowStart()
Definition: scantoken.cpp:87
YAML_PM::Scanner::ScanAnchorOrAlias
void ScanAnchorOrAlias()
Definition: scantoken.cpp:225
YAML_PM::RegEx::Match
int Match(const std::string &str) const
Definition: regeximpl.h:42
YAML_PM::Scanner::ScanDocEnd
void ScanDocEnd()
Definition: scantoken.cpp:73
YAML_PM::RegEx
Definition: regex.h:21
YAML_PM::Scanner::IndentMarker::column
int column
Definition: scanner.h:41
YAML_PM::Scanner::PopAllIndents
void PopAllIndents()
Definition: scanner.cpp:331
YAML_PM::Keys::FlowMapStart
const char FlowMapStart
Definition: exp.h:183
YAML_PM::Scanner::GetTopIndent
int GetTopIndent() const
Definition: scanner.cpp:366
YAML_PM::Exp::PlainScalar
const RegEx & PlainScalar()
Definition: exp.h:139
YAML_PM::Mark
Definition: mark.h:13
YAML_PM::RegEx::Matches
bool Matches(char ch) const
Definition: regeximpl.h:16
YAML_PM::Scanner::m_tokens
std::queue< Token > m_tokens
Definition: scanner.h:118
YAML_PM::Scanner::ScanQuotedScalar
void ScanQuotedScalar()
Definition: scantoken.cpp:335
YAML_PM::Scanner::m_indents
std::stack< IndentMarker * > m_indents
Definition: scanner.h:125
YAML_PM::Keys::FlowEntry
const char FlowEntry
Definition: exp.h:185
YAML_PM::Scanner::InvalidateSimpleKey
void InvalidateSimpleKey()
Definition: simplekey.cpp:87
YAML_PM::Token
Definition: token.h:39
YAML_PM::Exp::ValueInJSONFlow
const RegEx & ValueInJSONFlow()
Definition: exp.h:109
YAML_PM::Scanner::ThrowParserException
void ThrowParserException(const std::string &msg) const
Definition: scanner.cpp:377
scanner.h
YAML_PM::ErrorMsg::UNKNOWN_TOKEN
const char *const UNKNOWN_TOKEN
Definition: exceptions.h:40
YAML_PM::Scanner::PopIndentToHere
void PopIndentToHere()
Definition: scanner.cpp:307
YAML_PM::Scanner::m_startedStream
bool m_startedStream
Definition: scanner.h:121
YAML_PM::Scanner::peek
Token & peek()
Definition: scanner.cpp:38
YAML_PM::Scanner::ScanBlockEntry
void ScanBlockEntry()
Definition: scantoken.cpp:155
YAML_PM::Scanner::PopAllSimpleKeys
void PopAllSimpleKeys()
Definition: simplekey.cpp:133
YAML_PM::Keys::Directive
const char Directive
Definition: exp.h:180
token.h
YAML_PM::Stream::mark
const Mark mark() const
Definition: stream.h:38
YAML_PM::Scanner::ScanFlowEntry
void ScanFlowEntry()
Definition: scantoken.cpp:135
YAML_PM::Exp::Comment
const RegEx Comment()
Definition: exp.h:113
YAML_PM::Exp::KeyInFlow
const RegEx & KeyInFlow()
Definition: exp.h:97
YAML_PM::Exp::DocStart
const RegEx & DocStart()
Definition: exp.h:77
YAML_PM::Scanner::empty
bool empty()
Definition: scanner.cpp:21
YAML_PM::Stream::eat
void eat(int n=1)
Definition: stream.cpp:272
YAML_PM::Scanner::m_endedStream
bool m_endedStream
Definition: scanner.h:121
YAML_PM::Scanner::EndStream
void EndStream()
Definition: scanner.cpp:244


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:46:00