"...semantic_stable_diffusion/test_semantic_diffusion.py" did not exist on "05a36d5c1a17d0a3dbc7a1efb83ba1bdbfbf1fb2"
singledocparser.cpp 10 KB
Newer Older
1
2
3
#include "singledocparser.h"
#include "collectionstack.h"
#include "directives.h"
4
5
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/exceptions.h"
6
7
8
9
10
#include "scanner.h"
#include "tag.h"
#include "token.h"
#include <sstream>
#include <cstdio>
11
#include <algorithm>
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

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()) {
51
			eventHandler.OnNull(Mark::null(), NullAnchor);
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
			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);
		
77
78
79
80
81
82
		const Token& token = m_scanner.peek();
		
		// add non-specific tags
		if(tag.empty())
			tag = (token.type == Token::NON_PLAIN_SCALAR ? "!" : "?");
		
83
		// now split based on what kind of node we should be
84
85
86
87
		switch(token.type) {
			case Token::PLAIN_SCALAR:
			case Token::NON_PLAIN_SCALAR:
				eventHandler.OnScalar(mark, tag, anchor, token.value);
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
				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;
		}
		
115
116
117
118
		if(tag == "?")
			eventHandler.OnNull(mark, anchor);
		else
			eventHandler.OnScalar(mark, tag, anchor, "");
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
	}
	
	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) {
153
					eventHandler.OnNull(token.mark, NullAnchor);
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
					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 {
230
				eventHandler.OnNull(token.mark, NullAnchor);
231
232
233
234
235
236
237
			}
			
			// now grab value (optional)
			if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) {
				m_scanner.pop();
				HandleNode(eventHandler);
			} else {
238
				eventHandler.OnNull(token.mark, NullAnchor);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
			}
		}
		
		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();
Jesse Beder's avatar
Jesse Beder committed
256
            const Mark mark = token.mark;
257
258
259
260
261
262
263
264
265
266
267
			// 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 {
Jesse Beder's avatar
Jesse Beder committed
268
				eventHandler.OnNull(mark, NullAnchor);
269
270
271
272
273
274
275
			}
			
			// now grab value (optional)
			if(!m_scanner.empty() && m_scanner.peek().type == Token::VALUE) {
				m_scanner.pop();
				HandleNode(eventHandler);
			} else {
Jesse Beder's avatar
Jesse Beder committed
276
				eventHandler.OnNull(mark, NullAnchor);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
			}
			
			// 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
296
		Mark mark = m_scanner.peek().mark;
297
298
299
300
301
302
303
304
		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 {
305
			eventHandler.OnNull(mark, NullAnchor);
306
307
308
309
310
311
312
313
314
315
316
		}
		
		m_pCollectionStack->PopCollectionType(CollectionType::CompactMap);
	}
	
	// . Single ": value" pair in a flow sequence
	void SingleDocParser::HandleCompactMapWithNoKey(EventHandler& eventHandler)
	{
		m_pCollectionStack->PushCollectionType(CollectionType::CompactMap);
		
		// null key
317
		eventHandler.OnNull(m_scanner.peek().mark, NullAnchor);
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
		
		// 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;
	}
}