sequence.cpp 2.5 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

	void Sequence::Parse(Scanner *pScanner)
	{
27
28
29
30
		Clear();

		// split based on start token
		Token *pToken = pScanner->PeekNextToken();
31
32
33
34
35
36
37
38
39
40

		switch(pToken->type) {
			case TT_BLOCK_SEQ_START: ParseBlock(pScanner); break;
			case TT_BLOCK_ENTRY: ParseImplicit(pScanner); break;
			case TT_FLOW_SEQ_START: ParseFlow(pScanner); break;
		}
	}

	void Sequence::ParseBlock(Scanner *pScanner)
	{
41
42
43
		// eat start token
		pScanner->EatNextToken();

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
		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);
			pNode->Parse(pScanner);
		}
	}

	void Sequence::ParseImplicit(Scanner *pScanner)
	{
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		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);
			pNode->Parse(pScanner);
		}
80
81
82
83
	}

	void Sequence::ParseFlow(Scanner *pScanner)
	{
84
85
86
		// eat start token
		pScanner->EatNextToken();

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
		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);
			pNode->Parse(pScanner);

			// 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);
	}
120
}