-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
189 lines (169 loc) · 6.36 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
#include<bits/stdc++.h>
#include "parser.h"
using namespace std;
parser::parser(function<parse_return*(vector<token>, int)> f) {
func = f;
}
parser::parser(parse_return* f(vector<token>, int)) {
func = function<parse_return*(vector<token>, int)>(f);
}
parser::parser(const char * param) {
func = [=](vector<token> tokens, int pos) {
string s = param;
if(pos >= tokens.size()) {
if(debug) cerr << "Failed to match string literal '" + s + "' at position " << pos << ": Out of range" << endl;
return new parse_return("Failed to match string literal '" + s + "'.\n", s);
}
if (tokens[pos] == s) {
if(debug || match_only) cerr << "Matched string literal '" + s + "' at position " << pos << endl;
return new parse_return(tokens[pos].str, tokens[pos].line, tokens[pos].col, pos + 1, "<string constant '" + s + "'>");
}
if(debug) cerr << "Failed to match string literal '" + s + "' at position " << pos << endl;
return new parse_return("Failed to match string literal '" + s + "'.\n", s);
};
}
parser::parser(string s) {
func = [=](vector<token> tokens, int pos) {
parse_return * ret;
if(pos >= tokens.size()) {
if(debug) cerr << "Failed to match string literal '" + s + "' at position " << pos << ": Out of range" << endl;
return new parse_return("Failed to match string literal '" + s + "'.\n", s);
}
if (tokens[pos] == s) {
if(debug || match_only) cerr << "Matched string literal '" + s + "' at position " << pos << endl;
return new parse_return(tokens[pos].str, tokens[pos].line, tokens[pos].col, pos + 1, "<string constant '" + s + "'>");
}
if(debug) cerr << "Failed to match string literal '" + s + "' at position " << pos << endl;
return new parse_return("Failed to match string literal '" + s + "'.\n");
};
}
parser parse_list(string nm, parser elem, parser sep) {
return function<parse_return*(vector<token>, int)> ([=] (vector<token> tokens, int pos) {
parse_return * ret = new parse_return();
ret->name = nm;
parse_return * first = elem(tokens, pos);
if(!first->success()) {
ret->pos = pos;
return ret;
}
ret->nodes.push_back(first);
int curr_pos = first->pos;
parse_return * parsed_sep, * parsed_elem;
while(1) {
parsed_sep = sep(tokens, curr_pos);
if(!parsed_sep->success()) {
if(debug) cerr << "Failed to match " << parsed_sep->name << " separator at position " << curr_pos << endl;
break;
}
curr_pos = parsed_sep->pos;
parsed_elem = elem(tokens, curr_pos);
if(!parsed_elem->success()) {
if(debug) cerr << "Failed to match " << parsed_elem->name <<" element at position " << curr_pos << endl;
break;
}
ret->nodes.push_back(parsed_sep);
ret->nodes.push_back(parsed_elem);
curr_pos = parsed_elem->pos;
}
ret -> pos = curr_pos;
if(debug || match_only) cerr << "Matched '" << nm << "' at position " << pos << endl;
return ret;
});
}
parser parse_repeat(string nm, parser to_rep) {
return function<parse_return*(vector<token>, int)> ([=] (vector<token> tokens, int pos) {
parse_return * ret = new parse_return();
ret->name = nm;
int curr_pos = pos;
parse_return * parsed;
while(1) {
parsed = to_rep(tokens, curr_pos);
if(!parsed->success()) {
if(debug) cerr << "Failed to match " << parsed->name << " to repeat at position " << curr_pos << endl;
break;
}
ret -> nodes.push_back(parsed);
curr_pos = parsed->pos;
}
ret -> pos = curr_pos;
if(debug || match_only) cerr << "Matched '" << nm << "' at position " << pos << endl;
return ret;
});
}
parse_return* parser::operator () (vector<token> tokens, int pos) const {
return func(tokens, pos);
}
parser parse_optional(string nm, parser a) {
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* parsed = a(tokens, pos);
if(parsed->success()) {
if(debug || match_only) cerr << "Matched '" << nm << "' at position " << pos << endl;
parsed->name = nm;
return parsed;
}
if(debug || match_only) cerr << "Matched '" << "<EMPTY STRING>" << "' at position " << pos << endl;
return new parse_return("<EMPTY STRING>", tokens[pos].line, tokens[pos].col, pos, nm);
});
}
parser parse_any() {
return function<parse_return*(vector<token>, int)> ([=] (vector<token> tokens, int pos) {
if(pos >= tokens.size()) {
if(debug) cerr << "Failed to match '" << "ANY" << "' at position " << pos << " : Out of range" << endl;
return new parse_return("Out of range.");
}
return new parse_return(tokens[pos].str, tokens[pos].line, tokens[pos].col, pos + 1, "ANY");
});
}
parser parse_except(string nm, parser arg1, parser arg2) {
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* first = arg1(tokens, pos);
parse_return* second = arg2(tokens, pos);
if(!second->success()) {
if(debug || match_only) cerr << "Matched '" << nm << "' at position " << pos << endl;
return first;
}
if(debug) cerr << "Failed to match '" << nm << "' at position " << pos << endl;
return new parse_return("ERROR", nm);
});
}
void print_tree(parse_return* tree, string indent) {
if(!tree) return;
if(!tree -> success()) {
cerr << tree->error << endl;
return;
}
if(tree -> name[0] == '<') {
cout << indent << tree -> base << endl;
return;
}
cout << indent << tree->name << ":" << tree-> base << endl;
for(auto a : tree->nodes) {
print_tree(a, indent+" ");
}
}
parse_return * compress(parse_return * tree) {
if(tree->isBase()) return tree;
vector<parse_return*> v = tree->nodes;
tree->nodes.clear();
for(int i = 0; i < v.size(); i++) {
parse_return * res = compress(v[i]);
if(res) {
tree->nodes.push_back(res);
}
}
if(tree->nodes.size() == 0) {
return NULL;
}
if(tree->nodes.size() == 1) {
return compress(tree->nodes[0]);
}
return tree;
}