Global AST transformations

Next, we will look at a global AST transformation. Unlike the local transformations we have just encountered, which are targeted at a specific part of the Groovy code, a global transformation is invoked for all AST nodes in the compiled code. Let's take a look at a fully worked global AST transformation example.

A finite state machine DSL

We will be a bit brave with this transformation. In fact, the transformed AST code for this transformation will look nothing like the original compiled code. The goal of this AST transformation is to provide a mini DSL for defining finite state machines. The syntax we are aiming for takes its queue from Spock and uses labels and string literals to be part of the DSL syntax.

Let's start ...

Get Groovy for Domain-specific Languages - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.