Skip to end of metadata
Go to start of metadata





AstBuilder AST Templates








Hamlet D'Arcy



Last modification:



The AstBuilder was introduced in Groovy 1.7 as a way to ease the task of writing AST transformations. The "fromCode" appoach allows you generate the AST from a piece of code. Currently, the AstBuilder fromCode API accepts a closure as a parameter, and the code within the closure it transformed to AST. A limitation of the AstBuilder is that the closure parameter does not allow variables in the surrounding context to be referenced: there is no way to pass a parameter into the code block. It was initially discussed but the idea was deemed too much effort to fit into 1.7.

This AST Template feature allows you to pass parameters into the AstBuilder. Similar to how a $ works in GString interpolation, there needs to be a way to bind a variable from the enclosing scope into an AstBuilder closure. The syntax proposed is to use oxford brackets: var. An AstBuilder closure containing GEP 4 - AstBuilder AST Templates will have the variable within the brackets bound in.


The oxford brackets are used to bind, or quasi-quote, variables from the enclosing scope into an AstBuilder parameter.

Example 1:

String concatenation

Produces the AST

Example 2:

Producing a println

is equivilent to invoking this method:

Example 3.

Memoization of a method. Presumably, you would have the existing method AST in a variable called "methodNode":


There are many syntax alternatives:

  • Lisp uses ´ and ,
  • Template Haskell uses something similar to oxford brackets but with slightly different semantics
  • Boo uses uses GEP 4 - AstBuilder AST Templates and $ but with slightly different semantics
  • A $ was considered but cannot be used as it is already a meaningful identifier.

Boo Meta Method Comparison

The Boo Metamethod feature overlaps with the Groovy AST Transformation features. Instead of an @AstTransformation interface with a seperately defined AstTransformation subclass, Boo offers Meta methods, where the AST Transformation is called and generated at compile time.

While Groovy offers AstBuilder to turn code into AST, Boo uses the oxford brackets. And while GEP-4 proposed oxford brackets to signify an AST variable templating, Boo uses the $. Consider a simple println AST in Groovy

Compared to a similar construct in Boo:

GEP-4 proposes the opposite syntax from Boo: oxford brackets for AST Templating instead of the $. The $ is already a character in Java, and can be part of a variable name.

There are no real drawbacks to having an opposite syntax. The number of Boo AST Transformation writers migrating to Groovy is most likely quite low.


Mailing List Discussions

  • No labels


  1. In the Useful Links section, the link "Boo Meta Methods" seems broken. I get a 404 error.

  2. I think it would make code much cleaner if, instead of enclosing a variable, you could enclose an expression.  This is what Lisp does, and I assume the other languages as well.

  3. Can you expand on what you mean by "$ is a meaningful identifier"?  I doubt anyone uses a single $ as a variable name or method name in Groovy source, so why can't we reuse it?  GStrings reuse it and it doesn't seem to cause any problems.

  4. Given that Boo uses [| where we use new AstBuilder().buildFromCode, it seems confusing to use [| for something different.

  5. I will answer the comments on the dev mailing list. Thanks for the feedback guys!