node.cpp 7.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include "crt.h"
#include "node.h"
#include "token.h"
#include "scanner.h"
#include "content.h"
#include "parser.h"
#include "scalar.h"
#include "sequence.h"
#include "map.h"
10
#include "alias.h"
11
#include "iterpriv.h"
12
#include <iostream>
13
14
15
16
17
18
19
20
21

namespace YAML
{
	// the ordering!
	bool ltnode::operator ()(const Node *pNode1, const Node *pNode2) const
	{
		return *pNode1 < *pNode2;
	}

22
	Node::Node(): m_pContent(0), m_alias(false), m_pIdentity(this), m_referenced(true)
23
24
25
26
27
28
29
30
31
32
33
34
35
	{
	}

	Node::~Node()
	{
		Clear();
	}

	void Node::Clear()
	{
		delete m_pContent;
		m_pContent = 0;
		m_alias = false;
36
37
38
		m_referenced = false;
		m_anchor.clear();
		m_tag.clear();
39
40
41
42
43
44
	}

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

45
46
47
48
		// an empty node *is* a possibility
		if(pScanner->empty())
			return;

49
50
51
52
		// save location
		m_line = pScanner->peek().line;
		m_column = pScanner->peek().column;

53
54
		ParseHeader(pScanner, state);

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
		// is this an alias? if so, its contents are an alias to
		// a previously defined anchor
		if(m_alias) {
			// the scanner throws an exception if it doesn't know this anchor name
			const Node *pReferencedNode = pScanner->Retrieve(m_anchor);
			m_pIdentity = pReferencedNode;

			// mark the referenced node for the sake of the client code
			pReferencedNode->m_referenced = true;

			// use of an Alias object keeps the referenced content from
			// being deleted twice
			Content *pAliasedContent = pReferencedNode->m_pContent;
			if(pAliasedContent)
				m_pContent = new Alias(pAliasedContent);
			
71
			return;
72
		}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

		// now split based on what kind of node we should be
		switch(pScanner->peek().type) {
			case TT_SCALAR:
				m_pContent = new Scalar;
				break;
			case TT_FLOW_SEQ_START:
			case TT_BLOCK_SEQ_START:
			case TT_BLOCK_ENTRY:
				m_pContent = new Sequence;
				break;
			case TT_FLOW_MAP_START:
			case TT_BLOCK_MAP_START:
				m_pContent = new Map;
				break;
jbeder's avatar
jbeder committed
88
89
			default:
				break;
90
		}
91
92
93
94
95
96
97
98

		// Have to save anchor before parsing to allow for aliases as
		// contained node (recursive structure)
		if(!m_anchor.empty())
			pScanner->Save(m_anchor, this);

		if(m_pContent)
			m_pContent->Parse(pScanner, state);
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
	}

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

			switch(pScanner->peek().type) {
				case TT_TAG: ParseTag(pScanner, state); break;
				case TT_ANCHOR: ParseAnchor(pScanner, state); break;
				case TT_ALIAS: ParseAlias(pScanner, state); break;
				default: return;
			}
		}
	}

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

		m_tag = state.TranslateTag(token.value);

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

		m_anchor = token.value;
		m_alias = false;
		pScanner->pop();
	}

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

		m_anchor = token.value;
		m_alias = true;
		pScanner->pop();
	}

	void Node::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine) const
	{
157
158
159
160
		// If using an anchor or tag for the whole document, document start
		// must be explicit
		bool indicateDocStart = (indent == 0);

161
162
		// write anchor/alias
		if(m_anchor != "") {
163
164
165
166
167
			if (indicateDocStart) {
				out << "--- ";
				indicateDocStart = false;
			}

168
			if(m_alias)
169
				out << "*";
170
			else
171
172
				out << "&";
			out << m_anchor << " ";
173
174
175
176
177
178
			startedLine = true;
			onlyOneCharOnLine = false;
		}

		// write tag
		if(m_tag != "") {
179
180
181
182
183
			if (indicateDocStart) {
				out << "--- ";
				indicateDocStart = false;
			}

jbeder's avatar
jbeder committed
184
			// put the tag in the "proper" brackets
185
186
			if(m_tag.substr(0, 2) == std::string("!<") && m_tag.substr(m_tag.size() - 1) == std::string(">"))
				out << m_tag << " ";
jbeder's avatar
jbeder committed
187
			else
188
				out << "!<" << m_tag << "> ";
189
190
191
192
			startedLine = true;
			onlyOneCharOnLine = false;
		}

193
194
195
		if(!m_pContent)
			out << "\n";
		else
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
			m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine);
	}

	CONTENT_TYPE Node::GetType() const
	{
		if(!m_pContent)
			return CT_NONE;

		if(m_pContent->IsScalar())
			return CT_SCALAR;
		else if(m_pContent->IsSequence())
			return CT_SEQUENCE;
		else if(m_pContent->IsMap())
			return CT_MAP;
			
		return CT_NONE;
	}

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

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

		std::map <Node *, Node *, ltnode>::const_iterator mapIter;
		if(m_pContent->GetBegin(mapIter))
			return Iterator(new IterPriv(mapIter));

		return Iterator();
	}

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

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

		std::map <Node *, Node *, ltnode>::const_iterator mapIter;
		if(m_pContent->GetEnd(mapIter))
			return Iterator(new IterPriv(mapIter));

		return Iterator();
	}

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

	///////////////////////////////////////////////////////
	// Extraction
287
288
289
290
	// Note: these Read() functions are identical, but
	// they're not templated because they use a Content virtual
	// function, so we'd have to #include that in node.h, and
	// I don't want to.
291

292
	bool Node::Read(std::string& s) const
293
	{
294
295
		if(!m_pContent)
			return false;
296

297
		return m_pContent->Read(s);
298
299
	}

300
	bool Node::Read(int& i) const
301
	{
302
303
		if(!m_pContent)
			return false;
304

305
		return m_pContent->Read(i);
306
307
	}

308
	bool Node::Read(unsigned& u) const
309
	{
310
311
		if(!m_pContent)
			return false;
312

313
		return m_pContent->Read(u);
314
315
	}

316
	bool Node::Read(long& l) const
317
	{
318
319
		if(!m_pContent)
			return false;
320

321
		return m_pContent->Read(l);
322
323
	}

324
	bool Node::Read(float& f) const
325
	{
326
327
		if(!m_pContent)
			return false;
328

329
		return m_pContent->Read(f);
330
331
	}

332
	bool Node::Read(double& d) const
333
	{
334
335
		if(!m_pContent)
			return false;
336

337
		return m_pContent->Read(d);
338
339
	}

340
	bool Node::Read(char& c) const
341
	{
342
343
		if(!m_pContent)
			return false;
344

345
		return m_pContent->Read(c);
346
347
	}

348
349
350
351
352
353
354
355
	bool Node::Read(bool& b) const
	{
		if(!m_pContent)
			return false;

		return m_pContent->Read(b);
	}

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
	std::ostream& operator << (std::ostream& out, const Node& node)
	{
		node.Write(out, 0, false, false);
		return out;
	}

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