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

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

23
	Node::Node(): m_pContent(0), m_alias(false), m_pIdentity(this), m_referenced(true)
24
25
26
	{
	}

jbeder's avatar
jbeder committed
27
28
29
30
31
32
33
	Node::Node(const Mark& mark, const std::string& anchor, const std::string& tag, const Content *pContent)
	: m_mark(mark), m_anchor(anchor), m_tag(tag), m_pContent(0), m_alias(false), m_pIdentity(this), m_referenced(false)
	{
		if(m_pContent)
			m_pContent = pContent->Clone();
	}

34
35
36
37
38
39
40
41
42
43
	Node::~Node()
	{
		Clear();
	}

	void Node::Clear()
	{
		delete m_pContent;
		m_pContent = 0;
		m_alias = false;
44
45
46
		m_referenced = false;
		m_anchor.clear();
		m_tag.clear();
47
	}
jbeder's avatar
jbeder committed
48
49
50
51
52
53
54
55
	
	std::auto_ptr<Node> Node::Clone() const
	{
		if(m_alias)
			throw std::runtime_error("yaml-cpp: Can't clone alias");  // TODO: what to do about aliases?
		
		return std::auto_ptr<Node> (new Node(m_mark, m_anchor, m_tag, m_pContent));
	}
56

57
	void Node::Parse(Scanner *pScanner, ParserState& state)
58
59
60
	{
		Clear();

61
62
63
64
		// an empty node *is* a possibility
		if(pScanner->empty())
			return;

65
		// save location
66
		m_mark = pScanner->peek().mark;
67
68
69
70
71
72
73
		
		// special case: a value node by itself must be a map, with no header
		if(pScanner->peek().type == Token::VALUE) {
			m_pContent = new Map;
			m_pContent->Parse(pScanner, state);
			return;
		}
74

75
76
		ParseHeader(pScanner, state);

77
78
79
80
81
82
83
84
85
86
87
88
89
90
		// 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)
91
				m_pContent = new AliasContent(pAliasedContent);
92
			
93
			return;
94
		}
95
96
97

		// now split based on what kind of node we should be
		switch(pScanner->peek().type) {
jbeder's avatar
jbeder committed
98
			case Token::SCALAR:
99
100
				m_pContent = new Scalar;
				break;
jbeder's avatar
jbeder committed
101
102
			case Token::FLOW_SEQ_START:
			case Token::BLOCK_SEQ_START:
103
104
				m_pContent = new Sequence;
				break;
jbeder's avatar
jbeder committed
105
106
			case Token::FLOW_MAP_START:
			case Token::BLOCK_MAP_START:
107
108
				m_pContent = new Map;
				break;
109
110
111
112
113
			case Token::KEY:
				// compact maps can only go in a flow sequence
				if(state.GetCurCollectionType() == ParserState::FLOW_SEQ)
					m_pContent = new Map;
				break;
jbeder's avatar
jbeder committed
114
115
			default:
				break;
116
		}
117
118
119
120
121
122
123
124

		// 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);
125
126
127
128
	}

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

			switch(pScanner->peek().type) {
jbeder's avatar
jbeder committed
136
137
138
				case Token::TAG: ParseTag(pScanner, state); break;
				case Token::ANCHOR: ParseAnchor(pScanner, state); break;
				case Token::ALIAS: ParseAlias(pScanner, state); break;
139
140
141
142
143
				default: return;
			}
		}
	}

144
	void Node::ParseTag(Scanner *pScanner, ParserState& state)
145
146
147
	{
		Token& token = pScanner->peek();
		if(m_tag != "")
148
			throw ParserException(token.mark, ErrorMsg::MULTIPLE_TAGS);
149

150
151
		Tag tag(token);
		m_tag = tag.Translate(state);
152
153
154
		pScanner->pop();
	}
	
155
	void Node::ParseAnchor(Scanner *pScanner, ParserState& /*state*/)
156
157
158
	{
		Token& token = pScanner->peek();
		if(m_anchor != "")
159
			throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS);
160
161
162
163
164
165

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

166
	void Node::ParseAlias(Scanner *pScanner, ParserState& /*state*/)
167
168
169
	{
		Token& token = pScanner->peek();
		if(m_anchor != "")
170
			throw ParserException(token.mark, ErrorMsg::MULTIPLE_ALIASES);
171
		if(m_tag != "")
172
			throw ParserException(token.mark, ErrorMsg::ALIAS_CONTENT);
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

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

	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.
233
	std::size_t Node::size() const
234
235
236
237
238
239
240
	{
		if(!m_pContent)
			return 0;

		return m_pContent->GetSize();
	}

241
	const Node *Node::FindAtIndex(std::size_t i) const
242
243
	{
		if(!m_pContent)
244
245
246
			return 0;
		
		return m_pContent->GetNode(i);
247
248
	}

249
	bool Node::GetScalar(std::string& s) const
250
	{
jbeder's avatar
jbeder committed
251
		if(!m_pContent) {
252
253
254
255
			if(m_tag.empty())
				s = "~";
			else
				s = "";
jbeder's avatar
jbeder committed
256
257
258
			return true;
		}
		
259
		return m_pContent->GetScalar(s);
260
261
	}

262
	Emitter& operator << (Emitter& out, const Node& node)
263
	{
264
265
266
267
268
269
270
271
		// write anchor/alias
		if(node.m_anchor != "") {
			if(node.m_alias)
				out << Alias(node.m_anchor);
			else
				out << Anchor(node.m_anchor);
		}

272
273
		if(node.m_tag != "")
			out << VerbatimTag(node.m_tag);
274
275
276
277

		// write content
		if(node.m_pContent)
			node.m_pContent->Write(out);
278
		else if(!node.m_alias)
jbeder's avatar
jbeder committed
279
			out << Null;
280

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		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;
	}
}