summaryrefslogtreecommitdiffstats
path: root/yaml-cpp/src/singledocparser.cpp
blob: 47759c324fa329fb62acb1e6c723615e0d070ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
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
#include "singledocparser.h"
#include "collectionstack.h"
#include "directives.h"
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/exceptions.h"
#include "scanner.h"
#include "tag.h"
#include "token.h"
#include <sstream>
#include <cstdio>
#include <algorithm>

namespace YAML
{
	SingleDocParser::SingleDocParser(Scanner& scanner, const Directives& directives): m_scanner(scanner), m_directives(directives), m_pCollectionStack(new CollectionStack), m_curAnchor(0)
	{
	}

	SingleDocParser::~SingleDocParser()
	{
	}

	// HandleDocument
	// . Handles the next document
	// . Throws a ParserException on error.
	void SingleDocParser::HandleDocument(EventHandler& eventHandler)
	{
		assert(!m_scanner.empty()); // guaranteed that there are tokens
		assert(!m_curAnchor);

		eventHandler.OnDocumentStart(m_scanner.peek().mark);
		
		// eat doc start
		if(m_scanner.peek().type == Token::DOC_START)
			m_scanner.pop();
		
		// recurse!
		HandleNode(eventHandler);
		
		eventHandler.OnDocumentEnd();
		
		// and finally eat any doc ends we see
		while(!m_scanner.empty() && m_scanner.peek().type == Token::DOC_END)
			m_scanner.pop();
	}
	
	void SingleDocParser::HandleNode(EventHandler& eventHandler)
	{
		// an empty node *is* a possibility
		if(m_scanner.empty()) {
			eventHandler.OnNull(Mark::null(), NullAnchor);
			return;
		}
		
		// save location
		Mark mark = m_scanner.peek().mark;
		
		// special case: a value node by itself must be a map, with no header
		if(m_scanner.peek().type == Token::VALUE) {
			eventHandler.OnMapStart(mark, "", NullAnchor);
			HandleMap(eventHandler);
			eventHandler.OnMapEnd();
			return;
		}
		
		// special case: an alias node
		if(m_scanner.peek().type == Token::ALIAS) {
			eventHandler.OnAlias(mark, LookupAnchor(mark, m_scanner.peek().value));
			m_scanner.pop();
			return;
		}
		
		std::string tag;
		anchor_t anchor;
		ParseProperties(tag, anchor);
		
		const Token& token = m_scanner.peek();
		
		// add non-specific tags
		if(tag.empty())
			tag = (token.type == Token::NON_PLAIN_SCALAR ? "!" : "?");
		
		// now split based on what kind of node we should be
		switch(token.type) {
			case Token::PLAIN_SCALAR:
			case Token::NON_PLAIN_SCALAR:
				eventHandler.OnScalar(mark, tag, anchor, token.value);
				m_scanner.pop();
				return;
			case Token::FLOW_SEQ_START:
			case Token::BLOCK_SEQ_START:
				eventHandler.OnSequenceStart(mark, tag, anchor);
				HandleSequence(eventHandler);
				eventHandler.OnSequenceEnd();
				return;
			case Token::FLOW_MAP_START:
			case Token::BLOCK_MAP_START:
				eventHandler.OnMapStart(mark, tag, anchor);
				HandleMap(eventHandler);
				eventHandler.OnMapEnd();
				return;
			case Token::KEY:
				// compact maps can only go in a flow sequence
				if(m_pCollectionStack->GetCurCollectionType() == CollectionType::FlowSeq) {
					eventHandler.OnMapStart(mark, tag, anchor);
					HandleMap(eventHandler);
					eventHandler.OnMapEnd();
					return;
				}
				break;
			default:
				break;
		}
		
		if(tag == "?")
			eventHandler.OnNull(mark, anchor);
		else
			eventHandler.OnScalar(mark, tag, anchor, "");
	}
	
	void SingleDocParser::HandleSequence(EventHandler& eventHandler)
	{
		// split based on start token
		switch(m_scanner.peek().type) {
			case Token::BLOCK_SEQ_START: HandleBlockSequence(eventHandler); break;
			case Token::FLOW_SEQ_START: HandleFlowSequence(eventHandler); break;
			default: break;
		}
	}
	
	void SingleDocParser::HandleBlockSequence(EventHandler& eventHandler)
	{
		// eat start token
		m_scanner.pop();
		m_pCollectionStack->PushCollectionType(CollectionType::BlockSeq);
		
		while(1) {
			if(m_scanner.empty())
				throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ);
			
			Token token = m_scanner.peek();
			if(token.type != Token::BLOCK_ENTRY && token.type != Token::BLOCK_SEQ_END)
				throw ParserException(token.mark, ErrorMsg::END_OF_SEQ);
			
			m_scanner.pop();
			if(token.type == Token::BLOCK_SEQ_END)
				break;
			
			// check for null
			if(!m_scanner.empty()) {
				const Token& token = m_scanner.peek();
				if(token.type == Token::BLOCK_ENTRY || token.type == Token::BLOCK_SEQ_END) {
					eventHandler.OnNull(token.mark, NullAnchor);
					continue;
				}
			}
			
			HandleNode(eventHandler);
		}
		
		m_pCollectionStack->PopCollectionType(CollectionType::BlockSeq);
	}
	
	void SingleDocParser::HandleFlowSequence(EventHandler& eventHandler)
	{
		// eat start token
		m_scanner.pop();
		m_pCollectionStack->PushCollectionType(CollectionType::FlowSeq);
		
		while(1) {
			if(m_scanner.empty())
				throw ParserException(Mark::null(), ErrorMsg::END_OF_SEQ_FLOW);
			
			// first check for end
			if(m_scanner.peek().type == Token::FLOW_SEQ_END) {
				m_scanner.pop();
				break;
			}
			
			// then read the node
			HandleNode(eventHandler);
			
			// now eat the separator (or could be a sequence end, which we ignore - but if it's neither, then it's a bad node)
			Token& token = m_scanner.peek();
			if(token.type == Token::FLOW_ENTRY)
				m_scanner.pop();
			else if(token.type != Token::FLOW_SEQ_END)
				throw ParserException(token.mark, ErrorMsg::END_OF_SEQ_FLOW);
		}
		
		m_pCollectionStack->PopCollectionType(CollectionType::FlowSeq);
	}
	
	void SingleDocParser::HandleMap(EventHandler& eventHandler)
	{
		// split based on start token
		switch(m_scanner.peek().type) {
			case Token::BLOCK_MAP_START: HandleBlockMap(eventHandler); break;
			case Token::FLOW_MAP_START: HandleFlowMap(eventHandler); break;
			case Token::KEY: HandleCompactMap(eventHandler); break;
			case Token::VALUE: HandleCompactMapWithNoKey(eventHandler); break;
			default: break;
		}
	}
	
	void SingleDocParser::HandleBlockMap(EventHandler& eventHandler)
	{
		// eat start token
		m_scanner.pop();
		m_pCollectionStack->PushCollectionType(CollectionType::BlockMap);
		
		while(1) {
			if(m_scanner.empty())
				throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP);
			
			Token token = m_scanner.peek();
			if(token.type != Token::KEY && token.type != Token::VALUE && token.type != Token::BLOCK_MAP_END)
				throw ParserException(token.mark, ErrorMsg::END_OF_MAP);
			
			if(token.type == Token::BLOCK_MAP_END) {
				m_scanner.pop();
				break;
			}
			
			// grab key (if non-null)
			if(token.type == Token::KEY) {
				m_scanner.pop();
				HandleNode(eventHandler);
			} else {
				eventHandler.OnNull(token.mark, NullAnchor);
			}
			
			// now grab value (optional)
			if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) {
				m_scanner.pop();
				HandleNode(eventHandler);
			} else {
				eventHandler.OnNull(token.mark, NullAnchor);
			}
		}
		
		m_pCollectionStack->PopCollectionType(CollectionType::BlockMap);
	}
	
	void SingleDocParser::HandleFlowMap(EventHandler& eventHandler)
	{
		// eat start token
		m_scanner.pop();
		m_pCollectionStack->PushCollectionType(CollectionType::FlowMap);
		
		while(1) {
			if(m_scanner.empty())
				throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP_FLOW);
			
			Token& token = m_scanner.peek();
			// first check for end
			if(token.type == Token::FLOW_MAP_END) {
				m_scanner.pop();
				break;
			}
			
			// grab key (if non-null)
			if(token.type == Token::KEY) {
				m_scanner.pop();
				HandleNode(eventHandler);
			} else {
				eventHandler.OnNull(token.mark, NullAnchor);
			}
			
			// now grab value (optional)
			if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) {
				m_scanner.pop();
				HandleNode(eventHandler);
			} else {
				eventHandler.OnNull(token.mark, NullAnchor);
			}
			
			// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
			Token& nextToken = m_scanner.peek();
			if(nextToken.type == Token::FLOW_ENTRY)
				m_scanner.pop();
			else if(nextToken.type != Token::FLOW_MAP_END)
				throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW);
		}
		
		m_pCollectionStack->PopCollectionType(CollectionType::FlowMap);
	}
	
	// . Single "key: value" pair in a flow sequence
	void SingleDocParser::HandleCompactMap(EventHandler& eventHandler)
	{
		m_pCollectionStack->PushCollectionType(CollectionType::CompactMap);
		
		// grab key
		Mark mark = m_scanner.peek().mark;
		m_scanner.pop();
		HandleNode(eventHandler);
		
		// now grab value (optional)
		if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) {
			m_scanner.pop();
			HandleNode(eventHandler);
		} else {
			eventHandler.OnNull(mark, NullAnchor);
		}
		
		m_pCollectionStack->PopCollectionType(CollectionType::CompactMap);
	}
	
	// . Single ": value" pair in a flow sequence
	void SingleDocParser::HandleCompactMapWithNoKey(EventHandler& eventHandler)
	{
		m_pCollectionStack->PushCollectionType(CollectionType::CompactMap);
		
		// null key
		eventHandler.OnNull(m_scanner.peek().mark, NullAnchor);
		
		// grab value
		m_scanner.pop();
		HandleNode(eventHandler);
		
		m_pCollectionStack->PopCollectionType(CollectionType::CompactMap);
	}
	
	// ParseProperties
	// . Grabs any tag or anchor tokens and deals with them.
	void SingleDocParser::ParseProperties(std::string& tag, anchor_t& anchor)
	{
		tag.clear();
		anchor = NullAnchor;
		
		while(1) {
			if(m_scanner.empty())
				return;
			
			switch(m_scanner.peek().type) {
				case Token::TAG: ParseTag(tag); break;
				case Token::ANCHOR: ParseAnchor(anchor); break;
				default: return;
			}
		}
	}
	
	void SingleDocParser::ParseTag(std::string& tag)
	{
		Token& token = m_scanner.peek();
		if(!tag.empty())
			throw ParserException(token.mark, ErrorMsg::MULTIPLE_TAGS);
		
		Tag tagInfo(token);
		tag = tagInfo.Translate(m_directives);
		m_scanner.pop();
	}
	
	void SingleDocParser::ParseAnchor(anchor_t& anchor)
	{
		Token& token = m_scanner.peek();
		if(anchor)
			throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS);
		
		anchor = RegisterAnchor(token.value);
		m_scanner.pop();
	}
	
	anchor_t SingleDocParser::RegisterAnchor(const std::string& name)
	{
		if(name.empty())
			return NullAnchor;
		
		return m_anchors[name] = ++m_curAnchor;
	}
	
	anchor_t SingleDocParser::LookupAnchor(const Mark& mark, const std::string& name) const
	{
		Anchors::const_iterator it = m_anchors.find(name);
		if(it == m_anchors.end())
			throw ParserException(mark, ErrorMsg::UNKNOWN_ANCHOR);
		
		return it->second;
	}
}