What you are trying to do is called program conversion , that is, the automatic creation of one program from another. What you do is “wrong,” assumes that the parser is all that you need, and discovers that it is not, and that you need to fill in the gap.
Tools that do this with the help of parsers (for assembling AST) mean modification of AST (both procedural and directional templates) and beautiful printers that convert (modified) AST back to legal source code. You seem to be struggling with the fact that ANTLR does not come with beautiful prints; what is not part of his philosophy; ANTLR is a (excellent) parser. Other answers suggested using ANTLR "string patterns", which in themselves are not quite printable, but can be used to implement it at the selling price. It is harder to do than it seems; see my SO answer on AST compiling back to source .
The real problem here is the widespread but false assumption that "if I have a parser, I am on the way to creating complex software tools for analysis and transformation." See My essay on Life After Parsing for a long discussion of this; basically, you need a lot more tools to make the parser “just” if you don’t want to rebuild a significant part of the infrastructure yourself, instead of doing your job. Other useful features of practical conversion software systems include, as a rule, source-to-source conversion, which greatly simplifies the task of finding and replacing complex patterns in trees.
For example, if you have the ability to convert a source to a source (of our tool, the DMS Software Reengineering Toolkit , you would be able to write portions of your examples using these DMS conversions:
domain ECMAScript. tag replace; -- says this is a special kind of temporary tree rule barize(function_name:IDENTIFIER,list:expression_list,b:body): expression->expression = " \function_name ( '[' \list ']' ) " -> "\function_name( \firstarg\(\function_name\), \replace\(\list\))"; rule replace_unit_list(s:character_literal): expression_list -> expression_list replace(s) -> compute_index_for(s); rule replace_long_list(s:character_list, list:expression_list): expression_list -> expression_list "\replace\(\s\,\list)-> "compute_index_for\(\s\),\list";
with external meta rules, first_arg procedures (which knows how to calculate the bar with the identifier foo [I assume you want to do this) and compute_index_for, which sets the string literals, knows which integer to replace it.
Individual rewriting rules contain lists of parameters "(....)", which name the slots representing subtrees, the left side acting as a template for matching, and the right side acting as a replacement, as is commonly cited in metaquotes ", which separates the text rewrite text language from text in the language of the specified language (for example, JavaScript). In meta-images that indicate a special element of the rewrite rule language, a lot of meta-escapes ** are found. Usually these are parameter names and represent regardless of the type of name tree, which represents a parameter, or represents a call to an external meta-procedure (for example, first_arg; you will notice that its argument list (,) is metacotated!) or, finally, a “tag”, such as “replace”, which is a kind of tree view, which represents the future intention to make more transformations.
This specific set of rules works by replacing a call to a candidate function with a based version with the additional intention of “replacing” to convert the list. The other two transformations realize the intention, transforming the “replacement” by processing the list items one at a time, and then pushing the replacement further down the list until it finally falls from the end and the replacement is completed. (This is the transformational equivalent of a cycle).
Your specific example may vary somewhat, as you really were not accurate in the details.
By applying these rules to modify the parsing tree, the DMS can then trivially print the result (the default behavior in some configurations is “parse AST, apply rules before exhaustion, prettyprint AST,” because it's convenient).
You can see the complete process of "define language", "define rewriting rules", "apply rules and fingerprint" in (High School) Algebra as a DMS domain .
Other software conversion systems include TXL and Stratego . We present DMS as an industrial version in which we built all this infrastructure, including many standard language parsers and prettyprinters .