[stringtemplate-interest] [ST4] How to avoid map key iteration?
Terence Parr
parrt at cs.usfca.edu
Tue Feb 15 14:50:28 PST 2011
Everything we add to the language has a cost, which includes new syntax and semantics.
To me, it's simple--the ':' operator means apply a template or templates across a value or multiple values. The left-hand side of ':' is always serialized. If you do not wish it to be serialized, don't use the ':' on that object. You are basically trying to use it to simply get a new name:
<amap:{x | <x.field>}>
but you should simply use this instead:
<amap.field>
I explored the notion of no () on template mapping; as you point out we should be passing the template name not making a template instantiation. It turns out I decided I like highlighting the fact that it was a template application.
I've generated a lot of structured text with ST over the years from websites to bytecodes to Java. I've never found this to be an issue; at least from my perspective, there's nothing to fix.
Ter
On Feb 14, 2011, at 8:01 AM, Udo Borkowski wrote:
> Hi Sam,
>
> your suggestion of using a syntax like "line2(adr)" to invoke an anonymous template goes exactly in a direction I was thinking for a while now:
>
> (Start of dreams of the future)
>
> The "Unified ST4 template" syntax
>
> Currently we defined "normal" templates like
>
> t1(p1,p2) ::= <<…>>
>
> or
>
> t1(p1,p2) ::= "…"
>
> Anonymous templates (or sub templates) are defines like
>
> {p1,p2| …}.
>
>
> What about defining a "normal" template like this:
>
> t1 = {p1,p2| …}
>
> I.e. a "normal" template is just an anonymous template with a name, i.e. the template can be referenced through this name.
>
> To invoke a template use the same syntax as before:
>
> <t1(x,y)>
>
> However one can also just *reference* a template (without invoking it), just by naming it without the (...).
>
> The "iterate" feature would work on a (named or anonymous) template (as now), but would get a slightly different syntax for named templates:
>
> <x,y:t1>
>
> I.e. no "t1()" as with a template invocation, but just "t1" to reference the template. This perfectly fits into the "<x,y:{…}>" syntax: here the "{…}" is not written as "{…}()", too.
>
> This changed syntax also makes the difference between a normal template call:
>
> <t2()>
>
> and the iterate feature:
>
> <x,y:t1>
>
> a little bit more obvious. (Another way to see this: the () behind the template name in an iterate feature serve no real purpose, they will always be empty).
>
> To come back to my initial question "how to avoid map key iteration" I could imagine an extension to the "iterate" syntax:
>
> Either
>
> <x*,y:t1>
>
> Meaning: only the x parameter is used as an iterator, the other parameters are just passed to the template unchanged.
>
> Or
>
> <x,y!:t1>
>
> Meaning: every parameter not marked with "!" is used as an iterator.
>
> ( '*' and '!' are just used as examples. Use your favorite syntax here to mark a parameter either as "iterator" or as "pure value").
>
>
> (End of dreams of the future)
>
>
> Udo
>
>
> On 14.02.2011, at 15:35, Sam Harwell wrote:
>
>> Hi Udo,
>>
>> The problem is the operation in use when you write it the way you originally did – the ‘:’ is a map operation as opposed to a pass to a function method. In theory, the following syntax could solve the problem:
>>
>> t2(adr,line2={x|<x.zip> <x.city>}) ::= <<
>> <adr.firstname> <adr.lastname>
>> <line2(adr)>
>> >>
>>
>> I know this syntax doesn’t work in ST3, and I’m guessing it doesn’t in ST4 either. Considering the way parameters and local variables work in many languages, I don’t think allowing this would be unexpected.
>>
>> Sam
>>
>> From: Udo Borkowski [mailto:ub at abego-software.de]
>> Sent: Monday, February 14, 2011 3:21 AM
>> To: Sam Harwell
>> Cc: stringtemplate-interest List
>> Subject: Re: [stringtemplate-interest] [ST4] How to avoid map key iteration?
>>
>> Hi Sam,
>>
>> If you write it this way:
>> ...
>> <t2(adr=p,line2={<adr.city>, <adr.zip>})>
>>
>> Good point.
>>
>> I guess it is just my personal style (and background) that makes me raise the issue. Inside a subtemplate I'd prefer to access the parameters of the subtemplate (the {p1,p2|…}) over the access of something outside of the subtemplate, especially with the dynamic name lookup of ST4 in mind.
>>
>>
>> Udo
>>
>
> _______________________________________________
> 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