[antlr-interest] [v3] stringtemplate rewrite with '?'
Diehl, Matthew J
matthew.j.diehl at intel.com
Tue Jun 12 16:33:51 PDT 2007
I am clueless on what to do, and have been struggling with this for a
while. If I have produced an AST like:
myTree
: ^(SIGNAL identifier_list expression?)
How can I output the 'expression' part without getting a null pointer
when it doesn't exist? For example:
myTree
: ^(SIGNAL identifier expression?)
-> vector(id={$identifier}, expr={$expression})
This produces the following JAVA code (or similar code):
retval.st = templateLib.getInstanceOf("vector",
new STAttrMap().put("id", input.getTokenStream().toString(
input.getTreeAdaptor().getTokenStartIndex(identifier.start),
input.getTreeAdaptor().getTokenStopIndex(identifier.start))
).put("expr", input.getTokenStream().toString(
input.getTreeAdaptor().getTokenStartIndex(expression.start),
input.getTreeAdaptor().getTokenStopIndex(expression.start))));
But, if expression is null it throws an error.
The only way around this that I can see is to do two different templates
(one if there is an expression, and one without), which is a pain,
because many parts of the grammar have 3-4 '?' in them (8-16 different
combinations)
That or I guess I could try and trick it into thinking it's a list maybe
(haven't tried):
myTree
: ^(SIGNAL identifier exp1+=expression?)
-> vector(id={$identifier}, expr={$exp1})
Thanks for any advice,
Matt
More information about the antlr-interest
mailing list