map.cpp 4.32 KB
Newer Older
1
#include "crt.h"
2
3
#include "map.h"
#include "node.h"
4
5
#include "scanner.h"
#include "token.h"
Jesse Beder's avatar
Jesse Beder committed
6
#include "exceptions.h"
7
8
9
10
11
12
13
14

namespace YAML
{
	Map::Map()
	{
	}

	Map::~Map()
15
16
17
18
19
	{
		Clear();
	}

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

28
	bool Map::GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const
29
30
31
32
33
	{
		it = m_data.begin();
		return true;
	}

34
	bool Map::GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const
35
36
37
38
39
	{
		it = m_data.end();
		return true;
	}

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

		// split based on start token
45
		switch(pScanner->peek().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
		// eat start token
54
		pScanner->pop();
55

56
		while(1) {
57
			if(pScanner->empty())
58
				throw ParserException(-1, -1, ErrorMsg::END_OF_MAP);
59

60
			Token token = pScanner->peek();
61
62
			if(token.type != TT_KEY && token.type != TT_BLOCK_END)
				throw ParserException(token.line, token.column, ErrorMsg::END_OF_MAP);
63

64
			pScanner->pop();
65
			if(token.type == TT_BLOCK_END)
66
67
68
69
70
				break;

			Node *pKey = new Node;
			Node *pValue = new Node;

71
72
73
74
75
			try {
				// grab key
				pKey->Parse(pScanner, state);

				// now grab value (optional)
76
77
				if(!pScanner->empty() && pScanner->peek().type == TT_VALUE) {
					pScanner->pop();
78
79
80
81
82
83
84
85
					pValue->Parse(pScanner, state);
				}

				m_data[pKey] = pValue;
			} catch(Exception& e) {
				delete pKey;
				delete pValue;
				throw e;
86
87
88
89
			}
		}
	}

90
	void Map::ParseFlow(Scanner *pScanner, const ParserState& state)
91
	{
92
		// eat start token
93
		pScanner->pop();
94

95
		while(1) {
96
			if(pScanner->empty())
97
				throw ParserException(-1, -1, ErrorMsg::END_OF_MAP_FLOW);
98

99
			Token& token = pScanner->peek();
100
			// first check for end
101
			if(token.type == TT_FLOW_MAP_END) {
102
				pScanner->pop();
103
104
105
106
				break;
			}

			// now it better be a key
107
108
			if(token.type != TT_KEY)
				throw ParserException(token.line, token.column, ErrorMsg::END_OF_MAP_FLOW);
109

110
			pScanner->pop();
111
112
113
114

			Node *pKey = new Node;
			Node *pValue = new Node;

115
116
117
118
119
			try {
				// grab key
				pKey->Parse(pScanner, state);

				// now grab value (optional)
120
121
				if(!pScanner->empty() && pScanner->peek().type == TT_VALUE) {
					pScanner->pop();
122
123
124
125
					pValue->Parse(pScanner, state);
				}

				// now eat the separator (or could be a map end, which we ignore - but if it's neither, then it's a bad node)
126
				Token& nextToken = pScanner->peek();
127
				if(nextToken.type == TT_FLOW_ENTRY)
128
					pScanner->pop();
129
130
				else if(nextToken.type != TT_FLOW_MAP_END)
					throw ParserException(nextToken.line, nextToken.column, ErrorMsg::END_OF_MAP_FLOW);
131
132
133
134
135
136
137

				m_data[pKey] = pValue;
			} catch(Exception& e) {
				// clean up and rethrow
				delete pKey;
				delete pValue;
				throw e;
138
139
140
141
			}
		}
	}

142
	void Map::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
143
	{
144
145
		if(startedLine && !onlyOneCharOnLine)
			out << std::endl;
146
147

		for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
148
149
150
151
			if((startedLine && !onlyOneCharOnLine) || it != m_data.begin()) {
				for(int i=0;i<indent;i++)
					out << "  ";
			}
152

153
154
155
156
			out << "? ";
			it->first->Write(out, indent + 1, true, it!= m_data.begin() || !startedLine || onlyOneCharOnLine);

			for(int i=0;i<indent;i++)
157
				out << "  ";
158
159
			out << ": ";
			it->second->Write(out, indent + 1, true, true);
160
		}
161
162
163

		if(m_data.empty())
			out << std::endl;
164
	}
165

Jesse Beder's avatar
Jesse Beder committed
166
167
168
169
170
	CONTENT_TYPE Map::GetType() const
	{
		return CT_MAP;
	}

171
172
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
	int Map::Compare(Content *pContent)
	{
		return -pContent->Compare(this);
	}

	int Map::Compare(Map *pMap)
	{
		node_map::const_iterator it = m_data.begin(), jt = pMap->m_data.begin();
		while(1) {
			if(it == m_data.end()) {
				if(jt == pMap->m_data.end())
					return 0;
				else
					return -1;
			}
			if(jt == pMap->m_data.end())
				return 1;

			int cmp = it->first->Compare(*jt->first);
			if(cmp != 0)
				return cmp;

			cmp = it->second->Compare(*jt->second);
			if(cmp != 0)
				return cmp;
		}

		return 0;
	}
200
}