"examples/community/sd_text2img_k_diffusion.py" did not exist on "bf7b0bc25b4a10da78ae9dd19ae00ba840585d40"
node.cpp 6.11 KB
Newer Older
1
#include "crt.h"
Jesse Beder's avatar
Jesse Beder committed
2
#include "node.h"
3
4
#include "token.h"
#include "scanner.h"
5
#include "content.h"
6
7
8
#include "parser.h"
#include "scalar.h"
#include "sequence.h"
9
#include "map.h"
Jesse Beder's avatar
Jesse Beder committed
10
11
12

namespace YAML
{
13
14
15
16
17
18
	// the ordering!
	bool ltnode::operator ()(const Node *pNode1, const Node *pNode2) const
	{
		return *pNode1 < *pNode2;
	}

19
	Node::Node(): m_pContent(0), m_alias(false)
Jesse Beder's avatar
Jesse Beder committed
20
21
22
23
	{
	}

	Node::~Node()
24
25
26
27
28
29
30
31
	{
		Clear();
	}

	void Node::Clear()
	{
		delete m_pContent;
		m_pContent = 0;
32
		m_alias = false;
33
	}
34

35
	void Node::Parse(Scanner *pScanner, const ParserState& state)
36
	{
37
38
		Clear();

39
		ParseHeader(pScanner, state);
40

41
42
43
44
		// is this an alias? if so, it can have no content
		if(m_alias)
			return;

45
		// now split based on what kind of node we should be
46
		switch(pScanner->PeekToken().type) {
47
48
			case TT_SCALAR:
				m_pContent = new Scalar;
49
				m_pContent->Parse(pScanner, state);
50
51
52
53
54
				break;
			case TT_FLOW_SEQ_START:
			case TT_BLOCK_SEQ_START:
			case TT_BLOCK_ENTRY:
				m_pContent = new Sequence;
55
				m_pContent->Parse(pScanner, state);
56
57
58
59
				break;
			case TT_FLOW_MAP_START:
			case TT_BLOCK_MAP_START:
				m_pContent = new Map;
60
				m_pContent->Parse(pScanner, state);
61
				break;
62
63
64
65
66
		}
	}

	// ParseHeader
	// . Grabs any tag, alias, or anchor tokens and deals with them.
67
	void Node::ParseHeader(Scanner *pScanner, const ParserState& state)
68
69
	{
		while(1) {
70
71
			if(pScanner->IsEmpty())
				return;
72

73
			switch(pScanner->PeekToken().type) {
74
75
76
				case TT_TAG: ParseTag(pScanner, state); break;
				case TT_ANCHOR: ParseAnchor(pScanner, state); break;
				case TT_ALIAS: ParseAlias(pScanner, state); break;
77
				default: return;
78
79
80
81
			}
		}
	}

82
	void Node::ParseTag(Scanner *pScanner, const ParserState& state)
83
	{
84
		Token& token = pScanner->PeekToken();
85
		if(m_tag != "")
86
			throw ParserException(token.line, token.column, ErrorMsg::MULTIPLE_TAGS);
87

88
		m_tag = state.TranslateTag(token.value);
89

90
91
92
		for(unsigned i=0;i<token.params.size();i++)
			m_tag += token.params[i];
		pScanner->PopToken();
93
94
	}
	
95
	void Node::ParseAnchor(Scanner *pScanner, const ParserState& state)
96
	{
97
		Token& token = pScanner->PeekToken();
98
		if(m_anchor != "")
99
			throw ParserException(token.line, token.column, ErrorMsg::MULTIPLE_ANCHORS);
100

101
		m_anchor = token.value;
102
		m_alias = false;
103
		pScanner->PopToken();
104
105
	}

106
	void Node::ParseAlias(Scanner *pScanner, const ParserState& state)
107
	{
108
		Token& token = pScanner->PeekToken();
109
		if(m_anchor != "")
110
			throw ParserException(token.line, token.column, ErrorMsg::MULTIPLE_ALIASES);
111
		if(m_tag != "")
112
			throw ParserException(token.line, token.column, ErrorMsg::ALIAS_CONTENT);
113

114
		m_anchor = token.value;
115
		m_alias = true;
116
		pScanner->PopToken();
117
118
	}

119
	void Node::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) const
120
	{
121
		// write anchor/alias
122
123
		if(m_anchor != "") {
			if(m_alias)
124
				out << "*";
125
			else
126
127
128
129
130
131
132
133
134
135
136
				out << "&";
			out << m_anchor << " ";
			startedLine = true;
			onlyOneCharOnLine = false;
		}

		// write tag
		if(m_tag != "") {
			out << "!<" << m_tag << "> ";
			startedLine = true;
			onlyOneCharOnLine = false;
137
138
		}

139
		if(!m_pContent) {
140
			out << std::endl;
141
		} else {
142
			m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine);
143
		}
144
	}
145
146
147
148
149
150
151
152
153
154
155
156

	// begin
	// Returns an iterator to the beginning of this (sequence or map).
	Node::Iterator Node::begin() const
	{
		if(!m_pContent)
			return Iterator();

		std::vector <Node *>::const_iterator seqIter;
		if(m_pContent->GetBegin(seqIter))
			return Iterator(seqIter);

157
		std::map <Node *, Node *, ltnode>::const_iterator mapIter;
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		if(m_pContent->GetBegin(mapIter))
			return Iterator(mapIter);

		return Iterator();
	}

	// end
	// . Returns an iterator to the end of this (sequence or map).
	Node::Iterator Node::end() const
	{
		if(!m_pContent)
			return Iterator();

		std::vector <Node *>::const_iterator seqIter;
		if(m_pContent->GetEnd(seqIter))
			return Iterator(seqIter);

175
		std::map <Node *, Node *, ltnode>::const_iterator mapIter;
176
177
178
179
180
181
		if(m_pContent->GetEnd(mapIter))
			return Iterator(mapIter);

		return Iterator();
	}

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
	// size
	// . Returns the size of this node, if it's a sequence node.
	// . Otherwise, returns zero.
	unsigned Node::size() const
	{
		if(!m_pContent)
			return 0;

		return m_pContent->GetSize();
	}

	const Node& Node::operator [] (unsigned u) const
	{
		if(!m_pContent)
			throw BadDereference();

		Node *pNode = m_pContent->GetNode(u);
		if(pNode)
			return *pNode;

		return GetValue(u);
	}

	const Node& Node::operator [] (int i) const
	{
		if(!m_pContent)
			throw BadDereference();

		Node *pNode = m_pContent->GetNode(i);
		if(pNode)
			return *pNode;

		return GetValue(i);
	}

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
	///////////////////////////////////////////////////////
	// Extraction

	void operator >> (const Node& node, std::string& s)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(s);
	}

	void operator >> (const Node& node, int& i)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(i);
	}

	void operator >> (const Node& node, unsigned& u)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(u);
	}

	void operator >> (const Node& node, long& l)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(l);
	}

	void operator >> (const Node& node, float& f)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(f);
	}

	void operator >> (const Node& node, double& d)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(d);
	}

	void operator >> (const Node& node, char& c)
	{
		if(!node.m_pContent)
			throw;

		node.m_pContent->Read(c);
	}
275
276
277
278
279
280

	std::ostream& operator << (std::ostream& out, const Node& node)
	{
		node.Write(out, 0, false, false);
		return out;
	}
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

	int Node::Compare(const Node& rhs) const
	{
		// Step 1: no content is the smallest
		if(!m_pContent) {
			if(rhs.m_pContent)
				return -1;
			else
				return 0;
		}
		if(!rhs.m_pContent)
			return 1;

		return m_pContent->Compare(rhs.m_pContent);
	}

	bool operator < (const Node& n1, const Node& n2)
	{
		return n1.Compare(n2) < 0;
	}
Jesse Beder's avatar
Jesse Beder committed
301
}