What is Groovy's AST?

Groovy compiler maps the groovy source code in a tree form called Abstract Syntax Tree (AST). The compiler scans and transforms this source tree in various phases and finally generates the bytecode using on the final, transformed AST. In addition to the direct source-code-to-AST mapping, 2 other major sources of AST changes are -

AST modifications made by the compiler itself to make many of the groovy features possible, like MOP or boilerplate code reduction, etc.

AST Transformations feature, which allows the groovy users to plug into the compilation process and manipulate the AST mid-way so that the final code gets generated as they desire.

An example of compiler introducing AST changes would be the generation of getters/setters for the groovy properties, as shown below:

Code provided by user:

class Test { String name}

The class above gets modified by the groovy compiler as below, where the methods getName() and setName() are not provided by the user, but generated by groovy compiler to save the user from providing such boilerplate code:

Exploring the AST in various scenarios across various compilation phases can greatly help one understand how groovy internally works.

Out-of-the-box, groovy installation comes with 2 tools - Groovy Console and Groovy AST Browser - while Groovy Console lets you quickly experiment with the language features, AST Browser helps you visualize how the groovy compiler takes your source code all the way across to its final form - the bytecode and how it transforms it on the way in multiple phases.

While it is great to have these tools locally installed, the availability of these tools online provides an alternative to those who don’t want to have the local installations but still want to experiment with the language, explore its various features, etc (imagine having a groovy-discussion with your ruby, scala or other non-groovy collagues - wouldn’t such online tools be helpful there?).

How the AST Browser may help?

Say, you wanted to understand how "println ‘hello world’" works in groovy, and how is it mapped to a class for the JVM?

Here is answer shown by Groovy AST Browser - it shows that your 1-liner script resulted in a full class that was generated by groovy - it has a main(args) method and your script code became part of a run() method (which is internally invoked from main())

Ex 1: A 1-liner script converted to a full class by Groovy

Next is the AST shown for the @Delegate example discussed above and it clearly shows how the methods on the delegate's type are added onto delegating type and all these methods basically forward the calls to the delegate object:

Ex 2: AST for @Delegate AST Transformation usage

One little feature to mention about this AST browser is the "Include Synthetic Members" option. Groovy generates a lot of synthetic members to support its internal workings - for example, to support its MOP features. Sometimes you may want to exploring these synthetic members and at other times you may want to wish them away from your view. The "Include Synthetic Members" option can be used for the same. Following are the 2 AST snapshots for the same code - one with Groovy’s internally used methods and the other without them:

Ex 3: An AST snapshot with synthetic members:

Ex 4: An AST snapshot without synthetic members:

Finally, if you are interested in exploring the AST for a given piece of code across various compilation phases, you can use the "phase" selector on the AST Browser and refresh the AST generated for that phase, as shown below:

Ex 5: Exploring AST across various compilation phases

That's all there is to this web-based Groovy AST Browser. I hope, the information provided in the article and the web-based version of the Groovy AST Browser, are useful to you.