Permalink
Browse files

Support $this references in String interpolation

  • Loading branch information...
1 parent 100efc3 commit 812f374faadc8e7c2b19a5ed217d8165b9b3134c @mdr mdr committed Sep 4, 2012
View
1 CHANGELOG
@@ -1,5 +1,6 @@
0.1.3 (..)
* Add EOF to ComplilationUnit, ensuring entire source is represented in the tree
+* Support $this references in String interpolation
0.1.2 (7/May/12)
View
3 scalariform/src/main/scala/scalariform/lexer/ScalaLexer.scala
@@ -206,7 +206,8 @@ class ScalaLexer(
do {
nextChar()
} while (ch != SU && Character.isUnicodeIdentifierPart(ch))
- token(VARID)
+ val tokenType = Keywords(getTokenText).getOrElse(VARID)
+ token(tokenType)
} else {
if (stringInterpolationMode.initialSegment) {
stringInterpolationMode.initialSegment = false
View
6 scalariform/src/main/scala/scalariform/parser/ScalaParser.scala
@@ -442,8 +442,12 @@ class ScalaParser(tokens: Array[Token]) {
dropAnyBraces(pattern())
else if (isIdent)
makeExpr(ident())
+ else if (LBRACE)
+ makeExpr(expr())
+ else if (THIS)
+ makeExpr(nextToken())
else
- expr()
+ throw new ScalaParserException("Error in string interpolation: expected block, identifier or `this'")
stringPartsAndScala += ((stringPart, scalaSegment))
}
if (!STRING_LITERAL) // TODO: Can it be absent, as allowed by Scalac?
View
1 scalariform/src/test/scala/scalariform/formatter/StringInterpolationFormatterTest.scala
@@ -12,6 +12,7 @@ class StringInterpolationFormatterTest extends AbstractExpressionFormatterTest {
<t>s"foo"</t>.text ==> <t>s"foo"</t>.text
<t>s""</t>.text ==> <t>s""</t>.text
<t>s"my name is $name"</t>.text ==> <t>s"my name is $name"</t>.text
+ <t>s"my name is $this"</t>.text ==> <t>s"my name is $this"</t>.text
"""s"my name is ${bob}"""" ==> """s"my name is ${bob}""""
"""s"my name is ${ person.name }"""" ==> """s"my name is ${person.name}""""
View
2 scalariform/src/test/scala/scalariform/lexer/ScalaLexerTest.scala
@@ -130,6 +130,8 @@ class ScalaLexerTest extends FlatSpec with ShouldMatchers {
""" s"${foo}" """ producesTokens (WS, INTERPOLATION_ID, STRING_PART, LBRACE, VARID, RBRACE, STRING_LITERAL, WS)
""" s"${s"${x}"}" """ producesTokens (WS, INTERPOLATION_ID, STRING_PART, LBRACE, INTERPOLATION_ID, STRING_PART, LBRACE, VARID, RBRACE, STRING_LITERAL, RBRACE, STRING_LITERAL, WS)
+ """ s"$this" """ producesTokens (WS, INTERPOLATION_ID, STRING_PART, THIS, STRING_LITERAL, WS)
+
<t>s""""""</t>.text producesTokens (INTERPOLATION_ID, STRING_LITERAL)
<t>s"""""""""</t>.text producesTokens (INTERPOLATION_ID, STRING_LITERAL)
<t>s""" $foo """</t>.text producesTokens (INTERPOLATION_ID, STRING_PART, VARID, STRING_LITERAL)

0 comments on commit 812f374

Please sign in to comment.