Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
[ 185 ]

AST Transformations


In this chapter, we will take a look at Groovy abstract syntax tree (AST)
transformations. AST transformations are a mechanism for us to hook the
Groovy compilation process. Up to now, we have looked at Groovy's runtime
metaprogramming abilities. Here, we will look at compile time metaprogramming
and see how we can use AST transformations to build code on the fly during the
compilation process. This is a complex subject, for which we will focus on the
following aspects in detail:



  • Local AST transformations: We will look at how we can use a local AST
    transformation to implement an annotation

  • Manipulating the AST: We will look at the various mechanisms for adding
    code to our programs using the ASTNode APIs and the ASTBuilder class

  • Global AST transformations: We will build one complete DSL based on a
    Global AST transformation

  • Compilation errors: We will learn how to add our own compilation errors

  • Packaging an AST transformation: We will learn how to package an AST
    transformation so that it can be shared

Free download pdf