-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparser.js
315 lines (309 loc) · 10.2 KB
/
parser.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
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
/* Jison generated parser */
var grammar = (function(){
var parser = {trace: function trace() {
},
yy: {},
symbols_: {"error":2,"program":3,"stmt_list":4,"EOF":5,"stmt":6,".":7,"IDENT":8,"IS":9,"expr":10,"SHOW":11,"NUMBER":12,"+":13,"$accept":0,"$end":1},
terminals_: {"2":"error","5":"EOF","7":".","8":"IDENT","9":"IS","11":"SHOW","12":"NUMBER","13":"+"},
productions_: [0,[3,2],[4,2],[4,3],[6,3],[6,2],[10,1],[10,1],[10,3]],
performAction: function anonymous(yytext, yyleng, yylineno, yy) {
var $$ = arguments[5], $0 = arguments[5].length;
switch (arguments[4]) {
case 1:
return ["Program", {}].concat($$[$0 - 2 + 1 - 1]);
break;
case 2:
this.$ = [$$[$0 - 2 + 1 - 1]];
break;
case 3:
$$[$0 - 3 + 1 - 1].push($$[$0 - 3 + 2 - 1]);
this.$ = $$[$0 - 3 + 1 - 1];
break;
case 4:
this.$ = ["IsStmt", {}, $$[$0 - 3 + 1 - 1], $$[$0 - 3 + 3 - 1]];
break;
case 5:
this.$ = ["ShowStmt", {}, $$[$0 - 2 + 2 - 1]];
break;
case 6:
this.$ = ["IdentExpr", {name: yytext}];
break;
case 7:
this.$ = ["NumberLit", {val: Number(yytext)}];
break;
case 8:
this.$ = ["AddExpr", {}, $$[$0 - 3 + 1 - 1], $$[$0 - 3 + 3 - 1]];
break;
default:;
}
},
table: [{"3":1,"4":2,"6":3,"8":[1,4],"11":[1,5]},{"1":[3]},{"5":[1,6],"6":7,"8":[1,4],"11":[1,5]},{"7":[1,8]},{"9":[1,9]},{"10":10,"8":[1,11],"12":[1,12]},{"1":[2,1]},{"7":[1,13]},{"5":[2,2],"8":[2,2],"11":[2,2]},{"10":14,"8":[1,11],"12":[1,12]},{"13":[1,15],"7":[2,5]},{"7":[2,6],"13":[2,6]},{"7":[2,7],"13":[2,7]},{"5":[2,3],"8":[2,3],"11":[2,3]},{"13":[1,15],"7":[2,4]},{"10":16,"8":[1,11],"12":[1,12]},{"13":[2,8],"7":[2,8]}],
parseError: function parseError(str, hash) {
throw new Error(str);
},
parse: function parse(input) {
var self = this, stack = [0], vstack = [null], table = this.table, yytext = "", yylineno = 0, yyleng = 0, shifts = 0, reductions = 0, recovering = 0, TERROR = 2, EOF = 1;
this.lexer.setInput(input);
this.lexer.yy = this.yy;
this.yy.lexer = this.lexer;
var parseError = this.yy.parseError = typeof this.yy.parseError == "function" ? this.yy.parseError : this.parseError;
function popStack(n) {
stack.length = stack.length - 2 * n;
vstack.length = vstack.length - n;
}
function checkRecover(st) {
for (var p in table[st]) {
if (p == TERROR) {
return true;
}
}
return false;
}
function lex() {
var token;
token = self.lexer.lex() || 1;
if (typeof token !== "number") {
token = self.symbols_[token];
}
return token;
}
var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected, recovered = false;
symbol = lex();
while (true) {
state = stack[stack.length - 1];
action = table[state] && table[state][symbol];
if (typeof action === "undefined" || !action.length || !action[0]) {
if (!recovering) {
expected = [];
for (p in table[state]) {
if (this.terminals_[p] && p > 2) {
expected.push("'" + this.terminals_[p] + "'");
}
}
if (this.lexer.showPosition) {
parseError.call(this, "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", "), {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, expected: expected});
} else {
parseError.call(this, "Parse error on line " + (yylineno + 1) + ": Unexpected '" + this.terminals_[symbol] + "'", {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, expected: expected});
}
}
if (recovering == 3) {
if (symbol == EOF) {
throw "Parsing halted.";
}
yyleng = this.lexer.yyleng;
yytext = this.lexer.yytext;
yylineno = this.lexer.yylineno;
symbol = lex();
}
while (true) {
if (checkRecover(state)) {
break;
}
if (state == 0) {
throw "Parsing halted.";
}
popStack(1);
state = stack[stack.length - 1];
}
preErrorSymbol = symbol;
symbol = TERROR;
state = stack[stack.length - 1];
action = table[state] && table[state][TERROR];
recovering = 3;
}
if (action[0] instanceof Array && action.length > 1) {
throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
}
a = action;
switch (a[0]) {
case 1:
shifts++;
stack.push(symbol);
vstack.push(this.lexer.yytext);
stack.push(a[1]);
if (!preErrorSymbol) {
yyleng = this.lexer.yyleng;
yytext = this.lexer.yytext;
yylineno = this.lexer.yylineno;
symbol = lex();
if (recovering > 0) {
recovering--;
}
} else {
symbol = preErrorSymbol;
preErrorSymbol = null;
}
break;
case 2:
reductions++;
len = this.productions_[a[1]][1];
yyval.$ = vstack[vstack.length - len];
r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, a[1], vstack);
if (typeof r !== "undefined") {
return r;
}
if (len) {
stack = stack.slice(0, -1 * len * 2);
vstack = vstack.slice(0, -1 * len);
}
stack.push(this.productions_[a[1]][0]);
vstack.push(yyval.$);
newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
stack.push(newState);
break;
case 3:
this.reductionCount = reductions;
this.shiftCount = shifts;
return true;
default:;
}
}
return true;
}};/* Jison generated lexer */
var lexer = (function(){var lexer = ({EOF:"",
parseError:function parseError(str, hash) {
if (this.yy.parseError) {
this.yy.parseError(str, hash);
} else {
throw new Error(str);
}
},
setInput:function (input) {
this._input = input;
this._more = this._less = this.done = false;
this.yylineno = this.yyleng = 0;
this.yytext = this.matched = this.match = "";
return this;
},
input:function () {
var ch = this._input[0];
this.yytext += ch;
this.yyleng++;
this.match += ch;
this.matched += ch;
var lines = ch.match(/\n/);
if (lines) {
this.yylineno++;
}
this._input = this._input.slice(1);
return ch;
},
unput:function (ch) {
this._input = ch + this._input;
return this;
},
more:function () {
this._more = true;
return this;
},
pastInput:function () {
var past = this.matched.substr(0, this.matched.length - this.match.length);
return (past.length > 20 ? "..." : "") + past.substr(-20).replace(/\n/g, "");
},
upcomingInput:function () {
var next = this.match;
if (next.length < 20) {
next += this._input.substr(0, 20 - next.length);
}
return (next.substr(0, 20) + (next.length > 20 ? "..." : "")).replace(/\n/g, "");
},
showPosition:function () {
var pre = this.pastInput();
var c = (new Array(pre.length + 1)).join("-");
return pre + this.upcomingInput() + "\n" + c + "^";
},
next:function () {
if (this.done) {
return this.EOF;
}
if (!this._input) {
this.done = true;
}
var token, match, lines;
if (!this._more) {
this.yytext = "";
this.match = "";
}
for (var i = 0; i < this.rules.length; i++) {
match = this._input.match(this.rules[i]);
if (match) {
lines = match[0].match(/\n/g);
if (lines) {
this.yylineno += lines.length;
}
this.yytext += match[0];
this.match += match[0];
this.matches = match;
this.yyleng = this.yytext.length;
this._more = false;
this._input = this._input.slice(match[0].length);
this.matched += match[0];
token = this.performAction.call(this, this.yy, this, i);
if (token) {
return token;
} else {
return;
}
}
}
if (this._input == this.EOF) {
return this.EOF;
} else {
this.parseError("Lexical error on line " + (this.yylineno + 1) + ". Unrecognized text.\n" + this.showPosition(), {text: "", token: null, line: this.yylineno});
}
},
lex:function () {
var r = this.next();
if (typeof r !== "undefined") {
return r;
} else {
return this.lex();
}
}});
lexer.performAction = function anonymous(yy, yy_) {
switch (arguments[2]) {
case 0:
break;
case 1:
return 9;
break;
case 2:
return 11;
break;
case 3:
return 12;
break;
case 4:
return 8;
break;
case 5:
return 13;
break;
case 6:
return 7;
break;
case 7:
return 5;
break;
default:;
}
};
lexer.rules = [/^\s+/,/^is\b/,/^show\b/,/^[0-9]+(\.[0-9]+)?/,/^[a-zA-Z]+/,/^\+/,/^\./,/^$/];return lexer;})()
parser.lexer = lexer;
return parser;
})();
if (typeof require !== 'undefined') {
exports.parser = grammar;
exports.parse = function () { return grammar.parse.apply(grammar, arguments); }
exports.main = function commonjsMain(args) {
var cwd = require("file").path(require("file").cwd());
if (!args[1]) {
throw new Error("Usage: " + args[0] + " FILE");
}
var source = cwd.join(args[1]).read({charset: "utf-8"});
exports.parser.parse(source);
}
if (require.main === module) {
exports.main(require("system").args);
}
}