summaryrefslogtreecommitdiffstats
path: root/yaml-cpp/src/nodebuilder.cpp
blob: 13a703260a42b414d17c36d9c533fab980d2d033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
#include "nodebuilder.h"
#include "yaml-cpp/mark.h"
#include "yaml-cpp/node.h"
#include <cassert>

namespace YAML
{
	NodeBuilder::NodeBuilder(Node& root): m_root(root), m_initializedRoot(false), m_finished(false)
	{
		m_root.Clear();
		m_anchors.push_back(0); // since the anchors start at 1
	}
	
	NodeBuilder::~NodeBuilder()
	{
	}

	void NodeBuilder::OnDocumentStart(const Mark&)
	{
	}

	void NodeBuilder::OnDocumentEnd()
	{
		assert(m_finished);
	}

	void NodeBuilder::OnNull(const Mark& mark, anchor_t anchor)
	{
		Node& node = Push(anchor);
		node.Init(NodeType::Null, mark, "");
		Pop();
	}

	void NodeBuilder::OnAlias(const Mark& /*mark*/, anchor_t anchor)
	{
		Node& node = *m_anchors[anchor];
		Insert(node);
		node.MarkAsAliased();
	}

	void NodeBuilder::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value)
	{
		Node& node = Push(anchor);
		node.Init(NodeType::Scalar, mark, tag);
		node.SetScalarData(value);
		Pop();
	}

	void NodeBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor)
	{
		Node& node = Push(anchor);
		node.Init(NodeType::Sequence, mark, tag);
	}

	void NodeBuilder::OnSequenceEnd()
	{
		Pop();
	}

	void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor)
	{
		Node& node = Push(anchor);
		node.Init(NodeType::Map, mark, tag);
		m_didPushKey.push(false);
	}

	void NodeBuilder::OnMapEnd()
	{
		m_didPushKey.pop();
		Pop();
	}
	
	Node& NodeBuilder::Push(anchor_t anchor)
	{
		Node& node = Push();
		RegisterAnchor(anchor, node);
		return node;
	}
	
	Node& NodeBuilder::Push()
	{
		if(!m_initializedRoot) {
			m_initializedRoot = true;
			return m_root;
		}
		
		Node& node = m_root.CreateNode();
		m_stack.push(&node);
		return node;
	}
	
	Node& NodeBuilder::Top()
	{
		return m_stack.empty() ? m_root : *m_stack.top();
	}
	
	void NodeBuilder::Pop()
	{
		assert(!m_finished);
		if(m_stack.empty()) {
			m_finished = true;
			return;
		}
		
		Node& node = *m_stack.top();
		m_stack.pop();
		Insert(node);
	}
	
	void NodeBuilder::Insert(Node& node)
	{
		Node& curTop = Top();
		switch(curTop.Type()) {
			case NodeType::Null:
			case NodeType::Scalar:
				assert(false);
				break;
			case NodeType::Sequence:
				curTop.Append(node);
				break;
			case NodeType::Map:
				assert(!m_didPushKey.empty());
				if(m_didPushKey.top()) {
					assert(!m_pendingKeys.empty());

					Node& key = *m_pendingKeys.top();
					m_pendingKeys.pop();
					curTop.Insert(key, node);
					m_didPushKey.top() = false;
				} else {
					m_pendingKeys.push(&node);
					m_didPushKey.top() = true;
				}
				break;
		}
	}

	void NodeBuilder::RegisterAnchor(anchor_t anchor, Node& node)
	{
		if(anchor) {
			assert(anchor == m_anchors.size());
			m_anchors.push_back(&node);
		}
	}
}