scantoken.cpp 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "exp.h"
#include "scanscalar.h"
#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
21
		PopAllIndents();
22
		PopAllSimpleKeys();
23
24
25
26

		m_simpleKeyAllowed = false;

		// store pos and eat indicator
27
		Mark mark = INPUT.mark();
28
29
30
		INPUT.eat(1);

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

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

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

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

			params.push_back(param);
		}
		
jbeder's avatar
jbeder committed
52
		Token token(Token::DIRECTIVE, mark);
53
54
55
56
57
58
59
60
		token.value = name;
		token.params = params;
		m_tokens.push(token);
	}

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

		// eat
66
		Mark mark = INPUT.mark();
67
		INPUT.eat(3);
jbeder's avatar
jbeder committed
68
		m_tokens.push(Token(Token::DOC_START, mark));
69
70
71
72
73
	}

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

		// eat
79
		Mark mark = INPUT.mark();
80
		INPUT.eat(3);
jbeder's avatar
jbeder committed
81
		m_tokens.push(Token(Token::DOC_END, mark));
82
83
84
85
86
87
	}

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

		// eat
92
		Mark mark = INPUT.mark();
93
		char ch = INPUT.get();
94
95
96
		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);
97
		m_tokens.push(Token(type, mark));
98
99
100
101
102
	}

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

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

110
111
112
		m_simpleKeyAllowed = false;

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

		// 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);
123
		m_tokens.push(Token(type, mark));
124
125
126
127
128
	}

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

		// eat
136
		Mark mark = INPUT.mark();
137
		INPUT.eat(1);
jbeder's avatar
jbeder committed
138
		m_tokens.push(Token(Token::FLOW_ENTRY, mark));
139
140
141
142
143
144
	}

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

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

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

		// eat
156
		Mark mark = INPUT.mark();
157
		INPUT.eat(1);
jbeder's avatar
jbeder committed
158
		m_tokens.push(Token(Token::BLOCK_ENTRY, mark));
159
160
161
162
163
164
	}

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

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

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

		// eat
176
		Mark mark = INPUT.mark();
177
		INPUT.eat(1);
jbeder's avatar
jbeder committed
178
		m_tokens.push(Token(Token::KEY, mark));
179
180
181
182
183
	}

	// Value
	void Scanner::ScanValue()
	{
184
185
186
187
		// and check that simple key
		bool isSimpleKey = VerifySimpleKey();
		
		if(isSimpleKey) {
188
189
190
191
			// 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)
192
			if(InBlockContext()) {
193
				if(!m_simpleKeyAllowed)
194
					throw ParserException(INPUT.mark(), ErrorMsg::MAP_VALUE);
195

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

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

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

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

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

		// eat the indicator
220
		Mark mark = INPUT.mark();
221
222
223
224
225
226
227
228
229
		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())
230
			throw ParserException(INPUT.mark(), alias ? ErrorMsg::ALIAS_NOT_FOUND : ErrorMsg::ANCHOR_NOT_FOUND);
231
232

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

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

	// Tag
	void Scanner::ScanTag()
	{
		std::string handle, suffix;

		// insert a potential simple key
248
		InsertPotentialSimpleKey();
249
250
251
		m_simpleKeyAllowed = false;

		// eat the indicator
252
		Mark mark = INPUT.mark();
253
254
255
		handle += INPUT.get();

		// read the handle
256
		while(INPUT && INPUT.peek() != Keys::Tag && !Exp::BlankOrBreak.Matches(INPUT))
257
258
259
260
261
262
263
264
			handle += INPUT.get();

		// is there a suffix?
		if(INPUT.peek() == Keys::Tag) {
			// eat the indicator
			handle += INPUT.get();

			// then read it
265
			while(INPUT && !Exp::BlankOrBreak.Matches(INPUT))
266
267
268
269
270
271
272
				suffix += INPUT.get();
		} else {
			// this is a bit weird: we keep just the '!' as the handle and move the rest to the suffix
			suffix = handle.substr(1);
			handle = "!";
		}

jbeder's avatar
jbeder committed
273
		Token token(Token::TAG, mark);
274
275
276
277
278
279
280
281
282
283
284
285
		token.value = handle;
		token.params.push_back(suffix);
		m_tokens.push(token);
	}

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

		// set up the scanning parameters
		ScanScalarParams params;
286
		params.end = (InFlowContext() ? Exp::EndScalarInFlow : Exp::EndScalar) || (Exp::BlankOrBreak + Exp::Comment);
287
		params.eatEnd = false;
288
		params.indent = (InFlowContext() ? 0 : GetTopIndent() + 1);
jbeder's avatar
jbeder committed
289
		params.fold = FOLD_FLOW;
290
291
		params.eatLeadingWhitespace = true;
		params.trimTrailingSpaces = true;
292
		params.chomp = STRIP;
293
294
295
296
		params.onDocIndicator = BREAK;
		params.onTabInIndentation = THROW;

		// insert a potential simple key
297
		InsertPotentialSimpleKey();
298

299
		Mark mark = INPUT.mark();
300
301
302
303
304
305
306
		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))
307
		//	throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_SCALAR);
308

jbeder's avatar
jbeder committed
309
		Token token(Token::SCALAR, mark);
310
311
312
313
314
315
316
317
318
		token.value = scalar;
		m_tokens.push(token);
	}

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

319
320
		// 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();
321
322
323
324
325
326
327
328
		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;
329
		params.fold = FOLD_FLOW;
330
331
332
333
334
335
		params.eatLeadingWhitespace = true;
		params.trimTrailingSpaces = false;
		params.chomp = CLIP;
		params.onDocIndicator = THROW;

		// insert a potential simple key
336
		InsertPotentialSimpleKey();
337

338
		Mark mark = INPUT.mark();
339
340
341
342
343

		// now eat that opening quote
		INPUT.get();
		
		// and scan
344
345
346
		scalar = ScanScalar(INPUT, params);
		m_simpleKeyAllowed = false;

jbeder's avatar
jbeder committed
347
		Token token(Token::SCALAR, mark);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
		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 '>')
365
		Mark mark = INPUT.mark();
366
		char indicator = INPUT.get();
367
		params.fold = (indicator == Keys::FoldedScalar ? FOLD_BLOCK : DONT_FOLD);
368
369

		// eat chomping/indentation indicators
370
		params.chomp = CLIP;
371
372
373
374
375
376
377
378
379
		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')
380
					throw ParserException(INPUT.mark(), ErrorMsg::ZERO_INDENT_IN_BLOCK);
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397

				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))
398
			throw ParserException(INPUT.mark(), ErrorMsg::CHAR_IN_BLOCK);
399
400

		// set the initial indentation
401
402
		if(GetTopIndent() >= 0)
			params.indent += GetTopIndent();
403
404
405
406
407
408
409
410
411
412

		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;

jbeder's avatar
jbeder committed
413
		Token token(Token::SCALAR, mark);
414
415
416
417
		token.value = scalar;
		m_tokens.push(token);
	}
}