[antlr-interest] [v3] stringtemplate rewrite with '?'
Diehl, Matthew J
matthew.j.diehl at intel.com
Tue Jun 12 17:33:06 PDT 2007
Okay, I think I found another way around it. First, I tried changing
the template:
myTree
: ^(SIGNAL identifier expression?)
-> template(id={$identifier.text}, expr={$expression.st})
"<id><if(expr)><expr><endif>
But that still threw a null pointer whether or not I was sending
$expression.st or $expression.text
So I changed it to:
myTree
: ^(SIGNAL identifier expr1=expression?)
-> template(id={$identifier.text},
expr={(expr1!=null?$expr1.st:"")})
"<id><if(expr)><expr><endif>
And now it works fine.
Thanks for your time and consideration,
Matt
-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Diehl, Matthew J
Sent: Tuesday, June 12, 2007 4:34 PM
To: ANTR Interest
Subject: [antlr-interest] [v3] stringtemplate rewrite with '?'
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