[stringtemplate-interest] Programatically escape arbitrary text as a stringtemplate template?
Terence Parr
parrt at cs.usfca.edu
Sun Mar 2 13:44:32 PST 2008
Hi Darach, I have come across the same issue. It's easy. make a
template that is simply $data$ and then pass in the template as
attribute data. Then ST cannot be interpreting the text at all.
Ter
On Feb 25, 2008, at 4:19 PM, Darach Ennis wrote:
> 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.
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list