-
Notifications
You must be signed in to change notification settings - Fork 224
/
LegacyScanner.scala
1015 lines (945 loc) · 31.8 KB
/
LegacyScanner.scala
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 scala.meta
package internal
package tokenizers
import scala.annotation.{switch, tailrec}
import scala.collection.mutable
import Chars._
import LegacyToken._
import scala.meta.inputs._
import scala.meta.tokenizers.TokenizeException
class LegacyScanner(input: Input, dialect: Dialect) {
val reporter: Reporter = Reporter(input)
val curr: LegacyTokenData = new LegacyTokenData {}
val next: LegacyTokenData = new LegacyTokenData {}
val prev: LegacyTokenData = new LegacyTokenData {}
val reader: CharArrayReader = new CharArrayReader(input, dialect, reporter)
import curr._, reader._, reporter._
curr.input = this.input
next.input = this.input
prev.input = this.input
private var openComments = 0
private def putCommentChar(): Unit = nextCommentChar()
@tailrec private def skipLineComment(): Unit = ch match {
case SU | CR | LF =>
case '$' if isUnquoteNextNoDollar() =>
syntaxError("can't unquote into single-line comments", at = begCharOffset)
case _ => putCommentChar(); skipLineComment()
}
private def maybeOpen(): Unit = {
putCommentChar()
if (ch == '*') {
putCommentChar()
openComments += 1
}
}
private def maybeClose(): Boolean = {
putCommentChar()
(ch == '/') && {
openComments -= 1
val close = openComments == 0
if (close) nextChar() else putCommentChar()
close
}
}
@tailrec private final def skipNestedComments(): Unit = ch match {
case '/' => maybeOpen(); skipNestedComments()
case '*' => if (!maybeClose()) skipNestedComments()
case SU => incompleteInputError("unclosed comment", at = offset)
case '$' if isUnquoteNextNoDollar() =>
syntaxError("can't unquote into multi-line comments", at = begCharOffset)
case _ => putCommentChar(); skipNestedComments()
}
private def skipToCommentEnd(): Unit = {
val isLineComment = ch == '/'
putCommentChar()
if (isLineComment) skipLineComment()
else {
openComments = 1
if (ch == '*') {
putCommentChar()
// Check for the amazing corner case of /**/
if (ch == '/')
nextChar()
else
skipNestedComments()
} else skipNestedComments()
}
}
private def isAtEnd = endCharOffset >= buf.length
/**
* A character buffer for literals
*/
private val cbuf = new java.lang.StringBuilder
/**
* append Unicode character to "cbuf" buffer
*/
private def putChar(c: Int): Unit = {
cbuf.appendCodePoint(c)
}
/**
* Determines whether this scanner should emit identifier deprecation warnings, e.g. when seeing
* `macro' or `then', which are planned to become keywords in future versions of Scala.
*/
private def emitIdentifierDeprecationWarnings = true
/** Clear buffer and set name and token */
private def finishNamed(isBackquoted: Boolean = false): Unit = {
curr.setIdentifier(getAndResetCBuf(), dialect, check = !isBackquoted) { x =>
if (x.token == IDENTIFIER && emitIdentifierDeprecationWarnings)
deprecationWarning(
s"${x.name} is now a reserved word; usage as an identifier is deprecated",
at = x.token
)
}
}
/* much like endOffset, end is inclusive */
private def finishComposite(token: LegacyToken, endExclusive: Offset): Unit = {
val start = offset
curr.token = token
curr.strVal = new String(input.chars, start, endExclusive - start)
curr.endOffset = endExclusive - 1
reader.endCharOffset = endExclusive
reader.nextChar()
}
/** Clear buffer and set string */
private def setStrVal(): Unit = {
strVal = getAndResetCBuf()
}
private def getAndResetCBuf(): String = {
try cbuf.toString
finally cbuf.setLength(0)
}
/**
* a stack of tokens which indicates whether line-ends can be statement separators also used for
* keeping track of nesting levels. We keep track of the closing symbol of a region. This can be
* - RPAREN if region starts with '('
* - RBRACKET if region starts with '['
* - RBRACE if region starts with '{'
* - ARROW if region starts with `case`
* - STRINGLIT if region is a string interpolation expression starting with '${' (the STRINGLIT
* appears twice in succession on the stack iff the expression is a multiline string literal).
*/
private var sepRegions: List[LegacyToken] = List()
@inline private def pushSepRegions(sr: LegacyToken) = sepRegions = sr :: sepRegions
@inline private def popSepRegions() = sepRegions = sepRegions.tail
@inline private def isHead(legacyTokens: List[LegacyToken], head: LegacyToken) =
legacyTokens.nonEmpty && legacyTokens.head == head
@inline private def isSepRegionsHead(head: LegacyToken) = isHead(sepRegions, head)
/**
* A map of upcoming xml literal parts that are left to be returned in nextToken().
*
* The keys are offset start positions of an xml literal and the values are the respective offset
* end positions and a boolean indicating if the part is the last part.
*/
private val upcomingXmlLiteralParts = mutable.Map.empty[Offset, (Offset, Boolean)]
// Get next token ------------------------------------------------------------
/**
* Are we directly in a string interpolation expression?
*/
private def inStringInterpolation =
isSepRegionsHead(STRINGLIT)
/**
* STRINGPART follows STRINGLIT in multiline interpolation
*/
@inline private def startsStringPart(sr: List[LegacyToken]) = isHead(sr, STRINGPART)
private def popStringInterpolation(): Unit =
if (inStringInterpolation) {
popSepRegions()
if (startsStringPart(sepRegions))
popSepRegions()
}
/**
* Produce next token, filling curr TokenData fields of Scanner.
*/
def nextToken(): Unit = {
val lastToken = token
// Adapt sepRegions according to last token
(lastToken: @switch) match {
case LPAREN =>
pushSepRegions(RPAREN)
case LBRACKET =>
pushSepRegions(RBRACKET)
case LBRACE =>
pushSepRegions(RBRACE)
case CASE =>
pushSepRegions(ARROW)
case RBRACE =>
while (sepRegions.nonEmpty && sepRegions.head != RBRACE)
popSepRegions()
if (sepRegions.nonEmpty)
popSepRegions()
case RBRACKET | RPAREN =>
if (isSepRegionsHead(lastToken)) popSepRegions()
case ARROW =>
if (isSepRegionsHead(lastToken)) popSepRegions()
case STRINGLIT =>
popStringInterpolation()
case _ =>
}
// Read a token or copy it from `next` tokenData
if (next.token == EMPTY) {
lastOffset = begCharOffset
if (lastOffset > 0 && buf(lastOffset) == '\n' && buf(lastOffset - 1) == '\r') {
lastOffset -= 1
}
fetchToken()
if (token == ERROR) popStringInterpolation()
} else {
curr copyFrom next
next.token = EMPTY
}
// NOTE: endOffset is used to determine range positions for certain tokens.
// Most tokens (e.g. `(' or `;') have constant widths, so their range positions can be calculated trivially from their offsets,
// however some tokens have variable widths,
// and for them we need to remember where their parsing ended in order to calculate their positions.
// That's what endOffset does (indirectly): each token's position should be [curr.offset, curr.endOffset]
//
// Now how do we calculate endOffset?
// 1) What we have at hand is `charOffset`, which is the position right after the position of the character that's just been read.
// 2) This means that `charOffset - 1` is the position of the character that's just been read.
// 3) Since reading that character terminated fetchToken, this means that that character is the first character of the next token.
// 4) This means that `charOffset - 2` is where the last character of the our current token lies.
//
// The only corner case here is EOF. In that case the virtual position of the character that's just been read (or, more precisely,
// that's been attempted to be read) seems to be `buf.length`, but some other logic in the scanner suggests that sometimes it can even
// be `buf.length + 1` or more. Therefore, we don't bother ourselves with doing decrements and just assign endOffset to be `buf.length - 1`.
//
// upd. Speaking of corner cases, positions of tokens emitted by string interpolation tokenizers are simply insane,
// and need to be reverse engineered having some context (previous tokens, number of quotes in the interpolation) in mind.
// Therefore I don't even attempt to handle them here, and instead apply fixups elsewhere when converting legacy TOKENS into new LegacyToken instances.
if (curr.token != STRINGPART) { // endOffset of STRINGPART tokens is set elsewhere
curr.endOffset = begCharOffset - 1
if (endCharOffset >= buf.length && ch == SU) curr.endOffset = buf.length - 1
}
}
/**
* read next token, filling TokenData fields of Scanner.
*/
private final def fetchToken(): Unit = {
offset = begCharOffset
if (inStringInterpolation) return getStringPart(multiLine = startsStringPart(sepRegions.tail))
else if (fetchXmlPart()) return
@inline def getIdentRestCheckInterpolation() = {
getIdentRest()
if (ch == '"' && token == IDENTIFIER)
token = INTERPOLATIONID
}
(ch: @switch) match {
case ' ' | '\t' | CR | LF | FF =>
token = WHITESPACE
strVal = ch.toChar.toString
nextChar()
// nextToken()
case
// uppercase alpha
'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' |
// lowercase alpha
'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' |
// other ident chars
'_' =>
putChar(ch)
nextChar()
getIdentRestCheckInterpolation()
case '$' =>
if (isUnquoteNextNoDollar()) {
getUnquote()
} else {
putChar(ch)
nextChar()
if (dialect.allowSpliceAndQuote && lookaheadReader.nextNonWhitespace == '{') {
token = MACROSPLICE
setStrVal()
} else {
getIdentRestCheckInterpolation()
}
}
case '<' => // is XMLSTART?
def fetchLT() = {
val last = if (endCharOffset >= 2) buf(endCharOffset - 2) else ' '
nextChar()
last match {
case ' ' | '\t' | '\n' | '{' | '(' | '>' if isNameStart(ch) || ch == '!' || ch == '?' =>
if (dialect.allowXmlLiterals) getXml()
else syntaxError("xml literals are not supported", at = offset)
case _ =>
// Console.println("found '<', but last is '"+in.last+"'"); // DEBUG
putChar('<')
getOperatorRest()
}
}
fetchLT()
case '~' | '!' | '@' | '#' | '%' | '^' | '*' | '+' | '-' | /*'<' | */
'>' | '?' | ':' | '=' | '&' | '|' | '\\' =>
putChar(ch)
nextChar()
getOperatorRest()
case '/' =>
nextChar()
ch match {
case '/' | '*' =>
skipToCommentEnd()
token = COMMENT
case _ =>
putChar('/')
getOperatorRest()
}
case '0' =>
def fetchZero() = {
putChar(ch)
nextChar()
if (ch == 'x' || ch == 'X') {
putChar(ch)
nextChar()
base = 16
} else {
base = 8
}
getNumber()
}
fetchZero()
case '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' =>
base = 10
getNumber()
case '`' =>
getBackquotedIdent()
case '\"' =>
def fetchDoubleQuote(): Unit = {
if (token == INTERPOLATIONID) {
nextRawChar()
if (ch == '\"') {
if (lookaheadReader.getc() == '\"') {
nextRawChar() // now eat it
offset += 3
nextRawChar()
getStringPart(multiLine = true)
pushSepRegions(STRINGPART) // indicate string part
pushSepRegions(STRINGLIT) // once more to indicate multi line string part
} else {
nextChar()
token = STRINGLIT
strVal = ""
}
} else {
offset += 1
getStringPart(multiLine = false)
pushSepRegions(STRINGLIT) // indicate single line string part
}
} else {
nextChar()
if (ch == '\"') {
nextChar()
if (ch == '\"') {
nextRawChar()
getRawStringLit()
} else {
token = STRINGLIT
strVal = ""
}
} else {
getStringLit()
}
}
}
fetchDoubleQuote()
case '\'' =>
def fetchSingleQuote() = {
nextRawChar()
if (isUnquoteDollar())
syntaxError("can't unquote into character literals", at = begCharOffset)
else if (ch == LF && buf(begCharOffset) != '\\')
syntaxError("can't use unescaped LF in character literals", at = begCharOffset)
else if (isIdentifierStart(ch))
charLitOr(getIdentRest)
else if (isOperatorPart(ch) && (ch != '\\' || wasMultiChar))
charLitOr(getOperatorRest)
else {
def isNotBraceOrBracketLiteral = {
val lookahead = lookaheadReader
val nextNonWhitespace = lookahead.nextNonWhitespace
(nextNonWhitespace == '{' || nextNonWhitespace == '[') && {
lookahead.nextRawChar()
lookahead.ch != '\''
}
}
if (dialect.allowSpliceAndQuote && isNotBraceOrBracketLiteral) {
token = MACROQUOTE
setStrVal()
} else {
getLitChar()
if (ch == '\'') {
nextChar()
token = CHARLIT
setStrVal()
} else {
syntaxError("unclosed character literal", at = offset)
}
}
}
}
fetchSingleQuote()
case '.' =>
nextChar()
if ('0' <= ch && ch <= '9') {
putChar('.'); getFraction()
} else if (dialect.allowUnquotes && ch == '.') {
base = 0
while (ch == '.') {
base += 1
nextChar()
}
token = ELLIPSIS
} else {
token = DOT
}
case ';' =>
nextChar(); token = SEMI
case ',' =>
nextChar(); token = COMMA
case '(' =>
nextChar(); token = LPAREN
case '{' =>
nextChar(); token = LBRACE
case ')' =>
nextChar(); token = RPAREN
case '}' =>
nextChar(); token = RBRACE
case '[' =>
nextChar(); token = LBRACKET
case ']' =>
nextChar(); token = RBRACKET
case SU =>
if (isAtEnd) {
// NOTE: sometimes EOF's offset is `input.chars.length - 1`, and that might mess things up
offset = input.chars.length
token = EOF
} else {
syntaxError("illegal character", at = offset)
nextChar()
}
case _ =>
def fetchOther() = {
if (ch == '\u21D2') {
nextChar(); token = ARROW
} else if (ch == '\u2190') {
nextChar(); token = LARROW
} else if (Character.isUnicodeIdentifierStart(ch)) {
putChar(ch)
nextChar()
getIdentRest()
} else if (isSpecial(ch)) {
putChar(ch)
nextChar()
getOperatorRest()
} else {
syntaxError(
"illegal character '" + ("" + '\\' + 'u' + "%04x".format(ch.toInt)) + "'",
at = offset
)
nextChar()
}
}
fetchOther()
}
}
// Identifiers ---------------------------------------------------------------
private def getBackquotedIdent(): Unit = {
nextChar()
if (getLitChars('`')) {
nextChar()
finishNamed(isBackquoted = true)
if (name.isEmpty) syntaxError("empty quoted identifier", at = offset)
} else if (ch == '$') {
syntaxError("can't unquote into quoted identifiers", at = begCharOffset)
} else {
syntaxError("unclosed quoted identifier", at = offset)
}
}
@tailrec
private final def getIdentRest(): Unit = {
if (ch == '_') {
putChar(ch)
nextChar()
if (isIdentifierPart(ch))
getIdentRest()
else
getOperatorRest()
} else if (if (ch == '$') !isUnquoteNextNoDollar() else isUnicodeIdentifierPart(ch)) {
putChar(ch)
nextChar()
getIdentRest()
} else finishNamed()
}
@tailrec
private def getOperatorRest(): Unit = (ch: @switch) match {
case '~' | '!' | '@' | '#' | '%' | '^' | '*' | '+' | '-' | '<' | '>' | '?' | ':' | '=' | '&' |
'|' | '\\' =>
putChar(ch); nextChar(); getOperatorRest()
case '/' =>
val peekNextChar = lookaheadReader.getc()
if (peekNextChar == '/' || peekNextChar == '*') {
finishNamed()
} else {
putChar('/')
nextChar()
getOperatorRest()
}
case _ =>
if (isSpecial(ch)) { putChar(ch); nextChar(); getOperatorRest() }
else finishNamed()
}
// True means that we need to switch into unquote reading mode.
private def isUnquoteNextNoDollar(): Boolean = {
dialect.allowUnquotes && {
val isDollar = lookaheadReader.getc() == '$'
if (isDollar) {
// Skip the first dollar and move on to whatever we've been doing:
// starting or continuing tokenization of an identifier,
// or continuing reading a string literal, or whatever.
nextChar()
}
!isDollar
}
}
@inline private def isUnquoteDollar(): Boolean = ch == '$' && isUnquoteNextNoDollar()
// Literals -----------------------------------------------------------------
private def getStringLit() = {
if (getLitChars('"')) {
setStrVal()
nextChar()
token = STRINGLIT
} else if (ch == '$') {
syntaxError("can't unquote into string literals", at = begCharOffset)
} else {
syntaxError("unclosed string literal", at = offset)
}
}
@tailrec
private def getRawStringLit(): Unit = {
if (ch == '\"') {
nextRawChar()
if (isTripleQuote()) {
setStrVal()
token = STRINGLIT
} else
getRawStringLit()
} else if (ch == SU) {
incompleteInputError("unclosed multi-line string literal", at = offset)
} else if (isUnquoteDollar()) {
syntaxError("can't unquote into string literals", at = begCharOffset)
} else {
putChar(ch)
nextRawChar()
getRawStringLit()
}
}
@scala.annotation.tailrec
private def getStringPart(multiLine: Boolean): Unit = {
def finishStringPart() = {
setStrVal()
token = STRINGPART
next.lastOffset = begCharOffset
next.offset = begCharOffset
}
def identifier() = {
do {
putChar(ch)
nextRawChar()
} while (isUnicodeIdentifierPart(ch))
next.setIdentifier(getAndResetCBuf(), dialect) { x =>
if (x.token != IDENTIFIER && x.token != THIS)
syntaxError(
"invalid unquote: `$'ident, `$'BlockExpr, `$'this or `$'_ expected",
at = x.offset
)
}
}
if (ch == '"') {
if (multiLine) {
nextRawChar()
if (isTripleQuote()) {
setStrVal()
token = STRINGLIT
} else
getStringPart(multiLine)
} else {
nextChar()
setStrVal()
token = STRINGLIT
}
} else if (ch == '\\' && !multiLine) {
putChar(ch)
nextRawChar()
if (ch == '"' || ch == '\\') {
putChar(ch)
nextRawChar()
}
getStringPart(multiLine)
} else if (ch == '$' && !wasMultiChar) {
if (isUnquoteNextNoDollar()) {
syntaxError("can't unquote into string interpolations", at = begCharOffset)
} else {
nextRawChar()
if (ch == '$' || (ch == '"' && dialect.allowInterpolationDolarQuoteEscape)) {
putChar(ch)
nextRawChar()
getStringPart(multiLine)
} else if (ch == '{') {
finishStringPart()
endOffset = endCharOffset - 3
nextRawChar()
next.token = LBRACE
} else if (ch == '_' && dialect.allowSpliceUnderscores) {
finishStringPart()
endOffset = endCharOffset - 3
nextRawChar()
if (Character.isUnicodeIdentifierStart(ch)) {
putChar('_')
identifier()
} else next.token = USCORE
} else if (Character.isUnicodeIdentifierStart(ch)) {
finishStringPart()
endOffset = endCharOffset - 3
identifier()
} else {
var supportedCombos = List("`$$'", "`$'ident", "`$'this", "`$'BlockExpr")
if (dialect.allowSpliceUnderscores) supportedCombos = supportedCombos :+ "`$'_"
val s_supportedCombos = supportedCombos.init.mkString(", ") + supportedCombos.last
syntaxError(s_supportedCombos, at = offset)
}
}
} else {
val isUnclosedLiteral =
!wasMultiChar && (ch == SU || (!multiLine && (ch == CR || ch == LF)))
if (isUnclosedLiteral) {
if (multiLine)
incompleteInputError("unclosed multi-line string interpolation", at = offset)
else
syntaxError("unclosed string interpolation", at = offset)
} else {
putChar(ch)
nextRawChar()
getStringPart(multiLine)
}
}
}
private def fetchXmlPart(): Boolean =
// Clean up map, should be empty at EOF.
upcomingXmlLiteralParts.remove(offset) match {
case Some((end, isLastPart)) =>
finishComposite(XMLLIT, end)
if (isLastPart) next.token = XMLLITEND
true
case _ => false
}
private def isTripleQuote(): Boolean =
if (ch == '"') {
nextRawChar()
if (ch == '"') {
nextChar()
while (ch == '"') {
putChar('"')
nextChar()
}
true
} else {
putChar('"')
putChar('"')
false
}
} else {
putChar('"')
false
}
/**
* copy current character into cbuf, interpreting any escape sequences, and advance to next
* character.
*/
protected def getLitChar(): Unit =
if (ch == '\\' && !wasMultiChar) {
val start = begCharOffset
nextChar()
if ('0' <= ch && ch <= '7') {
val leadch = ch
var oct: Int = digit2int(ch, 8)
nextChar()
if ('0' <= ch && ch <= '7') {
oct = oct * 8 + digit2int(ch, 8)
nextChar()
if (leadch <= '3' && '0' <= ch && ch <= '7') {
oct = oct * 8 + digit2int(ch, 8)
nextChar()
}
}
val alt = if (oct == LF) "\\n" else "\\u%04x" format oct
def msg(what: String) = s"Octal escape literals are $what, use $alt instead."
deprecationWarning(msg("deprecated"), at = start)
putChar(oct)
} else {
ch match {
case 'b' => putChar('\b')
case 't' => putChar('\t')
case 'n' => putChar('\n')
case 'f' => putChar('\f')
case 'r' => putChar('\r')
case '\"' => putChar('\"')
case '\'' => putChar('\'')
case '\\' => putChar('\\')
case _ => invalidEscape()
}
nextChar()
}
} else if (isUnquoteDollar()) {
// bail and let the caller handle this
} else {
putChar(ch)
nextChar()
}
private def invalidEscape(): Unit = {
syntaxError("invalid escape character", at = begCharOffset)
putChar(ch)
}
@tailrec
private def getLitChars(delimiter: Char): Boolean = {
@inline def naturalBreak = (ch == SU || ch == CR || ch == LF) && !wasMultiChar
ch == delimiter || !isAtEnd && !naturalBreak && {
val offset = endCharOffset
getLitChar()
offset != endCharOffset && getLitChars(delimiter)
}
}
@inline private def isNumberSeparator(c: Int): Boolean = {
val isSeparator = c == '_'
if (isSeparator && !dialect.allowNumericLiteralUnderscoreSeparators)
syntaxError("numeric separators are not allowed", at = offset)
else isSeparator
}
private def checkNoTrailingSeparator(): Unit = {
val last = cbuf.length() - 1
if (last >= 0 && isNumberSeparator(cbuf.charAt(last))) {
syntaxError("trailing separator is not allowed", at = offset + last)
cbuf.setLength(last)
}
}
/**
* read fractional part and exponent of floating point number if one is present.
*/
private def getFraction(): Unit = {
while ('0' <= ch && ch <= '9' || isNumberSeparator(ch)) {
putChar(ch)
nextChar()
}
checkNoTrailingSeparator()
if (ch == 'e' || ch == 'E') {
val lookahead = lookaheadReader
lookahead.nextChar()
if (lookahead.ch == '+' || lookahead.ch == '-') {
lookahead.nextChar()
}
if ('0' <= lookahead.ch && lookahead.ch <= '9') {
putChar(ch)
nextChar()
if (ch == '+' || ch == '-') {
putChar(ch)
nextChar()
}
while ('0' <= ch && ch <= '9' || isNumberSeparator(ch)) {
putChar(ch)
nextChar()
}
checkNoTrailingSeparator()
}
token = DOUBLELIT
}
if (ch == 'd' || ch == 'D') {
putChar(ch)
nextChar()
token = DOUBLELIT
} else if (ch == 'f' || ch == 'F') {
putChar(ch)
nextChar()
token = FLOATLIT
} else {
token = DOUBLELIT
}
checkNoLetter()
setStrVal()
}
private def checkNoLetter(): Unit = {
if (isIdentifierPart(ch) && ch >= ' ' && !isNumberSeparator(ch))
syntaxError("Invalid literal number", at = offset)
}
/**
* Read a number into strVal and set base
*/
private def getNumber(): Unit = {
val base1 = if (base < 10) 10 else base
// Read 8,9's even if format is octal, produce a malformed number error afterwards.
// At this point, we have already read the first digit, so to tell an innocent 0 apart
// from an octal literal 0123... (which we want to disallow), we check whether there
// are any additional digits coming after the first one we have already read.
var notSingleZero = false
while (isNumberSeparator(ch) || digit2int(ch, base1) >= 0) {
putChar(ch)
nextChar()
notSingleZero = true
}
token = INTLIT
/* When we know for certain it's a number after using a touch of lookahead */
def restOfNumber() = {
putChar(ch)
nextChar()
getFraction()
}
def restOfUncertainToken() = {
def isEfd = ch match { case 'e' | 'E' | 'f' | 'F' | 'd' | 'D' => true; case _ => false }
def isL = ch match { case 'l' | 'L' => true; case _ => false }
if (base <= 10 && isEfd)
getFraction()
else {
// Checking for base == 8 is not enough, because base = 8 is set
// as soon as a 0 is read in `case '0'` of method fetchToken.
if (base == 8 && notSingleZero)
syntaxError("Non-zero integral values may not have a leading zero.", at = offset)
if (isL) {
putChar(ch)
setStrVal()
nextChar()
token = LONGLIT
} else {
setStrVal()
checkNoLetter()
}
}
}
checkNoTrailingSeparator()
if (base > 10 || ch != '.')
restOfUncertainToken()
else {
val lookahead = lookaheadReader
val c = lookahead.getc()
/* Prohibit 1. */
if (!Character.isDigit(c))
return setStrVal()
val isDefinitelyNumber = (c: @switch) match {
/** Another digit is a giveaway. */
case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' =>
true
/* Backquoted idents like 22.`foo`. */
case '`' =>
return setStrVal()
/** Note the early return */
/* These letters may be part of a literal, or a method invocation on an Int.
*/
case 'd' | 'D' | 'f' | 'F' =>
!isIdentifierPart(lookahead.getc())
/* A little more special handling for e.g. 5e7 */
case 'e' | 'E' =>
val ch = lookahead.getc()
!isIdentifierPart(ch) || (Character.isDigit(ch) || ch == '+' || ch == '-')
case x =>
!isIdentifierStart(x)
}
if (isDefinitelyNumber) restOfNumber()
else restOfUncertainToken()
}
}
/**
* Parse character literal if current character is followed by \', or follow with given op and
* return a symbol literal token
*/
private def charLitOr(op: () => Unit): Unit = {
putChar(ch)
nextChar()
if (ch == '\'') {
nextChar()
token = CHARLIT
setStrVal()
} else {
op()
token = SYMBOLLIT
strVal = name
}
}
private def getXml(): Unit = {
// 1. Collect positions of scala expressions inside this xml literal.
import fastparse.Parsed
val start = offset
val xmlParser = new XmlParser(dialect)
val result: Int = fastparse.parse(input.text, xmlParser.XmlExpr(_), startIndex = start) match {
case x: Parsed.Success[_] => x.index
case x: Parsed.Failure =>
syntaxError(
s"malformed xml literal, expected:\n${x.extra.traced.terminalsMsg}",
at = x.index
)
}
// 2. Populate upcomingXmlLiteralParts with xml literal part positions.
val lastFrom = xmlParser.splicePositions.foldLeft(start) { (lastFrom, pos) =>
// pos contains the start and end positions of a scala expression.
// We want the range of the xml literal part which starts at lastFrom
// and ends at pos.from.
val to = pos.from - 1
upcomingXmlLiteralParts.update(lastFrom, (to, false))
pos.to + 1
}
// The final xml literal part is not followed by any embedded scala expr.
upcomingXmlLiteralParts.update(lastFrom, (result, true))
// 3. Return only the first xml part.
fetchXmlPart()
}
// Unquotes -----------------------------------------------------------------
private def getUnquote(): Unit = {
require(ch == '$')
val start = endCharOffset
val endInclusive = {
val exploratoryInput = Input.Slice(input, start, input.chars.length)
val exploratoryScanner = new LegacyScanner(exploratoryInput, dialect.unquoteVariant())
exploratoryScanner.reader.nextChar()
exploratoryScanner.nextToken()
exploratoryScanner.curr.token match {
case LBRACE =>
@tailrec
def loop(balance: Int): Unit = {
exploratoryScanner.nextToken()
exploratoryScanner.curr.token match {
case LBRACE =>
loop(balance + 1)
case RBRACE =>
if (balance == 1) () // do nothing, this is the end of the unquote
else loop(balance - 1)
case _ =>
loop(balance)
}
}
try {
loop(balance = 1)
} catch {
case TokenizeException(pos, message) =>
syntaxError(s"invalid unquote: $message", at = start + pos.start)
}
case IDENTIFIER | THIS | USCORE =>
// do nothing, this is the end of the unquote
case _ =>
syntaxError(
"invalid unquote: `$'ident, `$'BlockExpr, `$'this or `$'_ expected",
at = start
)
}
start + exploratoryScanner.curr.endOffset
}
finishComposite(UNQUOTE, endInclusive + 1)
}