liufengyun / gestalt

gestalt : portable and solid macros for Scala

Home Page:https://github.com/scalacenter/macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A tree printer in terms of extractors

liufengyun opened this issue · comments

A tree printer in terms of extractors

The motivation behind this is better tools for macro writing and debugging. When I write macros I use println(tree) to look at the structure. This returns the dotty tree. It would be nice to be able to tell what I can extract from that tree.

Example:

val tree = q"Some(3+4)"
println(tree)
// prints to console:
Apply(Ident(Some),List(Apply(Select(Literal(Constant(3)),+),List(Literal(Constant(4))))))

// Proposed method. Name is still work in progress
println(toolbox.showExtractables(tree))

// if we only have Apply and Lit extractors available it will print this string in the console:
Apply(_,List(Apply(_),List(Lit(4)))))
// if we also have Select extractor
Apply(_,List(Apply(Select(Lit(3),+),List(Lit(4))))
// if we have all 4 extractors usable in the example.
// even with all extractors the structure is different from what we get from toString
Apply(Ident(Some),List(Apply(Select(Lit(3),+),List(Lit(4)))))
// it may even be more nicer to wrap string constants in double quotes,
// just so the output could be directly copied in a pattern match.
Apply(Ident("Some"),List(Apply(Select(Lit(3),"+"),List(Lit(4)))))
// * by importing toolbox._ first
import toolbox._
Apply(Ident("Some"),List(Apply(Select(Lit(3),"+"),List(Lit(4)))))