scantoken.cpp 10.1 KB
Newer Older
1
2
3
4
5
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "exp.h"
#include "scanscalar.h"
6
7
#include "scantag.h"
#include "tag.h"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <sstream>

namespace YAML
{
	///////////////////////////////////////////////////////////////////////
	// Specialization for scanning specific tokens

	// Directive
	// . Note: no semantic checking is done here (that's for the parser to do)
	void Scanner::ScanDirective()
	{
		std::string name;
		std::vector <std::string> params;

		// pop indents and simple keys
23
		PopAllIndents();
24
		PopAllSimpleKeys();
25
26
27
28

		m_simpleKeyAllowed = false;

		// store pos and eat indicator
29
		Token token(Token::DIRECTIVE, INPUT.mark());
30
31
32
		INPUT.eat(1);

		// read name
33
		while(INPUT && !Exp::BlankOrBreak.Matches(INPUT))
34
			token.value += INPUT.get();
35
36
37
38
39
40
41
42

		// read parameters
		while(1) {
			// first get rid of whitespace
			while(Exp::Blank.Matches(INPUT))
				INPUT.eat(1);

			// break on newline or comment
43
			if(!INPUT || Exp::Break.Matches(INPUT) || Exp::Comment.Matches(INPUT))
44
45
46
47
				break;

			// now read parameter
			std::string param;
48
			while(INPUT && !Exp::BlankOrBreak.Matches(INPUT))
49
50
				param += INPUT.get();

51
			token.params.push_back(param);
52
53
54
55
56
57
58
59
		}
		
		m_tokens.push(token);
	}

	// DocStart
	void Scanner::ScanDocStart()
	{
60
		PopAllIndents();
61
		PopAllSimpleKeys();
62
63
64
		m_simpleKeyAllowed = false;

		// eat
65
		Mark mark = INPUT.mark();
66
		INPUT.eat(3);
67
		m_tokens.push(Token(Token::DOC_START, mark));
68
69
70
71
72
	}

	// DocEnd
	void Scanner::ScanDocEnd()
	{
73
		PopAllIndents();
74
		PopAllSimpleKeys();
75
76
77
		m_simpleKeyAllowed = false;

		// eat
78
		Mark mark = INPUT.mark();
79
		INPUT.eat(3);
80
		m_tokens.push(Token(Token::DOC_END, mark));
81
82
83
84
85
86
	}

	// FlowStart
	void Scanner::ScanFlowStart()
	{
		// flows can be simple keys
87
		InsertPotentialSimpleKey();
88
89
90
		m_simpleKeyAllowed = true;

		// eat
91
		Mark mark = INPUT.mark();
92
		char ch = INPUT.get();
93
94
95
		FLOW_MARKER flowType = (ch == Keys::FlowSeqStart ? FLOW_SEQ : FLOW_MAP);
		m_flows.push(flowType);
		Token::TYPE type = (flowType == FLOW_SEQ ? Token::FLOW_SEQ_START : Token::FLOW_MAP_START);
96
		m_tokens.push(Token(type, mark));
97
98
99
100
101
	}

	// FlowEnd
	void Scanner::ScanFlowEnd()
	{
102
		if(InBlockContext())
103
			throw ParserException(INPUT.mark(), ErrorMsg::FLOW_END);
104

105
106
107
108
		// we might have a solo entry in the flow context
		if(VerifySimpleKey())
			m_tokens.push(Token(Token::VALUE, INPUT.mark()));

109
110
111
		m_simpleKeyAllowed = false;

		// eat
112
		Mark mark = INPUT.mark();
113
		char ch = INPUT.get();
114
115
116
117
118
119
120
121

		// check that it matches the start
		FLOW_MARKER flowType = (ch == Keys::FlowSeqEnd ? FLOW_SEQ : FLOW_MAP);
		if(m_flows.top() != flowType)
			throw ParserException(mark, ErrorMsg::FLOW_END);
		m_flows.pop();
		
		Token::TYPE type = (flowType ? Token::FLOW_SEQ_END : Token::FLOW_MAP_END);
122
		m_tokens.push(Token(type, mark));
123
124
125
126
127
	}

	// FlowEntry
	void Scanner::ScanFlowEntry()
	{
128
129
130
131
		 // we might have a solo entry in the flow context
		if(VerifySimpleKey())
			m_tokens.push(Token(Token::VALUE, INPUT.mark()));
		
132
133
134
		m_simpleKeyAllowed = true;

		// eat
135
		Mark mark = INPUT.mark();
136
		INPUT.eat(1);
137
		m_tokens.push(Token(Token::FLOW_ENTRY, mark));
138
139
140
141
142
143
	}

	// BlockEntry
	void Scanner::ScanBlockEntry()
	{
		// we better be in the block context!
144
		if(InFlowContext())
145
			throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY);
146
147
148

		// can we put it here?
		if(!m_simpleKeyAllowed)
149
			throw ParserException(INPUT.mark(), ErrorMsg::BLOCK_ENTRY);
150

151
		PushIndentTo(INPUT.column(), IndentMarker::SEQ);
152
153
154
		m_simpleKeyAllowed = true;

		// eat
155
		Mark mark = INPUT.mark();
156
		INPUT.eat(1);
157
		m_tokens.push(Token(Token::BLOCK_ENTRY, mark));
158
159
160
161
162
163
	}

	// Key
	void Scanner::ScanKey()
	{
		// handle keys diffently in the block context (and manage indents)
164
		if(InBlockContext()) {
165
			if(!m_simpleKeyAllowed)
166
				throw ParserException(INPUT.mark(), ErrorMsg::MAP_KEY);
167

168
			PushIndentTo(INPUT.column(), IndentMarker::MAP);
169
170
171
		}

		// can only put a simple key here if we're in block context
172
		m_simpleKeyAllowed = InBlockContext();
173
174

		// eat
175
		Mark mark = INPUT.mark();
176
		INPUT.eat(1);
177
		m_tokens.push(Token(Token::KEY, mark));
178
179
180
181
182
	}

	// Value
	void Scanner::ScanValue()
	{
183
184
185
186
		// and check that simple key
		bool isSimpleKey = VerifySimpleKey();
		
		if(isSimpleKey) {
187
188
189
190
			// can't follow a simple key with another simple key (dunno why, though - it seems fine)
			m_simpleKeyAllowed = false;
		} else {
			// handle values diffently in the block context (and manage indents)
191
			if(InBlockContext()) {
192
				if(!m_simpleKeyAllowed)
193
					throw ParserException(INPUT.mark(), ErrorMsg::MAP_VALUE);
194

195
				PushIndentTo(INPUT.column(), IndentMarker::MAP);
196
197
198
			}

			// can only put a simple key here if we're in block context
199
			m_simpleKeyAllowed = InBlockContext();
200
201
202
		}

		// eat
203
		Mark mark = INPUT.mark();
204
		INPUT.eat(1);
205
		m_tokens.push(Token(Token::VALUE, mark));
206
207
208
209
210
211
212
213
214
	}

	// AnchorOrAlias
	void Scanner::ScanAnchorOrAlias()
	{
		bool alias;
		std::string name;

		// insert a potential simple key
215
		InsertPotentialSimpleKey();
216
217
218
		m_simpleKeyAllowed = false;

		// eat the indicator
219
		Mark mark = INPUT.mark();
220
221
222
223
224
225
226
227
228
		char indicator = INPUT.get();
		alias = (indicator == Keys::Alias);

		// now eat the content
		while(Exp::AlphaNumeric.Matches(INPUT))
			name += INPUT.get();

		// we need to have read SOMETHING!
		if(name.empty())
229
			throw ParserException(INPUT.mark(), alias ? ErrorMsg::ALIAS_NOT_FOUND : ErrorMsg::ANCHOR_NOT_FOUND);
230
231

		// and needs to end correctly
232
		if(INPUT && !Exp::AnchorEnd.Matches(INPUT))
233
			throw ParserException(INPUT.mark(), alias ? ErrorMsg::CHAR_IN_ALIAS : ErrorMsg::CHAR_IN_ANCHOR);
234
235

		// and we're done
236
		Token token(alias ? Token::ALIAS : Token::ANCHOR, mark);
237
238
239
240
241
242
243
244
		token.value = name;
		m_tokens.push(token);
	}

	// Tag
	void Scanner::ScanTag()
	{
		// insert a potential simple key
245
		InsertPotentialSimpleKey();
246
247
		m_simpleKeyAllowed = false;

248
		Token token(Token::TAG, INPUT.mark());
249

250
251
252
253
254
		// eat the indicator
		INPUT.get();
		
		if(INPUT && INPUT.peek() == Keys::VerbatimTagStart){
			std::string tag = ScanVerbatimTag(INPUT);
255

256
257
			token.value = tag;
			token.data = Tag::VERBATIM;
258
		} else {
259
260
261
262
263
264
265
266
267
268
269
			bool canBeHandle;
			token.value = ScanTagHandle(INPUT, canBeHandle);
			token.data = (token.value.empty() ? Tag::SECONDARY_HANDLE : Tag::PRIMARY_HANDLE);
			
			// is there a suffix?
			if(canBeHandle && INPUT.peek() == Keys::Tag) {
				// eat the indicator
				INPUT.get();
				token.params.push_back(ScanTagSuffix(INPUT));
				token.data = Tag::NAMED_HANDLE;
			}
270
271
272
273
274
275
276
277
278
279
280
281
		}

		m_tokens.push(token);
	}

	// PlainScalar
	void Scanner::ScanPlainScalar()
	{
		std::string scalar;

		// set up the scanning parameters
		ScanScalarParams params;
282
		params.end = (InFlowContext() ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment);
283
		params.eatEnd = false;
284
		params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1);
285
		params.fold = FOLD_FLOW;
286
287
		params.eatLeadingWhitespace = true;
		params.trimTrailingSpaces = true;
288
		params.chomp = STRIP;
289
290
291
292
		params.onDocIndicator = BREAK;
		params.onTabInIndentation = THROW;

		// insert a potential simple key
293
		InsertPotentialSimpleKey();
294

295
		Mark mark = INPUT.mark();
296
297
298
299
300
301
302
		scalar = ScanScalar(INPUT, params);

		// can have a simple key only if we ended the scalar by starting a new line
		m_simpleKeyAllowed = params.leadingSpaces;

		// finally, check and see if we ended on an illegal character
		//if(Exp::IllegalCharInScalar.Matches(INPUT))
303
		//	throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_SCALAR);
304

305
		Token token(Token::SCALAR, mark);
306
307
308
309
310
311
312
313
314
		token.value = scalar;
		m_tokens.push(token);
	}

	// QuotedScalar
	void Scanner::ScanQuotedScalar()
	{
		std::string scalar;

315
316
		// peek at single or double quote (don't eat because we need to preserve (for the time being) the input position)
		char quote = INPUT.peek();
317
318
319
320
321
322
323
324
		bool single = (quote == '\'');

		// setup the scanning parameters
		ScanScalarParams params;
		params.end = (single ? RegEx(quote) && !Exp::EscSingleQuote : RegEx(quote));
		params.eatEnd = true;
		params.escape = (single ? '\'' : '\\');
		params.indent = 0;
325
		params.fold = FOLD_FLOW;
326
327
328
329
330
331
		params.eatLeadingWhitespace = true;
		params.trimTrailingSpaces = false;
		params.chomp = CLIP;
		params.onDocIndicator = THROW;

		// insert a potential simple key
332
		InsertPotentialSimpleKey();
333

334
		Mark mark = INPUT.mark();
335
336
337
338
339

		// now eat that opening quote
		INPUT.get();
		
		// and scan
340
341
342
		scalar = ScanScalar(INPUT, params);
		m_simpleKeyAllowed = false;

343
		Token token(Token::SCALAR, mark);
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
		token.value = scalar;
		m_tokens.push(token);
	}

	// BlockScalarToken
	// . These need a little extra processing beforehand.
	// . We need to scan the line where the indicator is (this doesn't count as part of the scalar),
	//   and then we need to figure out what level of indentation we'll be using.
	void Scanner::ScanBlockScalar()
	{
		std::string scalar;

		ScanScalarParams params;
		params.indent = 1;
		params.detectIndent = true;

		// eat block indicator ('|' or '>')
361
		Mark mark = INPUT.mark();
362
		char indicator = INPUT.get();
363
		params.fold = (indicator == Keys::FoldedScalar ? FOLD_BLOCK : DONT_FOLD);
364
365

		// eat chomping/indentation indicators
366
		params.chomp = CLIP;
367
368
369
370
371
372
373
374
375
		int n = Exp::Chomp.Match(INPUT);
		for(int i=0;i<n;i++) {
			char ch = INPUT.get();
			if(ch == '+')
				params.chomp = KEEP;
			else if(ch == '-')
				params.chomp = STRIP;
			else if(Exp::Digit.Matches(ch)) {
				if(ch == '0')
376
					throw ParserException(INPUT.mark(), ErrorMsg::ZERO_INDENT_IN_BLOCK);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

				params.indent = ch - '0';
				params.detectIndent = false;
			}
		}

		// now eat whitespace
		while(Exp::Blank.Matches(INPUT))
			INPUT.eat(1);

		// and comments to the end of the line
		if(Exp::Comment.Matches(INPUT))
			while(INPUT && !Exp::Break.Matches(INPUT))
				INPUT.eat(1);

		// if it's not a line break, then we ran into a bad character inline
		if(INPUT && !Exp::Break.Matches(INPUT))
394
			throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_BLOCK);
395
396

		// set the initial indentation
397
398
		if(GetTopIndent() >= 0)
			params.indent += GetTopIndent();
399
400
401
402
403
404
405
406
407
408

		params.eatLeadingWhitespace = false;
		params.trimTrailingSpaces = false;
		params.onTabInIndentation = THROW;

		scalar = ScanScalar(INPUT, params);

		// simple keys always ok after block scalars (since we're gonna start a new line anyways)
		m_simpleKeyAllowed = true;

409
		Token token(Token::SCALAR, mark);
410
411
412
413
		token.value = scalar;
		m_tokens.push(token);
	}
}