[stringtemplate-interest] Fun with ST4
Terence Parr
parrt at cs.usfca.edu
Sat Jan 22 11:29:06 PST 2011
On Jan 16, 2011, at 12:54 PM, Collin Fagan wrote:
> ST4 has been working well for me. My most complicated template files worked without a hitch,
hooray!
> 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. :(
yep. Can the new model adapter thing work for you?
> 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=",">
> >>
>
> I get this output:
>
> t.stg 2:40: invalid character '/'
> I found the books: Harry Potter,Learning XML
interesting. yeah, technically it shouldn't allow subdirectory names but I think I was allowing you to look up templates as keys of maps or something.
>
> 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.
ha!
> The template: <doc.bookstore/book/title; separator=","> works with no invalid character output.
hmm... should we make this illegal war added to the documentation is a feature? ;)
> 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.
<doc.(key)> uses key as a variable that holds the name of the field. would that work?
> 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.
Check out the new feature, format, that I added; top of this page
http://www.antlr.org/wiki/display/ST/Differences+between+v3+and+v4
> Anyway ST4 has been great, really great.
>
hooray!
Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110122/ed280a5e/attachment.html
More information about the stringtemplate-interest
mailing list