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

12
#if YAML_CPP_OLD_API
13
#include "yaml-cpp/old-api/node.h"
14
15
16
#include "old-api/nodebuilder.h"
#endif

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

	Parser::~Parser()
	{
	}

	Parser::operator bool() const
	{
34
		return m_pScanner.get() && !m_pScanner->empty();
35
36
37
38
	}

	void Parser::Load(std::istream& in)
	{
39
		m_pScanner.reset(new Scanner(in));
40
		m_pDirectives.reset(new Directives);
41
42
	}

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

		ParseDirectives();
		if(m_pScanner->empty())
54
55
			return false;
		
56
57
		SingleDocParser sdp(*m_pScanner, *m_pDirectives);
		sdp.HandleDocument(eventHandler);
58
		return true;
59
60
	}

61
#if YAML_CPP_OLD_API
62
63
64
65
66
67
68
69
	// GetNextDocument
	// . Reads the next document in the queue (of tokens).
	// . Throws a ParserException on error.
	bool Parser::GetNextDocument(Node& document)
	{
		NodeBuilder builder(document);
		return HandleNextDocument(builder);
	}
70
#endif
71

72
73
74
75
76
77
78
79
80
81
82
	// 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();
83
			if(token.type != Token::DIRECTIVE)
84
85
86
87
88
				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)
89
				m_pDirectives.reset(new Directives);
90
91

			readDirective = true;
92
			HandleDirective(token);
93
94
95
96
			m_pScanner->pop();
		}
	}

97
	void Parser::HandleDirective(const Token& token)
98
	{
99
100
101
102
		if(token.value == "YAML")
			HandleYamlDirective(token);
		else if(token.value == "TAG")
			HandleTagDirective(token);
103
104
105
106
	}

	// HandleYamlDirective
	// . Should be of the form 'major.minor' (like a version number)
107
	void Parser::HandleYamlDirective(const Token& token)
108
	{
109
110
111
		if(token.params.size() != 1)
			throw ParserException(token.mark, ErrorMsg::YAML_DIRECTIVE_ARGS);
		
112
		if(!m_pDirectives->version.isDefault)
113
			throw ParserException(token.mark, ErrorMsg::REPEATED_YAML_DIRECTIVE);
114

115
		std::stringstream str(token.params[0]);
116
		str >> m_pDirectives->version.major;
117
		str.get();
118
		str >> m_pDirectives->version.minor;
119
		if(!str || str.peek() != EOF)
120
			throw ParserException(token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]);
121

122
		if(m_pDirectives->version.major > 1)
123
			throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION);
124

125
		m_pDirectives->version.isDefault = false;
126
127
128
129
130
		// TODO: warning on major == 1, minor > 2?
	}

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

136
137
		const std::string& handle = token.params[0];
		const std::string& prefix = token.params[1];
138
		if(m_pDirectives->tags.find(handle) != m_pDirectives->tags.end())
139
140
			throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE);
		
141
		m_pDirectives->tags[handle] = prefix;
142
143
144
145
	}

	void Parser::PrintTokens(std::ostream& out)
	{
146
147
148
		if(!m_pScanner.get())
			return;
		
149
150
151
152
153
154
155
156
157
		while(1) {
			if(m_pScanner->empty())
				break;

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