[antlr-interest] StringTemplate enhancement question
Anthony Youngman
Anthony.Youngman at ECA-International.com
Wed Jun 23 06:19:26 PDT 2004
IF your stuff is just an array, look at RobC's MaVerick compiler and see
if you want to "borrow" some of his "dynamic array" code.
It wouldn't be that fast, because it would store everything in one big
string, unless you optimised it ...
It wouldn't suprise me in the slightest if I've completely misunderstood
what you're trying to do, but if "storing n-dimensional arrays in a hash
table" sounds like it would solve your problem, then talk to Rob and me.
Where n can change dynamically to be whatever value just happens to be
convenient at that moment in time :-) And there's a good chance Monty
will chime in and say "Oh I get it", too :-)
Cheers,
Wol
-----Original Message-----
From: Terence Parr [mailto:parrt at cs.usfca.edu]
Sent: 22 June 2004 18:48
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] StringTemplate enhancement question
To anyone playing with StringTemplate...
First, good news. The use of StringTemplate 2.0b1 in my ANTLR 3.0 code
generator is a truly a joy.
Next, I have a problem. Recall that the distinguishing characteristic
of ST is that it strictly enforces separation of model and view; that
is, your view literally cannot become part of the program. This
manifests itself in, for example, the ANTLR code generator as a
guarantee that the CodeGenerator object is truly language-independent
and consequently ANTLR should be easily retargetable. Literally every
character that goes out to the generated Java file comes from a
template; no literals in the CodeGenerator. :)
Ok, on to my desired enhancement...
I often have lists of things that need to be formatted, but the list
items are actually pieces of data that are not already in an object. I
need ST to do something like:
Ter=3432
Tom=32234
....
using template:
$items:{$attr.name$=$attr.type$}$
This example will call getName() on the objects in items attribute, but
what if they aren't objects? I have perhaps two parallel arrays
instead of a single array of objects containing two fields. One
solution is allow Maps to be handled like properties so that attr.name
would fail getName() but then see that it's a Map and do
attr.get("name") instead.
This very clean approach is espoused by some, but the problem is that
it's a hole in my separation rules. People can put the logic in the
view because you could say: "go get bob's data" in the view:
Bob's Phone: $db.bob.phone$
A view should not be part of the program and hence should never be able
to go ask for a specific person's data. Humor me that it's good to be
ruthless (or read my paper <snicker>). Is there another way to fake
aggregates by given separate pieces of data to ST and have it
aggregate?
Well, first I tried passing in
new Object() {
public String getName() {return "Ter";}
public String getType() {return "234";}
}
But it complains that the damn anonymous class is not public :(
Bastards! Those things suck.
So, what about something like:
st.setAttribute("items.name", "Ter");
st.setAttribute("items.type", "342");
But that isn't quite it. How does ST know when to make a new items
object in the list versus setting a prop within the last item added or
whatever. Or, what about a new method:
st.setupAttribute("items.name", "Ter");
st.setupAttribute("items.type", "342");
st.nextAttribute();
er something?
Seems like the various pieces could go in together like this:
st.setAttribute("tokens.name,type", new Object[] {"Ter","342"});
which is weird, but would work. I'd have to parse the attribute name a
bit and pull apart stuff to make a little hashtable out of name/type.
I suppose I could pass two arrays: one for the names and one for the
values. Ick. Could do this I suppose:
st.setAttribute("tokens", new Object[]
{"name","Ter","type","342"});
Just intersperse the keys with the values. gross ;)
A final option. Believe it or not, due to the wacky init {...} syntax
for anon inner classes, this would work too:
st.setAttribute("tokens", new HashMap() {{put("name","Ter");
put("type","342");}});
The double {{ }} would be required. That is, the following code
HashMap m = new HashMap() {{put("name","Ter"); put("type","342");}};
System.out.println(m);
prints
{type=342, name=Ter}
Again, though, how do I prevent people from passing in a regular
HashMap that is actually just a proxy for their DB? Then the view
could do DB accesses! Oh, I could make a special object that worked
like a HashMap, but which wouldn't allow subclasses so people couldn't
sneak in a modified version that was actually a database.
Which would you choose? Anybody have some insight?
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!
Cofounder, http://www.peerscope.com pure link sharing
Yahoo! Groups Links
****************************************************************************
This transmission is intended for the named recipient only. It may contain private and confidential information. If this has come to you in error you must not act on anything disclosed in it, nor must you copy it, modify it, disseminate it in any way, or show it to anyone. Please e-mail the sender to inform us of the transmission error or telephone ECA International immediately and delete the e-mail from your information system.
Telephone numbers for ECA International offices are: Sydney +61 (0)2 8272 5300, Hong Kong + 852 2121 2388, London +44 (0)20 7351 5000 and New York +1 212 582 2333.
****************************************************************************
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list