Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

No Format
prog : ^(PROGRAM (d+=decl)+) -> file(decls={$d}) ;
decl : ^(DECL type ID) ; // auto creates template from input tokens for decl

What about when a referenced rule returns a template? That output must be included rather than the original input associated with the subtree matched by that rule reference.

No Format

decl : ^(DECL type ID) ;
type : 'int' -> float(...) ;

The automatically create a template for decl cannot be the original input matched for that declaration. We have to build up the output template piecemeal again just like in the parser. The order of the elements will be the order as they are encountered in the tree so if you built a tree that had type as last instead of first child, the output would change. Here, the output would be whatever whitespace appeared before the first token associated with the subtree matched by type, followed by the template returned by type, followed by the whitespace in front of the ID followed by the text of the ID node. If this is not what you want, then you must specify what template to create. I am just trying to do something that will work in the common case.

The mechanism should also create templates for alternatives that do not have template specifications even when others do:

No Format

e : ^('+' e e) // auto create template
  | ^('*' e e)
  | INT -> intval(...)
  | ID  -> load(...)
  ;

A warning

Each tree grammar rule knows the text from which the associated subtree was created but only if the subtree has a single root. The following rule, because it has a single root, gives ANTLR a problem.

...