Compiler/AST interpreter: Difference between revisions
Content deleted Content added
Line 2,568:
case class LeafNode(name: String, value: String) extends Node
case object TerminalNode extends Node
}
</lang>
The above code depends on the function <tt>unescape()</tt> to perform string escape sequence translation. That function is defined in the following separate source file.
<lang scala>
package xyz.hyperreal
import java.io.ByteArrayOutputStream
package object rosettacodeCompiler {
val escapes = "\\\\b|\\\\f|\\\\t|\\\\r|\\\\n|\\\\\\\\|\\\\\"" r
def unescape(s: String) =
escapes.replaceAllIn(s, _.matched match {
case "\\b" => "\b"
case "\\f" => "\f"
case "\\t" => "\t"
case "\\r" => "\r"
case "\\n" => "\n"
case "\\\\" => "\\"
case "\\\"" => "\""
})
def capture(thunk: => Unit) = {
val buf = new ByteArrayOutputStream
Console.withOut(buf)(thunk)
buf.toString
}
}
|