[stringtemplate-interest] Suggestions: Map parameters and Template-identity checks

Swapnil Kashikar swapnil.kashikar at gmail.com
Wed Dec 2 19:09:05 PST 2009


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.
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. 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.

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>

Looking forward to STv4. :)

Regards,
Swapnil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20091202/c9313cd4/attachment.html 


More information about the stringtemplate-interest mailing list