This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlexer.js
427 lines (356 loc) · 8.87 KB
/
lexer.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import {assert} from 'rtts-assert';
export class Token {
constructor(index:number, text:string){
this.index = index;
this.text = text;
}
withOp(op) {
this.opKey = op;
return this;
}
withGetterSetter(key) {
this.key = key;
return this;
}
withValue(value) {
this.value = value;
return this;
}
toString() {
return `Token(${this.text})`;
}
}
export class ArrayOfToken {
static assert(obj) {
assert(obj).is(assert.arrayOf(Token));
}
constructor() {
assert.fail('type is not instantiable');
}
}
export class Lexer {
lex(text:string):ArrayOfToken {
var scanner = new Scanner(text);
var tokens = [];
var token = scanner.scanToken();
while (token) {
tokens.push(token);
token = scanner.scanToken();
}
return tokens;
}
}
export class Scanner {
constructor(input:string) {
this.input = input;
this.length = input.length;
this.peek = 0;
this.index = -1;
this.advance();
}
scanToken():Token {
// Skip whitespace.
while (this.peek <= $SPACE) {
if (++this.index >= this.length) {
this.peek = $EOF;
return null;
} else {
this.peek = this.input.charCodeAt(this.index);
}
}
// Handle identifiers and numbers.
if (isIdentifierStart(this.peek)) {
return this.scanIdentifier();
}
if (isDigit(this.peek)) {
return this.scanNumber(this.index);
}
var start = this.index;
switch (this.peek) {
case $PERIOD:
this.advance();
return isDigit(this.peek) ? this.scanNumber(start) : new Token(start, '.');
case $LPAREN:
case $RPAREN:
case $LBRACE:
case $RBRACE:
case $LBRACKET:
case $RBRACKET:
case $COMMA:
case $COLON:
case $SEMICOLON:
return this.scanCharacter(start, String.fromCharCode(this.peek));
case $SQ:
case $DQ:
return this.scanString();
case $PLUS:
case $MINUS:
case $STAR:
case $SLASH:
case $PERCENT:
case $CARET:
case $QUESTION:
return this.scanOperator(start, String.fromCharCode(this.peek));
case $LT:
case $GT:
case $BANG:
case $EQ:
return this.scanComplexOperator(start, $EQ, String.fromCharCode(this.peek), '=');
case $AMPERSAND:
return this.scanComplexOperator(start, $AMPERSAND, '&', '&');
case $BAR:
return this.scanComplexOperator(start, $BAR, '|', '|');
case $TILDE:
return this.scanComplexOperator(start, $SLASH, '~', '/');
case $NBSP:
while (isWhitespace(this.peek)){
this.advance();
}
return this.scanToken();
}
var character = String.fromCharCode(this.peek);
this.error(`Unexpected character [${character}]`);
return null;
}
scanCharacter(start:number, text:string):Token {
assert(this.peek == text.charCodeAt(0));
this.advance();
return new Token(start, text);
}
scanOperator(start:number, text:string):Token {
assert(this.peek == text.charCodeAt(0));
assert(OPERATORS.indexOf(text) != -1);
this.advance();
return new Token(start, text).withOp(text);
}
scanComplexOperator(start:number, code:number, one:string, two:string):Token {
assert(this.peek == one.charCodeAt(0));
this.advance();
var text = one;
if (this.peek == code) {
this.advance();
text += two;
}
assert(OPERATORS.indexOf(text) != -1);
return new Token(start, text).withOp(text);
}
scanIdentifier():Token {
assert(isIdentifierStart(this.peek));
var start = this.index;
this.advance();
while (isIdentifierPart(this.peek)) {
this.advance();
}
var text = this.input.substring(start, this.index);
var result = new Token(start, text);
// TODO(kasperl): Deal with null, undefined, true, and false in
// a cleaner and faster way.
if (OPERATORS.indexOf(text) != -1) {
result.withOp(text);
} else {
result.withGetterSetter(text);
}
return result;
}
scanNumber(start:number):Token {
assert(isDigit(this.peek));
var simple = (this.index == start);
this.advance(); // Skip initial digit.
while (true) {
if (isDigit(this.peek)) {
// Do nothing.
} else if (this.peek == $PERIOD) {
simple = false;
} else if (isExponentStart(this.peek)) {
this.advance();
if (isExponentSign(this.peek)){
this.advance();
}
if (!isDigit(this.peek)){
this.error('Invalid exponent', -1);
}
simple = false;
} else {
break;
}
this.advance();
}
var text = this.input.substring(start, this.index);
var value = simple ? parseInt(text) : parseFloat(text);
return new Token(start, text).withValue(value);
}
scanString():Token {
assert(this.peek == $SQ || this.peek == $DQ);
var start = this.index;
var quote = this.peek;
this.advance(); // Skip initial quote.
var buffer;
var marker = this.index;
while (this.peek != quote) {
if (this.peek == $BACKSLASH) {
if (buffer == null) {
buffer = [];
}
buffer.push(this.input.substring(marker, this.index));
this.advance();
var unescaped;
if (this.peek == $u) {
// TODO(kasperl): Check bounds? Make sure we have test
// coverage for this.
var hex = this.input.substring(this.index + 1, this.index + 5);
if(!/[A-Z0-9]{4}/.test(hex)){
this.error(`Invalid unicode escape [\\u${hex}]`);
}
unescaped = parseInt(hex, 16);
for (var i = 0; i < 5; i++) {
this.advance();
}
} else {
unescaped = decodeURIComponent(this.peek);
this.advance();
}
buffer.push(String.fromCharCode(unescaped));
marker = this.index;
} else if (this.peek == $EOF) {
this.error('Unterminated quote');
} else {
this.advance();
}
}
var last = this.input.substring(marker, this.index);
this.advance(); // Skip terminating quote.
var text = this.input.substring(start, this.index);
// Compute the unescaped string value.
var unescaped = last;
if (buffer != null) {
buffer.push(last);
unescaped = buffer.join('');
}
return new Token(start, text).withValue(unescaped);
}
advance() {
if (++this.index >= this.length){
this.peek = $EOF;
}else {
this.peek = this.input.charCodeAt(this.index);
}
}
error(message:string, offset:number = 0) {
// TODO(kasperl): Try to get rid of the offset. It is only used to match
// the error expectations in the lexer tests for numbers with exponents.
var position = this.index + offset;
throw new Error(`Lexer Error: ${message} at column ${position} in expression [${this.input}]`);
}
}
var OPERATORS = [
'undefined',
'null',
'true',
'false',
'+',
'-',
'*',
'/',
'~/',
'%',
'^',
'=',
'==',
'!=',
'<',
'>',
'<=',
'>=',
'&&',
'||',
'&',
'|',
'!',
'?',
];
var $EOF = 0;
var $TAB = 9;
var $LF = 10;
var $VTAB = 11;
var $FF = 12;
var $CR = 13;
var $SPACE = 32;
var $BANG = 33;
var $DQ = 34;
var $$ = 36;
var $PERCENT = 37;
var $AMPERSAND = 38;
var $SQ = 39;
var $LPAREN = 40;
var $RPAREN = 41;
var $STAR = 42;
var $PLUS = 43;
var $COMMA = 44;
var $MINUS = 45;
var $PERIOD = 46;
var $SLASH = 47;
var $COLON = 58;
var $SEMICOLON = 59;
var $LT = 60;
var $EQ = 61;
var $GT = 62;
var $QUESTION = 63;
var $0 = 48;
var $9 = 57;
var $A = 65;
var $E = 69;
var $Z = 90;
var $LBRACKET = 91;
var $BACKSLASH = 92;
var $RBRACKET = 93;
var $CARET = 94;
var $_ = 95;
var $a = 97;
var $e = 101;
var $f = 102;
var $n = 110;
var $r = 114;
var $t = 116;
var $u = 117;
var $v = 118;
var $z = 122;
var $LBRACE = 123;
var $BAR = 124;
var $RBRACE = 125;
var $TILDE = 126;
var $NBSP = 160;
function isWhitespace(code) {
return (code >= $TAB && code <= $SPACE) || (code == $NBSP);
}
function isIdentifierStart(code) {
return ($a <= code && code <= $z)
|| ($A <= code && code <= $Z)
|| (code == $_)
|| (code == $$);
}
function isIdentifierPart(code) {
return ($a <= code && code <= $z)
|| ($A <= code && code <= $Z)
|| ($0 <= code && code <= $9)
|| (code == $_)
|| (code == $$);
}
function isDigit(code) {
return ($0 <= code && code <= $9);
}
function isExponentStart(code) {
return (code == $e || code == $E);
}
function isExponentSign(code) {
return (code == $MINUS || code == $PLUS);
}
function unescape(code) {
switch(code) {
case $n: return $LF;
case $f: return $FF;
case $r: return $CR;
case $t: return $TAB;
case $v: return $VTAB;
default: return code;
}
}