views:

215

answers:

2

I have seen examples of how you can use the Groovy AST transformations to extend the language, e.g. to log before and after a method call as shown here. However, would it also be possible to use this framework to extend the syntax of the language itself? For instance, what if I wanted to be able to parse and transform the following into an AST and then generate a set of statements:

newClassKeyword C { /* something here */ }
+1  A: 

I'm no Groovy expert, but I have been looking at some of the new frameworks that have come out of the 1.6 release, and of particular interest to me is Spock. This is probably exactly what you're looking for, as they use AST transformations to define an entire specification language for testing.

I forgot to add that InfoQ has an article that outlines the new features of Groovy 1.6. Guillaume LaForge wrote the article, and in it he describes a little bit about the AST.

Nick Klauer
+1  A: 

You cannot extend Groovy's syntax with AST transformations, but you can give completely new semantics to existing syntax. Because Groovy's syntax is quite flexible, this can get you pretty far.

Peter Niederwieser