Skip to content

fix #1779: support $_ and $_id in interpolated string #1780

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

Merged
merged 2 commits into from
Dec 15, 2016
Merged
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
14 changes: 9 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ object Parsers {
val isNegated = negOffset < in.offset
atPos(negOffset) {
if (in.token == SYMBOLLIT) atPos(in.skipToken()) { SymbolLit(in.strVal) }
else if (in.token == INTERPOLATIONID) interpolatedString()
else if (in.token == INTERPOLATIONID) interpolatedString(inPattern)
else finish(in.token match {
case CHARLIT => in.charVal
case INTLIT => in.intVal(isNegated).toInt
Expand All @@ -621,10 +621,14 @@ object Parsers {
in.nextToken()
while (in.token == STRINGPART) {
segmentBuf += Thicket(
literal(),
literal(inPattern = inPattern),
atPos(in.offset) {
if (in.token == IDENTIFIER)
termIdent()
else if (in.token == USCORE && inPattern) {
in.nextToken()
Ident(nme.WILDCARD)
}
else if (in.token == THIS) {
in.nextToken()
This(EmptyTypeIdent)
Expand All @@ -633,12 +637,12 @@ object Parsers {
if (inPattern) Block(Nil, inBraces(pattern()))
else expr()
else {
ctx.error(InterpolatedStringError())
ctx.error(InterpolatedStringError(), source atPos Position(in.offset))
EmptyTree
}
})
}
if (in.token == STRINGLIT) segmentBuf += literal()
if (in.token == STRINGLIT) segmentBuf += literal(inPattern = inPattern)
InterpolatedString(interpolator, segmentBuf.toList)
}

Expand Down Expand Up @@ -1444,7 +1448,7 @@ object Parsers {
case XMLSTART =>
xmlLiteralPattern()
case _ =>
if (isLiteral) literal()
if (isLiteral) literal(inPattern = true)
else {
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern())
errorTermTree
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ object Scanners {
finishStringPart()
nextRawChar()
next.token = LBRACE
} else if (Character.isUnicodeIdentifierStart(ch)) {
} else if (Character.isUnicodeIdentifierStart(ch) || ch == '_') {
finishStringPart()
do {
putChar(ch)
Expand Down
13 changes: 13 additions & 0 deletions tests/neg/i1779.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
object Test {
implicit class Foo(sc: StringContext) {
object q {
def apply(arg: Any*): Int = 3
}
}

def f = {
val _parent = 3
q"val hello = $_parent"
q"class $_" // error // error
}
}
1 change: 1 addition & 0 deletions tests/run/i1779.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extends
13 changes: 13 additions & 0 deletions tests/run/i1779.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
object Test {
implicit class Foo(sc: StringContext) {
object q {
def unapply(arg: Any): Option[(Any, Any)] =
Some((sc.parts(0), sc.parts(1)))
}
}

def main(args: Array[String]): Unit = {
val q"class $_ extends $_parent" = new Object
println(_parent)
}
}