diff options
author | Som Snytt <som.snytt@gmail.com> | 2014-04-27 00:08:54 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2014-07-18 21:07:37 -0700 |
commit | abb58dcef1b4758f4e3af26c030e1f0630b8d145 (patch) | |
tree | 74f61c65e53ce8adfd3e985b4c23fbe9a3986e9b /src | |
parent | abdd570cee5788000724c6d3b89a978c48a7fa39 (diff) | |
download | scala-abb58dcef1b4758f4e3af26c030e1f0630b8d145.tar.gz scala-abb58dcef1b4758f4e3af26c030e1f0630b8d145.tar.bz2 scala-abb58dcef1b4758f4e3af26c030e1f0630b8d145.zip |
SI-6476 Improve error on escapement
Behavior of escape processing under string interpolation can
be confusing. This commit improves the exception message so
you know at least what sort of escapes are handled.
This came up on SO in the form `s"\d".r`, where it may not
be obvious what is throwing and how to work around it.
```
scala> s"\d".r
scala.StringContext$InvalidEscapeException: invalid escape '\d' not one of [\b, \t, \n, \f, \r, \\, \", \'] at index 0 in "\d". Use \\ for literal \.
scala> s"a\"
scala.StringContext$InvalidEscapeException: invalid escape at terminal index 1 in "a\". Use \\ for literal \.
```
Referencing SI-6476 because that has become the magnet ticket
for "escape processing under string interpolation, huh?"
This doesn't address `$"` and doesn't handle the more interesting
parse error `s"a\"b"`.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/StringContext.scala | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index c0468e8b02..0b08182102 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -175,8 +175,13 @@ object StringContext { * @param str The offending string * @param idx The index of the offending backslash character in `str`. */ - class InvalidEscapeException(str: String, @deprecatedName('idx) val index: Int) - extends IllegalArgumentException("invalid escape character at index "+index+" in \""+str+"\"") + class InvalidEscapeException(str: String, @deprecatedName('idx) val index: Int) extends IllegalArgumentException( + s"""invalid escape ${ + require(index >= 0 && index < str.length) + val ok = """[\b, \t, \n, \f, \r, \\, \", \']""" + if (index == str.length - 1) "at terminal" else s"'\\${str(index + 1)}' not one of $ok at" + } index $index in "$str". Use \\\\ for literal \\.""" + ) /** Expands standard Scala escape sequences in a string. * Escape sequences are: |