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

Darach Ennis darach at gmail.com
Mon Mar 3 00:42:53 PST 2008


Hi Terence,

I could use your suggested fix as an optimization for for no-arg and
empty-arg parameters.
I'll have to suck it and see for macro's with parameters which would imply
an ST template
of the form:
$data0$$arg0$$data1$$arg0$$data2$$arg1$$data3$$arg0$$arg1$$data4$.

However, your suggested fix has the advantage of full control over both
attribute names
and of the ST template text itself. This level of indirection might prove
useful.

Thanks!

Darach.

On Sun, Mar 2, 2008 at 9:44 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080303/c4a01d18/attachment.html 


More information about the stringtemplate-interest mailing list