Eyvind
Eyvind

Reputation: 5261

Can I use AST transformations in Groovy to extend its syntax?

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 */ }

Upvotes: 1

Views: 533

Answers (2)

Peter Niederwieser
Peter Niederwieser

Reputation: 123910

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.

Upvotes: 1

Nick Klauer
Nick Klauer

Reputation: 5943

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.

Upvotes: 2

Related Questions