scantoken.cpp 9.79 KB
Newer Older
1
2
3
4
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "exp.h"
5
#include "scanscalar.h"
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

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

	// StreamStartToken
	template <> StreamStartToken *Scanner::ScanToken(StreamStartToken *pToken)
	{
		m_startedStream = true;
		m_simpleKeyAllowed = true;
		m_indents.push(-1);

		return pToken;
	}

	// StreamEndToken
	template <> StreamEndToken *Scanner::ScanToken(StreamEndToken *pToken)
	{
		// force newline
26
27
		if(INPUT.column > 0)
			INPUT.column = 0;
28
29

		PopIndentTo(-1);
30
		VerifyAllSimpleKeys();
31
32
33
34
35
36
37

		m_simpleKeyAllowed = false;
		m_endedStream = true;

		return pToken;
	}

Jesse Beder's avatar
Jesse Beder committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	// DirectiveToken
	// . Note: no semantic checking is done here (that's for the parser to do)
	template <> DirectiveToken *Scanner::ScanToken(DirectiveToken *pToken)
	{
		// pop indents and simple keys
		PopIndentTo(-1);
		VerifyAllSimpleKeys();

		m_simpleKeyAllowed = false;

		// eat indicator
		INPUT.Eat(1);

		// read name
		while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT))
			pToken->name += INPUT.GetChar();

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

			// break on newline or comment
			if(INPUT.peek() == EOF || Exp::Break.Matches(INPUT) || Exp::Comment.Matches(INPUT))
				break;

			// now read parameter
			std::string param;
			while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT))
				param += INPUT.GetChar();

			pToken->params.push_back(param);
		}
		
		return pToken;
	}

76
77
78
	// DocumentStartToken
	template <> DocumentStartToken *Scanner::ScanToken(DocumentStartToken *pToken)
	{
79
		PopIndentTo(INPUT.column);
80
		VerifyAllSimpleKeys();
81
82
83
		m_simpleKeyAllowed = false;

		// eat
84
		INPUT.Eat(3);
85
86
87
88
89
90
91
		return pToken;
	}

	// DocumentEndToken
	template <> DocumentEndToken *Scanner::ScanToken(DocumentEndToken *pToken)
	{
		PopIndentTo(-1);
92
		VerifyAllSimpleKeys();
93
94
95
		m_simpleKeyAllowed = false;

		// eat
96
		INPUT.Eat(3);
97
98
99
100
101
102
		return pToken;
	}

	// FlowSeqStartToken
	template <> FlowSeqStartToken *Scanner::ScanToken(FlowSeqStartToken *pToken)
	{
Jesse Beder's avatar
Jesse Beder committed
103
104
		// flow sequences can be simple keys
		InsertSimpleKey();
Jesse Beder's avatar
Jesse Beder committed
105
		m_flowLevel++;
106
107
108
		m_simpleKeyAllowed = true;

		// eat
109
		INPUT.Eat(1);
110
111
112
113
114
115
		return pToken;
	}

	// FlowMapStartToken
	template <> FlowMapStartToken *Scanner::ScanToken(FlowMapStartToken *pToken)
	{
Jesse Beder's avatar
Jesse Beder committed
116
117
		// flow maps can be simple keys
		InsertSimpleKey();
Jesse Beder's avatar
Jesse Beder committed
118
		m_flowLevel++;
119
120
121
		m_simpleKeyAllowed = true;

		// eat
122
		INPUT.Eat(1);
123
124
125
126
127
128
		return pToken;
	}

	// FlowSeqEndToken
	template <> FlowSeqEndToken *Scanner::ScanToken(FlowSeqEndToken *pToken)
	{
Jesse Beder's avatar
Jesse Beder committed
129
130
131
132
		if(m_flowLevel == 0)
			throw IllegalFlowEnd();

		m_flowLevel--;
133
134
135
		m_simpleKeyAllowed = false;

		// eat
136
		INPUT.Eat(1);
137
138
139
140
141
142
		return pToken;
	}

	// FlowMapEndToken
	template <> FlowMapEndToken *Scanner::ScanToken(FlowMapEndToken *pToken)
	{
Jesse Beder's avatar
Jesse Beder committed
143
144
145
146
		if(m_flowLevel == 0)
			throw IllegalFlowEnd();

		m_flowLevel--;
147
148
149
		m_simpleKeyAllowed = false;

		// eat
150
		INPUT.Eat(1);
151
152
153
154
155
156
157
158
159
		return pToken;
	}

	// FlowEntryToken
	template <> FlowEntryToken *Scanner::ScanToken(FlowEntryToken *pToken)
	{
		m_simpleKeyAllowed = true;

		// eat
160
		INPUT.Eat(1);
161
162
163
164
165
166
167
		return pToken;
	}

	// BlockEntryToken
	template <> BlockEntryToken *Scanner::ScanToken(BlockEntryToken *pToken)
	{
		// we better be in the block context!
Jesse Beder's avatar
Jesse Beder committed
168
169
		if(m_flowLevel > 0)
			throw IllegalBlockEntry();
170

Jesse Beder's avatar
Jesse Beder committed
171
172
173
		// can we put it here?
		if(!m_simpleKeyAllowed)
			throw IllegalBlockEntry();
174

175
		PushIndentTo(INPUT.column, true);
176
177
178
		m_simpleKeyAllowed = true;

		// eat
179
		INPUT.Eat(1);
180
181
182
183
184
185
		return pToken;
	}

	// KeyToken
	template <> KeyToken *Scanner::ScanToken(KeyToken *pToken)
	{
Jesse Beder's avatar
Jesse Beder committed
186
		// handle keys diffently in the block context (and manage indents)
187
188
189
190
		if(m_flowLevel == 0) {
			if(!m_simpleKeyAllowed)
				throw IllegalMapKey();

191
			PushIndentTo(INPUT.column, false);
192
193
194
195
196
197
198
199
200
		}

		// can only put a simple key here if we're in block context
		if(m_flowLevel == 0)
			m_simpleKeyAllowed = true;
		else
			m_simpleKeyAllowed = false;

		// eat
201
		INPUT.Eat(1);
202
203
204
205
206
207
		return pToken;
	}

	// ValueToken
	template <> ValueToken *Scanner::ScanToken(ValueToken *pToken)
	{
Jesse Beder's avatar
Jesse Beder committed
208
		// does this follow a simple key?
209
		if(m_isLastKeyValid) {
Jesse Beder's avatar
Jesse Beder committed
210
211
			// can't follow a simple key with another simple key (dunno why, though - it seems fine)
			m_simpleKeyAllowed = false;
212
		} else {
Jesse Beder's avatar
Jesse Beder committed
213
			// handle values diffently in the block context (and manage indents)
214
215
216
217
			if(m_flowLevel == 0) {
				if(!m_simpleKeyAllowed)
					throw IllegalMapValue();

218
				PushIndentTo(INPUT.column, false);
219
220
			}

Jesse Beder's avatar
Jesse Beder committed
221
222
223
224
225
226
			// can only put a simple key here if we're in block context
			if(m_flowLevel == 0)
				m_simpleKeyAllowed = true;
			else
				m_simpleKeyAllowed = false;
		}
227
228

		// eat
229
		INPUT.Eat(1);
230
231
232
		return pToken;
	}

Jesse Beder's avatar
Jesse Beder committed
233
234
235
236
237
238
239
240
241
	// AnchorToken
	template <> AnchorToken *Scanner::ScanToken(AnchorToken *pToken)
	{
		// insert a potential simple key
		if(m_simpleKeyAllowed)
			InsertSimpleKey();
		m_simpleKeyAllowed = false;

		// eat the indicator
242
		char indicator = INPUT.GetChar();
Jesse Beder's avatar
Jesse Beder committed
243
244
245
246
247
		pToken->alias = (indicator == Keys::Alias);

		// now eat the content
		std::string tag;
		while(Exp::AlphaNumeric.Matches(INPUT))
248
			tag += INPUT.GetChar();
Jesse Beder's avatar
Jesse Beder committed
249
250
251
252
253
254
255
256
257
258
259
260
261

		// we need to have read SOMETHING!
		if(tag.empty())
			throw AnchorNotFound();

		// and needs to end correctly
		if(INPUT.peek() != EOF && !Exp::AnchorEnd.Matches(INPUT))
			throw IllegalCharacterInAnchor();

		// and we're done
		pToken->value = tag;
		return pToken;
	}
262

Jesse Beder's avatar
Jesse Beder committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
	// TagToken
	template <> TagToken *Scanner::ScanToken(TagToken *pToken)
	{
		// insert a potential simple key
		if(m_simpleKeyAllowed)
			InsertSimpleKey();
		m_simpleKeyAllowed = false;

		// eat the indicator
		INPUT.Eat(1);

		// read the handle
		while(INPUT.peek() != EOF && INPUT.peek() != Keys::Tag && !Exp::BlankOrBreak.Matches(INPUT))
			pToken->handle += INPUT.GetChar();

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

			// then read it
			while(INPUT.peek() != EOF && !Exp::BlankOrBreak.Matches(INPUT))
				pToken->suffix += INPUT.GetChar();
		}

		return pToken;
	}

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
	// PlainScalarToken
	template <> PlainScalarToken *Scanner::ScanToken(PlainScalarToken *pToken)
	{
		// set up the scanning parameters
		ScanScalarParams params;
		params.end = (m_flowLevel > 0 ? Exp::EndScalarInFlow : Exp::EndScalar) || (RegEx(' ') + Exp::Comment);
		params.eatEnd = false;
		params.indent = (m_flowLevel > 0 ? 0 : m_indents.top() + 1);
		params.fold = true;
		params.eatLeadingWhitespace = true;
		params.trimTrailingSpaces = true;
		params.chomp = CLIP;
		params.onDocIndicator = BREAK;
		params.onTabInIndentation = THROW;

		// insert a potential simple key
		if(m_simpleKeyAllowed)
			InsertSimpleKey();

		pToken->value = ScanScalar(INPUT, params);

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

		// finally, we can't have any colons in a scalar, so if we ended on a colon, there
		// had better be a break after it
		if(Exp::IllegalColonInScalar.Matches(INPUT))
			throw IllegalScalar();

		return pToken;
	}

	// QuotedScalarToken
	template <> QuotedScalarToken *Scanner::ScanToken(QuotedScalarToken *pToken)
	{
		// eat single or double quote
		char quote = INPUT.GetChar();
		pToken->single = (quote == '\'');

		// setup the scanning parameters
		ScanScalarParams params;
		params.end = (pToken->single ? RegEx(quote) && !Exp::EscSingleQuote : RegEx(quote));
		params.eatEnd = true;
		params.escape = (pToken->single ? '\'' : '\\');
		params.indent = 0;
		params.fold = true;
		params.eatLeadingWhitespace = true;
		params.trimTrailingSpaces = false;
		params.chomp = CLIP;
		params.onDocIndicator = THROW;

		// insert a potential simple key
		if(m_simpleKeyAllowed)
			InsertSimpleKey();

		pToken->value = ScanScalar(INPUT, params);
		m_simpleKeyAllowed = false;

		return pToken;
	}

	// 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.
	template <> BlockScalarToken *Scanner::ScanToken(BlockScalarToken *pToken)
	{
		ScanScalarParams params;
		params.indent = 1;
		params.detectIndent = true;

		// eat block indicator ('|' or '>')
		char indicator = INPUT.GetChar();
		params.fold = (indicator == Keys::FoldedScalar);

		// eat chomping/indentation indicators
		int n = Exp::Chomp.Match(INPUT);
		for(int i=0;i<n;i++) {
			char ch = INPUT.GetChar();
			if(ch == '+')
				params.chomp = KEEP;
			else if(ch == '-')
				params.chomp = STRIP;
			else if(Exp::Digit.Matches(ch)) {
				if(ch == '0')
					throw ZeroIndentationInBlockScalar();

				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))
			throw UnexpectedCharacterInBlockScalar();

		// set the initial indentation
		if(m_indents.top() >= 0)
			params.indent += m_indents.top();

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

		pToken->value = ScanScalar(INPUT, params);

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