"examples/community/stable_diffusion_repaint.py" did not exist on "99c39b40121a6ea6fcecc575d38bc6ff12b0d979"
node.cpp 6.47 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"
10
#include "iterpriv.h"
Jesse Beder's avatar
Jesse Beder committed
11
12
13

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

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

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

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

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

40
		ParseHeader(pScanner, state);
41

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

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

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

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

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

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

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

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

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

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

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

		// write tag
		if(m_tag != "") {
135
			out << std::string("!<") << m_tag << std::string("> ");
136
137
			startedLine = true;
			onlyOneCharOnLine = false;
138
139
		}

140
		if(!m_pContent) {
141
			out << std::string("\n");
142
		} else {
143
			m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine);
144
		}
145
	}
146

Jesse Beder's avatar
Jesse Beder committed
147
148
149
150
	CONTENT_TYPE Node::GetType() const
	{
		if(!m_pContent)
			return CT_NONE;
Jesse Beder's avatar
Jesse Beder committed
151
152
153
154
155
156
157

		if(m_pContent->IsScalar())
			return CT_SCALAR;
		else if(m_pContent->IsSequence())
			return CT_SEQUENCE;
		else if(m_pContent->IsMap())
			return CT_MAP;
158
159
			
		return CT_NONE;
Jesse Beder's avatar
Jesse Beder committed
160
161
	}

162
163
	// begin
	// Returns an iterator to the beginning of this (sequence or map).
164
	Iterator Node::begin() const
165
166
167
168
169
170
	{
		if(!m_pContent)
			return Iterator();

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

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

		return Iterator();
	}

	// end
	// . Returns an iterator to the end of this (sequence or map).
182
	Iterator Node::end() const
183
184
185
186
187
188
	{
		if(!m_pContent)
			return Iterator();

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

191
		std::map <Node *, Node *, ltnode>::const_iterator mapIter;
192
		if(m_pContent->GetEnd(mapIter))
193
			return Iterator(new IterPriv(mapIter));
194
195
196
197

		return Iterator();
	}

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
	// 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);
	}

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
	///////////////////////////////////////////////////////
	// 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);
	}
291
292
293
294
295
296

	std::ostream& operator << (std::ostream& out, const Node& node)
	{
		node.Write(out, 0, false, false);
		return out;
	}
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

	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
317
}