[stringtemplate-interest] cannot embed template anonymous in itself

Terence Parr parrt at cs.usfca.edu
Tue Jan 26 12:22:05 PST 2010


hi. the error when you try to say set an st's enclosing instance to itself. nested/recursive templates is a design requirement so it's allowd but you can't create infinite enclosing cycles.  I can't be my own parent.
Ter
On Jan 26, 2010, at 11:58 AM, Stevenson, Todd (GE Healthcare, consultant) wrote:

> I don't understand the restrictions in StringTemplate that won't allow me to build a template that is recursive in nature.  The code where this is failing looks like this code.  It recursively builds a tree until there are no child nodes left.  However StringTemplate throws an exception not allowing this recursive method call. 
>  
> Any ideas?
>  
> public StringTemplate printTree() {
> 
>     StringTemplate st = new StringTemplate("$parent$\n $child;separator=\"\n\"$\n");
> 
> 
>     st.setAttribute("parent", parent.toST());
> 
> 
> // print all your child nodes
> 
>     MyNode curr = this.child;
> 
>     while (curr!= null) {
> 
>         st.setAttribute("child", curr.printTree());
> 
>         curr = curr.sibling;
> 
>     }
> 
> 
>     return st;
> 
> }
> 



More information about the stringtemplate-interest mailing list