"vscode:/vscode.git/clone" did not exist on "e739c5771adc27eafe28af03f7ec284a407129dd"
map.cpp 3.12 KB
Newer Older
1
2
#include "map.h"
#include "node.h"
3
4
#include "scanner.h"
#include "token.h"
5
6
7
8
9
10
11
12

namespace YAML
{
	Map::Map()
	{
	}

	Map::~Map()
13
14
15
16
17
	{
		Clear();
	}

	void Map::Clear()
18
19
20
21
22
	{
		for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
			delete it->first;
			delete it->second;
		}
23
		m_data.clear();
24
	}
25

26
27
28
29
30
31
32
33
34
35
36
37
	bool Map::GetBegin(std::map <Node *, Node *>::const_iterator& it)
	{
		it = m_data.begin();
		return true;
	}

	bool Map::GetEnd(std::map <Node *, Node *>::const_iterator& it)
	{
		it = m_data.end();
		return true;
	}

38
	void Map::Parse(Scanner *pScanner, const ParserState& state)
39
	{
40
41
42
43
		Clear();

		// split based on start token
		Token *pToken = pScanner->PeekNextToken();
44
45

		switch(pToken->type) {
46
47
			case TT_BLOCK_MAP_START: ParseBlock(pScanner, state); break;
			case TT_FLOW_MAP_START: ParseFlow(pScanner, state); break;
48
49
50
		}
	}

51
	void Map::ParseBlock(Scanner *pScanner, const ParserState& state)
52
	{
53
54
55
		// eat start token
		pScanner->EatNextToken();

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		while(1) {
			Token *pToken = pScanner->PeekNextToken();
			if(!pToken)
				break;  // TODO: throw?

			if(pToken->type != TT_KEY && pToken->type != TT_BLOCK_END)
				break;  // TODO: throw?

			pScanner->PopNextToken();
			if(pToken->type == TT_BLOCK_END)
				break;

			Node *pKey = new Node;
			Node *pValue = new Node;
			m_data[pKey] = pValue;

			// grab key
73
			pKey->Parse(pScanner, state);
74
75
76
77

			// now grab value (optional)
			if(pScanner->PeekNextToken() && pScanner->PeekNextToken()->type == TT_VALUE) {
				pScanner->PopNextToken();
78
				pValue->Parse(pScanner, state);
79
80
81
82
			}
		}
	}

83
	void Map::ParseFlow(Scanner *pScanner, const ParserState& state)
84
	{
85
86
87
		// eat start token
		pScanner->EatNextToken();

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
		while(1) {
			Token *pToken = pScanner->PeekNextToken();
			if(!pToken)
				break;  // TODO: throw?

			// first check for end
			if(pToken->type == TT_FLOW_MAP_END) {
				pScanner->EatNextToken();
				break;
			}

			// now it better be a key
			if(pToken->type != TT_KEY)
				break;  // TODO: throw?

			pScanner->PopNextToken();

			Node *pKey = new Node;
			Node *pValue = new Node;
			m_data[pKey] = pValue;

			// grab key
110
			pKey->Parse(pScanner, state);
111
112
113
114

			// now grab value (optional)
			if(pScanner->PeekNextToken() && pScanner->PeekNextToken()->type == TT_VALUE) {
				pScanner->PopNextToken();
115
				pValue->Parse(pScanner, state);
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
			}

			// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
			pToken = pScanner->PeekNextToken();
			if(pToken->type == TT_FLOW_ENTRY)
				pScanner->EatNextToken();
			else if(pToken->type != TT_FLOW_MAP_END)
				break;  // TODO: throw?
		}
	}

	void Map::Write(std::ostream& out, int indent)
	{
		for(int i=0;i<indent;i++)
			out << "  ";
		out << "{map}\n";

		for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
			for(int i=0;i<indent + 1;i++)
				out << "  ";
			out << "{key}\n";
			it->first->Write(out, indent + 2);

			for(int i=0;i<indent + 1;i++)
				out << "  ";
			out << "{value}\n";
			it->second->Write(out, indent + 2);
		}
	}
145
}