Search code examples
antlrantlr3stringtemplate

Using $ as delimiter in StringTemplate from ANTRL rewriter grammars


I'm trying to write an ANTLR3 grammar that generates HTML output using StringTemplate. To avoid having to escape all the HTML tags in the template rules (e.g. \<p\><variable>\</p\>), I'd prefer to use dollar as the delimiter for StringTemplate (e.g. <p>$variable$</p>).

While the latter seems to be the default when StringTemplate is used on its own, the parser code generated by ANTRL always uses AngleBracketTemplateLexer when initializing StringTemplate.

How can I get ANTLR to generate code using DefaultTemplateLexer (i.e. the variant that uses dollar as the delimiter)?


Solution

  • Try setting the DefaultTemplateLexer.class in the StringTemplateGroup like this:

    StringTemplateGroup group = new StringTemplateGroup(reader, 
                                                        DefaultTemplateLexer.class);