[stringtemplate-interest] mapping templates

Sam Harwell sharwell at pixelminegames.com
Thu Oct 8 14:59:08 PDT 2009


One possibility that allows easy identification of visible (in the
output) whitespace would be an altered anonymous template syntax. In all
of the following, [ and ] delimit the syntax terms (since I don't have
italics, etc) and are not themselves part of the syntax.

<[sequence]:[template]>

Where [sequence] is a sequence (iterable in Java) and [template] is a
template taking exactly one argument. The [template] can be an anonymous
template, which can be used when the target of the mapping doesn't match
the required template prototype (one argument).

<[sequence]:[anonymous-variable-name | ( anonymous-variable-name-list )]
=> [anonymous-template-body]>

sequence : identifier;
anonymous-variable-name : identifier;
anonymous-variable-name-list : anonymous-variable-name |
anonymous-variable-name-list , anonymous-variable-name;
anonymous-template-body : { template-text (preserves whitespace) }

For the bitsets example, you'd need to write it as either one of the
following. The interpreter treats both identically (the first is
shorthand for the second).

<bitsets:b =>
{bitset(name={FOLLOW_<b.name>_in_<b.inName><b.tokenIndex>},
words64=b.bits)}>

<bitsets:(b) =>
{bitset(name={FOLLOW_<b.name>_in_<b.inName><b.tokenIndex>},
words64=b.bits)}>

For the foo example, you could use any of the following, but the first
is preferred for performance reasons (fewer templates *and* less
nesting).

<foo:parameterScope>

<foo:it => {parameterScope(scope=it)}>

<foo:(it) => {parameterScope(scope=it)}>

------ New section of ideas starts here ------

You could also consider closure support:

bar(arg1, arg2, arg3) ::= "whatever"

All 4 equivalent:

foo(seq) ::= "<seq:bind(bar, arg1:3, arg2:2)>"
foo(seq) ::= "<seq:bind(bar, 3, 2)>"
foo(seq) ::= "<seq:it => {bar(3, 2, it)}>"
foo(seq) ::= "<seq:it => {bar(arg1=3, arg2=2, arg3=it)}>"

All 3 equivalent:

foo(seq) ::= "<seq:bind(bar, arg2:3, arg3:2)>"
foo(seq) ::= "<seq:it => {bar(it, 3, 2)}>"
foo(seq) ::= "<seq:it => {bar(it, arg2=3, arg3=2)}>"



-----Original Message-----
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of Terence
Parr
Sent: Thursday, October 08, 2009 3:32 PM
To: stringtemplate-interest Template
Subject: Re: [stringtemplate-interest] mapping templates

ooops, what about args that are part of the map? here's an ANTLR  
example usage:

<bitsets:bitset(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>},  
words64=it.bits)>

I want to set some args as well as the "it".

I do it again here:

<foo:parameterScope(scope=it)>

But, scope is only arg so it's same as:

<foo:parameterScope()>

or

<parameterScope(foo)>

So, anybody else use the args in map call?  I'm thinking it's  
confusing and we should remove.  E.g., in what context is "it"  
evaluated above?

I could rephrase as

<bitsets:{b | <bitset(name= 
{FOLLOW_<b.name>_in_<b.inName><it.tokenIndex>}, words64=b.bits)>}>

so we don't need I guess. Objections?

Ter
On Oct 7, 2009, at 12:28 PM, Terence Parr wrote:

> Hi, there is a syntax bugaboo in ST v3.
>
> <names:bold()> means apply bold to each element of names but t=foo()
> in an arg list or wherever means invoke foo and put ST into t.  Seems
> like foo should be a template name and foo() should be an invocation.
> That would imply we'd chnage to:
>
> <names:bold>
>
> Makes more sense if you ask me.  Other thoughts?
>
> Ter
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

_______________________________________________
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