-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbnfparser.js
261 lines (222 loc) · 8.71 KB
/
bnfparser.js
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
var bnf = (function () {
/**
* 字符串解码 < > &pipe; | and &
* */
function ndecode(x) {
return x.replace(/</g, "<").replace(/>/g, ">").replace(/&pipe;/g, "|").replace(/&x7C;/g, "|").replace(/&/g, "&");
}
/**
* 字符串编码
* */
function nencode(x) {
return x.replace(/&/g, "&").replace(/\|/g, "|").replace(/</g, "<").replace(/>/g, ">");
}
/**
* 解析bnf
* */
function parse_rule(rule) {
var res = [], resraw = rule.split(/\s*\|\s*/);
for (var i = 0; i < resraw.length; ++i) {
var psh = [], pshraw = resraw[i].split(/(<.*?>)/);
for (var j = 0; j < pshraw.length; ++j) if (pshraw[j].length > 0) {
if (pshraw[j][0] != '<') {
isp = ndecode(pshraw[j]).split(/(\s+)/);
for (var k = 0; k < isp.length; ++k) if (isp[k].length > 0)
psh.push(isp[k].trim().length > 0 ? isp[k] : " ");
}
else
psh.push(pshraw[j]);
}
res.push(psh);
}
return res;
}
/**
* 解析bnf 规则
* @param bnf 单条bnf 规则
* @return {Object}
**/
function parse_rules(bnf) {
rules_raw = bnf.split(/(<.*?>)\s*::=\s*(.*)\s*/);
rules = {};
for (var i = 0; i + 2 < rules_raw.length; i += 3) {
rules[rules_raw[i + 1]] = parse_rule(rules_raw[i + 2]);
}
return rules;
}
var sqwat = 0;
function parse_string(string, rules) {
var pos = 0;
var max_streak = 0;
var most_likely_error = {};
function visit_rule_opt(rule, reject_ast, attempt) {
var ast = reject_ast;
function backtrack(errtxt, heuristic) {
if (pos >= max_streak && errtxt != undefined && errtxt) {
if (pos > max_streak)
most_likely_error = {};
if (most_likely_error[errtxt] == undefined)
most_likely_error[errtxt] = 0;
most_likely_error[errtxt] = Math.max(most_likely_error[errtxt], heuristic);
max_streak = pos;
}
while (ast.length) {
var olditem = ast[ast.length - 1];
if (olditem == undefined)
throw " err";
if (olditem.type != "rule") {
ast.pop();
continue;
}
var different_ast = visit_rule(olditem.name, rules[olditem.name], olditem);
if (different_ast != null) {
ast[ast.length - 1] = different_ast; // Keep the new interpretation
return true; // Continue parsing from here
}
ast.pop();
}
return false;
}
if (ast.length)
if (!backtrack("Extra symbols at end of input", 10))
return null;
for (var i = ast.length; i < rule.length; ++i) {
var item = rule[i];
var spos = pos;
if (item in rules) {
if (attempt) {
if (i < attempt.length && attempt[i].pos == pos && attempt[i].type == "rule" && attempt[i].name == item) {
ast[i] = attempt[i];
pos = attempt[i].epos;
continue;
}
attempt = null;
}
var stree = visit_rule(item, rules[item], (i < ast.length) ? ast[i] : null);
if (stree == null) {
if (!backtrack("Expected " + item, i))
return null;
i = ast.length - 1;
continue;
}
ast[i] = stree;
continue;
}
else if (item == " ") {
var spos = pos;
while (/\s/.test(string[pos])) ++pos;
ast[i] = {type: "white", pos: spos, epos: pos, name: " "};
continue;
}
else {
if (string.substr(pos, item.length) != item) {
if (!backtrack("Expected `" + item + "'", i))
return null;
i = ast.length - 1;
continue;
}
if (attempt) {
if (i < attempt.length && attempt[i].pos == pos && attempt[i].type == "token" && attempt[i].name == item) {
ast[i] = attempt[i];
pos = attempt[i].epos;
continue;
}
attempt = null;
}
//成功转换为token
pos += item.length;
ast[i] = {type: "token", name: item, pos: spos, epos: pos}
}
}
return ast;
}
var tolerance = 64;
var worthless_nests = 0;
var last_position = 0;
function visit_rule(rule_name, whole_rule, reject_ast) {
var ast = reject_ast;
var nonnullsub = null;
var offset = 0;
if (pos > last_position) {
last_position = pos;
worthless_nests = 0;
}
if (++worthless_nests >= tolerance) {
--worthless_nests;
return null;
}
if (ast != null) {
nonnullsub = ast.ast;
pos = ast.pos;
if (ast.type != "rule" || ast.ind == undefined) {
if (worthless_nests > 0) --worthless_nests;
return null;
}
var different_ast = visit_rule_opt(whole_rule[ast.ind], ast.ast, null);
if (different_ast != null) {
if (worthless_nests > 0) --worthless_nests;
return {ind: ast.ind, type: "rule", name: rule_name, ast: different_ast, pos: ast.pos, epos: pos};
}
offset = ast.ind + 1;
}
for (var i = offset; i < whole_rule.length; ++i) {
var rule = whole_rule[i];
var spos = pos;
var ap = visit_rule_opt(rule, [], nonnullsub);
if (ap != null) {
if (worthless_nests > 0) --worthless_nests;
return ({ind: i, type: "rule", name: rule_name, ast: ap, pos: spos, epos: pos});
}
pos = spos;
}
if (worthless_nests > 0) --worthless_nests;
return null;
}
sqwat = 0;
brute_rule = [];
for (var r in rules)
brute_rule.push([r]);
var res = visit_rule("<>", brute_rule, null);
while (pos < string.length) {
if (res == null) break;
res = visit_rule("<>", brute_rule, res);
}
if (!res) {
if (most_likely_error) {
var encompassed = string.substr(0, max_streak);
var line = (encompassed.match(/(\r\n|\n|\r)/g) || []).length + 1;
var errpos = max_streak - Math.max(encompassed.lastIndexOf("\n"), encompassed.lastIndexOf("\r"));
var ec = 0;
var the_most_likely_error = "[No error text]";
for (err in most_likely_error)
if (most_likely_error[err] > ec) {
the_most_likely_error = err;
ec = most_likely_error[err];
} else if (most_likely_error[err] == ec)
the_most_likely_error += " OR " + err;
res = {type: "error", error: the_most_likely_error, line: line, position: errpos}
console.log(most_likely_error);
}
}
return res;
}
function operate_string(ast, ops) {
p_ops = eval("operate = bnf.operate; ({ " + ops + " })");
if (!p_ops) return "Failure";
return operate(ast, p_ops);
}
function operate(ast, ops) {
if (ast.type == "rule" && ops[ast.name] != undefined){
return (ops[ast.name])(ast, ops);
}
return (ops['generic'])(ast, ops);
}
return {
parse_rules: parse_rules,
parse_string: parse_string,
operate_string: operate_string,
operate: operate,
nencode: nencode,
ndecode: ndecode
};
})();