[stringtemplate-interest] How best to do recursive hetero template calls?
Gerald Rosenberg
gerald at certiv.net
Tue Feb 16 14:49:16 PST 2010
Hi Ter,
Yes, slippery slope and all that. Quite agree with the argument against
model view entanglement -- actually the argument is mostly against
putting combinatorial logic in the view to evaluate model aspects.
To explain a bit, the attribute being provided to the view is not some
fancy data structure, but a simple, literal representation of the view
to be generated. Nested templates are a direct map to the view - each
node's type field is the literal name of the template to be applied to
the node. A two-dimensional walk in ST (no exceptions, no conditionals)
should be functionally little different from a one-dimensional ST list
iteration. So, this narrow use of recursion should not be too much of an
entanglement violation, right?
Given the view representation is heavily nested, if I instead use
external walker I wind up having to not just choose node templates, but
also have to provide the decision logic to stitch together parent entry
and exit templates for each node. And, manage the correspondence
between the walker logic and the model and all of the view templates.
Doable, but seems clumsy.
Anyway, I have started to experiment using ST4 and, so far, looks
promising.
Purely functional hetero template calls work nicely!
<(first(element.children).type)(...)>
Only issue found is that STGroup#getInstanceOf(String name) needs a null
check on name - the rest operator can return a null list and that can
result in an NPE when rest is used to provide an attribute to another
template.
Thanks,
Gerald
On 2/16/2010 10:02 AM, Terence Parr wrote:
> 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