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

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

11
12
13
14
15
16
17
	}

	Sequence::~Sequence()
	{
		for(unsigned i=0;i<m_data.size();i++)
			delete m_data[i];
	}
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

	void Sequence::Parse(Scanner *pScanner)
	{
		// grab start token
		Token *pToken = pScanner->GetNextToken();

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

		delete pToken;
	}

	void Sequence::ParseBlock(Scanner *pScanner)
	{
		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)
	{
		// TODO
	}

	void Sequence::ParseFlow(Scanner *pScanner)
	{
		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);
	}
93
}