"...composable_kernel.git" did not exist on "29a118c68fa207d9cf842a6bc337741b655fe5a2"
parser.cpp 3.25 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
13

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

	Parser::~Parser()
	{
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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