[stringtemplate-interest] Problems with counting using i/i0
Morten Olav Hansen
mortenoh at gmail.com
Sun Nov 14 06:40:40 PST 2010
Do you mean a parameter to value_n? theres was a typo in what i wrote,
value_n should have a parameter called n. This is a somewhat
simplified version of the program i'm trying. Maybe it helps showing
the grammar and template:
Grammar (added to java 1.5):
regionDecl
: 'region' '{' (states+=stateDecl|transitions+=transitionDecl)* '}'
-> region(states={$states}, transitions={$transitions})
;
stateDecl
: 'state' Identifier ';'
-> state(id={$Identifier.text})
;
transitionDecl
: 'transition' '(' from=Identifier ',' to=Identifier ')' ';'
-> transition(from={$from.text}, to={$to.text})
;
Template:
group StateBuilder;
statemachine(regions) ::= <<
<regions: region_n(); separator="\n\n">
>>
region_n() ::= <<
Region region<i0>;
<it.states: state_n(i0); separator="\n">
>>
state_n(rid) ::= <<
State r<rid>_<i0>;
>>
region(states, transitions) ::= << >>
state(id) ::= << >>
transition(from, to) ::= << >>
So given:
public class XYZ {
region {
state x;
}
region {
state y;
}
}
It outputs:
public class XYZ {
Region region0;
State r0_0;
Region region1;
State r0_0;
}
But I want the last state to be r1_0, which I'm having trouble with.
--
Morten
On Sat, Nov 13, 2010 at 10:15 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
> Can you set a parameter to data_n? Using the default i0 is a little unconventional. If you name the parameter, then there is no ambiguity about what i0 means .
> Ter
> On Nov 9, 2010, at 8:35 AM, Morten Olav Hansen wrote:
>
>> Hi
>>
>> I'm having a problem with using a counter in my stringtemplate (also
>> using ANTLR3).
>>
>> In my StringTemplate I have:
>>
>> datavalues(datas) ::= <<
>> <datas: data_n()>
>>>>
>>
>> data_n() ::= <<
>> Data <i0>;
>> <it.values: value_n(i0)>
>>>>
>>
>> value_n() ::= <<
>> Value <n>_<i0>;
>>>>
>>
>> data(values) ::= << >>
>> value() ::= << >>
>>
>> data and value has corresponding -> data/value template rewrites in
>> the antlr-grammar.
>>
>> This does not work, since it seems the i0 parameter to value_n is
>> evaluated in value_n, so <i0> == <n>, I'm looking for something like:
>>
>> Data 0;
>> Value 0_0;
>> Value 0_1;
>>
>> Data 1;
>> Value 1_0;
>> Value 1_1;
>>
>> Is there something I can do about this behavior ? or are there any
>> other ways of accomplishing this? any ideas would be appreciated.
>>
>> Regards,
>> Morten
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>
More information about the stringtemplate-interest
mailing list