[stringtemplate-interest] Programatically escape arbitrary text as a stringtemplate template?

Darach Ennis darach at gmail.com
Mon Feb 25 16:19:15 PST 2008


Hi guys,

I would like to escape some arbitrary text as a string template template.
Specifically
the template text is macro text from an ANTLR grammar but this question is
purely
a StringTemplate question/request. Given input of the form:

$A $B $C c:\foo\bar\baz <> <> <>

It is not obvious how to escape arbitrary text such that when output from a
StringTemplate
template it will match the input source. I am generating the string
programatically so I 'know'
when $ is literal and when $...$ represents an intentional attribute
reference when I generate
the input string. Here is what I came up with viza vis a testcase:

        final String TEST_INPUT = "$A $B $C c:\\foo\\bar\\baz <> <>"; //
double slash in a literal string represents a literal slash
        System.out.println("raw: " + TEST_INPUT);
        StringTemplate st = new StringTemplate(TEST_INPUT);
        System.out.println("rst: " + st.toString());
        final String escaped =
TEST_INPUT.replace("\\","\\\\").replace("$","\\$");
        System.out.println("esc: " + escaped);
        StringTemplate st2 = new StringTemplate(escaped);
        System.out.println("???: " + st2.toString());

White outputs the following

raw: $A $B $C c:\foo\bar\baz <> <>
problem parsing template 'anonymous'
line 1:30: expecting '$', found '<EOF>'
    at org.antlr.stringtemplate.language.DefaultTemplateLexer.nextToken(
DefaultTemplateLexer.java:149)
    at antlr.TokenBuffer.fill(TokenBuffer.java:69)
    at antlr.TokenBuffer.LA(TokenBuffer.java:80)
    at antlr.LLkParser.LA(LLkParser.java:52)
    at org.antlr.stringtemplate.language.TemplateParser.template(
TemplateParser.java:103)
    at org.antlr.stringtemplate.StringTemplate.breakTemplateIntoChunks(
StringTemplate.java:853)
    at org.antlr.stringtemplate.StringTemplate.setTemplate(
StringTemplate.java:455)
    at org.antlr.stringtemplate.StringTemplate.<init>(StringTemplate.java
:311)
    at org.antlr.stringtemplate.StringTemplate.<init>(StringTemplate.java
:296)
    at spike.Test.main(Test.java:9)
rst: B
esc: \$A \$B \$C c:\\foo\\bar\\baz <> <>
???: $A $B $C c:\foo\bar\baz <> <>

Are there any other possible gotchas, such as 'unfortunate' attribute names?
Is there a recommended
strategy for escaping attribute names?

It would be worthwhile adding an encode utility method to the StringTemplate
API as the inadvertent
use of java's String.replaceAll now changes the meaning of 'slash' and '$'
as the first argument is a
regular expression. So string now means 'end of line' as well under certain
conditions... At the least
this should be documented in the wiki somewhere.

Regards,

Darach.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080226/8a9d677b/attachment.html 


More information about the stringtemplate-interest mailing list