-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
366 lines (363 loc) · 9.62 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
(function(global) {
function InputStream(input) {
var pos = 0, line = 1, col = 0;
return {
next : next,
peek : peek,
eof : eof,
croak : croak
};
function next() {
var ch = input.charAt(pos++);
if (ch == "\n") {
line++;
col = 0;
} else {
col++;
}
return ch;
}
function peek() {
return input.charAt(pos);
}
function eof() {
return peek() == "";
}
function croak(msg) {
throw new Error(msg + " (" + line + ":" + col + ")");
}
}
function TokenStream(input) {
var current = null;
var keywords = " IF ELSE THEN FUNC TRUE FALSE DO END ";
return {
next : next,
peek : peek,
eof : eof,
croak : input.croak
};
function is_keyword(x) {
return keywords.indexOf(" " + x + " ") >= 0;
}
function is_digit(ch) {
return /[0-9]/i.test(ch);
}
function is_id_start(ch) {
return /[a-z_\$]/i.test(ch);
}
function is_id(ch) {
return is_id_start(ch) || "_$0123456789".indexOf(ch) >= 0;
}
function is_op_char(ch) {
return "+-*/%=&|<>!".indexOf(ch) >= 0;
}
function is_punc(ch) {
return ",(){}[]".indexOf(ch) >= 0;
}
function is_separator(ch) {
return "\n".indexOf(ch) >= 0;
}
function is_whitespace(ch) {
return " \t".indexOf(ch) >= 0;
}
function read_while(predicate) {
var str = "";
while(!input.eof() && predicate(input.peek()))
str += input.next();
return str;
}
function read_number() {
var has_dot = false;
var number = read_while(function(ch) {
if (ch == ".") {
if (has_dot) return false;
has_dot = true;
return true;
}
return is_digit(ch);
});
return { type: "num", value: parseFloat(number) };
}
function read_ident() {
var id = read_while(is_id);
return {
type: is_keyword(id) ? "kw" : "var",
value: id
};
}
function read_escaped(end) {
var escaped = false, str = "";
input.next();
while (!input.eof()) {
var ch = input.next();
if (escaped) {
str += ch;
escaped = false;
} else if (ch == "\\") {
escaped = true;
} else if (ch == end) {
break;
} else {
str += ch;
}
}
return str;
}
function read_string() {
return { type: "str", value: read_escaped('"') };
}
function skip_separator() {
read_while(function(ch) {
return is_whitespace(ch) || is_separator(ch);
});
}
function skip_comment() {
read_while(function(ch) { return ch != "\n" });
input.next();
}
function read_next() {
read_while(is_whitespace);
if (input.eof()) return null;
var ch = input.peek();
if (ch == "#") {
skip_comment();
return read_next();
}
if (ch == '"') return read_string();
if (is_digit(ch)) return read_number();
if (is_id_start(ch)) return read_ident();
if (is_punc(ch)) return {
type : "punc",
value : input.next()
};
if (is_op_char(ch)) return {
type : "op",
value : read_while(is_op_char)
};
if (is_separator(ch)) {
skip_separator();
return {
type : "separator"
};
}
input.croak("Can't handle character: " + ch);
}
function peek() {
return current || (current = read_next());
}
function next() {
var tok = current;
current = null;
return tok || read_next();
}
function eof() {
return peek() == null;
}
}
var FALSE = { type: "bool", value: false };
function parse(input) {
var PRECEDENCE = {
"=": 1,
"||": 2,
"&&": 3,
"<": 7, ">": 7, "<=": 7, ">=": 7, "==": 7, "!=": 7,
"+": 10, "-": 10,
"*": 20, "/": 20, "%": 20,
};
return parse_toplevel();
function is_punc(ch) {
var tok = input.peek();
return tok && tok.type == "punc" && (!ch || tok.value == ch) && tok;
}
function is_kw(kw) {
var tok = input.peek();
return tok && tok.type == "kw" && (!kw || tok.value == kw) && tok;
}
function is_op(op) {
var tok = input.peek();
return tok && tok.type == "op" && (!op || tok.value == op) && tok;
}
function is_separator() {
var tok = input.peek();
return tok && tok.type == "separator";
}
function skip_punc(ch) {
if (is_punc(ch)) input.next();
else input.croak("Expecting punctuation: \"" + ch + "\"");
}
function skip_kw(kw) {
if (is_kw(kw)) input.next();
else input.croak("Expecting keyword: \"" + kw + "\"");
}
function skip_op(op) {
if (is_op(op)) input.next();
else input.croak("Expecting operator: \"" + op + "\"");
}
function skip_separator() {
if (is_separator()) input.next();
else input.croak("Expecting line break");
}
function unexpected() {
input.croak("Unexpected token: " + JSON.stringify(input.peek()));
}
function maybe_binary(left, my_prec) {
var tok = is_op();
if (tok) {
var his_prec = PRECEDENCE[tok.value];
if (his_prec > my_prec) {
input.next();
return maybe_binary({
type : tok.value == "=" ? "assign" : "binary",
operator : tok.value,
left : left,
right : maybe_binary(parse_atom(), his_prec)
}, my_prec);
}
}
return left;
}
function parse_param(parser, ignore_kw) {
var a = [], first = true;
while (!input.eof()) {
if (is_kw("DO") || is_separator()) break;
if (first) first = false; else skip_punc(",");
if (is_kw("DO") || is_separator()) break;
a.push(parser());
}
if (!ignore_kw && is_kw("DO")) {
a.push(parse_prog());
}
return a;
}
function delimited(start, stop, separator, parser) {
var a = [], first = true;
skip_kw(start);
if (is_separator()) skip_separator();
while (!input.eof()) {
if (is_kw(stop)) break;
if (first) first = false;
else {
if (separator == "\n") skip_separator();
else skip_punc(separator);
}
if (is_kw(stop)) break;
a.push(parser());
}
if (is_separator()) skip_separator();
skip_kw(stop);
return a;
}
function delimited_if(separator, parser) {
var a = [], first = true;
while (!input.eof()) {
if (is_kw('ELSE') || is_kw('END')) break;
if (first) first = false;
else {
if (separator == "\n") skip_separator();
else skip_punc(separator);
}
if (is_kw('ELSE') || is_kw('END')) break;
a.push(parser());
}
return a;
}
function parse_call(func) {
return {
type: "call",
func: func,
args: parse_param(parse_expression),
};
}
function parse_varname() {
var name = input.next();
if (name.type != "var") input.croak("Expecting variable name");
return name.value;
}
function parse_if() {
skip_kw("IF");
var cond = parse_expression();
skip_kw("THEN");
if (is_separator()) skip_separator();
var then = parse_if_prog();
var ret = {
type: "if",
cond: cond,
then: then,
};
if (is_kw("ELSE")) {
input.next();
if (is_separator()) skip_separator();
ret.else = parse_if_prog();
}
skip_kw("END");
return ret;
}
function parse_func() {
return {
type: "func",
vars: parse_param(parse_varname, true),
body: parse_prog()
};
}
function parse_bool() {
return {
type : "bool",
value : input.next().value == "TRUE"
};
}
function maybe_call(expr) {
expr = expr();
return (expr.type == "var" && !is_op() && !is_separator() && (!is_kw() || is_kw("DO")) && !input.eof()) ? parse_call(expr) : expr;
}
function parse_atom() {
return maybe_call(function(){
if (is_punc("(")) {
input.next();
var exp = parse_expression();
skip_punc(")");
return exp;
}
if (is_kw("DO")) return parse_prog();
if (is_kw("IF")) return parse_if();
if (is_kw("TRUE") || is_kw("FALSE")) return parse_bool();
if (is_kw("FUNC")) {
input.next();
return parse_func();
}
var tok = input.next();
if (tok.type == "var" || tok.type == "num" || tok.type == "str")
return tok;
unexpected();
});
}
function parse_toplevel() {
var prog = [];
while (!input.eof()) {
prog.push(parse_expression());
if (!input.eof()) skip_separator();
}
return { type: "prog", prog: prog };
}
function parse_prog() {
var prog = delimited("DO", "END", "\n", parse_expression);
if (prog.length == 0) return FALSE;
if (prog.length == 1) return prog[0];
return { type: "prog", prog: prog };
}
function parse_if_prog() {
var prog = delimited_if("\n", parse_expression);
if (prog.length == 0) return FALSE;
if (prog.length == 1) return prog[0];
return { type: "prog", prog: prog };
}
function parse_expression() {
return maybe_call(function(){
return maybe_binary(parse_atom(), 0);
});
}
}
global.Mason = global.Mason || {};
global.Mason.AST = function(code) {
return parse(TokenStream(InputStream(code)));
};
})(window);