Skip to content

Commit

Permalink
Fix lookahead logic in Scanner
Browse files Browse the repository at this point in the history
Executing lookaheads/reset pairs from the same position caused a token to be lost.
  • Loading branch information
odersky committed Feb 25, 2022
1 parent 1b70511 commit 2c9af15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
35 changes: 15 additions & 20 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,30 +321,27 @@ object Scanners {
case _ =>
}

/** Produce next token, filling TokenData fields of Scanner.
*/
def nextToken(): Unit = {
val lastToken = token
adjustSepRegions(lastToken)

// Read a token or copy it from `next` tokenData
if (next.token == EMPTY) {
/** Read a token or copy it from `next` tokenData */
private def getNextToken(lastToken: Token): Unit =
if next.token == EMPTY then
lastOffset = lastCharOffset
currentRegion match {
currentRegion match
case InString(multiLine, _) if lastToken != STRINGPART => fetchStringPart(multiLine)
case _ => fetchToken()
}
if (token == ERROR) adjustSepRegions(STRINGLIT) // make sure we exit enclosing string literal
}
else {
if token == ERROR then adjustSepRegions(STRINGLIT) // make sure we exit enclosing string literal
else
this.copyFrom(next)
next.token = EMPTY
}

if (isAfterLineEnd) handleNewLine(lastToken)
/** Produce next token, filling TokenData fields of Scanner.
*/
def nextToken(): Unit =
val lastToken = token
adjustSepRegions(lastToken)
getNextToken(lastToken)
if isAfterLineEnd then handleNewLine(lastToken)
postProcessToken()
printState()
}

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

def lookAhead() = {
def lookAhead() =
prev.copyFrom(this)
lastOffset = lastCharOffset
fetchToken()
getNextToken(token)
if token == END && !isEndMarker then token = IDENTIFIER
}

def reset() = {
next.copyFrom(this)
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/fewer-braces.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import language.experimental.fewerBraces

object Test:

assert((new Object: Any).isInstanceOf[Object])

0 comments on commit 2c9af15

Please sign in to comment.