[stringtemplate-interest] Anonymous template inside another anonymous template

Terence Parr parrt at cs.usfca.edu
Thu Dec 2 17:50:25 PST 2010


ok, got it.  So, your model should walk the tree or whatever you get for the region input.  It creates a template which is a list of the flattened elements.  The trick is the model not the templates compute the path from root to element. I do this for tree of imported grammars in antlr. Then, the templates are braindead simple:

region(path, i) ::= << region <path; separator="_">_r<i> >>
state(path) ::= << region <path; separator="_">_s >>

I'm presuming that the model computes the path as a list of names. 

Ter

On Dec 2, 2010, at 5:06 PM, Morten Olav Hansen wrote:

> Hi! Yes, of course.
> 
> I'm extending Java 1.5 with UML State Machine support, and is using
> the ANTLR3 TokenRewriteStream to rewrite the source from my extended
> version, to a version of Java that is compilable. I'm doing this, so
> that I can only rewrite the parts I need, and just grab the text of
> the rest.
> 
> So my input is usually like this:
> 
> 
> class ABC {
> 	region {
> 		state s {
> 			region {
> 				state s { regions, etc }
> 			}
> 
> 			region {
> 				state s { regions, etc }
> 			}
> 	}
> 
>        /* normal javacode below here */
> }
> 
> And I want it flattened to something like this (very simplified, the
> names are the important part here)
> 
> class ABC {
> 	private _construct_sm() {
> 		region r0;
> 		state r0_s;
> 		region r0_s_r0;
> 		state r0_s_r0_s;
> 		region r0_s_r1;
> 		state r0_s_r1_s;
> 	}
> 
>        /* more javacode below */
> }
> 
> I was using a prefix argument to create the names of the variables,
> but I'm facing the problem outlined before.
> 
> The general setup of my templates are like this:
> KEYWORD_builder(keyword, prefix) ::= <<
> generate text for template here, and if calling another template, add
> something to the prefix.
>>> 
> 
> I can solve this changing prefix to keyword_prefix, but this quickly
> stops working because of recursion (when the same template is called
> several times).
> 
> Regards,
> Morten
> 
> On Fri, Dec 3, 2010 at 1:33 AM, Terence Parr <parrt at cs.usfca.edu> wrote:
>> Howdy.  Can you explain more at the high level of what you're trying to do?  are you passing down a recursion chain a bigger and bigger arg?
>> 
>> I'm finding now that when I want recursion, i'm walking the model with the template; less good.  I recurse or traverse my model and build a tree of embedded templates rather than having the template invoke itself.
>> 
>> Ter
>> On Dec 2, 2010, at 2:52 PM, Morten Olav Hansen wrote:
>> 
>>> After more digging around, it seems that the problem is that i have
>>> recursive templates with the same arguments.
>>> 
>>> Something like this fails:
>>> a(prefix) ::= <<
>>> <b(prefix={added_<prefix>}>
>>>>> 
>>> 
>>> b(prefix) ::= <<
>>> <prefix> ...
>>>>> 
>>> 
>>> but using b_prefix instead works fine.
>>> 
>>> The problem I'm facing is that I have recursion, and I can't create a
>>> template for every possible level.
>>> 
>>> Any suggestions on how to fix this?
>>> 
>>> Regards,
>>> Morten
>>> 
>>> On Thu, Nov 25, 2010 at 9:44 PM, Morten Olav Hansen <mortenoh at gmail.com> wrote:
>>>> Hi
>>>> 
>>>> Is it possible to have an anonymous template inside another template
>>>> somehow? I'm trying to generate an argument to a template (which is
>>>> called from an anonymous template).
>>>> 
>>>> I have something like this:
>>>> 
>>>> <r.states: {s|<state_builder(state=s, region_ns={r<region_n>})>}>
>>>> 
>>>> But i get an exception:
>>>> Exception in thread "main" java.lang.IllegalArgumentException: cannot
>>>> embed template <anonymous template argument> in itself
>>>> 
>>>> I'm just passing region_ns=region_n now and adding the r on the
>>>> template side (and this works), but this argument will potentially
>>>> have several arguments concatenated together.
>>>> 
>>>> 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