11package dotty .tools .dotc .quoted
22
3+ import dotty .tools .dotc .ast .tpd
34import dotty .tools .dotc .Driver
45import dotty .tools .dotc .core .Contexts .{Context , FreshContext }
56import dotty .tools .dotc .core .StdNames ._
67import dotty .tools .io .VirtualDirectory
78import dotty .tools .repl .AbstractFileClassLoader
89
910import scala .quoted .Expr
10- import java .io .ByteArrayOutputStream
11- import java .io .PrintStream
12- import java .nio .charset .StandardCharsets
1311
1412class QuoteDriver extends Driver {
13+ import tpd ._
1514
1615 def run [T ](expr : Expr [T ]): T = {
1716 val ctx : Context = initCtx.fresh
@@ -31,19 +30,19 @@ class QuoteDriver extends Driver {
3130 method.invoke(instance).asInstanceOf [T ]
3231 }
3332
34- def show (expr : Expr [_]): String = {
33+ def show (expr : Expr [_]): String =
34+ withTree(expr, (tree, ctx) => tree.show(ctx))
35+
36+ def withTree [T ](expr : Expr [_], f : (Tree , Context ) => T ): T = {
3537 val ctx : Context = initCtx.fresh
3638 ctx.settings.color.update(" never" )(ctx) // TODO support colored show
37- val baos = new ByteArrayOutputStream
38- var ps : PrintStream = null
39- try {
40- ps = new PrintStream (baos, true , " utf-8" )
41-
42- new ExprDecompiler (ps).newRun(ctx).compileExpr(expr)
43-
44- new String (baos.toByteArray, StandardCharsets .UTF_8 )
39+ var output : Option [T ] = None
40+ def registerTree (tree : tpd.Tree )(ctx : Context ): Unit = {
41+ assert(output.isEmpty)
42+ output = Some (f(tree, ctx))
4543 }
46- finally if (ps != null ) ps.close()
44+ new ExprDecompiler (registerTree).newRun(ctx).compileExpr(expr)
45+ output.getOrElse(throw new Exception (" Could not extact " + expr))
4746 }
4847
4948 override def initCtx : Context = {
0 commit comments