Skip to content

Instantly share code, notes, and snippets.

@lrytz
Last active Mar 8, 2021
Embed
What would you like to do?
object T {
import scala.util.matching._
object sessionNames {
// All values are configurable by passing e.g. -Dscala.repl.name.read=XXX
final def propOr(name: String): String = propOr(name, "$" + name)
final def propOr(name: String, default: String): String =
sys.props.getOrElse("scala.repl.name." + name, default)
// Prefixes used in repl machinery. Default to $line, $read, etc.
def line = propOr("line")
def read = "$" + "read"
def iw = "$" + "iw"
def eval = propOr("eval")
def print = propOr("print")
def result = propOr("result")
def packageName(lineId: Int) = line + lineId
/** Create the name for the temp val used in the -Yclass-based REPL wrapper to refer to the state of a previous line. */
final def lineReadValName(linePackageName: String) = s"${linePackageName}${read}"
// The prefix for unnamed results: by default res0, res1, etc.
def res = propOr("res", "res") // INTERPRETER_VAR_PREFIX
// Internal ones
def ires = propOr("ires")
}
def linePattern: String = {
import Regex.{quote => q}
val sn = sessionNames
val lineN = raw"${q(sn.line)}\d+"
val lineNRead = raw"$lineN(${q(sn.read)})?"
val lambda = """(\.this\.|\.|/|\$\$(?=\$Lambda)|\$|$)"""
raw"($lineNRead|${q(sn.read)}(\.INSTANCE)?(\$$${q(sn.iw)})?|${q(sn.eval)}|${q(sn.print)}|${q(sn.iw)})$lambda"
}
val esc = "\u001b"
val csi = raw"$esc\[[0-9;]*([\x40-\x7E])"
val cleaner = raw"$csi|([^\p{Print}\p{Space}]+)|$linePattern".r
def clean(m: Regex.Match): Option[String] = {
println(m.subgroups)
if ("m" == m.group(1) ) None
else if (m.group(1) != null || m.group(2) != null) Some("?" * (m.end - m.start))
else Some("")
}
def run(str: String): String = {
println(cleaner.regex.toCharArray.map(_.toInt).toList)
cleaner.replaceSomeIn(str, clean)
}
}
val m = scala.tools.nsc.interpreter.Naming.getClass.getDeclaredMethod("cleaner")
m.setAccessible(true)
println(m.invoke(scala.tools.nsc.interpreter.Naming).asInstanceOf[scala.util.matching.Regex].regex.toCharArray.map(_.toInt).toList)
T.run("ö")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment