diff options
author | Lex Spoon <lex@lexspoon.org> | 2007-11-28 04:56:02 +0000 |
---|---|---|
committer | Lex Spoon <lex@lexspoon.org> | 2007-11-28 04:56:02 +0000 |
commit | 8c9e9f7b7d1de79b104d954035613e51e0acaaf2 (patch) | |
tree | f632a1b88ef7b122cef7aa7036c1ae4634a0f89f /src | |
parent | 25258b3d6d1152884471fbee1d9a3ad7d9649170 (diff) | |
download | scala-8c9e9f7b7d1de79b104d954035613e51e0acaaf2.tar.gz scala-8c9e9f7b7d1de79b104d954035613e51e0acaaf2.tar.bz2 scala-8c9e9f7b7d1de79b104d954035613e51e0acaaf2.zip |
escape characters as necessary when
inserting types into string literals
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/Interpreter.scala | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala index 701db42de3..6b7bd86104 100644 --- a/src/compiler/scala/tools/nsc/Interpreter.scala +++ b/src/compiler/scala/tools/nsc/Interpreter.scala @@ -71,6 +71,7 @@ class Interpreter(val settings: Settings, out: PrintWriter) { import compiler.CompilationUnit import compiler.{Symbol,Name,Type} import compiler.nme + import Interpreter.string2code /** construct an interpreter that reports to Console */ def this(settings: Settings) = @@ -220,6 +221,9 @@ class Interpreter(val settings: Settings, out: PrintWriter) { }) } + implicit def name2string(name: Name) = name.toString + + /** Compute imports that allow definitions from previous * requests to be visible in a new request. Returns * three pieces of related code: @@ -691,8 +695,12 @@ class Interpreter(val settings: Settings, out: PrintWriter) { def resultExtractionCode(code: PrintWriter) { for (vname <- valAndVarNames) { - code.print(" + \"" + vname + ": " + typeOf(vname) + " = \" + " + - " (if(" + fullPath(vname) + ".toString.contains('\\n')) " + + code.print(" + \"" + vname + ": " + + string2code(typeOf(vname)) + + " = \" + " + + " (if(" + + fullPath(vname) + + ".toString.contains('\\n')) " + " \"\\n\" else \"\") + " + fullPath(vname) + " + \"\\n\"") } @@ -790,7 +798,8 @@ class Interpreter(val settings: Settings, out: PrintWriter) { def defTypesSummary: String = stringFrom(summ => { for (methname <- defNames) - summ.println("" + methname + ": " + typeOf(methname)) + summ.println("" + methname + ": " + + string2code(typeOf(methname))) }) } @@ -808,8 +817,11 @@ class Interpreter(val settings: Settings, out: PrintWriter) { /** Print out lhs instead of the generated varName */ override def resultExtractionCode(code: PrintWriter) { - code.print(" + \"" + lhs + ": " + typeOf(compiler.encode(varName)) + - " = \" + " + fullPath(varName) + " + \"\\n\"") + code.print(" + \"" + lhs + ": " + + string2code(typeOf(compiler.encode(varName))) + + " = \" + " + + string2code(fullPath(varName)) + + " + \"\\n\"") // override def resultExtractionCode(code: PrintWriter) { // {wrapperObj; lhs} // } @@ -836,7 +848,9 @@ class Interpreter(val settings: Settings, out: PrintWriter) { } override def resultExtractionCode(code: PrintWriter) { super.resultExtractionCode(code) - code.println(" + \"defined module " + moduleName + "\\n\"") + code.println(" + \"defined module " + + string2code(moduleName) + + "\\n\"") } } @@ -860,7 +874,7 @@ class Interpreter(val settings: Settings, out: PrintWriter) { " + \"defined " + keyword + " " + - newClassName + + string2code(newClassName) + "\\n\"") } } @@ -912,7 +926,7 @@ object Interpreter { /** Delete a directory tree recursively. Use with care! */ - def deleteRecursively(path: File) { + private[nsc] def deleteRecursively(path: File) { path match { case _ if !path.exists => () @@ -932,4 +946,34 @@ object Interpreter { val wrapregex = "(line[0-9]+\\$object[$.])?(\\$iw[$.])*" str.replaceAll(wrapregex, "") } + + /** Convert a string into code that can recreate the string. + * This requires replacing all special characters by escape + * codes. It does not add the surrounding " marks. */ + def string2code(str: String): String = { + /** Convert a character to a backslash-u escape */ + def char2uescape(c: Char): String = { + var rest = c.toInt + val buf = new StringBuilder + for (i <- 1 to 4) { + buf ++= (rest % 16).toHexString + rest = rest / 16 + } + "\\" + "u" + buf.toString.reverse + } + + + val res = new StringBuilder + for (c <- str) { + if ("'\"\\" contains c) { + res += '\\' + res += c + } else if (!c.isControl) { + res += c + } else { + res ++= char2uescape(c) + } + } + res.toString + } } |