-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
lexer.go
2238 lines (1940 loc) · 47.2 KB
/
lexer.go
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lexer
// The lexer converts a source file to a stream of tokens. Unlike many
// compilers, esbuild does not run the lexer to completion before the parser is
// started. Instead, the lexer is called repeatedly by the parser as the parser
// parses the file. This is because many tokens are context-sensitive and need
// high-level information from the parser. Examples are regular expression
// literals and JSX elements.
//
// For efficiency, the text associated with textual tokens is stored in two
// separate ways depending on the token. Identifiers use UTF-8 encoding which
// allows them to be slices of the input file without allocating extra memory.
// Strings use UTF-16 encoding so they can represent unicode surrogates
// accurately.
import (
"fmt"
"strconv"
"strings"
"unicode"
"unicode/utf16"
"unicode/utf8"
"github.com/evanw/esbuild/internal/ast"
"github.com/evanw/esbuild/internal/logging"
)
type T uint
// If you add a new token, remember to add it to "tokenToString" too
const (
TEndOfFile T = iota
TSyntaxError
// "#!/usr/bin/env node"
THashbang
// Literals
TNoSubstitutionTemplateLiteral // Contents are in lexer.StringLiteral ([]uint16)
TNumericLiteral // Contents are in lexer.Number (float64)
TStringLiteral // Contents are in lexer.StringLiteral ([]uint16)
TBigIntegerLiteral // Contents are in lexer.Identifier (string)
// Pseudo-literals
TTemplateHead // Contents are in lexer.StringLiteral ([]uint16)
TTemplateMiddle // Contents are in lexer.StringLiteral ([]uint16)
TTemplateTail // Contents are in lexer.StringLiteral ([]uint16)
// Punctuation
TAmpersand
TAmpersandAmpersand
TAsterisk
TAsteriskAsterisk
TAt
TBar
TBarBar
TCaret
TCloseBrace
TCloseBracket
TCloseParen
TColon
TComma
TDot
TDotDotDot
TEqualsEquals
TEqualsEqualsEquals
TEqualsGreaterThan
TExclamation
TExclamationEquals
TExclamationEqualsEquals
TGreaterThan
TGreaterThanEquals
TGreaterThanGreaterThan
TGreaterThanGreaterThanGreaterThan
TLessThan
TLessThanEquals
TLessThanLessThan
TMinus
TMinusMinus
TOpenBrace
TOpenBracket
TOpenParen
TPercent
TPlus
TPlusPlus
TQuestion
TQuestionDot
TQuestionQuestion
TSemicolon
TSlash
TTilde
// Assignments
TAmpersandEquals
TAsteriskAsteriskEquals
TAsteriskEquals
TBarEquals
TCaretEquals
TEquals
TGreaterThanGreaterThanEquals
TGreaterThanGreaterThanGreaterThanEquals
TLessThanLessThanEquals
TMinusEquals
TPercentEquals
TPlusEquals
TSlashEquals
// Identifiers
TIdentifier // Contents are in lexer.Identifier (string)
TEscapedKeyword // A keyword that has been escaped as an identifer
// Reserved words
TBreak
TCase
TCatch
TClass
TConst
TContinue
TDebugger
TDefault
TDelete
TDo
TElse
TEnum
TExport
TExtends
TFalse
TFinally
TFor
TFunction
TIf
TImport
TIn
TInstanceof
TNew
TNull
TReturn
TSuper
TSwitch
TThis
TThrow
TTrue
TTry
TTypeof
TVar
TVoid
TWhile
TWith
// Strict mode reserved words
TImplements
TInterface
TLet
TPackage
TPrivate
TProtected
TPublic
TStatic
TYield
)
var keywords = map[string]T{
// Reserved words
"break": TBreak,
"case": TCase,
"catch": TCatch,
"class": TClass,
"const": TConst,
"continue": TContinue,
"debugger": TDebugger,
"default": TDefault,
"delete": TDelete,
"do": TDo,
"else": TElse,
"enum": TEnum,
"export": TExport,
"extends": TExtends,
"false": TFalse,
"finally": TFinally,
"for": TFor,
"function": TFunction,
"if": TIf,
"import": TImport,
"in": TIn,
"instanceof": TInstanceof,
"new": TNew,
"null": TNull,
"return": TReturn,
"super": TSuper,
"switch": TSwitch,
"this": TThis,
"throw": TThrow,
"true": TTrue,
"try": TTry,
"typeof": TTypeof,
"var": TVar,
"void": TVoid,
"while": TWhile,
"with": TWith,
// Strict mode reserved words
"implements": TImplements,
"interface": TInterface,
"let": TLet,
"package": TPackage,
"private": TPrivate,
"protected": TProtected,
"public": TPublic,
"static": TStatic,
"yield": TYield,
}
func Keywords() map[string]T {
result := make(map[string]T)
for k, v := range keywords {
result[k] = v
}
return result
}
type json struct {
parse bool
allowComments bool
}
type Lexer struct {
log logging.Log
source logging.Source
current int
start int
end int
Token T
HasNewlineBefore bool
codePoint rune
StringLiteral []uint16
Identifier string
Number float64
rescanCloseBraceAsTemplateToken bool
json json
// The log is disabled during speculative scans that may backtrack
IsLogDisabled bool
}
type LexerPanic struct{}
func NewLexer(log logging.Log, source logging.Source) Lexer {
lexer := Lexer{
log: log,
source: source,
}
lexer.step()
lexer.Next()
return lexer
}
func NewLexerJSON(log logging.Log, source logging.Source, allowComments bool) Lexer {
lexer := Lexer{
log: log,
source: source,
json: json{
parse: true,
allowComments: allowComments,
},
}
lexer.step()
lexer.Next()
return lexer
}
func (lexer *Lexer) Loc() ast.Loc {
return ast.Loc{int32(lexer.start)}
}
func (lexer *Lexer) Range() ast.Range {
return ast.Range{ast.Loc{int32(lexer.start)}, int32(lexer.end - lexer.start)}
}
func (lexer *Lexer) Raw() string {
return lexer.source.Contents[lexer.start:lexer.end]
}
func (lexer *Lexer) RawTemplateContents() string {
switch lexer.Token {
case TNoSubstitutionTemplateLiteral, TTemplateTail:
// "`x`" or "}x`"
return lexer.source.Contents[lexer.start+1 : lexer.end-1]
case TTemplateHead, TTemplateMiddle:
// "`x${" or "}x${"
return lexer.source.Contents[lexer.start+1 : lexer.end-2]
default:
return ""
}
}
func (lexer *Lexer) IsIdentifierOrKeyword() bool {
return lexer.Token >= TIdentifier
}
func (lexer *Lexer) IsContextualKeyword(text string) bool {
return lexer.Token == TIdentifier && lexer.Raw() == text
}
func (lexer *Lexer) ExpectContextualKeyword(text string) {
if !lexer.IsContextualKeyword(text) {
lexer.ExpectedString(fmt.Sprintf("%q", text))
}
lexer.Next()
}
func (lexer *Lexer) SyntaxError() {
loc := ast.Loc{int32(lexer.end)}
message := "Unexpected end of file"
if lexer.end < len(lexer.source.Contents) {
c, _ := utf8.DecodeRuneInString(lexer.source.Contents[lexer.end:])
if c < 0x20 {
message = fmt.Sprintf("Syntax error \"\\x%02X\"", c)
} else if c >= 0x80 {
message = fmt.Sprintf("Syntax error \"\\u{%x}\"", c)
} else if c != '"' {
message = fmt.Sprintf("Syntax error \"%c\"", c)
} else {
message = "Syntax error '\"'"
}
}
lexer.addError(loc, message)
panic(LexerPanic{})
}
func (lexer *Lexer) ExpectedString(text string) {
found := fmt.Sprintf("%q", lexer.Raw())
if lexer.start == len(lexer.source.Contents) {
found = "end of file"
}
lexer.addRangeError(lexer.Range(), fmt.Sprintf("Expected %s but found %s", text, found))
panic(LexerPanic{})
}
func (lexer *Lexer) Expected(token T) {
if text, ok := tokenToString[token]; ok {
lexer.ExpectedString(text)
} else {
lexer.Unexpected()
}
}
func (lexer *Lexer) Unexpected() {
found := fmt.Sprintf("%q", lexer.Raw())
if lexer.start == len(lexer.source.Contents) {
found = "end of file"
}
lexer.addRangeError(lexer.Range(), fmt.Sprintf("Unexpected %s", found))
panic(LexerPanic{})
}
func (lexer *Lexer) Expect(token T) {
if lexer.Token != token {
lexer.Expected(token)
}
lexer.Next()
}
func (lexer *Lexer) ExpectOrInsertSemicolon() {
if lexer.Token == TSemicolon || (!lexer.HasNewlineBefore &&
lexer.Token != TCloseBrace && lexer.Token != TEndOfFile) {
lexer.Expect(TSemicolon)
}
}
// This parses a single "<" token. If that is the first part of a longer token,
// this function splits off the first "<" and leaves the remainder of the
// current token as another, smaller token. For example, "<<=" becomes "<=".
func (lexer *Lexer) ExpectLessThan(isInsideJSXElement bool) {
switch lexer.Token {
case TLessThan:
if isInsideJSXElement {
lexer.NextInsideJSXElement()
} else {
lexer.Next()
}
case TLessThanEquals:
lexer.Token = TEquals
lexer.start++
case TLessThanLessThan:
lexer.Token = TLessThan
lexer.start++
case TLessThanLessThanEquals:
lexer.Token = TLessThanEquals
lexer.start++
default:
lexer.Expected(TLessThan)
}
}
// This parses a single ">" token. If that is the first part of a longer token,
// this function splits off the first ">" and leaves the remainder of the
// current token as another, smaller token. For example, ">>=" becomes ">=".
func (lexer *Lexer) ExpectGreaterThan(isInsideJSXElement bool) {
switch lexer.Token {
case TGreaterThan:
if isInsideJSXElement {
lexer.NextInsideJSXElement()
} else {
lexer.Next()
}
case TGreaterThanEquals:
lexer.Token = TEquals
lexer.start++
case TGreaterThanGreaterThan:
lexer.Token = TGreaterThan
lexer.start++
case TGreaterThanGreaterThanEquals:
lexer.Token = TGreaterThanEquals
lexer.start++
case TGreaterThanGreaterThanGreaterThan:
lexer.Token = TGreaterThanGreaterThan
lexer.start++
case TGreaterThanGreaterThanGreaterThanEquals:
lexer.Token = TGreaterThanGreaterThanEquals
lexer.start++
default:
lexer.Expected(TGreaterThan)
}
}
func NumberToMinifiedName(i int) string {
j := i % 54
name := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"[j : j+1]
i = i / 54
for i > 0 {
i--
j := i % 64
name += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$0123456789"[j : j+1]
i = i / 64
}
return name
}
func IsIdentifier(text string) bool {
if len(text) == 0 {
return false
}
for i, codePoint := range text {
if i == 0 {
if !IsIdentifierStart(codePoint) {
return false
}
} else {
if !IsIdentifierContinue(codePoint) {
return false
}
}
}
return true
}
// This does "IsIdentifier(UTF16ToString(text))" without any allocations
func IsIdentifierUTF16(text []uint16) bool {
n := len(text)
if n == 0 {
return false
}
for i := 0; i < n; i++ {
r1 := rune(text[i])
if utf16.IsSurrogate(r1) && i+1 < n {
r2 := rune(text[i+1])
r1 = (r1-0xD800)<<10 | (r2 - 0xDC00) + 0x10000
i++
}
if i == 0 {
if !IsIdentifierStart(r1) {
return false
}
} else {
if !IsIdentifierContinue(r1) {
return false
}
}
}
return true
}
func IsIdentifierStart(codePoint rune) bool {
switch codePoint {
case '_', '$',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z':
return true
}
// All ASCII identifier start code points are listed above
if codePoint < 0x7F {
return false
}
return unicode.Is(idStart, codePoint)
}
func IsIdentifierContinue(codePoint rune) bool {
switch codePoint {
case '_', '$', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z':
return true
}
// All ASCII identifier start code points are listed above
if codePoint < 0x7F {
return false
}
// ZWNJ and ZWJ are allowed in identifiers
if codePoint == 0x200C || codePoint == 0x200D {
return true
}
return unicode.Is(idContinue, codePoint)
}
func RangeOfIdentifier(source logging.Source, loc ast.Loc) ast.Range {
text := source.Contents[loc.Start:]
if len(text) == 0 {
return ast.Range{loc, 0}
}
i := 0
c, width := utf8.DecodeRuneInString(text)
i += width
if IsIdentifierStart(c) {
// Search for the end of the identifier
for i < len(text) {
c2, width2 := utf8.DecodeRuneInString(text[i:])
if !IsIdentifierContinue(c2) {
return ast.Range{loc, int32(i)}
}
i += width2
}
}
return ast.Range{loc, 0}
}
func (lexer *Lexer) ExpectJSXElementChild(token T) {
if lexer.Token != token {
lexer.Expected(token)
}
lexer.NextJSXElementChild()
}
func (lexer *Lexer) NextJSXElementChild() {
lexer.HasNewlineBefore = false
originalStart := lexer.end
for {
lexer.start = lexer.end
lexer.Token = 0
switch lexer.codePoint {
case -1: // This indicates the end of the file
lexer.Token = TEndOfFile
case '\r', '\n', '\u2028', '\u2029':
lexer.step()
lexer.HasNewlineBefore = true
continue
case '\t', '\f', '\v', ' ', '\xA0', '\uFEFF':
lexer.step()
continue
case '{':
lexer.step()
lexer.Token = TOpenBrace
case '<':
lexer.step()
lexer.Token = TLessThan
default:
// This needs fixing if we skipped over whitespace characters earlier
needsFixing := lexer.start != originalStart
stringLiteral:
for {
switch lexer.codePoint {
case -1:
// Reaching the end of the file without a closing element is an error
lexer.SyntaxError()
case '&', '\r', '\n', '\u2028', '\u2029':
// This needs fixing if it has an entity or if it's a multi-line string
needsFixing = true
lexer.step()
case '{', '<':
// Stop when the string ends
break stringLiteral
default:
// Non-ASCII strings need the slow path
if lexer.codePoint >= 0x80 {
needsFixing = true
}
lexer.step()
}
}
lexer.Token = TStringLiteral
text := lexer.source.Contents[originalStart:lexer.end]
if needsFixing {
// Slow path
lexer.StringLiteral = fixWhitespaceAndDecodeJSXEntities(text)
} else {
// Fast path
n := len(text)
copy := make([]uint16, n)
for i := 0; i < n; i++ {
copy[i] = uint16(text[i])
}
lexer.StringLiteral = copy
}
}
break
}
}
func (lexer *Lexer) ExpectInsideJSXElement(token T) {
if lexer.Token != token {
lexer.Expected(token)
}
lexer.NextInsideJSXElement()
}
func (lexer *Lexer) NextInsideJSXElement() {
lexer.HasNewlineBefore = false
for {
lexer.start = lexer.end
lexer.Token = 0
switch lexer.codePoint {
case -1: // This indicates the end of the file
lexer.Token = TEndOfFile
case '\r', '\n', '\u2028', '\u2029':
lexer.step()
lexer.HasNewlineBefore = true
continue
case '\t', '\f', '\v', ' ', '\xA0', '\uFEFF':
lexer.step()
continue
case '.':
lexer.step()
lexer.Token = TDot
case '=':
lexer.step()
lexer.Token = TEquals
case '{':
lexer.step()
lexer.Token = TOpenBrace
case '}':
lexer.step()
lexer.Token = TCloseBrace
case '<':
lexer.step()
lexer.Token = TLessThan
case '>':
lexer.step()
lexer.Token = TGreaterThan
case '/':
// '/' or '//' or '/* ... */'
lexer.step()
switch lexer.codePoint {
case '/':
singleLineComment:
for {
lexer.step()
switch lexer.codePoint {
case '\r', '\n', '\u2028', '\u2029':
break singleLineComment
case -1: // This indicates the end of the file
break singleLineComment
}
}
continue
case '*':
lexer.step()
multiLineComment:
for {
switch lexer.codePoint {
case '*':
lexer.step()
if lexer.codePoint == '/' {
lexer.step()
break multiLineComment
}
case '\r', '\n', '\u2028', '\u2029':
lexer.step()
lexer.HasNewlineBefore = true
case -1: // This indicates the end of the file
lexer.start = lexer.end
lexer.addError(lexer.Loc(), "Expected \"*/\" to terminate multi-line comment")
lexer.Token = TSyntaxError
break multiLineComment
default:
lexer.step()
}
}
continue
default:
lexer.Token = TSlash
}
case '\'', '"':
quote := lexer.codePoint
needsDecode := false
lexer.step()
stringLiteral:
for {
switch lexer.codePoint {
case -1: // This indicates the end of the file
lexer.SyntaxError()
case '&':
needsDecode = true
lexer.step()
case quote:
lexer.step()
break stringLiteral
default:
// Non-ASCII strings need the slow path
if lexer.codePoint >= 0x80 {
needsDecode = true
}
lexer.step()
}
}
lexer.Token = TStringLiteral
text := lexer.source.Contents[lexer.start+1 : lexer.end-1]
if needsDecode {
// Slow path
lexer.StringLiteral = decodeJSXEntities([]uint16{}, text)
} else {
// Fast path
n := len(text)
copy := make([]uint16, n)
for i := 0; i < n; i++ {
copy[i] = uint16(text[i])
}
lexer.StringLiteral = copy
}
default:
if IsIdentifierStart(lexer.codePoint) {
lexer.step()
for IsIdentifierContinue(lexer.codePoint) || lexer.codePoint == '-' {
lexer.step()
}
lexer.Identifier = lexer.Raw()
lexer.Token = TIdentifier
break
}
lexer.end = lexer.current
lexer.Token = TSyntaxError
}
return
}
}
func (lexer *Lexer) Next() {
lexer.HasNewlineBefore = false
for {
lexer.start = lexer.end
lexer.Token = 0
switch lexer.codePoint {
case -1: // This indicates the end of the file
lexer.Token = TEndOfFile
case '#':
if lexer.start == 0 && strings.HasPrefix(lexer.source.Contents, "#!") {
lexer.Token = THashbang
hashbang:
for {
lexer.step()
switch lexer.codePoint {
case '\r', '\n', '\u2028', '\u2029':
break hashbang
case -1: // This indicates the end of the file
break hashbang
}
}
lexer.Identifier = lexer.Raw()
} else {
lexer.SyntaxError()
}
case '\r', '\n', '\u2028', '\u2029':
lexer.step()
lexer.HasNewlineBefore = true
continue
case '\t', '\f', '\v', ' ', '\xA0', '\uFEFF':
lexer.step()
continue
case '(':
lexer.step()
lexer.Token = TOpenParen
case ')':
lexer.step()
lexer.Token = TCloseParen
case '[':
lexer.step()
lexer.Token = TOpenBracket
case ']':
lexer.step()
lexer.Token = TCloseBracket
case '{':
lexer.step()
lexer.Token = TOpenBrace
case '}':
lexer.step()
lexer.Token = TCloseBrace
case ',':
lexer.step()
lexer.Token = TComma
case ':':
lexer.step()
lexer.Token = TColon
case ';':
lexer.step()
lexer.Token = TSemicolon
case '@':
lexer.step()
lexer.Token = TAt
case '~':
lexer.step()
lexer.Token = TTilde
case '?':
// '?' or '??' or '?.'
lexer.step()
switch lexer.codePoint {
case '?':
lexer.step()
lexer.Token = TQuestionQuestion
case '.':
lexer.Token = TQuestion
current := lexer.current
contents := lexer.source.Contents
// Lookahead to disambiguate with 'a?.1:b'
if current < len(contents) {
c := contents[current]
if c < '0' || c > '9' {
lexer.step()
lexer.Token = TQuestionDot
}
}
default:
lexer.Token = TQuestion
}
case '%':
// '%' or '%='
lexer.step()
switch lexer.codePoint {
case '=':
lexer.step()
lexer.Token = TPercentEquals
default:
lexer.Token = TPercent
}
case '&':
// '&' or '&=' or '&&'
lexer.step()
switch lexer.codePoint {
case '=':
lexer.step()
lexer.Token = TAmpersandEquals
case '&':
lexer.step()
lexer.Token = TAmpersandAmpersand
default:
lexer.Token = TAmpersand
}
case '|':
// '|' or '|=' or '||'
lexer.step()
switch lexer.codePoint {
case '=':
lexer.step()
lexer.Token = TBarEquals
case '|':
lexer.step()
lexer.Token = TBarBar
default:
lexer.Token = TBar
}
case '^':
// '^' or '^='
lexer.step()
switch lexer.codePoint {
case '=':
lexer.step()
lexer.Token = TCaretEquals
default:
lexer.Token = TCaret
}
case '+':
// '+' or '+=' or '++'
lexer.step()
switch lexer.codePoint {
case '=':
lexer.step()
lexer.Token = TPlusEquals
case '+':
lexer.step()
lexer.Token = TPlusPlus
default:
lexer.Token = TPlus
}
case '-':
// '-' or '-=' or '--'
lexer.step()
switch lexer.codePoint {
case '=':
lexer.step()
lexer.Token = TMinusEquals
case '-':
lexer.step()
lexer.Token = TMinusMinus
default:
lexer.Token = TMinus
}
case '*':
// '*' or '*=' or '**' or '**='
lexer.step()
switch lexer.codePoint {