-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_template_defs.cpp
48 lines (46 loc) · 1.85 KB
/
parser_template_defs.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
template<class ... Ts> parser parse_consecutive(string nm, Ts ... args) {
vector<parser> funcs = { args... };
return function<parse_return*(vector<token>, int)> ([=] (vector<token> tokens, int pos) {
if(pos >= tokens.size()) {
if(debug) cerr << "Failed to match '" << nm << "' at position " << pos << " : Out of range" << endl;
return new parse_return("ERROR", nm);
}
int curr_pos = pos;
parse_return * ret = new parse_return();
ret -> name = nm;
for(int i = 0; i < funcs.size() ; i++) {
parse_return * current = funcs[i](tokens, curr_pos);
if(!current->success()) {
if(debug) cerr << "Failed to match '" << nm << "' at position " << pos << endl;
return new parse_return("ERROR", nm);
}
curr_pos = current->pos;
ret->nodes.push_back(current);
}
ret -> pos = curr_pos;
if(debug || match_only) cerr << "Matched '" << nm << "' at position " << pos << endl;
return ret;
});
}
template<class ... Ts> parser parse_or(string nm, Ts ... args) {
vector<parser> funcs = { args... };
return function<parse_return*(vector<token>, int)> ([=] (vector<token> tokens, int pos) {
if(pos >= tokens.size()) {
if(debug) cerr << "Failed to match '" << nm << "' at position " << pos << " : Out of range" << endl;
return new parse_return("ERROR", nm);
}
parse_return * ret = new parse_return();
ret -> name = nm;
for(int i = 0; i < funcs.size() ; i++) {
parse_return * current = funcs[i](tokens, pos);
if(current->success()) {
ret->nodes.push_back(current);
ret->pos = current -> pos;
if(debug || match_only) cerr << "Matched '" << nm << "' at position " << pos << endl;
return ret;
}
}
if(debug) cerr << "Failed to match '" << nm << "' at position " << pos << endl;
return new parse_return("ERROR", nm);
});
}