Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse groovy code?

Since groovy is good at parsing nearly anything, a search on how to parse groovy code will not reveal any good results - so I hope that the SO community is able to help :-)

I would like to write some kind of (graphical) editor for grails domain classes, but don't want to reinvent the wheel.

It's easy to inspect the domain class through reflection, but I would like to go one step further - I want to modify the code and write it back as a domain class file.

One problem is that reflection (and afaik the AST too) will throw away all comments and formatting (formatting is not the big problem, I could pretty print the file)...

Any ideas where I can find a groovy parser upon which I can build my ideas?

like image 972
rdmueller Avatar asked Jan 16 '13 08:01

rdmueller


1 Answers

You should have an intensive look at the GroovyDocTool class source.

GroovyDoc uses the GroovyLexer and GroovyRecognizer to parse class texts (to generate GroovyDoc HTML documentation files, similar to JavaDoc HTML files) and utilizes these classes to create an AST from the given source text.

The generated AST and the source code text are used to walk through the class structure (see SimpleGroovyClassDocAssembler), extract comments and various other meta-data to fill the GroovyDoc specific data structures.

like image 161
Andre Steingress Avatar answered Sep 18 '22 13:09

Andre Steingress