summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Gruntz <dominik.gruntz@fhnw.ch>2012-07-17 15:38:58 +0200
committerDominik Gruntz <dominik.gruntz@fhnw.ch>2012-07-17 15:48:37 +0200
commit9c4b0d0402559921d4f36e9ebdd1af9a818fdde7 (patch)
treeba04a2af0e0c83b3a278e69d8efa9389f9031f99
parentbad93927836f302be3b973335a63bb5e69b2237c (diff)
downloadscala-9c4b0d0402559921d4f36e9ebdd1af9a818fdde7.tar.gz
scala-9c4b0d0402559921d4f36e9ebdd1af9a818fdde7.tar.bz2
scala-9c4b0d0402559921d4f36e9ebdd1af9a818fdde7.zip
SI-5856 enables use of $this in string interpolation
This pull request fixes SI-5856. The scanner has been modified to return the correct token if keywords appear in $-expressions. The parser has been modified to issue an error and to only accept $<identifier>, $this and $block.
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/Parsers.scala7
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/Scanners.scala6
-rw-r--r--test/files/neg/t5856.check31
-rw-r--r--test/files/neg/t5856.scala11
-rw-r--r--test/files/run/t5856.scala10
5 files changed, 63 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
index 3232bde3b4..656c0fa79a 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
@@ -1169,7 +1169,12 @@ self =>
if (inPattern) dropAnyBraces(pattern())
else {
if (in.token == IDENTIFIER) atPos(in.offset)(Ident(ident()))
- else expr()
+ else if(in.token == LBRACE) expr()
+ else if(in.token == THIS) { in.nextToken(); atPos(in.offset)(This(tpnme.EMPTY)) }
+ else {
+ syntaxErrorOrIncomplete("error in interpolated string: identifier or block expected", true)
+ EmptyTree
+ }
}
}
}
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
index 6ba273b8ea..db04aedf29 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/Scanners.scala
@@ -729,8 +729,12 @@ trait Scanners extends ScannersCommon {
next.token = IDENTIFIER
next.name = newTermName(cbuf.toString)
cbuf.clear()
+ val idx = next.name.start - kwOffset
+ if (idx >= 0 && idx < kwArray.length) {
+ next.token = kwArray(idx)
+ }
} else {
- syntaxError("invalid string interpolation")
+ syntaxError("invalid string interpolation: $$, $ident or $block expected")
}
} else {
val isUnclosedLiteral = !isUnicodeEscape && (ch == SU || (!multiLine && (ch == CR || ch == LF)))
diff --git a/test/files/neg/t5856.check b/test/files/neg/t5856.check
new file mode 100644
index 0000000000..d42bcdb524
--- /dev/null
+++ b/test/files/neg/t5856.check
@@ -0,0 +1,31 @@
+t5856.scala:10: error: invalid string interpolation: $$, $ident or $block expected
+ val s9 = s"$"
+ ^
+t5856.scala:10: error: unclosed string literal
+ val s9 = s"$"
+ ^
+t5856.scala:2: error: error in interpolated string: identifier or block expected
+ val s1 = s"$null"
+ ^
+t5856.scala:3: error: error in interpolated string: identifier or block expected
+ val s2 = s"$false"
+ ^
+t5856.scala:4: error: error in interpolated string: identifier or block expected
+ val s3 = s"$true"
+ ^
+t5856.scala:5: error: error in interpolated string: identifier or block expected
+ val s4 = s"$yield"
+ ^
+t5856.scala:6: error: error in interpolated string: identifier or block expected
+ val s5 = s"$return"
+ ^
+t5856.scala:7: error: error in interpolated string: identifier or block expected
+ val s6 = s"$new"
+ ^
+t5856.scala:8: error: error in interpolated string: identifier or block expected
+ val s7 = s"$s1 $null $super"
+ ^
+t5856.scala:9: error: error in interpolated string: identifier or block expected
+ val s8 = s"$super"
+ ^
+10 errors found
diff --git a/test/files/neg/t5856.scala b/test/files/neg/t5856.scala
new file mode 100644
index 0000000000..2ceee590af
--- /dev/null
+++ b/test/files/neg/t5856.scala
@@ -0,0 +1,11 @@
+object Test {
+ val s1 = s"$null"
+ val s2 = s"$false"
+ val s3 = s"$true"
+ val s4 = s"$yield"
+ val s5 = s"$return"
+ val s6 = s"$new"
+ val s7 = s"$s1 $null $super"
+ val s8 = s"$super"
+ val s9 = s"$"
+} \ No newline at end of file
diff --git a/test/files/run/t5856.scala b/test/files/run/t5856.scala
new file mode 100644
index 0000000000..d1e9bd6e58
--- /dev/null
+++ b/test/files/run/t5856.scala
@@ -0,0 +1,10 @@
+object Test extends App {
+ override def toString = "Test"
+
+ assert(s"$this" == "Test")
+ assert(s"$this$this" == "TestTest")
+ assert(s"$this$$" == "Test$")
+ assert(s"$this.##" == "Test.##")
+ assert(s"$this.toString" == "Test.toString")
+ assert(s"$this=THIS" == "Test=THIS")
+} \ No newline at end of file