parser.cpp 3.02 KB
Newer Older
beder's avatar
beder committed
1
#include "parser.h"
2
#include "scanner.h"
3
#include "token.h"
beder's avatar
beder committed
4
#include "exceptions.h"
5
#include <sstream>
beder's avatar
beder committed
6
7
8

namespace YAML
{
9
	Parser::Parser(std::istream& in): m_pScanner(0)
beder's avatar
beder committed
10
	{
11
		Load(in);
beder's avatar
beder committed
12
13
14
15
	}

	Parser::~Parser()
	{
16
		delete m_pScanner;
beder's avatar
beder committed
17
18
	}

19
20
21
22
23
	Parser::operator bool() const
	{
		return m_pScanner->PeekNextToken() != 0;
	}

24
25
26
27
28
29
30
	void Parser::Load(std::istream& in)
	{
		delete m_pScanner;
		m_pScanner = new Scanner(in);
		m_state.Reset();
	}

31
32
	// GetNextDocument
	// . Reads the next document in the queue (of tokens).
33
	// . Throws (ParserException|ParserException)s on errors.
34
	void Parser::GetNextDocument(Node& document)
beder's avatar
beder committed
35
	{
36
37
38
		// clear node
		document.Clear();

39
40
41
		// first read directives
		ParseDirectives();

42
43
44
45
46
47
48
49
50
		// we better have some tokens in the queue
		if(!m_pScanner->PeekNextToken())
			return;

		// first eat doc start (optional)
		if(m_pScanner->PeekNextToken()->type == TT_DOC_START)
			m_pScanner->EatNextToken();

		// now parse our root node
51
		document.Parse(m_pScanner, m_state);
52
53
54
55

		// and finally eat any doc ends we see
		while(m_pScanner->PeekNextToken() && m_pScanner->PeekNextToken()->type == TT_DOC_END)
			m_pScanner->EatNextToken();
56
57
	}

58
59
	// ParseDirectives
	// . Reads any directives that are next in the queue.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	void Parser::ParseDirectives()
	{
		bool readDirective = false;

		while(1) {
			Token *pToken = m_pScanner->PeekNextToken();
			if(!pToken || pToken->type != TT_DIRECTIVE)
				break;

			// we keep the directives from the last document if none are specified;
			// but if any directives are specific, then we reset them
			if(!readDirective)
				m_state.Reset();

			readDirective = true;
75
			HandleDirective(pToken);
76
77
78
79
			m_pScanner->PopNextToken();
		}
	}

80
	void Parser::HandleDirective(Token *pToken)
81
	{
82
83
84
85
		if(pToken->value == "YAML")
			HandleYamlDirective(pToken);
		else if(pToken->value == "TAG")
			HandleTagDirective(pToken);
86
87
88
89
	}

	// HandleYamlDirective
	// . Should be of the form 'major.minor' (like a version number)
90
	void Parser::HandleYamlDirective(Token *pToken)
91
	{
92
93
		if(pToken->params.size() != 1)
			throw ParserException(pToken->line, pToken->column, "YAML directives must have exactly one argument");
94

95
		std::stringstream str(pToken->params[0]);
96
97
98
99
		str >> m_state.version.major;
		str.get();
		str >> m_state.version.minor;
		if(!str)
100
101
			throw ParserException(pToken->line, pToken->column, "bad YAML directive");
				// TODO: or throw if there are any more characters in the stream?
102

beder's avatar
beder committed
103
		// TODO: throw on major > 1? warning on major == 1, minor > 2?
104
105
	}

106
	void Parser::HandleTagDirective(Token *pToken)
107
	{
108
109
		if(pToken->params.size() != 2)
			throw ParserException(pToken->line, pToken->column, "TAG directives must have exactly two arguments");
110

111
		std::string handle = pToken->params[0], prefix = pToken->params[1];
112
		m_state.tags[handle] = prefix;
beder's avatar
beder committed
113
	}
114

115
	void Parser::PrintTokens(std::ostream& out)
116
117
118
119
120
121
	{
		while(1) {
			Token *pToken = m_pScanner->GetNextToken();
			if(!pToken)
				break;

122
			out << *pToken << std::endl;
123
124
		}
	}
beder's avatar
beder committed
125
}