parser.cpp 3.28 KB
Newer Older
1
2
3
#include "yaml-cpp/parser.h"
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/exceptions.h"
4
#include "yaml-cpp/node.h"
5
#include "directives.h"
6
#include "scanner.h"
7
8
#include "singledocparser.h"
#include "tag.h"
9
10
#include "token.h"
#include <sstream>
11
#include <cstdio>
12
13
14

namespace YAML
{
15
16
17
18
19
	Parser::Parser()
	{
	}
	
	Parser::Parser(std::istream& in)
20
21
22
23
24
25
26
27
28
29
	{
		Load(in);
	}

	Parser::~Parser()
	{
	}

	Parser::operator bool() const
	{
30
		return m_pScanner.get() && !m_pScanner->empty();
31
32
33
34
	}

	void Parser::Load(std::istream& in)
	{
35
		m_pScanner.reset(new Scanner(in));
36
		m_pDirectives.reset(new Directives);
37
38
	}

39
40
	// HandleNextDocument
	// . Handles the next document
41
	// . Throws a ParserException on error.
42
43
	// . Returns false if there are no more documents
	bool Parser::HandleNextDocument(EventHandler& eventHandler)
44
	{
45
46
		if(!m_pScanner.get())
			return false;
47
48
49

		ParseDirectives();
		if(m_pScanner->empty())
50
51
			return false;
		
52
53
		SingleDocParser sdp(*m_pScanner, *m_pDirectives);
		sdp.HandleDocument(eventHandler);
54
		return true;
55
56
57
58
59
60
61
62
63
64
65
66
67
	}

	// ParseDirectives
	// . Reads any directives that are next in the queue.
	void Parser::ParseDirectives()
	{
		bool readDirective = false;

		while(1) {
			if(m_pScanner->empty())
				break;

			Token& token = m_pScanner->peek();
68
			if(token.type != Token::DIRECTIVE)
69
70
71
72
73
				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)
74
				m_pDirectives.reset(new Directives);
75
76

			readDirective = true;
77
			HandleDirective(token);
78
79
80
81
			m_pScanner->pop();
		}
	}

82
	void Parser::HandleDirective(const Token& token)
83
	{
84
85
86
87
		if(token.value == "YAML")
			HandleYamlDirective(token);
		else if(token.value == "TAG")
			HandleTagDirective(token);
88
89
90
91
	}

	// HandleYamlDirective
	// . Should be of the form 'major.minor' (like a version number)
92
	void Parser::HandleYamlDirective(const Token& token)
93
	{
94
95
96
		if(token.params.size() != 1)
			throw ParserException(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS);
		
97
		if(!m_pDirectives->version.isDefault)
98
			throw ParserException(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE);
99

100
		std::stringstream str(token.params[0]);
101
		str >> m_pDirectives->version.major;
102
		str.get();
103
		str >> m_pDirectives->version.minor;
104
		if(!str || str.peek() != EOF)
105
			throw ParserException(token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]);
106

107
		if(m_pDirectives->version.major > 1)
108
			throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION);
109

110
		m_pDirectives->version.isDefault = false;
111
112
113
114
115
		// TODO: warning on major == 1, minor > 2?
	}

	// HandleTagDirective
	// . Should be of the form 'handle prefix', where 'handle' is converted to 'prefix' in the file.
116
	void Parser::HandleTagDirective(const Token& token)
117
	{
118
119
		if(token.params.size() != 2)
			throw ParserException(token.mark, ErrorMsg::TAG_DIRECTIVE_ARGS);
120

121
122
		const std::string& handle = token.params[0];
		const std::string& prefix = token.params[1];
123
		if(m_pDirectives->tags.find(handle) != m_pDirectives->tags.end())
124
125
			throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE);
		
126
		m_pDirectives->tags[handle] = prefix;
127
128
129
130
	}

	void Parser::PrintTokens(std::ostream& out)
	{
131
132
133
		if(!m_pScanner.get())
			return;
		
134
135
136
137
138
139
140
141
142
		while(1) {
			if(m_pScanner->empty())
				break;

			out << m_pScanner->peek() << "\n";
			m_pScanner->pop();
		}
	}
}