This repository has been archived by the owner on May 19, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
index.js
885 lines (753 loc) · 27.4 KB
/
index.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/* eslint max-len: 0 */
// @flow
import type { TokenType } from "./types";
import type { Options } from "../options";
import type { Position } from "../util/location";
import { isIdentifierStart, isIdentifierChar, isKeyword } from "../util/identifier";
import { types as tt, keywords as keywordTypes } from "./types";
import { type TokContext, types as ct } from "./context";
import LocationParser from "../parser/location";
import { SourceLocation } from "../util/location";
import { lineBreak, lineBreakG, isNewLine, nonASCIIwhitespace } from "../util/whitespace";
import State from "./state";
// Object type used to represent tokens. Note that normally, tokens
// simply exist as properties on the parser object. This is only
// used for the onToken callback and the external tokenizer.
export class Token {
constructor(state: State) {
this.type = state.type;
this.value = state.value;
this.start = state.start;
this.end = state.end;
this.loc = new SourceLocation(state.startLoc, state.endLoc);
}
type: TokenType;
value: any;
start: number;
end: number;
loc: SourceLocation;
}
// ## Tokenizer
function codePointToString(code: number): string {
// UTF-16 Decoding
if (code <= 0xFFFF) {
return String.fromCharCode(code);
} else {
return String.fromCharCode(((code - 0x10000) >> 10) + 0xD800, ((code - 0x10000) & 1023) + 0xDC00);
}
}
export default class Tokenizer extends LocationParser {
// Forward-declarations
// parser/util.js
+unexpected: (pos?: ?number, messageOrType?: string | TokenType) => empty;
isLookahead: boolean;
constructor(options: Options, input: string) {
super();
this.state = new State;
this.state.init(options, input);
}
// Move to the next token
next(): void {
if (!this.isLookahead) {
this.state.tokens.push(new Token(this.state));
}
this.state.lastTokEnd = this.state.end;
this.state.lastTokStart = this.state.start;
this.state.lastTokEndLoc = this.state.endLoc;
this.state.lastTokStartLoc = this.state.startLoc;
this.nextToken();
}
// TODO
eat(type: TokenType): boolean {
if (this.match(type)) {
this.next();
return true;
} else {
return false;
}
}
// TODO
match(type: TokenType): boolean {
return this.state.type === type;
}
// TODO
isKeyword(word: string): boolean {
return isKeyword(word);
}
// TODO
lookahead(): State {
const old = this.state;
this.state = old.clone(true);
this.isLookahead = true;
this.next();
this.isLookahead = false;
const curr = this.state.clone(true);
this.state = old;
return curr;
}
// Toggle strict mode. Re-reads the next number or string to please
// pedantic tests (`"use strict"; 010;` should fail).
setStrict(strict: boolean): void {
this.state.strict = strict;
if (!this.match(tt.num) && !this.match(tt.string)) return;
this.state.pos = this.state.start;
while (this.state.pos < this.state.lineStart) {
this.state.lineStart = this.input.lastIndexOf("\n", this.state.lineStart - 2) + 1;
--this.state.curLine;
}
this.nextToken();
}
curContext(): TokContext {
return this.state.context[this.state.context.length - 1];
}
// Read a single token, updating the parser object's token-related
// properties.
nextToken(): void {
const curContext = this.curContext();
if (!curContext || !curContext.preserveSpace) this.skipSpace();
this.state.containsOctal = false;
this.state.octalPosition = null;
this.state.start = this.state.pos;
this.state.startLoc = this.state.curPosition();
if (this.state.pos >= this.input.length) return this.finishToken(tt.eof);
if (curContext.override) {
return curContext.override(this);
} else {
return this.readToken(this.fullCharCodeAtPos());
}
}
readToken(code: number): void {
// Identifier or keyword. '\uXXXX' sequences are allowed in
// identifiers, so '\' also dispatches to that.
if (isIdentifierStart(code) || code === 92 /* '\' */) {
return this.readWord();
} else {
return this.getTokenFromCode(code);
}
}
fullCharCodeAtPos(): number {
const code = this.input.charCodeAt(this.state.pos);
if (code <= 0xd7ff || code >= 0xe000) return code;
const next = this.input.charCodeAt(this.state.pos + 1);
return (code << 10) + next - 0x35fdc00;
}
pushComment(block: boolean, text: string, start: number, end: number, startLoc: Position, endLoc: Position): void {
const comment = {
type: block ? "CommentBlock" : "CommentLine",
value: text,
start: start,
end: end,
loc: new SourceLocation(startLoc, endLoc)
};
if (!this.isLookahead) {
this.state.tokens.push(comment);
this.state.comments.push(comment);
this.addComment(comment);
}
}
skipBlockComment(): void {
const startLoc = this.state.curPosition();
const start = this.state.pos;
const end = this.input.indexOf("*/", this.state.pos += 2);
if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment");
this.state.pos = end + 2;
lineBreakG.lastIndex = start;
let match;
while ((match = lineBreakG.exec(this.input)) && match.index < this.state.pos) {
++this.state.curLine;
this.state.lineStart = match.index + match[0].length;
}
this.pushComment(true, this.input.slice(start + 2, end), start, this.state.pos, startLoc, this.state.curPosition());
}
skipLineComment(startSkip: number): void {
const start = this.state.pos;
const startLoc = this.state.curPosition();
let ch = this.input.charCodeAt(this.state.pos += startSkip);
while (this.state.pos < this.input.length && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) {
++this.state.pos;
ch = this.input.charCodeAt(this.state.pos);
}
this.pushComment(false, this.input.slice(start + startSkip, this.state.pos), start, this.state.pos, startLoc, this.state.curPosition());
}
// Called at the start of the parse and after every token. Skips
// whitespace and comments, and.
skipSpace(): void {
loop: while (this.state.pos < this.input.length) {
const ch = this.input.charCodeAt(this.state.pos);
switch (ch) {
case 32: case 160: // ' '
++this.state.pos;
break;
case 13:
if (this.input.charCodeAt(this.state.pos + 1) === 10) {
++this.state.pos;
}
case 10: case 8232: case 8233:
++this.state.pos;
++this.state.curLine;
this.state.lineStart = this.state.pos;
break;
case 47: // '/'
switch (this.input.charCodeAt(this.state.pos + 1)) {
case 42: // '*'
this.skipBlockComment();
break;
case 47:
this.skipLineComment(2);
break;
default:
break loop;
}
break;
default:
if (ch > 8 && ch < 14 || ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
++this.state.pos;
} else {
break loop;
}
}
}
}
// Called at the end of every token. Sets `end`, `val`, and
// maintains `context` and `exprAllowed`, and skips the space after
// the token, so that the next one's `start` will point at the
// right position.
finishToken(type: TokenType, val: any): void {
this.state.end = this.state.pos;
this.state.endLoc = this.state.curPosition();
const prevType = this.state.type;
this.state.type = type;
this.state.value = val;
this.updateContext(prevType);
}
// ### Token reading
// This is the function that is called to fetch the next token. It
// is somewhat obscure, because it works in character codes rather
// than characters, and because operator parsing has been inlined
// into it.
//
// All in the name of speed.
//
readToken_dot(): void {
const next = this.input.charCodeAt(this.state.pos + 1);
if (next >= 48 && next <= 57) {
return this.readNumber(true);
}
const next2 = this.input.charCodeAt(this.state.pos + 2);
if (next === 46 && next2 === 46) { // 46 = dot '.'
this.state.pos += 3;
return this.finishToken(tt.ellipsis);
} else {
++this.state.pos;
return this.finishToken(tt.dot);
}
}
readToken_slash(): void { // '/'
if (this.state.exprAllowed) {
++this.state.pos;
return this.readRegexp();
}
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === 61) {
return this.finishOp(tt.assign, 2);
} else {
return this.finishOp(tt.slash, 1);
}
}
readToken_mult_modulo(code: number): void { // '%*'
let type = code === 42 ? tt.star : tt.modulo;
let width = 1;
let next = this.input.charCodeAt(this.state.pos + 1);
if (next === 42) { // '*'
width++;
next = this.input.charCodeAt(this.state.pos + 2);
type = tt.exponent;
}
if (next === 61) {
width++;
type = tt.assign;
}
return this.finishOp(type, width);
}
readToken_pipe_amp(code: number): void { // '|&'
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === code) return this.finishOp(code === 124 ? tt.logicalOR : tt.logicalAND, 2);
if (next === 61) return this.finishOp(tt.assign, 2);
if (code === 124 && next === 125 && this.hasPlugin("flow")) return this.finishOp(tt.braceBarR, 2);
return this.finishOp(code === 124 ? tt.bitwiseOR : tt.bitwiseAND, 1);
}
readToken_caret(): void { // '^'
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === 61) {
return this.finishOp(tt.assign, 2);
} else {
return this.finishOp(tt.bitwiseXOR, 1);
}
}
readToken_plus_min(code: number): void { // '+-'
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === code) {
if (next === 45 && this.input.charCodeAt(this.state.pos + 2) === 62 && lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.pos))) {
// A `-->` line comment
this.skipLineComment(3);
this.skipSpace();
return this.nextToken();
}
return this.finishOp(tt.incDec, 2);
}
if (next === 61) {
return this.finishOp(tt.assign, 2);
} else {
return this.finishOp(tt.plusMin, 1);
}
}
readToken_lt_gt(code: number): void { // '<>'
const next = this.input.charCodeAt(this.state.pos + 1);
let size = 1;
if (next === code) {
size = code === 62 && this.input.charCodeAt(this.state.pos + 2) === 62 ? 3 : 2;
if (this.input.charCodeAt(this.state.pos + size) === 61) return this.finishOp(tt.assign, size + 1);
return this.finishOp(tt.bitShift, size);
}
if (next === 33 && code === 60 && this.input.charCodeAt(this.state.pos + 2) === 45 && this.input.charCodeAt(this.state.pos + 3) === 45) {
if (this.inModule) this.unexpected();
// `<!--`, an XML-style comment that should be interpreted as a line comment
this.skipLineComment(4);
this.skipSpace();
return this.nextToken();
}
if (next === 61) {
// <= | >=
size = 2;
}
return this.finishOp(tt.relational, size);
}
readToken_eq_excl(code: number): void { // '=!'
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === 61) return this.finishOp(tt.equality, this.input.charCodeAt(this.state.pos + 2) === 61 ? 3 : 2);
if (code === 61 && next === 62) { // '=>'
this.state.pos += 2;
return this.finishToken(tt.arrow);
}
return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1);
}
getTokenFromCode(code: number): void {
switch (code) {
case 35: // '#'
if (this.hasPlugin("classPrivateProperties") && this.state.inClass) {
++this.state.pos; return this.finishToken(tt.hash);
} else {
this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`);
}
// The interpretation of a dot depends on whether it is followed
// by a digit or another two dots.
case 46: // '.'
return this.readToken_dot();
// Punctuation tokens.
case 40: ++this.state.pos; return this.finishToken(tt.parenL);
case 41: ++this.state.pos; return this.finishToken(tt.parenR);
case 59: ++this.state.pos; return this.finishToken(tt.semi);
case 44: ++this.state.pos; return this.finishToken(tt.comma);
case 91: ++this.state.pos; return this.finishToken(tt.bracketL);
case 93: ++this.state.pos; return this.finishToken(tt.bracketR);
case 123:
if (this.hasPlugin("flow") && this.input.charCodeAt(this.state.pos + 1) === 124) {
return this.finishOp(tt.braceBarL, 2);
} else {
++this.state.pos;
return this.finishToken(tt.braceL);
}
case 125:
++this.state.pos; return this.finishToken(tt.braceR);
case 58:
if (this.hasPlugin("functionBind") && this.input.charCodeAt(this.state.pos + 1) === 58) {
return this.finishOp(tt.doubleColon, 2);
} else {
++this.state.pos;
return this.finishToken(tt.colon);
}
case 63: ++this.state.pos; return this.finishToken(tt.question);
case 64: ++this.state.pos; return this.finishToken(tt.at);
case 96: // '`'
++this.state.pos;
return this.finishToken(tt.backQuote);
case 48: // '0'
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === 120 || next === 88) return this.readRadixNumber(16); // '0x', '0X' - hex number
if (next === 111 || next === 79) return this.readRadixNumber(8); // '0o', '0O' - octal number
if (next === 98 || next === 66) return this.readRadixNumber(2); // '0b', '0B' - binary number
// Anything else beginning with a digit is an integer, octal
// number, or float.
case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
return this.readNumber(false);
// Quotes produce strings.
case 34: case 39: // '"', "'"
return this.readString(code);
// Operators are parsed inline in tiny state machines. '=' (61) is
// often referred to. `finishOp` simply skips the amount of
// characters it is given as second argument, and returns a token
// of the type given by its first argument.
case 47: // '/'
return this.readToken_slash();
case 37: case 42: // '%*'
return this.readToken_mult_modulo(code);
case 124: case 38: // '|&'
return this.readToken_pipe_amp(code);
case 94: // '^'
return this.readToken_caret();
case 43: case 45: // '+-'
return this.readToken_plus_min(code);
case 60: case 62: // '<>'
return this.readToken_lt_gt(code);
case 61: case 33: // '=!'
return this.readToken_eq_excl(code);
case 126: // '~'
return this.finishOp(tt.prefix, 1);
}
this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`);
}
finishOp(type: TokenType, size: number): void {
const str = this.input.slice(this.state.pos, this.state.pos + size);
this.state.pos += size;
return this.finishToken(type, str);
}
readRegexp(): void {
const start = this.state.pos;
let escaped, inClass;
for (;;) {
if (this.state.pos >= this.input.length) this.raise(start, "Unterminated regular expression");
const ch = this.input.charAt(this.state.pos);
if (lineBreak.test(ch)) {
this.raise(start, "Unterminated regular expression");
}
if (escaped) {
escaped = false;
} else {
if (ch === "[") {
inClass = true;
} else if (ch === "]" && inClass) {
inClass = false;
} else if (ch === "/" && !inClass) {
break;
}
escaped = ch === "\\";
}
++this.state.pos;
}
const content = this.input.slice(start, this.state.pos);
++this.state.pos;
// Need to use `readWord1` because '\uXXXX' sequences are allowed
// here (don't ask).
const mods = this.readWord1();
if (mods) {
const validFlags = /^[gmsiyu]*$/;
if (!validFlags.test(mods)) this.raise(start, "Invalid regular expression flag");
}
return this.finishToken(tt.regexp, {
pattern: content,
flags: mods
});
}
// Read an integer in the given radix. Return null if zero digits
// were read, the integer value otherwise. When `len` is given, this
// will return `null` unless the integer has exactly `len` digits.
readInt(radix: number, len?: number): number | null {
const start = this.state.pos;
let total = 0;
for (let i = 0, e = len == null ? Infinity : len; i < e; ++i) {
const code = this.input.charCodeAt(this.state.pos);
let val;
if (code >= 97) {
val = code - 97 + 10; // a
} else if (code >= 65) {
val = code - 65 + 10; // A
} else if (code >= 48 && code <= 57) {
val = code - 48; // 0-9
} else {
val = Infinity;
}
if (val >= radix) break;
++this.state.pos;
total = total * radix + val;
}
if (this.state.pos === start || len != null && this.state.pos - start !== len) return null;
return total;
}
readRadixNumber(radix: number): void {
this.state.pos += 2; // 0x
const val = this.readInt(radix);
if (val == null) this.raise(this.state.start + 2, "Expected number in radix " + radix);
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number");
return this.finishToken(tt.num, val);
}
// Read an integer, octal integer, or floating-point number.
readNumber(startsWithDot: boolean): void {
const start = this.state.pos;
let octal = this.input.charCodeAt(start) === 48; // '0'
let isFloat = false;
if (!startsWithDot && this.readInt(10) === null) this.raise(start, "Invalid number");
if (octal && this.state.pos == start + 1) octal = false; // number === 0
let next = this.input.charCodeAt(this.state.pos);
if (next === 46 && !octal) { // '.'
++this.state.pos;
this.readInt(10);
isFloat = true;
next = this.input.charCodeAt(this.state.pos);
}
if ((next === 69 || next === 101) && !octal) { // 'eE'
next = this.input.charCodeAt(++this.state.pos);
if (next === 43 || next === 45) ++this.state.pos; // '+-'
if (this.readInt(10) === null) this.raise(start, "Invalid number");
isFloat = true;
}
if (isIdentifierStart(this.fullCharCodeAtPos())) this.raise(this.state.pos, "Identifier directly after number");
const str = this.input.slice(start, this.state.pos);
let val;
if (isFloat) {
val = parseFloat(str);
} else if (!octal || str.length === 1) {
val = parseInt(str, 10);
} else if (this.state.strict) {
this.raise(start, "Invalid number");
} else if (/[89]/.test(str)) {
val = parseInt(str, 10);
} else {
val = parseInt(str, 8);
}
return this.finishToken(tt.num, val);
}
// Read a string value, interpreting backslash-escapes.
readCodePoint(throwOnInvalid: boolean): number | null {
const ch = this.input.charCodeAt(this.state.pos);
let code;
if (ch === 123) { // '{'
const codePos = ++this.state.pos;
code = this.readHexChar(this.input.indexOf("}", this.state.pos) - this.state.pos, throwOnInvalid);
++this.state.pos;
if (code === null) {
// $FlowFixMe (is this always non-null?)
--this.state.invalidTemplateEscapePosition; // to point to the '\'' instead of the 'u'
} else if (code > 0x10FFFF) {
if (throwOnInvalid) {
this.raise(codePos, "Code point out of bounds");
} else {
this.state.invalidTemplateEscapePosition = codePos - 2;
return null;
}
}
} else {
code = this.readHexChar(4, throwOnInvalid);
}
return code;
}
readString(quote: number): void {
let out = "", chunkStart = ++this.state.pos;
for (;;) {
if (this.state.pos >= this.input.length) this.raise(this.state.start, "Unterminated string constant");
const ch = this.input.charCodeAt(this.state.pos);
if (ch === quote) break;
if (ch === 92) { // '\'
out += this.input.slice(chunkStart, this.state.pos);
// $FlowFixMe
out += this.readEscapedChar(false);
chunkStart = this.state.pos;
} else {
if (isNewLine(ch)) this.raise(this.state.start, "Unterminated string constant");
++this.state.pos;
}
}
out += this.input.slice(chunkStart, this.state.pos++);
return this.finishToken(tt.string, out);
}
// Reads template string tokens.
readTmplToken(): void {
let out = "", chunkStart = this.state.pos, containsInvalid = false;
for (;;) {
if (this.state.pos >= this.input.length) this.raise(this.state.start, "Unterminated template");
const ch = this.input.charCodeAt(this.state.pos);
if (ch === 96 || ch === 36 && this.input.charCodeAt(this.state.pos + 1) === 123) { // '`', '${'
if (this.state.pos === this.state.start && this.match(tt.template)) {
if (ch === 36) {
this.state.pos += 2;
return this.finishToken(tt.dollarBraceL);
} else {
++this.state.pos;
return this.finishToken(tt.backQuote);
}
}
out += this.input.slice(chunkStart, this.state.pos);
return this.finishToken(tt.template, containsInvalid ? null : out);
}
if (ch === 92) { // '\'
out += this.input.slice(chunkStart, this.state.pos);
const escaped = this.readEscapedChar(true);
if (escaped === null) {
containsInvalid = true;
} else {
out += escaped;
}
chunkStart = this.state.pos;
} else if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.state.pos);
++this.state.pos;
switch (ch) {
case 13:
if (this.input.charCodeAt(this.state.pos) === 10) ++this.state.pos;
case 10:
out += "\n";
break;
default:
out += String.fromCharCode(ch);
break;
}
++this.state.curLine;
this.state.lineStart = this.state.pos;
chunkStart = this.state.pos;
} else {
++this.state.pos;
}
}
}
// Used to read escaped characters
readEscapedChar(inTemplate: boolean): string | null {
const throwOnInvalid = !inTemplate;
const ch = this.input.charCodeAt(++this.state.pos);
++this.state.pos;
switch (ch) {
case 110: return "\n"; // 'n' -> '\n'
case 114: return "\r"; // 'r' -> '\r'
case 120: { // 'x'
const code = this.readHexChar(2, throwOnInvalid);
return code === null ? null : String.fromCharCode(code);
}
case 117: { // 'u'
const code = this.readCodePoint(throwOnInvalid);
return code === null ? null : codePointToString(code);
}
case 116: return "\t"; // 't' -> '\t'
case 98: return "\b"; // 'b' -> '\b'
case 118: return "\u000b"; // 'v' -> '\u000b'
case 102: return "\f"; // 'f' -> '\f'
case 13: if (this.input.charCodeAt(this.state.pos) === 10) ++this.state.pos; // '\r\n'
case 10: // ' \n'
this.state.lineStart = this.state.pos;
++this.state.curLine;
return "";
default:
if (ch >= 48 && ch <= 55) {
const codePos = this.state.pos - 1;
// $FlowFixMe
let octalStr = this.input.substr(this.state.pos - 1, 3).match(/^[0-7]+/)[0];
let octal = parseInt(octalStr, 8);
if (octal > 255) {
octalStr = octalStr.slice(0, -1);
octal = parseInt(octalStr, 8);
}
if (octal > 0) {
if (inTemplate) {
this.state.invalidTemplateEscapePosition = codePos;
return null;
} else if (this.state.strict) {
this.raise(codePos, "Octal literal in strict mode");
} else if (!this.state.containsOctal) {
// These properties are only used to throw an error for an octal which occurs
// in a directive which occurs prior to a "use strict" directive.
this.state.containsOctal = true;
this.state.octalPosition = codePos;
}
}
this.state.pos += octalStr.length - 1;
return String.fromCharCode(octal);
}
return String.fromCharCode(ch);
}
}
// Used to read character escape sequences ('\x', '\u').
readHexChar(len: number, throwOnInvalid: boolean): number | null {
const codePos = this.state.pos;
const n = this.readInt(16, len);
if (n === null) {
if (throwOnInvalid) {
this.raise(codePos, "Bad character escape sequence");
} else {
this.state.pos = codePos - 1;
this.state.invalidTemplateEscapePosition = codePos - 1;
}
}
return n;
}
// Read an identifier, and return it as a string. Sets `this.state.containsEsc`
// to whether the word contained a '\u' escape.
//
// Incrementally adds only escaped chars, adding other chunks as-is
// as a micro-optimization.
readWord1(): string {
this.state.containsEsc = false;
let word = "", first = true, chunkStart = this.state.pos;
while (this.state.pos < this.input.length) {
const ch = this.fullCharCodeAtPos();
if (isIdentifierChar(ch)) {
this.state.pos += ch <= 0xffff ? 1 : 2;
} else if (ch === 92) { // "\"
this.state.containsEsc = true;
word += this.input.slice(chunkStart, this.state.pos);
const escStart = this.state.pos;
if (this.input.charCodeAt(++this.state.pos) !== 117) { // "u"
this.raise(this.state.pos, "Expecting Unicode escape sequence \\uXXXX");
}
++this.state.pos;
const esc = this.readCodePoint(true);
// $FlowFixMe (thinks esc may be null, but throwOnInvalid is true)
if (!(first ? isIdentifierStart : isIdentifierChar)(esc, true)) {
this.raise(escStart, "Invalid Unicode escape");
}
// $FlowFixMe
word += codePointToString(esc);
chunkStart = this.state.pos;
} else {
break;
}
first = false;
}
return word + this.input.slice(chunkStart, this.state.pos);
}
// Read an identifier or keyword token. Will check for reserved
// words when necessary.
readWord(): void {
const word = this.readWord1();
let type = tt.name;
if (!this.state.containsEsc && this.isKeyword(word)) {
type = keywordTypes[word];
}
return this.finishToken(type, word);
}
braceIsBlock(prevType: TokenType): boolean {
if (prevType === tt.colon) {
const parent = this.curContext();
if (parent === ct.braceStatement || parent === ct.braceExpression) {
return !parent.isExpr;
}
}
if (prevType === tt._return) {
return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start));
}
if (prevType === tt._else || prevType === tt.semi || prevType === tt.eof || prevType === tt.parenR) {
return true;
}
if (prevType === tt.braceL) {
return this.curContext() === ct.braceStatement;
}
return !this.state.exprAllowed;
}
updateContext(prevType: TokenType): void {
const type = this.state.type;
let update;
if (type.keyword && prevType === tt.dot) {
this.state.exprAllowed = false;
} else if (update = type.updateContext) {
update.call(this, prevType);
} else {
this.state.exprAllowed = type.beforeExpr;
}
}
}