[stringtemplate-interest] Fun with ST4

Collin Fagan collin.fagan at gmail.com
Sun Jan 16 12:54:13 PST 2011


ST4 has been working well for me. My most complicated template files worked
without a hitch, I guess I never used any of the shortcuts that got axed.
I've been doing some experimenting with the model adapters and here is my
feedback.

*Task 1: ST + Doclet API*
Status: Success, but ...

The 'but' involves the fact that the names of the the methods in the Doclet
API are unadorned like 'classes()' and 'methods()' etc. That left me one
choice, and I know you aren't going to like it. I had to just lookup the
method reflectively. While this makes working with the API really easy... it
could also be used to invoke any method which is bad. :(

*Task 2: ST + XML/XPath*
Status: Almost a Success

So I thought it would be interesting to try to build a model adapter that
takes an XPath expression as a "property". The expression is then used to
get the right data our of the XML document.

main(doc) ::= <<
     I found the books: <doc./bookstore/book/title; separator=",">
>>

Given a document like this:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

    <book>
        <title lang="eng">Harry Potter</title>
        <price>29.99</price>
    </book>

    <book>
        <title lang="eng">Learning XML</title>
        <price>39.95</price>
    </book>

</bookstore>

I get this output:

t.stg 2:40: invalid character '/'
I found the books: Harry Potter,Learning XML

I'm guessing that you never anticipated a property 'name' containing
slashes. The funny thing is it only complains if the property starts with a
slash.

The template: <doc.bookstore/book/title; separator=","> works with no
invalid character output.

Other XPath syntax that contains @ or [] breaks ST which is unsurprising
since none of those are valid charactes for a Java identifier.

In renderers one can pass whatever symbols they like to the format argument.
I'm guessing this is because they are surrounded by quotes. Would it be
reasonabale to ask for some optional delimiter on property names?

Something like: <doc."//bookstore/book/title"; separator=",">

or: <doc.{//bookstore/book/title}; separator=","> or something like that.

I hope this doesn't push the idea too far. I'm not really creating arbitrary
methods or logic.. ok you can add numbers in XPath, but that about it. It's
not a touring complete language at all.

*Task 3: *JSR 223 Bindings Proof Of Concept.
Status: Success

So JSR 223 is the scripting API for Java. This is a common framework that
allows Java objects to be exposed for consumption by JVM scripting
languages. JRuby, Groovy, JPython all have compatible bindings implemented.
Some other template engines (boooo, hisss) also provide bindings for JSR
223. Since it's so close to the ST API I thought I see what could be done.

While I don't have the Factory lookup stuff all in place the implementation
of the main interface "ScriptEngine" is pretty straight forward.

This allows me to execute the following code:

STScriptEngine engine = new STScriptEngine();
engine.put("attributeName", "world");
System.out.println(engine.eval("Hello <attributeName>"));

Mine is just a wrapper class that just delegates to ST. I'm sure a more
serious effort would result in code that pokes at the insides of ST.

Anyway ST4 has been great, really great.

Thanks,

Collin



On Sun, Jan 16, 2011 at 11:00 AM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Hiya. ST3 needs ANTLR3 as does ST4.  ANTLR3 needs ST3 so yes, they are
> mutually depend.  ANTLR v4 will need ST4 :)
> Ter
> On Jan 15, 2011, at 7:05 PM, Collin Fagan wrote:
>
> > Hi,
> >
> > I noticed that when trying to port some of my old blog entries from ST3
> to ST4 that ANTLR seems to include a copy of ST3. Is there a circular
> dependency between ST and ANTLR?
> >
> > Collin
> > _______________________________________________
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110116/6d5e42fc/attachment.html 


More information about the stringtemplate-interest mailing list