sequence.cpp 3.75 KB
Newer Older
1
#include "crt.h"
2
3
#include "sequence.h"
#include "node.h"
4
5
#include "scanner.h"
#include "token.h"
6
7
8

namespace YAML
{
Jesse Beder's avatar
Jesse Beder committed
9
	Sequence::Sequence()
10
	{
Jesse Beder's avatar
Jesse Beder committed
11

12
13
14
	}

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

	void Sequence::Clear()
20
21
22
	{
		for(unsigned i=0;i<m_data.size();i++)
			delete m_data[i];
23
		m_data.clear();
24
	}
25

26
	bool Sequence::GetBegin(std::vector <Node *>::const_iterator& it) const
27
28
29
30
31
	{
		it = m_data.begin();
		return true;
	}

32
	bool Sequence::GetEnd(std::vector <Node *>::const_iterator& it) const
33
34
35
36
37
	{
		it = m_data.end();
		return true;
	}

38
39
40
41
42
43
44
45
46
47
48
49
	Node *Sequence::GetNode(unsigned i) const
	{
		if(i < m_data.size())
			return m_data[i];
		return 0;
	}

	unsigned Sequence::GetSize() const
	{
		return m_data.size();
	}

50
	void Sequence::Parse(Scanner *pScanner, const ParserState& state)
51
	{
52
53
54
		Clear();

		// split based on start token
55
		switch(pScanner->peek().type) {
56
57
58
			case TT_BLOCK_SEQ_START: ParseBlock(pScanner, state); break;
			case TT_BLOCK_ENTRY: ParseImplicit(pScanner, state); break;
			case TT_FLOW_SEQ_START: ParseFlow(pScanner, state); break;
59
60
61
		}
	}

62
	void Sequence::ParseBlock(Scanner *pScanner, const ParserState& state)
63
	{
64
		// eat start token
65
		pScanner->pop();
66

67
		while(1) {
68
			if(pScanner->empty())
69
				throw ParserException(-1, -1, ErrorMsg::END_OF_SEQ);
70

71
			Token token = pScanner->peek();
72
73
			if(token.type != TT_BLOCK_ENTRY && token.type != TT_BLOCK_END)
				throw ParserException(token.line, token.column, ErrorMsg::END_OF_SEQ);
74

75
			pScanner->pop();
76
			if(token.type == TT_BLOCK_END)
77
78
79
80
				break;

			Node *pNode = new Node;
			m_data.push_back(pNode);
81
			pNode->Parse(pScanner, state);
82
83
84
		}
	}

85
	void Sequence::ParseImplicit(Scanner *pScanner, const ParserState& state)
86
	{
87
88
		while(1) {
			// we're actually *allowed* to have no tokens at some point
89
			if(pScanner->empty())
90
91
92
				break;

			// and we end at anything other than a block entry
93
			Token& token = pScanner->peek();
94
			if(token.type != TT_BLOCK_ENTRY)
95
96
				break;

97
			pScanner->pop();
98
99
100

			Node *pNode = new Node;
			m_data.push_back(pNode);
101
			pNode->Parse(pScanner, state);
102
		}
103
104
	}

105
	void Sequence::ParseFlow(Scanner *pScanner, const ParserState& state)
106
	{
107
		// eat start token
108
		pScanner->pop();
109

110
		while(1) {
111
			if(pScanner->empty())
112
				throw ParserException(-1, -1, ErrorMsg::END_OF_SEQ_FLOW);
113
114

			// first check for end
115
116
			if(pScanner->peek().type == TT_FLOW_SEQ_END) {
				pScanner->pop();
117
118
119
120
121
122
				break;
			}

			// then read the node
			Node *pNode = new Node;
			m_data.push_back(pNode);
123
			pNode->Parse(pScanner, state);
124
125

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

134
	void Sequence::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
135
	{
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		if(startedLine && !onlyOneCharOnLine)
			out << std::endl;

		for(unsigned i=0;i<m_data.size();i++) {
			if((startedLine && !onlyOneCharOnLine) || i > 0) {
				for(int j=0;j<indent;j++)
					out  << "  ";
			}

			out << "- ";
			m_data[i]->Write(out, indent + 1, true, i > 0 || !startedLine || onlyOneCharOnLine);
		}

		if(m_data.empty())
			out << std::endl;
151
	}
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

	int Sequence::Compare(Content *pContent)
	{
		return -pContent->Compare(this);
	}

	int Sequence::Compare(Sequence *pSeq)
	{
		unsigned n = m_data.size(), m = pSeq->m_data.size();
		if(n < m)
			return -1;
		else if(n > m)
			return 1;

		for(unsigned i=0;i<n;i++) {
			int cmp = m_data[i]->Compare(*pSeq->m_data[i]);
			if(cmp != 0)
				return cmp;
		}

		return 0;
	}
174
}