summaryrefslogtreecommitdiff
path: root/src/core/parser/ParserStack.cpp
blob: cc875cba4671c1956f97881afa40ea8e811f6256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
    Ousía
    Copyright (C) 2014, 2015  Benjamin Paaßen, Andreas Stöckel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <sstream>

#include <core/common/Utils.hpp>
#include <core/common/Exceptions.hpp>
#include <core/model/Project.hpp>

#include "ParserScope.hpp"
#include "ParserStack.hpp"

namespace ousia {

/* A default handler */

/**
 * The DefaultHandler class is used in case no element handler is specified in
 * the ParserState descriptor.
 */
class DefaultHandler : public Handler {
public:
	using Handler::Handler;

	void start(Variant::mapType &args) override {}

	void end() override {}

	static Handler *create(const HandlerData &handlerData)
	{
		return new DefaultHandler{handlerData};
	}
};

/* Class Handler */

void Handler::data(const std::string &data, int field)
{
	for (auto &c : data) {
		if (!Utils::isWhitespace(c)) {
			logger().error("Expected command but found character data.");
			return;
		}
	}
}

/* Class ParserStack */

/**
 * Returns an Exception that should be thrown when a currently invalid command
 * is thrown.
 */
static LoggableException InvalidCommand(const std::string &name,
                                        const std::set<std::string> &expected)
{
	if (expected.empty()) {
		return LoggableException{
		    std::string{"No nested elements allowed, but got \""} + name +
		    std::string{"\""}};
	} else {
		return LoggableException{
		    std::string{"Expected "} +
		    (expected.size() == 1 ? std::string{"\""}
		                          : std::string{"one of \""}) +
		    Utils::join(expected, "\", \"") + std::string{"\", but got \""} +
		    name + std::string{"\""}};
	}
}

ParserStack::ParserStack(
    ParserContext &ctx,
    const std::multimap<std::string, const ParserState *> &states)
    : ctx(ctx), states(states)
{
}

bool ParserStack::deduceState()
{
	// Assemble all states
	std::vector<const ParserState *> states;
	for (const auto &e : this->states) {
		states.push_back(e.second);
	}

	// Fetch the type signature of the scope and derive all possible states,
	// abort if no unique parser state was found
	std::vector<const ParserState *> possibleStates =
	    ParserStateDeductor(ctx.getScope().getStackTypeSignature(), states)
	        .deduce();
	if (possibleStates.size() != 1) {
		ctx.getLogger().error(
		    "Error while including file: Cannot deduce parser state.");
		return false;
	}

	// Switch to this state by creating a dummy handler
	const ParserState *state = possibleStates[0];
	Handler *handler =
	    DefaultHandler::create({ctx, "", *state, *state, SourceLocation{}});
	stack.emplace(handler);
	return true;
}

std::set<std::string> ParserStack::expectedCommands(const ParserState &state)
{
	std::set<std::string> res;
	for (const auto &v : states) {
		if (v.second->parents.count(&state)) {
			res.insert(v.first);
		}
	}
	return res;
}

const ParserState &ParserStack::currentState()
{
	return stack.empty() ? ParserStates::None : stack.top()->state();
}

std::string ParserStack::currentCommandName()
{
	return stack.empty() ? std::string{} : stack.top()->name();
}

void ParserStack::start(std::string name, Variant::mapType &args,
                        const SourceLocation &location)
{
	// Fetch the current handler and the current state
	ParserState const *currentState = &(this->currentState());

	// Fetch the correct Handler descriptor for this
	ParserState const *targetState = nullptr;
	HandlerConstructor ctor = nullptr;
	auto range = states.equal_range(name);
	for (auto it = range.first; it != range.second; it++) {
		const ParserStateSet &parents = it->second->parents;
		if (parents.count(currentState) || parents.count(&ParserStates::All)) {
			targetState = it->second;
			ctor = targetState->elementHandler ? targetState->elementHandler
			                                   : DefaultHandler::create;
			break;
		}
	}

	// Try to use the child handler if one was given
	if (!targetState && currentState->childHandler) {
		targetState = currentState;
		ctor = targetState->childHandler;
	}

	// No descriptor found, throw an exception.
	if (!targetState || !ctor) {
		throw InvalidCommand(name, expectedCommands(*currentState));
	}

	// Canonicalize the arguments, allow additional arguments
	targetState->arguments.validateMap(args, ctx.getLogger(), true);

	// Instantiate the handler and call its start function
	Handler *handler = ctor({ctx, name, *targetState, *currentState, location});
	handler->start(args);
	stack.emplace(handler);
}

void ParserStack::start(std::string name, const Variant::mapType &args,
                        const SourceLocation &location)
{
	Variant::mapType argsCopy(args);
	start(name, argsCopy);
}

void ParserStack::end()
{
	// Check whether the current command could be ended
	if (stack.empty()) {
		throw LoggableException{"No command to end."};
	}

	// Remove the current HandlerInstance from the stack
	std::shared_ptr<Handler> inst{stack.top()};
	stack.pop();

	// Call the end function of the last Handler
	inst->end();
}

void ParserStack::data(const std::string &data, int field)
{
	// Check whether there is any command the data can be sent to
	if (stack.empty()) {
		throw LoggableException{"No command to receive data."};
	}

	// Pass the data to the current Handler instance
	stack.top()->data(data, field);
}
}