-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathlexer.ts
861 lines (783 loc) · 23.2 KB
/
lexer.ts
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
/**
* Quoted as "used by external libraries".
* Missing exports from `graphql`:
* * isPunctuatorTokenKind @internal
* Should we still expose this file?
*/
import { syntaxError } from '../error/syntaxError';
import { Token } from './ast';
import { dedentBlockStringLines } from './blockString';
import { isDigit, isNameContinue, isNameStart } from './characterClasses';
import type { Source } from './source';
import { TokenKind } from './tokenKind';
/**
* Given a Source object, creates a Lexer for that source.
* A Lexer is a stateful stream generator in that every time
* it is advanced, it returns the next token in the Source. Assuming the
* source lexes, the final Token emitted by the lexer will be of kind
* EOF, after which the lexer will repeatedly return the same EOF token
* whenever called.
*/
export class Lexer {
source: Source;
/**
* The previously focused non-ignored token.
*/
lastToken: Token;
/**
* The currently focused non-ignored token.
*/
token: Token;
/**
* The (1-indexed) line containing the current token.
*/
line: number;
/**
* The character offset at which the current line begins.
*/
lineStart: number;
constructor(source: Source) {
const startOfFileToken = new Token(TokenKind.SOF, 0, 0, 0, 0);
this.source = source;
this.lastToken = startOfFileToken;
this.token = startOfFileToken;
this.line = 1;
this.lineStart = 0;
}
get [Symbol.toStringTag]() {
return 'Lexer';
}
/**
* Advances the token stream to the next non-ignored token.
*/
advance(): Token {
this.lastToken = this.token;
const token = (this.token = this.lookahead());
return token;
}
/**
* Looks ahead and returns the next non-ignored token, but does not change
* the state of Lexer.
*/
lookahead(): Token {
let token = this.token;
if (token.kind !== TokenKind.EOF) {
do {
if (token.next) {
token = token.next;
} else {
// Read the next token and form a link in the token linked-list.
const nextToken = readNextToken(this, token.end);
// @ts-expect-error next is only mutable during parsing.
token.next = nextToken;
// @ts-expect-error prev is only mutable during parsing.
nextToken.prev = token;
token = nextToken;
}
} while (token.kind === TokenKind.COMMENT);
}
return token;
}
}
/**
* @internal
*/
export function isPunctuatorTokenKind(kind: TokenKind): boolean {
return (
kind === TokenKind.BANG ||
kind === TokenKind.DOLLAR ||
kind === TokenKind.AMP ||
kind === TokenKind.PAREN_L ||
kind === TokenKind.PAREN_R ||
kind === TokenKind.SPREAD ||
kind === TokenKind.COLON ||
kind === TokenKind.EQUALS ||
kind === TokenKind.AT ||
kind === TokenKind.BRACKET_L ||
kind === TokenKind.BRACKET_R ||
kind === TokenKind.BRACE_L ||
kind === TokenKind.PIPE ||
kind === TokenKind.BRACE_R
);
}
/**
* A Unicode scalar value is any Unicode code point except surrogate code
* points. In other words, the inclusive ranges of values 0x0000 to 0xD7FF and
* 0xE000 to 0x10FFFF.
*
* SourceCharacter ::
* - "Any Unicode scalar value"
*/
function isUnicodeScalarValue(code: number): boolean {
return (
(code >= 0x0000 && code <= 0xd7ff) || (code >= 0xe000 && code <= 0x10ffff)
);
}
/**
* The GraphQL specification defines source text as a sequence of unicode scalar
* values (which Unicode defines to exclude surrogate code points). However
* JavaScript defines strings as a sequence of UTF-16 code units which may
* include surrogates. A surrogate pair is a valid source character as it
* encodes a supplementary code point (above U+FFFF), but unpaired surrogate
* code points are not valid source characters.
*/
function isSupplementaryCodePoint(body: string, location: number): boolean {
return (
isLeadingSurrogate(body.charCodeAt(location)) &&
isTrailingSurrogate(body.charCodeAt(location + 1))
);
}
function isLeadingSurrogate(code: number): boolean {
return code >= 0xd800 && code <= 0xdbff;
}
function isTrailingSurrogate(code: number): boolean {
return code >= 0xdc00 && code <= 0xdfff;
}
/**
* Prints the code point (or end of file reference) at a given location in a
* source for use in error messages.
*
* Printable ASCII is printed quoted, while other points are printed in Unicode
* code point form (ie. U+1234).
*/
function printCodePointAt(lexer: Lexer, location: number): string {
const code = lexer.source.body.codePointAt(location);
if (code === undefined) {
return TokenKind.EOF;
} else if (code >= 0x0020 && code <= 0x007e) {
// Printable ASCII
const char = String.fromCodePoint(code);
return char === '"' ? "'\"'" : `"${char}"`;
}
// Unicode code point
return 'U+' + code.toString(16).toUpperCase().padStart(4, '0');
}
/**
* Create a token with line and column location information.
*/
function createToken(
lexer: Lexer,
kind: TokenKind,
start: number,
end: number,
value?: string,
): Token {
const line = lexer.line;
const col = 1 + start - lexer.lineStart;
return new Token(kind, start, end, line, col, value);
}
/**
* Gets the next token from the source starting at the given position.
*
* This skips over whitespace until it finds the next lexable token, then lexes
* punctuators immediately or calls the appropriate helper function for more
* complicated tokens.
*/
function readNextToken(lexer: Lexer, start: number): Token {
const body = lexer.source.body;
const bodyLength = body.length;
let position = start;
while (position < bodyLength) {
const code = body.charCodeAt(position);
// SourceCharacter
switch (code) {
// Ignored ::
// - UnicodeBOM
// - WhiteSpace
// - LineTerminator
// - Comment
// - Comma
//
// UnicodeBOM :: "Byte Order Mark (U+FEFF)"
//
// WhiteSpace ::
// - "Horizontal Tab (U+0009)"
// - "Space (U+0020)"
//
// Comma :: ,
case 0xfeff: // <BOM>
case 0x0009: // \t
case 0x0020: // <space>
case 0x002c: // ,
++position;
continue;
// LineTerminator ::
// - "New Line (U+000A)"
// - "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
// - "Carriage Return (U+000D)" "New Line (U+000A)"
case 0x000a: // \n
++position;
++lexer.line;
lexer.lineStart = position;
continue;
case 0x000d: // \r
if (body.charCodeAt(position + 1) === 0x000a) {
position += 2;
} else {
++position;
}
++lexer.line;
lexer.lineStart = position;
continue;
// Comment
case 0x0023: // #
return readComment(lexer, position);
// Token ::
// - Punctuator
// - Name
// - IntValue
// - FloatValue
// - StringValue
//
// Punctuator :: one of ! $ & ( ) ... : = @ [ ] { | }
case 0x0021: // !
return createToken(lexer, TokenKind.BANG, position, position + 1);
case 0x0024: // $
return createToken(lexer, TokenKind.DOLLAR, position, position + 1);
case 0x0026: // &
return createToken(lexer, TokenKind.AMP, position, position + 1);
case 0x0028: // (
return createToken(lexer, TokenKind.PAREN_L, position, position + 1);
case 0x0029: // )
return createToken(lexer, TokenKind.PAREN_R, position, position + 1);
case 0x002e: // .
if (
body.charCodeAt(position + 1) === 0x002e &&
body.charCodeAt(position + 2) === 0x002e
) {
return createToken(lexer, TokenKind.SPREAD, position, position + 3);
}
break;
case 0x003a: // :
return createToken(lexer, TokenKind.COLON, position, position + 1);
case 0x003d: // =
return createToken(lexer, TokenKind.EQUALS, position, position + 1);
case 0x0040: // @
return createToken(lexer, TokenKind.AT, position, position + 1);
case 0x005b: // [
return createToken(lexer, TokenKind.BRACKET_L, position, position + 1);
case 0x005d: // ]
return createToken(lexer, TokenKind.BRACKET_R, position, position + 1);
case 0x007b: // {
return createToken(lexer, TokenKind.BRACE_L, position, position + 1);
case 0x007c: // |
return createToken(lexer, TokenKind.PIPE, position, position + 1);
case 0x007d: // }
return createToken(lexer, TokenKind.BRACE_R, position, position + 1);
// StringValue
case 0x0022: // "
if (
body.charCodeAt(position + 1) === 0x0022 &&
body.charCodeAt(position + 2) === 0x0022
) {
return readBlockString(lexer, position);
}
return readString(lexer, position);
}
// IntValue | FloatValue (Digit | -)
if (isDigit(code) || code === 0x002d) {
return readNumber(lexer, position, code);
}
// Name
if (isNameStart(code)) {
return readName(lexer, position);
}
throw syntaxError(
lexer.source,
position,
code === 0x0027
? 'Unexpected single quote character (\'), did you mean to use a double quote (")?'
: isUnicodeScalarValue(code) || isSupplementaryCodePoint(body, position)
? `Unexpected character: ${printCodePointAt(lexer, position)}.`
: `Invalid character: ${printCodePointAt(lexer, position)}.`,
);
}
return createToken(lexer, TokenKind.EOF, bodyLength, bodyLength);
}
/**
* Reads a comment token from the source file.
*
* ```
* Comment :: # CommentChar* [lookahead != CommentChar]
*
* CommentChar :: SourceCharacter but not LineTerminator
* ```
*/
function readComment(lexer: Lexer, start: number): Token {
const body = lexer.source.body;
const bodyLength = body.length;
let position = start + 1;
while (position < bodyLength) {
const code = body.charCodeAt(position);
// LineTerminator (\n | \r)
if (code === 0x000a || code === 0x000d) {
break;
}
// SourceCharacter
if (isUnicodeScalarValue(code)) {
++position;
} else if (isSupplementaryCodePoint(body, position)) {
position += 2;
} else {
break;
}
}
return createToken(
lexer,
TokenKind.COMMENT,
start,
position,
body.slice(start + 1, position),
);
}
/**
* Reads a number token from the source file, either a FloatValue or an IntValue
* depending on whether a FractionalPart or ExponentPart is encountered.
*
* ```
* IntValue :: IntegerPart [lookahead != {Digit, `.`, NameStart}]
*
* IntegerPart ::
* - NegativeSign? 0
* - NegativeSign? NonZeroDigit Digit*
*
* NegativeSign :: -
*
* NonZeroDigit :: Digit but not `0`
*
* FloatValue ::
* - IntegerPart FractionalPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
* - IntegerPart FractionalPart [lookahead != {Digit, `.`, NameStart}]
* - IntegerPart ExponentPart [lookahead != {Digit, `.`, NameStart}]
*
* FractionalPart :: . Digit+
*
* ExponentPart :: ExponentIndicator Sign? Digit+
*
* ExponentIndicator :: one of `e` `E`
*
* Sign :: one of + -
* ```
*/
function readNumber(lexer: Lexer, start: number, firstCode: number): Token {
const body = lexer.source.body;
let position = start;
let code = firstCode;
let isFloat = false;
// NegativeSign (-)
if (code === 0x002d) {
code = body.charCodeAt(++position);
}
// Zero (0)
if (code === 0x0030) {
code = body.charCodeAt(++position);
if (isDigit(code)) {
throw syntaxError(
lexer.source,
position,
`Invalid number, unexpected digit after 0: ${printCodePointAt(
lexer,
position,
)}.`,
);
}
} else {
position = readDigits(lexer, position, code);
code = body.charCodeAt(position);
}
// Full stop (.)
if (code === 0x002e) {
isFloat = true;
code = body.charCodeAt(++position);
position = readDigits(lexer, position, code);
code = body.charCodeAt(position);
}
// E e
if (code === 0x0045 || code === 0x0065) {
isFloat = true;
code = body.charCodeAt(++position);
// + -
if (code === 0x002b || code === 0x002d) {
code = body.charCodeAt(++position);
}
position = readDigits(lexer, position, code);
code = body.charCodeAt(position);
}
// Numbers cannot be followed by . or NameStart
if (code === 0x002e || isNameStart(code)) {
throw syntaxError(
lexer.source,
position,
`Invalid number, expected digit but got: ${printCodePointAt(
lexer,
position,
)}.`,
);
}
return createToken(
lexer,
isFloat ? TokenKind.FLOAT : TokenKind.INT,
start,
position,
body.slice(start, position),
);
}
/**
* Returns the new position in the source after reading one or more digits.
*/
function readDigits(lexer: Lexer, start: number, firstCode: number): number {
if (!isDigit(firstCode)) {
throw syntaxError(
lexer.source,
start,
`Invalid number, expected digit but got: ${printCodePointAt(
lexer,
start,
)}.`,
);
}
const body = lexer.source.body;
let position = start + 1; // +1 to skip first firstCode
while (isDigit(body.charCodeAt(position))) {
++position;
}
return position;
}
/**
* Reads a single-quote string token from the source file.
*
* ```
* StringValue ::
* - `""` [lookahead != `"`]
* - `"` StringCharacter+ `"`
*
* StringCharacter ::
* - SourceCharacter but not `"` or `\` or LineTerminator
* - `\u` EscapedUnicode
* - `\` EscapedCharacter
*
* EscapedUnicode ::
* - `{` HexDigit+ `}`
* - HexDigit HexDigit HexDigit HexDigit
*
* EscapedCharacter :: one of `"` `\` `/` `b` `f` `n` `r` `t`
* ```
*/
function readString(lexer: Lexer, start: number): Token {
const body = lexer.source.body;
const bodyLength = body.length;
let position = start + 1;
let chunkStart = position;
let value = '';
while (position < bodyLength) {
const code = body.charCodeAt(position);
// Closing Quote (")
if (code === 0x0022) {
value += body.slice(chunkStart, position);
return createToken(lexer, TokenKind.STRING, start, position + 1, value);
}
// Escape Sequence (\)
if (code === 0x005c) {
value += body.slice(chunkStart, position);
const escape =
body.charCodeAt(position + 1) === 0x0075 // u
? body.charCodeAt(position + 2) === 0x007b // {
? readEscapedUnicodeVariableWidth(lexer, position)
: readEscapedUnicodeFixedWidth(lexer, position)
: readEscapedCharacter(lexer, position);
value += escape.value;
position += escape.size;
chunkStart = position;
continue;
}
// LineTerminator (\n | \r)
if (code === 0x000a || code === 0x000d) {
break;
}
// SourceCharacter
if (isUnicodeScalarValue(code)) {
++position;
} else if (isSupplementaryCodePoint(body, position)) {
position += 2;
} else {
throw syntaxError(
lexer.source,
position,
`Invalid character within String: ${printCodePointAt(
lexer,
position,
)}.`,
);
}
}
throw syntaxError(lexer.source, position, 'Unterminated string.');
}
// The string value and lexed size of an escape sequence.
interface EscapeSequence {
value: string;
size: number;
}
function readEscapedUnicodeVariableWidth(
lexer: Lexer,
position: number,
): EscapeSequence {
const body = lexer.source.body;
let point = 0;
let size = 3;
// Cannot be larger than 12 chars (\u{00000000}).
while (size < 12) {
const code = body.charCodeAt(position + size++);
// Closing Brace (})
if (code === 0x007d) {
// Must be at least 5 chars (\u{0}) and encode a Unicode scalar value.
if (size < 5 || !isUnicodeScalarValue(point)) {
break;
}
return { value: String.fromCodePoint(point), size };
}
// Append this hex digit to the code point.
point = (point << 4) | readHexDigit(code);
if (point < 0) {
break;
}
}
throw syntaxError(
lexer.source,
position,
`Invalid Unicode escape sequence: "${body.slice(
position,
position + size,
)}".`,
);
}
function readEscapedUnicodeFixedWidth(
lexer: Lexer,
position: number,
): EscapeSequence {
const body = lexer.source.body;
const code = read16BitHexCode(body, position + 2);
if (isUnicodeScalarValue(code)) {
return { value: String.fromCodePoint(code), size: 6 };
}
// GraphQL allows JSON-style surrogate pair escape sequences, but only when
// a valid pair is formed.
if (isLeadingSurrogate(code)) {
// \u
if (
body.charCodeAt(position + 6) === 0x005c &&
body.charCodeAt(position + 7) === 0x0075
) {
const trailingCode = read16BitHexCode(body, position + 8);
if (isTrailingSurrogate(trailingCode)) {
// JavaScript defines strings as a sequence of UTF-16 code units and
// encodes Unicode code points above U+FFFF using a surrogate pair of
// code units. Since this is a surrogate pair escape sequence, just
// include both codes into the JavaScript string value. Had JavaScript
// not been internally based on UTF-16, then this surrogate pair would
// be decoded to retrieve the supplementary code point.
return { value: String.fromCodePoint(code, trailingCode), size: 12 };
}
}
}
throw syntaxError(
lexer.source,
position,
`Invalid Unicode escape sequence: "${body.slice(position, position + 6)}".`,
);
}
/**
* Reads four hexadecimal characters and returns the positive integer that 16bit
* hexadecimal string represents. For example, "000f" will return 15, and "dead"
* will return 57005.
*
* Returns a negative number if any char was not a valid hexadecimal digit.
*/
function read16BitHexCode(body: string, position: number): number {
// readHexDigit() returns -1 on error. ORing a negative value with any other
// value always produces a negative value.
return (
(readHexDigit(body.charCodeAt(position)) << 12) |
(readHexDigit(body.charCodeAt(position + 1)) << 8) |
(readHexDigit(body.charCodeAt(position + 2)) << 4) |
readHexDigit(body.charCodeAt(position + 3))
);
}
/**
* Reads a hexadecimal character and returns its positive integer value (0-15).
*
* '0' becomes 0, '9' becomes 9
* 'A' becomes 10, 'F' becomes 15
* 'a' becomes 10, 'f' becomes 15
*
* Returns -1 if the provided character code was not a valid hexadecimal digit.
*
* HexDigit :: one of
* - `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
* - `A` `B` `C` `D` `E` `F`
* - `a` `b` `c` `d` `e` `f`
*/
function readHexDigit(code: number): number {
return code >= 0x0030 && code <= 0x0039 // 0-9
? code - 0x0030
: code >= 0x0041 && code <= 0x0046 // A-F
? code - 0x0037
: code >= 0x0061 && code <= 0x0066 // a-f
? code - 0x0057
: -1;
}
/**
* | Escaped Character | Code Point | Character Name |
* | ----------------- | ---------- | ---------------------------- |
* | `"` | U+0022 | double quote |
* | `\` | U+005C | reverse solidus (back slash) |
* | `/` | U+002F | solidus (forward slash) |
* | `b` | U+0008 | backspace |
* | `f` | U+000C | form feed |
* | `n` | U+000A | line feed (new line) |
* | `r` | U+000D | carriage return |
* | `t` | U+0009 | horizontal tab |
*/
function readEscapedCharacter(lexer: Lexer, position: number): EscapeSequence {
const body = lexer.source.body;
const code = body.charCodeAt(position + 1);
switch (code) {
case 0x0022: // "
return { value: '\u0022', size: 2 };
case 0x005c: // \
return { value: '\u005c', size: 2 };
case 0x002f: // /
return { value: '\u002f', size: 2 };
case 0x0062: // b
return { value: '\u0008', size: 2 };
case 0x0066: // f
return { value: '\u000c', size: 2 };
case 0x006e: // n
return { value: '\u000a', size: 2 };
case 0x0072: // r
return { value: '\u000d', size: 2 };
case 0x0074: // t
return { value: '\u0009', size: 2 };
}
throw syntaxError(
lexer.source,
position,
`Invalid character escape sequence: "${body.slice(
position,
position + 2,
)}".`,
);
}
/**
* Reads a block string token from the source file.
*
* ```
* StringValue ::
* - `"""` BlockStringCharacter* `"""`
*
* BlockStringCharacter ::
* - SourceCharacter but not `"""` or `\"""`
* - `\"""`
* ```
*/
function readBlockString(lexer: Lexer, start: number): Token {
const body = lexer.source.body;
const bodyLength = body.length;
let lineStart = lexer.lineStart;
let position = start + 3;
let chunkStart = position;
let currentLine = '';
const blockLines = [];
while (position < bodyLength) {
const code = body.charCodeAt(position);
// Closing Triple-Quote (""")
if (
code === 0x0022 &&
body.charCodeAt(position + 1) === 0x0022 &&
body.charCodeAt(position + 2) === 0x0022
) {
currentLine += body.slice(chunkStart, position);
blockLines.push(currentLine);
const token = createToken(
lexer,
TokenKind.BLOCK_STRING,
start,
position + 3,
// Return a string of the lines joined with U+000A.
dedentBlockStringLines(blockLines).join('\n'),
);
lexer.line += blockLines.length - 1;
lexer.lineStart = lineStart;
return token;
}
// Escaped Triple-Quote (\""")
if (
code === 0x005c &&
body.charCodeAt(position + 1) === 0x0022 &&
body.charCodeAt(position + 2) === 0x0022 &&
body.charCodeAt(position + 3) === 0x0022
) {
currentLine += body.slice(chunkStart, position);
chunkStart = position + 1; // skip only slash
position += 4;
continue;
}
// LineTerminator
if (code === 0x000a || code === 0x000d) {
currentLine += body.slice(chunkStart, position);
blockLines.push(currentLine);
if (code === 0x000d && body.charCodeAt(position + 1) === 0x000a) {
position += 2;
} else {
++position;
}
currentLine = '';
chunkStart = position;
lineStart = position;
continue;
}
// SourceCharacter
if (isUnicodeScalarValue(code)) {
++position;
} else if (isSupplementaryCodePoint(body, position)) {
position += 2;
} else {
throw syntaxError(
lexer.source,
position,
`Invalid character within String: ${printCodePointAt(
lexer,
position,
)}.`,
);
}
}
throw syntaxError(lexer.source, position, 'Unterminated string.');
}
/**
* Reads an alphanumeric + underscore name from the source.
*
* ```
* Name ::
* - NameStart NameContinue* [lookahead != NameContinue]
* ```
*/
function readName(lexer: Lexer, start: number): Token {
const body = lexer.source.body;
const bodyLength = body.length;
let position = start + 1;
while (position < bodyLength) {
const code = body.charCodeAt(position);
if (isNameContinue(code)) {
++position;
} else {
break;
}
}
return createToken(
lexer,
TokenKind.NAME,
start,
position,
body.slice(start, position),
);
}