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

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

11
12
13
	}

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

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

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

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

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

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

		switch(pToken->type) {
45
46
47
			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;
48
49
50
		}
	}

51
	void Sequence::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
		while(1) {
			Token *pToken = pScanner->PeekNextToken();
			if(!pToken)
				break;  // TODO: throw?

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

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

			Node *pNode = new Node;
			m_data.push_back(pNode);
70
			pNode->Parse(pScanner, state);
71
72
73
		}
	}

74
	void Sequence::ParseImplicit(Scanner *pScanner, const ParserState& state)
75
	{
76
77
78
79
80
81
82
83
84
85
86
87
88
89
		while(1) {
			Token *pToken = pScanner->PeekNextToken();
			// we're actually *allowed* to have no tokens at some point
			if(!pToken)
				break;

			// and we end at anything other than a block entry
			if(pToken->type != TT_BLOCK_ENTRY)
				break;

			pScanner->PopNextToken();

			Node *pNode = new Node;
			m_data.push_back(pNode);
90
			pNode->Parse(pScanner, state);
91
		}
92
93
	}

94
	void Sequence::ParseFlow(Scanner *pScanner, const ParserState& state)
95
	{
96
97
98
		// eat start token
		pScanner->EatNextToken();

99
100
101
102
103
104
105
106
107
108
109
110
111
112
		while(1) {
			Token *pToken = pScanner->PeekNextToken();
			if(!pToken)
				break;  // TODO: throw?

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

			// then read the node
			Node *pNode = new Node;
			m_data.push_back(pNode);
113
			pNode->Parse(pScanner, state);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

			// now eat the separator (or could be a sequence 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_SEQ_END)
				break;  // TODO: throw?
		}
	}

	void Sequence::Write(std::ostream& out, int indent)
	{
		for(int i=0;i<indent;i++)
			out << "  ";
		out << "{sequence}\n";
		for(unsigned i=0;i<m_data.size();i++)
			m_data[i]->Write(out, indent + 1);
	}
132
}