[stringtemplate-interest] File based templates and maps.
Caleb Lyness
caleb.lyness at ezswitch.net
Fri Oct 20 03:28:48 PDT 2006
John Snyders wrote:
> There are many characters that can't be used as a map key without enclosing
> in ().
> I don't think it matters if the map is declared in code or in the group
> file.
>
> It has to do with what is allowed after . in an attribute reference
> atom DOT ID is allowed where atom is ID, STRING, INT, or ANONYMOUS_TEMPLATE.
>
> So if your map key is "a:.b" or even "first" $map.a:.b$ and $map.first$
> won't work.
>
Ah, I see how this could be a problem. However this is still a bug or
caveat or whatever you want to
call it. Either the definition for map needs to be changed to be more
restrictive or the syntax of the
"atom dot id" needs to be changed....
See here:
map : '['
keyValuePair (',' keyValuePair)*
( ',' "default" ':' keyValue )?
']'
;
keyValuePair
: *STRING *':' keyValue
;
keyValue
: BIGSTRING
| STRING
| "key"
|
;
You will notice that the key can be defined as a string.
If an attribute reference stands as follows:
atom DOT ID
atom is ID, STRING, INT, or ANONYMOUS_TEMPLATE.
Then ID is not encompassing enough. The above should by the
same thread prevent an attribute reference to bean property
first.... e.g $something.first$. So the definition would be
limiting the expressiveness. If start does not do the trick then
the bean 1: i.e. .get1() .set1() can't be used. Ok, completely
contrived, but perfectly valid in the Java. (May not be considered
a valid bean though...)
Here is some code:
import org.antlr.stringtemplate.StringTemplate;
public class t {
static class arb {
private int _1 = 1;
private int first = 1;
String A = "A string";
public int get1() { return _1; }
public String getA() { return A; }
public int getFirst() {return first; }
}
public static void main(String[] args)
throws Exception
{
String templateDef = "$a.A$ $a.1$"; // This does not work
String templateDef1 = "$a.A$ $a.first$"; // This does not work
String templateDef2 = "$a.A$ $a.(1)$"; // This works
String templateDef3 = "$a.A$ $a.(first)$"; // This does not work
String templateDef4 = "$a.A$ $a.(\"first\")$"; // This works
String templateDef5 = "$a.A$ $a.(\"1\")$"; // This works
StringTemplate template = new StringTemplate(templateDef);
template.setAttribute("a", new arb());
System.out.println(template.toString());
}
}
Well there you have it. Draw your own conclusion. By using the ("id")
construct one can work around the problem completely. Not just for maps
but attributes too. I can understand why this work. But perhaps it would
be more convenient to allow
atom DOT atomID
where atomID is ID or STRING?
or fix the more general problem with .(first) and use the brackets as a way
to escape from built in identifiers?
Cheers
Caleb
> The thing I thought to myself when I first noticed this (I had a key called
> first) is why does ST need to know anything about integers? I don't recall
> seeing anything like $5600$ shown in any example in the documentation (this
> renders as 5600 btw). The only thing I could think of is that perhaps if
> there were a specific render for integers then you could have literal
> integers in the template get the desired formatting that matches the
> integers that come from the code. I never did try to see if that would work.
> If that is the reason then couldn't a case be made for literal floating
> point numbers and date literals as well?
>
> So I'm not sure this (integer keys) is a bug but I'm still confused as to
> why ST needs to parse integers at all.
>
I can't answer that. Its not really a question of integers, but rather
Strings looking
like integers. Perhaps Terence can answer the integer question you pose?
Cheers
Caleb
>> -----Original Message-----
>> From: stringtemplate-interest-bounces at antlr.org
>> [mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Terence
>> Parr
>> Sent: Thursday, October 19, 2006 7:14 PM
>> To: Caleb Lyness
>> Cc: StringTemplate; ANTLR Interest
>> Subject: Re: [stringtemplate-interest] File based templates and maps.
>>
>>
>> Wow! Interesting...seems a bug.
>>
>> added:
>>
>> http://www.antlr.org/wiki/display/ST/numbers+not+allowed+as+keys
>>
>> Ter
>> On Oct 19, 2006, at 4:05 PM, Caleb Lyness wrote:
>>
>>
>>> Terence Parr wrote:
>>>
>>>> try now...a spammer created 100 spam users. :(
>>>>
>>> Sorted.
>>> -----
>>>
>>> Here is another problem I ran into while playing with maps:
>>>
>>> If the map has a number in the key you can't reference it directly.
>>>
>>> E.g.
>>>
>>> The map is defined as:
>>>
>>> collectionFailure ::= [
>>> "1": "Revoked",
>>> "2": "Other",
>>> "A": "Settled",
>>> "B": "Frozen",
>>> default: "Unknown"
>>> ]
>>>
>>> If a tempate .st file refers to the map directly
>>>
>>> $collectionFailure.1$ - it fails (see below)
>>> $collectionFailure.A$ - works
>>>
>>> but if I use $collectionFailure.(reasonCode)$
>>> then Bobs your uncle it can be 1 or A...
>>>
>>> action parse error in group base templates line 0; template context
>>> is [bodies/example1]
>>> line 1:19: unexpected token: 1
>>> at org.antlr.stringtemplate.language.ActionParser.primaryExpr
>>> (ActionParser.java:703)
>>> at org.antlr.stringtemplate.language.ActionParser.expr
>>> (ActionParser.java:430)
>>> at org.antlr.stringtemplate.language.ActionParser.templatesExpr
>>> (ActionParser.java:212)
>>> at org.antlr.stringtemplate.language.ActionParser.action
>>> (ActionParser.java:126)
>>>
>>>
>>>> Ter
>>>>
>>>> On Oct 19, 2006, at 10:39 AM, Caleb Lyness wrote:
>>>>
>>>>
>>>>>>> I think it would be nice for future newbies if there was an
>>>>>>> example along side the file based template (.st) method in the
>>>>>>> source. Would speed the learning curve up a bit. cf: src\org
>>>>>>> \antlr\stringtemplate\test.
>>>>>>>
>>>>>> Add a quick tutorial :) http://www.antlr.org/wiki/display/ST/
>>>>>> Tutorials
>>>>>>
>>>>> Pitty, I cannot:
>>>>> I don't have a login and I can't create one.
>>>>>
>>>>> The following error(s) occurred:
>>>>> This installation of Confluence is not licensed to add any more
>>>>> users. Please contact the site administrators for more information.
>>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061020/a8d02975/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3269 bytes
Desc: S/MIME Cryptographic Signature
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061020/a8d02975/attachment.bin
More information about the stringtemplate-interest
mailing list