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

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

	Parser::~Parser()
	{
	}

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

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

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

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

58
59
60
61
62
63
64
65
66
	// 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);
	}

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

			readDirective = true;
87
			HandleDirective(token);
88
89
90
91
			m_pScanner->pop();
		}
	}

92
	void Parser::HandleDirective(const Token& token)
93
	{
94
95
96
97
		if(token.value == "YAML")
			HandleYamlDirective(token);
		else if(token.value == "TAG")
			HandleTagDirective(token);
98
99
100
101
	}

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

110
		std::stringstream str(token.params[0]);
111
		str >> m_pDirectives->version.major;
112
		str.get();
113
		str >> m_pDirectives->version.minor;
114
		if(!str || str.peek() != EOF)
115
			throw ParserException(token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]);
116

117
		if(m_pDirectives->version.major > 1)
118
			throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION);
119

120
		m_pDirectives->version.isDefault = false;
121
122
123
124
125
		// TODO: warning on major == 1, minor > 2?
	}

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

131
132
		const std::string& handle = token.params[0];
		const std::string& prefix = token.params[1];
133
		if(m_pDirectives->tags.find(handle) != m_pDirectives->tags.end())
134
135
			throw ParserException(token.mark, ErrorMsg::REPEATED_TAG_DIRECTIVE);
		
136
		m_pDirectives->tags[handle] = prefix;
137
138
139
140
	}

	void Parser::PrintTokens(std::ostream& out)
	{
141
142
143
		if(!m_pScanner.get())
			return;
		
144
145
146
147
148
149
150
151
152
		while(1) {
			if(m_pScanner->empty())
				break;

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