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
56import dotty .tools .dotc .core .StdNames ._
67import dotty .tools .io .VirtualDirectory
7-
88import dotty .tools .repl .AbstractFileClassLoader
99
1010import scala .quoted .Expr
1111
12- import java .io .ByteArrayOutputStream
13- import java .io .PrintStream
14- import java .nio .charset .StandardCharsets
15-
1612class QuoteDriver extends Driver {
13+ import tpd ._
1714
1815 def run [T ](expr : Expr [T ]): T = {
1916 val ctx : Context = initCtx.fresh
@@ -33,19 +30,16 @@ class QuoteDriver extends Driver {
3330 method.invoke(instance).asInstanceOf [T ]
3431 }
3532
36- def show (expr : Expr [_]): String = {
33+ def toTreeWithContext (expr : Expr [_]): ( Tree , Context ) = {
3734 val ctx : Context = initCtx.fresh
3835 ctx.settings.color.update(" never" )(ctx) // TODO support colored show
39- val baos = new ByteArrayOutputStream
40- var ps : PrintStream = null
41- try {
42- ps = new PrintStream (baos, true , " utf-8" )
43-
44- new ExprDecompiler (ps).newRun(ctx).compileExpr(expr)
45-
46- new String (baos.toByteArray, StandardCharsets .UTF_8 )
36+ var output : (tpd.Tree , Context ) = null
37+ def registerTree (tree : tpd.Tree )(ctx : Context ): Unit = {
38+ assert(output eq null )
39+ output = (tree, ctx)
4740 }
48- finally if (ps != null ) ps.close()
41+ new ExprDecompiler (registerTree).newRun(ctx).compileExpr(expr)
42+ output
4943 }
5044
5145 override def initCtx : Context = {
0 commit comments