As you may know, since version 3.1 ANTLR supports rewriting of trees . This is especially useful when you have a very complex transformation from input to output. If so you can bring the input tree closer to the desired output tree with each tree transformation and finally spit out the desired textual output format using templates.
Bird's eye view cookbook
- Have a very clear idea what input should be translated to what output.
- Start with a lexer and a parser that parses the input and spits out a reasonable AST
- Think of reasonable templates for the output
- Analyse which data you would need for each template to fill it
- Informally make up an idea how a the ideal tree to feed your template would look like
- Write down a list of simple modification steps that would transform your initial AST into a tree that comes close to the ideal one you made up
- Write a new tree transformer that traverses the AST and spits it out again unmodified
- Adapt the new transformer to perform the next modification step
- Continue with (7) until you have a tree transformer for each modification step
- Add new transformations steps when more complex statements seem to justify this
- When you have a transformer for each step, write a tree parser that feeds our templates
- After you are functionall done completely, consider performance. Try combining tree transformations to one when performance is not acceptable. Do this thoughtfully as this will most likely make your code less obvious and less maintainable.
- Do this in a depth-first manner - start by transforming a simple input to a valid output, then make it more complex and adapt the transformation steps
- Do not add too many tree transformers as with every change in a transformer's output AST all subsequent transformers need adjustment
- Tests are crucial. Compare expected transformation output with actual.
- Consider doing this in a test first manner, as such a transformation task is really complex.
Add your Patterns for tree transformation.