[antlr-interest] Need quick ANTLR/ST3 hack

Bill Andersen bill.andersen at mac.com
Wed Sep 14 11:25:50 PDT 2011


Folks

I want to write a template containing a conditional expansion for a multi-valued attribute that does one thing if the attribute is singleton and another if there are more than 1 element

For example, in an ANTLR grammar with ST output

rule
  : FOO (bars+=bar)+ -> foo(args={$bars})
  ;

where the template looks like

foo(args) ::= <<
<if(???)>
  something
<else>
  something else
<endif>
>>

I want some expression for ??? to test for args being singleton.

Is this possible in ST3?  Else I imagine I could write:

rule
  : FOO (bars+=bar)+ -> foo(args={$bars},singleton={$bars.size() == 1})
  ;

and

foo(args,singleton) ::= <<
<if(singleton)>
  something with args
<else>
  something else with args
<endif>
>>

... but I would really rather not.


More information about the antlr-interest mailing list