Skip to content

Post-process STRINGPART #15519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,10 @@ object Scanners {
getNextToken(token)
if token == END && !isEndMarker then token = IDENTIFIER

def reset() = {
def reset() =
assert(next.token == EMPTY || isInstanceOf[LookaheadScanner], s"lookAhead/reset would erase next token ${tokenString(next.token)} after ${tokenString(token)}")
next.copyFrom(this)
this.copyFrom(prev)
}

def closeIndented() = currentRegion match
case r: Indented if !r.isOutermost => insert(OUTDENT, offset)
Expand Down Expand Up @@ -751,6 +751,8 @@ object Scanners {
closeIndented()
case EOF =>
if !source.maybeIncomplete then closeIndented()
case STRINGPART =>
finishStringPart()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Finnish string part would be the violins in Sibelius's Finlandia.

case _ =>
}
}
Expand Down Expand Up @@ -1269,46 +1271,13 @@ object Scanners {
getStringPart(multiLine)
}
else if (ch == '$') {
def getInterpolatedIdentRest(hasSupplement: Boolean): Unit =
@tailrec def loopRest(): Unit =
if ch != SU && isUnicodeIdentifierPart(ch) then
putChar(ch) ; nextRawChar()
loopRest()
else if atSupplementary(ch, isUnicodeIdentifierPart) then
putChar(ch) ; nextRawChar()
putChar(ch) ; nextRawChar()
loopRest()
else
finishNamedToken(IDENTIFIER, target = next)
end loopRest
setStrVal()
token = STRINGPART
next.lastOffset = charOffset - 1
next.offset = charOffset - 1
putChar(ch) ; nextRawChar()
if hasSupplement then
putChar(ch) ; nextRawChar()
loopRest()
end getInterpolatedIdentRest

nextRawChar()
if (ch == '$' || ch == '"') {
if ch == '$' || ch == '"' then
putChar(ch)
nextRawChar()
getStringPart(multiLine)
}
else if (ch == '{') {
setStrVal()
token = STRINGPART
}
else if isUnicodeIdentifierStart(ch) || ch == '_' then
getInterpolatedIdentRest(hasSupplement = false)
else if atSupplementary(ch, isUnicodeIdentifierStart) then
getInterpolatedIdentRest(hasSupplement = true)
else
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected", off = charOffset - 2)
putChar('$')
getStringPart(multiLine)
token = STRINGPART
}
else {
val isUnclosedLiteral = !isUnicodeEscape && (ch == SU || (!multiLine && (ch == CR || ch == LF)))
Expand All @@ -1325,6 +1294,42 @@ object Scanners {
}
end getStringPart

private def finishStringPart() =
def getInterpolatedIdentRest(hasSupplement: Boolean): Unit =
@tailrec def loopRest(): Unit =
if ch != SU && isUnicodeIdentifierPart(ch) then
putChar(ch) ; nextRawChar()
loopRest()
else if atSupplementary(ch, isUnicodeIdentifierPart) then
putChar(ch) ; nextRawChar()
putChar(ch) ; nextRawChar()
loopRest()
else
finishNamedToken(IDENTIFIER, target = next)
end loopRest
setStrVal()
token = STRINGPART
next.lastOffset = charOffset - 1
next.offset = charOffset - 1
putChar(ch) ; nextRawChar()
if hasSupplement then
putChar(ch) ; nextRawChar()
loopRest()
end getInterpolatedIdentRest
// begin
if ch == '{' then
setStrVal()
token = STRINGPART
else if isUnicodeIdentifierStart(ch) || ch == '_' then
getInterpolatedIdentRest(hasSupplement = false)
else if atSupplementary(ch, isUnicodeIdentifierStart) then
getInterpolatedIdentRest(hasSupplement = true)
else
error("invalid string interpolation: `$$`, `$\"`, `$`ident or `$`BlockExpr expected", off = charOffset - 2)
putChar('$')
getStringPart(multiLine = false)
end finishStringPart

private def fetchStringPart(multiLine: Boolean) = {
offset = charOffset - 1
getStringPart(multiLine)
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i15514.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

object Main { s"Hello $Main.toStr!" }

object Alt { s"Hello ${Alt}.toStr!" }