map.cpp 4.02 KB
Newer Older
1
2
3
4
5
6
7
#include "crt.h"
#include "map.h"
#include "node.h"
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include <iostream>
8
#include <memory>
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

namespace YAML
{
	Map::Map()
	{
	}

	Map::~Map()
	{
		Clear();
	}

	void Map::Clear()
	{
		for(node_map::const_iterator it=m_data.begin();it!=m_data.end();++it) {
			delete it->first;
			delete it->second;
		}
		m_data.clear();
	}

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

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

	void Map::Parse(Scanner *pScanner, const ParserState& state)
	{
		Clear();

		// split based on start token
		switch(pScanner->peek().type) {
			case TT_BLOCK_MAP_START: ParseBlock(pScanner, state); break;
			case TT_FLOW_MAP_START: ParseFlow(pScanner, state); break;
Jesse Beder's avatar
Jesse Beder committed
50
			default: break;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
		}
	}

	void Map::ParseBlock(Scanner *pScanner, const ParserState& state)
	{
		// eat start token
		pScanner->pop();

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

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

			pScanner->pop();
			if(token.type == TT_BLOCK_END)
				break;

71
			std::auto_ptr <Node> pKey(new Node), pValue(new Node);
72

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

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

			// assign the map with the actual pointers
			m_data[pKey.release()] = pValue.release();
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
		}
	}

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

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

			Token& token = pScanner->peek();
			// first check for end
			if(token.type == TT_FLOW_MAP_END) {
				pScanner->pop();
				break;
			}

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

			pScanner->pop();

109
110
111
112
113
114
115
116
117
			std::auto_ptr <Node> pKey(new Node), pValue(new Node);

			// grab key
			pKey->Parse(pScanner, state);

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

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

			// assign the map with the actual pointers
			m_data[pKey.release()] = pValue.release();
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
		}
	}

	void Map::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
	{
		if(startedLine && !onlyOneCharOnLine)
			out << "\n";

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

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

			for(int i=0;i<indent;i++)
				out << "  ";
			out << ": ";
			it->second->Write(out, indent + 1, true, true);
		}

		if(m_data.empty())
			out << "\n";
	}

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