sequence.cpp 3.76 KB
Newer Older
1
#include "crt.h"
2
3
#include "sequence.h"
#include "node.h"
4
#include "scanner.h"
5
6
#include "token.h"
#include <iostream>
7
8
9

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

13
14
15
	}

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

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

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

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

39
40
41
42
43
44
45
46
47
48
49
50
	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();
	}

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

		// split based on start token
56
		switch(pScanner->peek().type) {
57
58
59
			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;
60
61
62
		}
	}

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

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

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

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

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

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

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

98
			pScanner->pop();
99
100
101

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

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

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

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

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

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

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

		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())
151
			out << "\n";
Jesse Beder's avatar
Jesse Beder committed
152
153
	}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
	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;
	}
175
}