-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
380 lines (380 loc) · 14.5 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"use strict";
Array.prototype.peek = function () {
if (this.length === 0) {
throw new Error('Illegal state: peek at empty stack');
}
return this[this.length - 1];
};
Array.prototype.clear = function () {
// this is allowed because JavaScript is silly
this.length = 0;
};
var Parser = /** @class */ (function () {
function Parser(keepComments) {
if (keepComments === void 0) { keepComments = false; }
this.keepComments = keepComments;
this.src = '';
this.quotes = [];
this.pos = 0;
this.stringLiteralBuilder = null;
this.stringLiteralDelimiter = null;
}
Parser.parse = function (src, keepComments) {
if (keepComments === void 0) { keepComments = false; }
var p = new Parser(keepComments);
p.read(src);
var q = p.parse();
if (q === null) {
throw new Error('Syntax error: unexpected end of source');
}
return q;
};
Parser.prototype.read = function (src) {
this.src = src;
this.pos = 0;
};
Parser.prototype.parse = function () {
if (this.quotes.length === 0) {
this.quotes.push(new Quote());
this.stringLiteralBuilder = null;
this.stringLiteralDelimiter = null;
}
else if (this.stringLiteralBuilder != null) {
this.stepStringLiteral(this.quotes.peek());
}
while (this.pos < this.src.length) {
this.step();
}
if (this.quotes.length === 1 && this.stringLiteralBuilder === null) {
return this.quotes.pop();
}
else {
return null;
}
};
Parser.prototype.step = function () {
var c = this.src[this.pos];
var q = this.quotes.peek();
if (/\s|\n/.test(c)) {
this.stepWhitespace();
}
else if (c === '#') {
var startPos = ++this.pos;
this.stepComment();
if (this.keepComments) {
q.ops.push(new CommentOp(this.src.substring(startPos, this.pos)));
}
}
else if (c === '(') {
++this.pos;
this.quotes.push(new Quote());
}
else if (c === ')') {
++this.pos;
if (this.quotes.length > 1) {
this.quotes.pop();
this.quotes.peek().ops.push(new PushOp(q));
}
else {
throw new Error("Syntax error: unexpected character ) at position " + this.pos);
}
}
else if (c === '!') {
++this.pos;
q.ops.push(NativeOp.NOW);
}
else if (c === '@') {
var i = 0;
do {
++this.pos;
++i;
} while (this.pos < this.src.length && this.src[this.pos] === '@');
q.ops.push(new DupOp(i));
}
else if (c === '.') {
if (++this.pos === this.src.length) {
throw new Error("Syntax error: unexpected end of source: expected .[ or .{ or .identifier");
}
c = this.src[this.pos];
if (c === '[') {
++this.pos;
q.ops.push(NativeOp.STACK_ENTER);
}
else if (c === '{') {
++this.pos;
q.ops.push(NativeOp.SCOPE_ENTER);
}
else if (this.identifierChar(c)) {
var name_1 = this.nextIdentifier(true);
q.ops.push(NativeOp.SCOPE_ENTER);
q.ops.push(new LocalReadOp(name_1));
q.ops.push(NativeOp.SCOPE_ASCEND);
}
else {
throw new Error("Syntax error: unexpected character " + c + " at position " + this.pos);
}
}
else if (c === '[') {
++this.pos;
q.ops.push(NativeOp.STACK_DESCEND);
}
else if (c === '{') {
++this.pos;
q.ops.push(NativeOp.SCOPE_DESCEND);
}
else if (c === ']') {
if (++this.pos === this.src.length || this.src[this.pos] !== '.') {
q.ops.push(NativeOp.STACK_ASCEND);
}
else {
++this.pos;
q.ops.push(NativeOp.STACK_EXIT);
}
}
else if (c === '}') {
if (++this.pos === this.src.length || this.src[this.pos] !== '.') {
q.ops.push(NativeOp.SCOPE_ASCEND);
}
else {
++this.pos;
q.ops.push(NativeOp.SCOPE_EXIT);
}
}
else if (c === '"' || c === "'") {
this.stepStringLiteral(q);
}
else if (this.digitChar(c) || (c === '-' && this.pos + 1 < this.src.length && this.digitChar(this.src[this.pos + 1]))) {
q.ops.push(new PushOp(this.nextNumber()));
}
else if ((c === '<' || c === '>') && this.pos + 1 < this.src.length && (this.identifierChar(this.src[this.pos + 1]) || (c === '>' && this.src[this.pos + 1] === '!'))) {
var Assignment = /** @class */ (function () {
function Assignment() {
this.names = [];
this.doNow = false;
}
return Assignment;
}());
// assign var
var assignments = [];
do {
var a = new Assignment();
++this.pos;
if (c === '>' && this.src[this.pos] === '!') {
a.doNow = true;
++this.pos;
}
do {
a.names.push(this.nextIdentifier(true));
} while (this.pos < this.src.length && this.src[this.pos] === '.' && ++this.pos < this.src.length /* skip the . */);
assignments.push(a);
} while (this.pos < this.src.length && this.src[this.pos] === ',');
while (assignments.length) {
var a = (c === '<' ? assignments.shift() : assignments.pop());
var scopes = 0;
while (true) {
var name_2 = a.names.shift();
if (!a.names.length) {
q.ops.push(c === '<' ? new LocalReadOp(name_2) : new AssignOp(name_2, a.doNow));
break;
}
else {
q.ops.push(scopes === 0 ? new ReadOp(name_2) : new LocalReadOp(name_2));
q.ops.push(NativeOp.SCOPE_ENTER);
++scopes;
}
}
while (--scopes >= 0) {
q.ops.push(NativeOp.SCOPE_ASCEND);
}
}
}
else {
var name_3;
if (this.identifierChar(c)) {
name_3 = this.nextIdentifier(false);
}
else if (this.opChar(c)) {
name_3 = this.nextOpName();
}
else {
throw new Error("Syntax error: unexpected character " + c + " at position " + this.pos);
}
var op = NativeOp.getByName(name_3);
if (op !== null) {
q.ops.push(op);
}
else {
// TODO: are built-in operators allowed to be overloaded?
q.ops.push(new ReadOp(name_3));
}
}
};
Parser.prototype.stepWhitespace = function () {
// TODO: search for next non-whitespace character after pos
while (/\s|\n/.test(this.src[this.pos]) && ++this.pos < this.src.length)
;
};
Parser.prototype.stepComment = function () {
while (this.pos < this.src.length && this.src[this.pos++] !== '\n')
;
};
Parser.prototype.digitChar = function (c) {
return /[0-9]/.test(c);
};
Parser.prototype.identifierChar = function (c) {
return /[a-zA-Z0-9_]/.test(c);
};
Parser.prototype.opChar = function (c) {
// TODO: replace with regex?
return c === '>' || c === '<' || c === '='
|| c === '?' || c === '\\'
|| c === '+' || c === '-' || c === '*' || c === '/' || c === '%'
|| c === '&' || c === '|' || c === '!' || c === '~' || c === '^';
};
Parser.prototype.nextNumber = function () {
var startPos = this.pos;
if (this.src[this.pos] === '-') {
++this.pos;
}
var hasExponent = false;
var isDouble = false;
while (this.pos < this.src.length) {
var c = this.src[this.pos];
if (c === '.') {
if (isDouble) {
throw new Error("Syntax error: unexpected character . at position " + this.pos);
}
isDouble = true;
}
else if (c === 'e' || c === 'E') {
if (hasExponent) {
throw new Error("Syntax error: unexpected character " + c + " at position " + this.pos);
}
hasExponent = true;
}
else if (!this.digitChar(c)) {
break;
}
++this.pos;
}
var n = this.src.substring(startPos, this.pos);
try {
if (isDouble) {
// TODO
throw new Error('Error: doubles are not implemented');
return new DoubleValue(parseFloat(n));
}
else {
// TODO: use bigint?
return new IntValue(parseInt(n));
}
}
catch (e) {
throw new Error("Syntax error: invalid numeric literal " + n + " at position " + startPos);
}
};
Parser.prototype.stepStringLiteral = function (q) {
if (this.stringLiteralBuilder === null || this.stringLiteralDelimiter === null) {
var d = this.src.charAt(this.pos++);
this.stringLiteralDelimiter = d;
if (this.src[this.pos] === d && this.src[this.pos + 1] === d) {
this.pos += 2;
this.stringLiteralDelimiter = d + d + d;
}
this.stringLiteralBuilder = [];
}
while (this.pos < this.src.length) {
var c = this.src[this.pos++];
switch (c) {
case '\\':
if (this.pos === this.src.length) {
throw new Error("Syntax error: unexpected end of source: expected escape sequence in String literal");
}
c = this.src[this.pos++];
switch (c) {
case '"':
case "'":
case '\\':
this.stringLiteralBuilder.push(c);
break;
case 'n':
this.stringLiteralBuilder.push('\n');
break;
case 'r':
this.stringLiteralBuilder.push('\r');
break;
case 't':
this.stringLiteralBuilder.push('\t');
break;
case 'b':
this.stringLiteralBuilder.push('\b');
break;
case 'f':
this.stringLiteralBuilder.push('\f');
break;
case 'u':
if (this.pos + 4 > this.src.length) {
throw new Error("Syntax error: unexpected end of source: expected unicode escape sequence in String literal");
}
var hex = parseInt(this.src.substring(this.pos, this.pos + 4), 16);
this.stringLiteralBuilder.push(String.fromCharCode(hex));
this.pos += 4;
break;
default:
throw new Error("Syntax error: unknown escape sequence \\" + c + " in String literal at position " + this.pos);
}
break;
case '\n':
if (this.stringLiteralDelimiter.length === 1) {
throw new Error("Syntax error: unexpected newline in String literal at position " + this.pos + " (use triple delimiter?)");
}
else {
this.stringLiteralBuilder.push('\n');
}
break;
case '"':
case "'":
if (this.src.substring(this.pos - 1, this.pos + this.stringLiteralDelimiter.length - 1) === this.stringLiteralDelimiter) {
this.pos += this.stringLiteralDelimiter.length - 1;
var s = this.stringLiteralBuilder.join('');
this.stringLiteralBuilder = null;
this.stringLiteralDelimiter = null;
q.ops.push(new PushOp(new StringValue(s)));
return;
}
// intentional fall-through
default:
this.stringLiteralBuilder.push(c);
}
}
if (this.stringLiteralDelimiter.length === 3) {
this.stringLiteralBuilder.push("\n");
}
else {
throw new Error("Syntax error: unexpected end of source: expected delimiter " + this.stringLiteralDelimiter + " in String literal");
}
};
Parser.prototype.nextIdentifier = function (throwIfKeyword) {
var startPos = this.pos;
while (++this.pos < this.src.length && this.identifierChar(this.src[this.pos]))
;
if (startPos === this.pos || this.digitChar(this.src.charAt(startPos))) {
throw new Error("Syntax error: expected identifier at position " + startPos);
}
var name = this.src.substring(startPos, this.pos);
if (name === '') {
throw new Error('Parser error: empty name at position ' + startPos + " (shouldn't happen)");
}
else if (throwIfKeyword && NativeOp.getByName(name) !== null) {
throw new Error("Syntax error: unexpected keyword " + name + " at position " + startPos);
}
return name;
};
Parser.prototype.nextOpName = function () {
var startPos = this.pos;
while (++this.pos < this.src.length && this.opChar(this.src[this.pos]))
;
return this.src.substring(startPos, this.pos);
};
return Parser;
}());