[stringtemplate-interest] "rest" and template recursion

Terence Parr parrt at cs.usfca.edu
Fri May 30 22:28:37 PDT 2008


Yeah, I use an iterator rather than making a copy of the list, which  
of course causes trouble with recursion.  rest() returns something  
with a side-effect...seems you're right...perhaps we should make it  
copy the list (well, minus first element).

Anybody vote yes or no?

Ter
On May 30, 2008, at 8:22 PM, Sasha Crosland wrote:

> Hi everyone,
>
> I had been having difficulty getting template recursion to work as I  
> had hoped it would, and when I searched briefly for anything  
> addressing this topic in the list archives, I found that others had  
> apparently had a similar problem to mine.  From what I gathered, the  
> problem is not to do with recursion itself, but with use of the  
> "rest" operator on a multi-valued attribute, which I have been  
> trying to use in the implementation of my recursive template.   
> Something to do with "rest" returning an iterator instead of a list?
>
> What I want to do is to generate the names of Java types such as  
> "Fn<P1, Fn<P2, Fn<P3, R>>>," which I am using as a "curried"  
> representation of a three-argument function with type P1 x P2 x P3 - 
> > R.  I tried to define a template called curriedFnType taking a  
> single attribute called arity, which is a list of integers such that  
> arity[i] = i for i from 1 to length(arity).
>
> curriedFnType(arity) ::= <<
> <if(arity)>
> Fn\<P<first(arity)>, <curriedFnType(arity=rest(arity))>\>
> <else>
> R
> <endif>
> >>
>
> The problem is that for an arity of four ([1,2,3,4]), for example,  
> the template is instantiated to "Fn<P1, Fn<P2, Fn<P4, R>>>", which  
> somehow skips over P3.  I encounter similar quirkiness with whatever  
> sort of recursive template I try to implement in this manner.  And I  
> don't think that using the colon operator to apply a template to the  
> multi-valued arity attribute will work directly, will it?, since we  
> aren't dealing with tail-recursion.  The alternative
>
> curriedFnType2(arity) ::= "<arity:{Fn\<P<it>, }>R<arity:{\>}>
>
> seems to work, but, to be honest, it feels rather lame that I should  
> have to resort to this.  Can anybody shed any light on the issue?   
> Is this indeed a bug, or am I overlooking something?  I am using  
> StringTemplate 3.1.
>
>
> Sasha
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest



More information about the stringtemplate-interest mailing list