Where can I learn about constructing AST's for Scala macros?

后端 未结 2 663
无人及你
无人及你 2020-12-07 10:37

Where I can learn how to construct the AST\'s that Scala\'s macros generate?

The Scaladoc isn\'t as helpful as I\'d like. For example:

abstract def A         


        
2条回答
  •  感动是毒
    2020-12-07 11:06

    There isn't a lot of documentation for the internals of the compiler available, but the things that are available should be enough to get started.

    Mirko Stocker, has written his Master Thesis about Scala Refactoring. In Appendix D (p. 95) he describes the architecture of the AST. It includes also a graphical overview:

    Scala AST

    Another way to find information about the AST is to look directly into the sources of reflect.internal.Trees, which contains the AST.

    If one needs to find out how a specific source code snippet is represented internally there is reify:

    scala> import reflect.runtime.universe._
    import reflect.runtime.universe._
    
    scala> showRaw(reify{val i = 0}.tree)
    res8: String = Block(List(ValDef(Modifiers(), newTermName("i"), TypeTree(),
      Literal(Constant(0)))), Literal(Constant(())))
    

提交回复
热议问题