-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
80 lines (65 loc) · 2 KB
/
parser.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
#include <bits/stdc++.h>
#include "lexer.h"
#define ANY parse_any()
#define pstr(f) parser f(#f)
#define pconsec(f, args...) parser f = parse_consecutive(string(#f), args)
#define por(f, args...) parser f = parse_or(string(#f), args)
#define plist(f, arg1, arg2) parser f = parse_list(string(#f), arg1, arg2)
#define prep(f, arg) parser f = parse_repeat(string(#f), arg)
#define popt(f, arg) parser f = parse_optional(string(#f), arg)
#define pexc(f, arg1, arg2) parser f = parse_except(string(#f), arg1, arg2)
namespace data {
static bool debug, match_only;
}
using namespace data;
typedef struct parse_return {
string name;
string error;
int line;
int col;
int pos;
vector<parse_return*> nodes;
string base;
parse_return() {
error = "";
name = "";
}
parse_return(string s, int l, int c, int p, string nm="<unnamed string constant>") {
base = s;
line = l;
col = c;
name = nm;
pos = p;
}
parse_return(string err_msg, string nm = "<unnamed>") {
error = err_msg;
name = nm;
}
bool success() {
return error == "";
}
bool isBase() {
return base != "";
}
} parse_return;
class parser {
public:
function<parse_return*(vector<token>, int)> func;
parser(const char* s);
parser(string s);
parser(function<parse_return*(vector<token>, int)> f);
parser(parse_return* f(vector<token>, int));
parse_return* operator() (vector<token> tokens, int pos) const;
parser operator|(parser other);
};
template<class ... Ts> parser parse_consecutive(string nm, Ts ... args);
template<class ... Ts> parser parse_or(string nm, Ts ... args);
parser parse_list(string nm, parser a, parser b);
parser parse_repeat(string nm, parser a);
parser parse_optional(string nm, parser a);
parser parse_any();
parser parse_except(string nm, parser arg1, parser arg2);
void print_tree(parse_return* tree, string indent = "");
parse_return * compress(parse_return* tree);
parse_return* parse(vector<token> tokens, bool debug, bool match);
#include "parser_template_defs.cpp"