summaryrefslogtreecommitdiff
path: root/src/core/parser/ParserScope.cpp
blob: df123df74a601d9056cc21f6dfca2d25441d87a6 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
    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 <core/common/Exceptions.hpp>
#include <core/common/Utils.hpp>

#include "ParserScope.hpp"

namespace ousia {

/* Class ParserScopeBase */

ParserScopeBase::ParserScopeBase() {}

ParserScopeBase::ParserScopeBase(const NodeVector<Node> &nodes) : nodes(nodes)
{
}

Rooted<Node> ParserScopeBase::resolve(const std::vector<std::string> &path,
                                      const Rtti &type, Logger &logger)
{
	// Go up the stack and try to resolve the
	for (auto it = nodes.rbegin(); it != nodes.rend(); it++) {
		std::vector<ResolutionResult> res = (*it)->resolve(path, type);

		// Abort if the object could not be resolved
		if (res.empty()) {
			continue;
		}

		// Log an error if the object is not unique
		if (res.size() > 1) {
			logger.error(std::string("The reference \"") +
			             Utils::join(path, ".") + ("\" is ambigous!"));
			logger.note("Referenced objects are:", SourceLocation{},
			            MessageMode::NO_CONTEXT);
			for (const ResolutionResult &r : res) {
				logger.note(Utils::join(r.path(), "."), *(r.node));
			}
		}
		return res[0].node;
	}
	return nullptr;
}

/* Class DeferredResolution */

DeferredResolution::DeferredResolution(const NodeVector<Node> &nodes,
                                       const std::vector<std::string> &path,
                                       const Rtti &type,
                                       ResolutionResultCallback resultCallback,
                                       const SourceLocation &location)
    : scope(nodes),
      resultCallback(resultCallback),
      path(path),
      type(type),
      location(location)
{
}

bool DeferredResolution::resolve(Logger &logger)
{
	Rooted<Node> res = scope.resolve(path, type, logger);
	if (res != nullptr) {
		try {
			resultCallback(res, logger);
		}
		catch (LoggableException ex) {
			logger.log(ex);
		}
		return true;
	}
	return false;
}

/* Class ParserScope */

ParserScope::ParserScope(const NodeVector<Node> &nodes)
    : ParserScopeBase(nodes), topLevelDepth(nodes.size())
{
}

bool ParserScope::checkUnwound(Logger &logger) const
{
	if (nodes.size() != topLevelDepth) {
		logger.error("Not all open elements have been closed!",
		             SourceLocation{}, MessageMode::NO_CONTEXT);
		logger.note("Still open elements are: ", SourceLocation{},
		            MessageMode::NO_CONTEXT);
		for (size_t i = topLevelDepth + 1; i < nodes.size(); i++) {
			logger.note(std::string("Element of interal type ") +
			                nodes[i]->type().name +
			                std::string(" defined here:"),
			            nodes[i]->getLocation());
		}
		return false;
	}
	return true;
}

ParserScope ParserScope::fork() { return ParserScope{nodes}; }

bool ParserScope::join(const ParserScope &fork, Logger &logger)
{
	// Make sure the fork has been unwound
	if (!fork.checkUnwound(logger)) {
		return false;
	}

	// Insert the deferred resolutions of the fork into our own deferred
	// resolution list
	deferred.insert(deferred.end(), fork.deferred.begin(), fork.deferred.end());
	return true;
}

ParserScope::ParserScope() : topLevelDepth(0) {}

void ParserScope::push(Handle<Node> node)
{
	if (nodes.size() == topLevelDepth) {
		topLevelNodes.push_back(node);
	}
	nodes.push_back(node);
}

void ParserScope::pop()
{
	if (nodes.size() == topLevelDepth) {
		throw LoggableException{"No element here to end!"};
	}
	nodes.pop_back();
}

NodeVector<Node> ParserScope::getTopLevelNodes() const { return topLevelNodes; }

Rooted<Node> ParserScope::getRoot() const { return nodes.front(); }

Rooted<Node> ParserScope::getLeaf() const { return nodes.back(); }

bool ParserScope::resolve(const std::vector<std::string> &path,
                          const Rtti &type, Logger &logger,
                          ResolutionImposterCallback imposterCallback,
                          ResolutionResultCallback resultCallback,
                          const SourceLocation &location)
{
	if (!resolve(path, type, logger, resultCallback, location)) {
		resultCallback(imposterCallback(), logger);
		return false;
	}
	return true;
}

bool ParserScope::resolve(const std::vector<std::string> &path,
                          const Rtti &type, Logger &logger,
                          ResolutionResultCallback resultCallback,
                          const SourceLocation &location)
{
	Rooted<Node> res = ParserScopeBase::resolve(path, type, logger);
	if (res != nullptr) {
		try {
			resultCallback(res, logger);
		}
		catch (LoggableException ex) {
			logger.log(ex, location);
		}
		return true;
	}
	deferred.emplace_back(nodes, path, type, resultCallback, location);
	return false;
}

bool ParserScope::performDeferredResolution(Logger &logger)
{
	// Repeat the resolution process as long as something has changed in the
	// last iteration (resolving a node may cause other nodes to be resolvable).
	while (true) {
		// Iterate over all deferred resolution processes,
		bool hasChange = false;
		for (auto it = deferred.begin(); it != deferred.end();) {
			if (it->resolve(logger)) {
				it = deferred.erase(it);
				hasChange = true;
			} else {
				it++;
			}
		}

		// Abort if nothing has changed in the last iteration
		if (!hasChange) {
			break;
		}
	}

	// We were successful if there are no more deferred resolutions
	if (deferred.empty()) {
		return true;
	}

	// Output error messages for all elements for which resolution did not
	// succeed.
	for (const auto &failed : deferred) {
		logger.error(std::string("Could not resolve ") + failed.type.name +
		                 std::string(" \"") + Utils::join(failed.path, ".") +
		                 std::string("\""),
		             failed.location);
	}
	deferred.clear();
	return false;
}
}