Skip to content

Commit 582536e

Browse files
committed
Fix lookahead logic in Scanner
Executing lookaheads/reset pairs from the same position caused a token to be lost.
1 parent 1b70511 commit 582536e

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -321,30 +321,27 @@ object Scanners {
321321
case _ =>
322322
}
323323

324-
/** Produce next token, filling TokenData fields of Scanner.
325-
*/
326-
def nextToken(): Unit = {
327-
val lastToken = token
328-
adjustSepRegions(lastToken)
329-
330-
// Read a token or copy it from `next` tokenData
331-
if (next.token == EMPTY) {
324+
/** Read a token or copy it from `next` tokenData */
325+
private def getNextToken(lastToken: Token): Unit =
326+
if next.token == EMPTY then
332327
lastOffset = lastCharOffset
333-
currentRegion match {
328+
currentRegion match
334329
case InString(multiLine, _) if lastToken != STRINGPART => fetchStringPart(multiLine)
335330
case _ => fetchToken()
336-
}
337-
if (token == ERROR) adjustSepRegions(STRINGLIT) // make sure we exit enclosing string literal
338-
}
339-
else {
331+
if token == ERROR then adjustSepRegions(STRINGLIT) // make sure we exit enclosing string literal
332+
else
340333
this.copyFrom(next)
341334
next.token = EMPTY
342-
}
343335

344-
if (isAfterLineEnd) handleNewLine(lastToken)
336+
/** Produce next token, filling TokenData fields of Scanner.
337+
*/
338+
def nextToken(): Unit =
339+
val lastToken = token
340+
adjustSepRegions(lastToken)
341+
getNextToken(lastToken)
342+
if isAfterLineEnd then handleNewLine(lastToken)
345343
postProcessToken()
346344
printState()
347-
}
348345

349346
final def printState() =
350347
if debugTokenStream && (showLookAheadOnDebug || !isInstanceOf[LookaheadScanner]) then
@@ -602,12 +599,10 @@ object Scanners {
602599
insert(OUTDENT, offset)
603600
case _ =>
604601

605-
def lookAhead() = {
602+
def lookAhead() =
606603
prev.copyFrom(this)
607-
lastOffset = lastCharOffset
608-
fetchToken()
604+
getNextToken(token)
609605
if token == END && !isEndMarker then token = IDENTIFIER
610-
}
611606

612607
def reset() = {
613608
next.copyFrom(this)

tests/neg/i1779.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ object Test {
88
def f = {
99
val _parent = 3
1010
q"val hello = $_parent"
11-
q"class $_" // error // error // error
11+
q"class $_" // error // error
1212
}
1313
}

tests/pos/fewer-braces.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import language.experimental.fewerBraces
2+
3+
object Test:
4+
5+
assert((new Object: Any).isInstanceOf[Object])

0 commit comments

Comments
 (0)