node.cpp 6.17 KB
Newer Older
1
#include "crt.h"
beder's avatar
beder committed
2
#include "node.h"
3
4
#include "token.h"
#include "scanner.h"
5
#include "content.h"
beder's avatar
beder committed
6
7
8
#include "parser.h"
#include "scalar.h"
#include "sequence.h"
9
#include "map.h"
10
#include "iterpriv.h"
beder's avatar
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)
beder's avatar
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->PeekToken().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
72
			if(pScanner->IsEmpty())
				return;
73

74
			switch(pScanner->PeekToken().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->PeekToken();
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
93
		for(unsigned i=0;i<token.params.size();i++)
			m_tag += token.params[i];
		pScanner->PopToken();
94
95
	}
	
96
	void Node::ParseAnchor(Scanner *pScanner, const ParserState& state)
97
	{
98
		Token& token = pScanner->PeekToken();
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->PopToken();
105
106
	}

107
	void Node::ParseAlias(Scanner *pScanner, const ParserState& state)
108
	{
109
		Token& token = pScanner->PeekToken();
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->PopToken();
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 << "*";
126
			else
127
128
129
130
131
132
133
134
135
136
137
				out << "&";
			out << m_anchor << " ";
			startedLine = true;
			onlyOneCharOnLine = false;
		}

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

140
		if(!m_pContent) {
141
			out << std::endl;
142
		} else {
143
			m_pContent->Write(out, indent, startedLine, onlyOneCharOnLine);
144
		}
145
	}
146
147
148

	// begin
	// Returns an iterator to the beginning of this (sequence or map).
149
	Iterator Node::begin() const
150
151
152
153
154
155
	{
		if(!m_pContent)
			return Iterator();

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

158
		std::map <Node *, Node *, ltnode>::const_iterator mapIter;
159
		if(m_pContent->GetBegin(mapIter))
160
			return Iterator(new IterPriv(mapIter));
161
162
163
164
165
166

		return Iterator();
	}

	// end
	// . Returns an iterator to the end of this (sequence or map).
167
	Iterator Node::end() const
168
169
170
171
172
173
	{
		if(!m_pContent)
			return Iterator();

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

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

		return Iterator();
	}

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

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
	///////////////////////////////////////////////////////
	// 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);
	}
276
277
278
279
280
281

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

	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;
	}
beder's avatar
beder committed
302
}