[stringtemplate-interest] How best to do recursive hetero template calls?
Terence Parr
parrt at cs.usfca.edu
Tue Feb 16 10:02:47 PST 2010
hi Gerald,
this brings up an important conclusion that I've come to. While we can get ST to do some fancy data structure iteration/walking using recursion and first/rest, I've decided that such walking really belongs in the model. In this case, I fear that the model and view have been entangled. While it's not always possible to completely isolate the view from the data structure, I try to limit it to access to properties of a single object rather than forcing ST to do the walking.
So, translation is really about walking your input model in creating an output model. In this case, you have some kind of Walker for Element and have it request templates for various kinds of elements. Then, you stitch them together into an overall output model. In this case, you get a hierarchy of templates. then, just render it to text and you have your output.
I believe I went into this a little bit in the translation section of the new language implementation patterns book. you might take a peek back at that chapter; it's probably been a while since you reviewed it
Ter
On Feb 15, 2010, at 10:40 AM, Gerald Rosenberg wrote:
> Looking for the best/proper way to do a recursive heterogeneous template
> expansion.
>
> The initial attribute is effectively defined as:
>
> public class Element {
> public String type; // can be "TypeA" or "TypeB"
> public Element[ ] children;
> . . .
> }
>
> The actual implementation is a fair bit more complex, so before I go too
> far down the wrong path, can anyone please comment on this as being a
> workable approach. Something better? Alternates? Gotchas(how to
> handle first == rest)?
>
> Thanks...
>
> /////////////////////////////////////////////////////////////////////////////
> Main(rootElement) ::= <<
> <(rootElement.type)(first(rootElement.children),
> rest(rootElement.children))>
> }
>>>
>
> TypeA(element, remainder) ::= <<
> BEGIN-A
> <(first(remainder.children).type)(first(remainder.children),
> rest(remainder.children))>
> END-A
>>>
>
> TypeB(element, remainder) ::= <<
> BEGIN-B
> <(first(remainder.children).type)(first(remainder.children),
> rest(remainder.children))>
> END-B
>>>
> /////////////////////////////////////////////////////////////////////////////
>
>
>
> _______________________________________________
> 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