-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
562 lines (487 loc) · 13.3 KB
/
parser.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <easy_iterator.h>
#include <peg_parser/parser.h>
#include <algorithm>
#include <sstream>
#include <stack>
#include <tuple>
// Macros for debugging parsers
//#define PEG_PARSER_TRACE
#ifdef PEG_PARSER_TRACE
#define PEG_PARSER_DEBUG_LOG
#define PARSER_TRACE(X) \
LOG("parser[" << state.getPosition() << "," << state.current() \
<< "]: " << __INDENT << X)
#define PARSER_ADVANCE(X) \
LOG("parser[" << getPosition() << "," << current() << "]: " << __INDENT << X)
#else
#define PARSER_TRACE(X)
#define PARSER_ADVANCE(X)
#endif
#ifdef PEG_PARSER_DEBUG_LOG
#include <iostream>
#define LOG(X) std::cout << X << std::endl;
namespace {
std::string __INDENT = "";
}
#define INCREASE_INDENT __INDENT = __INDENT + " "
#define DECREASE_INDENT __INDENT = __INDENT.substr(0, __INDENT.size() - 2)
#else
#define INCREASE_INDENT
#define DECREASE_INDENT
#endif
namespace {
/** alternative to `std::get` that works on iOS < 11 */
template<class T, class V>
const T&
pget(const V& v)
{
if (auto r = std::get_if<T>(&v)) {
return *r;
} else {
throw std::runtime_error("corrupted grammar node");
}
}
} // namespace
using namespace peg_parser;
namespace {
// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// http://stackoverflow.com/questions/4948780
template<class T>
inline void
hash_combine(std::size_t& seed, T const& v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
// Recursive template code derived from Matthieu M.
template<class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl
{
static void apply(size_t& seed, Tuple const& tuple)
{
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template<class Tuple>
struct HashValueImpl<Tuple, 0>
{
static void apply(size_t& seed, Tuple const& tuple)
{
hash_combine(seed, std::get<0>(tuple));
}
};
template<class Tuple>
struct TupleHasher
{
size_t operator()(Tuple const& tt) const
{
size_t seed = 0;
HashValueImpl<Tuple>::apply(seed, tt);
return seed;
}
};
template<class T>
std::string
streamToString(T&& v)
{
std::stringstream stream;
stream << v;
return stream.str();
}
class State
{
public:
std::string_view string;
private:
size_t position;
using CacheKey = std::tuple<size_t, grammar::Rule*>;
using Cache = std::
unordered_map<CacheKey, std::shared_ptr<SyntaxTree>, TupleHasher<CacheKey>>;
Cache cache;
std::shared_ptr<SyntaxTree> errorTree;
public:
size_t maxPosition;
State(const std::string_view& s, size_t c = 0)
: string(s)
, position(c)
, maxPosition(c)
{}
grammar::Letter current()
{
return position < string.size() ? string[position] : '\0';
}
void advance(size_t amount = 1)
{
position += amount;
if (position > string.size()) {
position = string.size();
}
if (position > maxPosition) {
maxPosition = position;
}
PARSER_ADVANCE("advancing " << amount << " to " << position << ": '"
<< current() << "'");
}
void setPosition(size_t p)
{
if (p == position) {
return;
}
position = p;
PARSER_ADVANCE("resetting to " << position << ": '" << current() << "'");
}
size_t getPosition() { return position; }
struct Saved
{
size_t position;
size_t innerCount;
};
Saved save()
{
return Saved{ position, stack.size() > 0 ? stack.back()->inner.size() : 0 };
}
void load(const Saved& s)
{
if (stack.size() > 0) {
stack.back()->end = getPosition();
stack.back()->inner.resize(s.innerCount);
}
setPosition(s.position);
}
bool isAtEnd() { return position == string.size(); }
std::shared_ptr<SyntaxTree> getCached(
const std::shared_ptr<grammar::Rule>& rule)
{
auto it = cache.find(std::make_pair(position, rule.get()));
if (it != cache.end())
return it->second;
return std::shared_ptr<SyntaxTree>();
}
void addToCache(const std::shared_ptr<SyntaxTree>& tree)
{
cache[std::make_pair(tree->begin, tree->rule.get())] = tree;
}
const Cache& getCache() { return cache; }
void removeFromCache(const std::shared_ptr<SyntaxTree>& tree)
{
auto it = cache.find(std::make_pair(tree->begin, tree->rule.get()));
if (it != cache.end()) {
cache.erase(it);
}
}
void addInnerSyntaxTree(const std::shared_ptr<SyntaxTree>& tree)
{
if (stack.size() > 0 && !tree->rule->hidden) {
stack.back()->inner.push_back(tree);
}
}
std::vector<std::shared_ptr<SyntaxTree>> stack;
std::shared_ptr<SyntaxTree> getErrorTree() { return errorTree; }
void trackError(const std::shared_ptr<SyntaxTree>& tree)
{
if (!tree) {
return;
}
if (tree->length() > 0 && !tree->rule->hidden) {
if (errorTree) {
if (tree->end >= errorTree->end) {
errorTree = tree;
}
} else {
errorTree = tree;
}
}
}
};
bool
parse(const std::shared_ptr<grammar::Node>& node, State& state);
std::shared_ptr<SyntaxTree>
parseRule(const std::shared_ptr<grammar::Rule>& rule,
State& state,
bool useCache = true)
{
PARSER_TRACE("enter rule " << rule->name);
INCREASE_INDENT;
if (useCache && rule->cacheable) {
auto cached = state.getCached(rule);
if (cached) {
PARSER_TRACE("cached");
if (cached->valid) {
state.addInnerSyntaxTree(cached);
state.advance();
state.setPosition(cached->end);
} else {
PARSER_TRACE("failed");
if (cached->active && !cached->recursive) {
PARSER_TRACE("found left recursion");
cached->recursive = true;
}
}
DECREASE_INDENT;
PARSER_TRACE("exit rule " << rule->name);
return cached;
}
}
auto syntaxTree =
std::make_shared<SyntaxTree>(rule, state.string, state.getPosition());
if (useCache) {
state.addToCache(syntaxTree);
}
auto saved = state.save();
state.stack.push_back(syntaxTree);
syntaxTree->valid = parse(rule->node, state);
syntaxTree->end = state.getPosition();
syntaxTree->active = false;
state.stack.pop_back();
if (syntaxTree->valid) {
if (useCache && syntaxTree->recursive) {
PARSER_TRACE("enter left recursion: " << rule->name);
while (true) {
State recursionState(state.string, syntaxTree->begin);
recursionState.trackError(state.getErrorTree());
// Copy the cache except the currect position to the recursion state
// TODO: keeping the current state and modifying the cache in place is
// probably much more efficient.
for (auto& cached : state.getCache()) {
if (std::get<0>(cached.first) != syntaxTree->begin) {
recursionState.addToCache(cached.second);
}
}
recursionState.addToCache(syntaxTree);
auto tmp = parseRule(rule, recursionState, false);
state.trackError(recursionState.getErrorTree());
if (tmp->valid && tmp->end > syntaxTree->end) {
PARSER_TRACE("parsed left recursion");
syntaxTree = tmp;
if (useCache) {
state.addToCache(syntaxTree);
}
state.setPosition(tmp->end);
} else {
break;
}
}
PARSER_TRACE("exit left recursion");
}
state.addInnerSyntaxTree(syntaxTree);
} else {
state.trackError(syntaxTree);
state.load(saved);
}
DECREASE_INDENT;
PARSER_TRACE("exit rule " << rule->name);
return syntaxTree;
}
bool
parse(const std::shared_ptr<grammar::Node>& node, State& state)
{
using Node = peg_parser::grammar::Node;
using Symbol = Node::Symbol;
PARSER_TRACE("parsing " << *node);
auto c = state.current();
switch (node->symbol) {
case peg_parser::grammar::Node::Symbol::WORD: {
auto saved = state.save();
for (auto c : pget<std::string>(node->data)) {
if (state.current() != c) {
state.load(saved);
PARSER_TRACE("failed");
return false;
}
state.advance();
}
return true;
}
case peg_parser::grammar::Node::Symbol::ANY: {
if (state.isAtEnd()) {
PARSER_TRACE("failed");
return false;
} else {
state.advance();
return true;
}
}
case Symbol::RANGE: {
auto& v = pget<std::array<grammar::Letter, 2>>(node->data);
if (c >= v[0] && c <= v[1]) {
state.advance();
return true;
} else {
PARSER_TRACE("failed");
return false;
}
}
case Symbol::SEQUENCE: {
auto saved = state.save();
for (auto n : pget<std::vector<grammar::Node::Shared>>(node->data)) {
if (!parse(n, state)) {
state.load(saved);
return false;
}
}
return true;
}
case Symbol::CHOICE: {
for (auto n : pget<std::vector<grammar::Node::Shared>>(node->data)) {
if (parse(n, state)) {
return true;
}
}
return false;
}
case Symbol::ZERO_OR_MORE: {
auto data = pget<Node::Shared>(node->data);
while (parse(data, state)) {
}
return true;
}
case peg_parser::grammar::Node::Symbol::ONE_OR_MORE: {
const auto& data = pget<Node::Shared>(node->data);
if (!parse(data, state)) {
return false;
}
while (parse(data, state)) {
}
return true;
}
case peg_parser::grammar::Node::Symbol::OPTIONAL: {
const auto& data = pget<Node::Shared>(node->data);
parse(data, state);
return true;
}
case peg_parser::grammar::Node::Symbol::ALSO: {
const auto& data = pget<Node::Shared>(node->data);
auto saved = state.save();
auto result = parse(data, state);
state.load(saved);
return result;
}
case peg_parser::grammar::Node::Symbol::NOT: {
const auto& data = pget<Node::Shared>(node->data);
auto saved = state.save();
auto result = parse(data, state);
state.load(saved);
return !result;
}
case peg_parser::grammar::Node::Symbol::ERROR: {
return false;
}
case peg_parser::grammar::Node::Symbol::EMPTY: {
return true;
}
case peg_parser::grammar::Node::Symbol::RULE: {
const auto& rule = pget<std::shared_ptr<grammar::Rule>>(node->data);
return parseRule(rule, state)->valid;
}
case peg_parser::grammar::Node::Symbol::WEAK_RULE: {
const auto& data = pget<std::weak_ptr<grammar::Rule>>(node->data);
if (auto rule = data.lock()) {
return parseRule(rule, state)->valid;
} else {
throw Parser::GrammarError(Parser::GrammarError::INVALID_RULE, node);
}
}
case peg_parser::grammar::Node::Symbol::END_OF_FILE: {
auto res = state.isAtEnd();
if (!res) {
PARSER_TRACE("failed");
}
return res;
}
case peg_parser::grammar::Node::Symbol::FILTER: {
const auto& callback = pget<grammar::Node::FilterCallback>(node->data);
bool res;
if (state.stack.size() > 0) {
auto tree = state.stack.back();
tree->end = state.getPosition();
res = callback(tree);
state.setPosition(tree->end);
} else {
res = false;
}
if (!res) {
PARSER_TRACE("failed");
}
return res;
}
}
throw Parser::GrammarError(Parser::GrammarError::UNKNOWN_SYMBOL, node);
}
} // namespace
SyntaxTree::SyntaxTree(const std::shared_ptr<grammar::Rule>& r,
std::string_view s,
size_t p)
: rule(r)
, fullString(s)
, begin(p)
, end(p)
, valid(false)
, active(true)
{}
const char*
peg_parser::Parser::GrammarError::what() const noexcept
{
if (buffer.size() == 0) {
std::string typeName;
switch (type) {
case UNKNOWN_SYMBOL:
typeName = "UNKNOWN_SYMBOL";
break;
case INVALID_RULE:
typeName = "INVALID_RULE";
break;
}
buffer = "internal error in grammar node (" + typeName +
"): " + streamToString(*node);
}
return buffer.c_str();
}
Parser::Parser(const std::shared_ptr<grammar::Rule>& g)
: grammar(g)
{}
Parser::Result
Parser::parseAndGetError(const std::string_view& str,
std::shared_ptr<grammar::Rule> grammar)
{
State state(str);
PARSER_TRACE("Begin parsing of: '" << str << "'");
auto result = parseRule(grammar, state);
auto error = state.getErrorTree();
if (!error) {
error = result;
}
return Parser::Result{ result, error };
}
std::shared_ptr<SyntaxTree>
Parser::parse(const std::string_view& str,
std::shared_ptr<grammar::Rule> grammar)
{
return parseAndGetError(str, grammar).syntax;
}
std::shared_ptr<SyntaxTree>
Parser::parse(const std::string_view& str) const
{
return parse(str, grammar);
}
Parser::Result
Parser::parseAndGetError(const std::string_view& str) const
{
return parseAndGetError(str, grammar);
}
std::ostream&
peg_parser::operator<<(std::ostream& stream, const SyntaxTree& tree)
{
stream << tree.rule->name << '(';
if (tree.inner.size() == 0) {
stream << '\'' << tree.view() << '\'';
} else {
for (auto&& [i, arg] : easy_iterator::enumerate(tree.inner)) {
stream << (*arg) << (i + 1 == tree.inner.size() ? "" : ", ");
}
}
stream << ')';
return stream;
}