[antlr-interest] Inline StringTemplates - sample?

Tom Smith yotommy at gmail.com
Sun Nov 18 16:55:03 PST 2007


I have a couple of days experience with stringtemplate, so I'll take a
go at this:

> public class Main {
>     public static void main(String[] args) throws Exception {
>         ANTLRInputStream input = new ANTLRInputStream(System.in);
>         st1Lexer lexer = new st1Lexer(input);
>         CommonTokenStream tokens = new CommonTokenStream(lexer);
>         st1Parser parser = new st1Parser(tokens);
>         parser.start();
>     }
> }
> ---
>
> Obviously I need to add something (currently it outputs nothing), but
> what? I do have stringtemplate-3.0.jar on the CLASSPATH.

You need to get the template output from the parser via code like:

st1Parser.start_return r = parser.start();
StringTemplate output = r.getTemplate();
System.out.println(output.toString());

See java code in section 9.6 of the book for an example test rig.

> Also, question 2: what is the correct way to express a rule outputting
> a stringtemplate that is composed of some literal text and its
> children's stringtemplates. For example, a "bold" rule needs to output
> <b>, followed by whatever its children output, followed by </b>. The
> examples mostly use child.$text which obviously isn't right, as it's
> not the child's raw text that I want to output, but their transformed
> text...

I think you are looking for $child.st instead of $child.text.

Hope this helps,
Tom.


More information about the antlr-interest mailing list