[stringtemplate-interest] Suggestions: Map parameters and Template-identity checks
Terence Parr
parrt at cs.usfca.edu
Fri Dec 4 11:34:25 PST 2009
On Dec 2, 2009, at 7:09 PM, Swapnil Kashikar wrote:
> Hi Terence,
>
> I have two suggestions for the STv4, the lack of which requires some very complex workarounds in our code:
>
> 1. Map Parameters
> Maps allow arbitrary templates in their 'value' expressions. However, since maps only take one parameter, the key, all these 'value' templates have to resort to accessing 'outside' values through dynamic-scoping only.
an interesting point
> That in turn means each use of such map anywhere in group file must have the exact names of the attributes referenced in the 'value' expressions of the map in scope.
yes, I encountered that problem when writing an example for the book. it's very ugly.
> It is not trivial to do this and we have to resort to tricks to do this. For ex, using parallel iteration to rename attributes to those used in map-value-attributes (Refer my post about parallel iteration syntax a few months ago). But this makes our code very complex and introduces bugs that are tough to figure out.
>
> To solve this I would suggest that maps be allowed to take more than one parameter. The first one can be designated as the 'key' and the rest can function like the regular parameters of a named-template.
Hmm...I think the easiest way to handle this is to use the default parameter. I should have done this in the book. basically let's say we have a map that looks like this ( or dictionary as I'm calling it now):
m ::= [ "foo" : {*<it>*} ]
Then we can access that with <m.foo> but a how do we set "it"? perhaps, <"dog":m.foo> ???
> 2. Template-identity checks
> We frequently create heterogeneous lists of templates and then render them. For ex, a list of variable declarations represented by named templates scalar() and complex(). We use this list to generate an initializer list and to generate a variable declarations part of code by applying two named templates to this list: genInits() and genDecls(). Further,lets say, genInits() needs to skip over complex vars in the list while genDecls() will render all the vars in the list. How does genInits() skip over the complex-vars? Today, we add dummy attributes 'isScalar' to *both* scalar() and complex() templates. Then, while creating the template instance for the variables, set the proper attribute depending on whether the variable is scalar or complex. Then in genInits() we write code like: <if(var.isScalar)><var>:=0;<endif>
>
> It would be much simpler if we could check the identity of the templates directly using their actual names without having to declare dummy identity-attributes. Something like:
> <if(var.instanceOf(scalar))><var>:=0;<endif>
I recently encountered this exact problem when generating code in an example for the book. What I discovered ultimately is that this is data filtering, which is really the purview of the model, not the view. Hence, I sent in filtered lists, one for scalars and one for objects. In fact, I think it was the exact same problem as you have :)
> Looking forward to STv4. :)
it's almost ready!
Ter
More information about the stringtemplate-interest
mailing list