[stringtemplate-interest] ST v4 with ANTLR 3.3

Michael Bedward michael.bedward at gmail.com
Mon Jun 13 20:39:22 PDT 2011


Hi Alejandro,

I was hoping someone else would reply to your question with an easier
suggestion than my own, but since they haven't...

Using ST4 in an ANTLR 3.3. grammar, you don't have the normal string
template syntax available. Instead you have to do everything
"manually".  The way that I've done this is to have a superclass which
is responsible for loading the template group file...

// group is a field in the superclass
group = new STGroupFile( path );

The superclass also provides a helper method to get individual templates...

    protected ST getTemplate(String name) {
        ST st = group.getInstanceOf(name);
        if (st == null) {
            throw new RuntimeException("template not found: " + name);
        }
        return st;
    }

Then in my grammar I use that helper method and have each rule return
an ST object, as in this example...

assignmentExpression returns [ST st]
                : ^(op=assignmentOp id=assignableVar e=expression)
                {
                    $st = getTemplate("binaryexpr");
                    $st.add("lhs", $id.st);
                    $st.add("op", $op.st);
                    $st.add("rhs", $e.st);
                }
                ;

Of course, when ANTLR catches up with ST4 this will be unnecessary.

Hope this helps.

Michael



On 8 June 2011 01:58,  <alejandro.osso at integraware.com.br> wrote:
> Good morning to all!
>
> I would like to use ST v4 along with an ANTLR 3.3 tree grammar.  Can I do
> this?   ANTLR complains about the setTemplateLib(group) for STv4 groups.
>
>
> Kind regards,
>
> Alejandro Osso.
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>
>


More information about the stringtemplate-interest mailing list