-
Notifications
You must be signed in to change notification settings - Fork 22
/
interpreter.h
171 lines (141 loc) 路 5.37 KB
/
interpreter.h
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
#pragma once
#include <iterator>
#include <type_traits>
#include "parser.h"
namespace peg_parser {
struct InterpreterError : public std::exception {
std::shared_ptr<SyntaxTree> tree;
mutable std::string buffer;
InterpreterError(const std::shared_ptr<SyntaxTree> &t) : tree(t) {}
const char *what() const noexcept override;
};
template <class R, typename... Args> class Interpreter {
public:
class Expression;
using Callback = std::function<R(const Expression &e, Args... args)>;
class Expression {
protected:
struct iterator {
using iterator_category = std::input_iterator_tag;
using value_type = Expression;
using pointer = Expression *;
using reference = Expression &;
const Expression &parent;
size_t idx;
iterator(const Expression &p, size_t i) : parent(p), idx(i) {}
iterator &operator++() {
idx++;
return *this;
}
Expression operator*() const { return parent[idx]; }
bool operator!=(const iterator &other) const {
return other.idx != idx || &other.parent != &parent;
}
};
const Interpreter<R, Args...> &interpreter;
std::shared_ptr<SyntaxTree> syntaxTree;
public:
Expression(const Interpreter<R, Args...> &i, std::shared_ptr<SyntaxTree> s)
: interpreter(i), syntaxTree(s) {}
auto size() const { return syntaxTree->inner.size(); }
auto view() const { return syntaxTree->view(); }
auto string() const { return std::string(view()); }
auto position() const { return syntaxTree->begin; }
auto length() const { return syntaxTree->length(); }
auto rule() const { return syntaxTree->rule; }
auto syntax() const { return syntaxTree; }
Expression operator[](size_t idx) const {
return interpreter.interpret(syntaxTree->inner[idx]);
}
iterator begin() const { return iterator(*this, 0); }
iterator end() const { return iterator(*this, size()); }
template <class R2, typename... Args2>
auto evaluateBy(const Interpreter<R2, Args2...> &interpreter, Args2... args) const {
return interpreter.evaluate(syntaxTree, args...);
}
R evaluate(Args... args) const {
auto it = interpreter.evaluators.find(syntaxTree->rule.get());
if (it == interpreter.evaluators.end()) {
if (interpreter.defaultEvaluator) {
return interpreter.defaultEvaluator(*this, args...);
}
throw InterpreterError(syntaxTree);
}
return it->second(*this, args...);
}
};
private:
std::unordered_map<grammar::Rule *, Callback> evaluators;
static R __defaultEvaluator(const Expression &e, Args... args) {
size_t N = e.size();
if (N > 0) {
for (size_t i = 0; i < N - 1; ++i) {
e[i].evaluate(std::forward<Args>(args)...);
}
return e[N - 1].evaluate(std::forward<Args>(args)...);
}
if (!std::is_same<R, void>::value) {
throw InterpreterError(e.syntax());
}
};
public:
Callback defaultEvaluator = __defaultEvaluator;
std::shared_ptr<grammar::Rule> makeRule(const std::string_view &name,
const grammar::Node::Shared &node,
const Callback &callback) {
auto rule = std::make_shared<grammar::Rule>(name, node);
setEvaluator(rule, callback);
return rule;
}
std::shared_ptr<grammar::Rule> makeRule(const std::string &name,
const std::shared_ptr<grammar::Rule> &rule,
const Callback &callback) {
return makeRule(name, grammar::Node::Rule(rule), callback);
}
void setEvaluator(const std::shared_ptr<grammar::Rule> &rule, const Callback &callback) {
if (callback) {
evaluators[rule.get()] = callback;
} else {
auto it = evaluators.find(rule.get());
if (it != evaluators.end()) {
evaluators.erase(it);
}
}
}
Expression interpret(const std::shared_ptr<SyntaxTree> &tree) const {
return Expression{*this, tree};
}
R evaluate(const std::shared_ptr<SyntaxTree> &tree, Args... args) const {
return interpret(tree).evaluate(args...);
}
};
class SyntaxError : public std::exception {
private:
mutable std::string buffer;
public:
std::shared_ptr<SyntaxTree> syntax;
SyntaxError(const std::shared_ptr<SyntaxTree> &t) : syntax(t) {}
const char *what() const noexcept override;
};
template <class R, typename... Args> struct Program {
using Expression = typename Interpreter<R, Args...>::Expression;
Parser parser;
Interpreter<R, Args...> interpreter;
std::shared_ptr<SyntaxTree> parse(const std::string_view &str) const {
return parser.parse(str);
}
Expression interpret(const std::shared_ptr<SyntaxTree> &tree) const {
if (!tree->valid) {
throw SyntaxError(tree);
}
return interpreter.interpret(tree);
}
R run(const std::string_view &str, Args &&... args) const {
auto parsed = parser.parseAndGetError(str);
if (!parsed.syntax->valid || parsed.syntax->end < str.size()) {
throw SyntaxError(parsed.error);
}
return interpret(parsed.syntax).evaluate(std::forward<Args>(args)...);
}
};
} // namespace peg_parser