Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing issue when having template files including interpolated strings #102

Closed
etorreborre opened this issue Mar 15, 2013 · 23 comments · Fixed by #563
Closed

Parsing issue when having template files including interpolated strings #102

etorreborre opened this issue Mar 15, 2013 · 23 comments · Fixed by #563

Comments

@etorreborre
Copy link

Please try to g8 etorreborre/etorreborre/specs2-scala-2.10.g8.

You should have an error like:

action parse error; template context is [anonymous]
line 1:1: unexpected token: e1
    at org.antlr.stringtemplate.language.ActionParser.primaryExpr(ActionParser.java:813)
    at org.antlr.stringtemplate.language.ActionParser.expr(ActionParser.java:539)
    at org.antlr.stringtemplate.language.ActionParser.templatesExpr(ActionParser.java:222)
    at org.antlr.stringtemplate.language.ActionParser.action(ActionParser.java:126)
    at org.antlr.stringtemplate.StringTemplate.parseAction(StringTemplate.java:876)
    at org.antlr.stringtemplate.language.TemplateParser.action(TemplateParser.java:162)
    at org.antlr.stringtemplate.language.TemplateParser.template(TemplateParser.java:127)
    at org.antlr.stringtemplate.StringTemplate.breakTemplateIntoChunks(StringTemplate.java:850)
    at org.antlr.stringtemplate.StringTemplate.setTemplate(StringTemplate.java:441)
    at org.antlr.stringtemplate.StringTemplate.<init>(StringTemplate.java:308)
    at org.antlr.stringtemplate.StringTemplate.<init>(StringTemplate.java:293)
    at org.clapper.scalasti.adapter.ScalastiStringTemplate.<init>(ScalastiStringTemplate.java:57)
    at org.clapper.scalasti.StringTemplate.<init>(StringTemplate.scala:101)
...
@sebnozzi
Copy link
Contributor

sebnozzi commented Feb 3, 2014

I'm having the same issue with a file that includes a regular expression involving a "$".

I thought the $ could be escaped. Tried $ and $$ without success.

@sebnozzi
Copy link
Contributor

sebnozzi commented Feb 3, 2014

My lousy workaround, until this is fixed, was to include this in the code:

  // This is to work around a parsing issue in giter8 - replace inlined dollar signs
  val DOLLAR = (0x24).toChar

And then:

  When("""^I retrieve the result""" + DOLLAR) { () =>

@sebnozzi
Copy link
Contributor

It also fails for an HTML file containing...

$(function() {

Even if written as:

\$(function() {

Fortunately this time it was also possible to find a workaround:

jQuery(function() {

though I would prefer the "$" form.

@etorreborre
Copy link
Author

I have another workaround. In the default.properties file define dollar=$. Then in your templates use When("""^I retrieve the result""" + $dollar$).

@agemooij
Copy link

agemooij commented May 6, 2015

Did you test that? I just get an unexpected char: '$' error while it is parsing the default.properties file and asking questions.

@etorreborre
Copy link
Author

That was definitely working for me but I haven't tried recently.

@argast
Copy link

argast commented Aug 21, 2015

You can use $"$"$ combination to work around this problem.

e.g. in Scala:
s"$var" -> s"$"$"$var"

@vladap
Copy link

vladap commented Nov 9, 2016

It gets quite ugly with javascript templates for AngularJs. As well as with Maven properties, checkstyle and scalastyle xmls.

Parser should forward look for a complete $var$ pattern where var can't have spaces, line breaks and any char which doesn't make sense in var name.

Without a fix it looks like dollar was an unfortunate choice for escaping, instead of {{ }}, <% %>, whatever. Few places for placeholders but potentially many for dollars in different contexts.

@vladap
Copy link

vladap commented Nov 9, 2016

As well I would change title because it is more general problem than just with interpolated strings.

@eed3si9n eed3si9n added this to the 0.7.2 milestone Nov 15, 2016
@foxmk
Copy link
Contributor

foxmk commented Feb 7, 2017

I can change the behaviour of templating engine to fix this issue, but there
are several possible solutions:

  • Escape $: \$ or $$. But this would not allow to develop template independently, which I would like to achieve.
  • Restrict variable names to [a-zA-Z_-], which can break some existing templates and, in some cases won't work (If template contains println(s"$name_$foo"), for example).
  • Look for existing properties and ignore unknown variables. But we should at least notify user about them.

Personally I would prefer the second solution with optional escaping.

@eed3si9n
Copy link
Member

eed3si9n commented Feb 7, 2017

With Pamflet I've always used \$ to escape. I wonder if that's something specific to Pamflet, or if there's been some change to StringTemplate.

@eed3si9n
Copy link
Member

eed3si9n commented Feb 7, 2017

\$ works for me.
Maybe this is why I tagged this issue with Documentation.

@foxmk
Copy link
Contributor

foxmk commented Feb 7, 2017

The problem with escaping is that for example, I want to develop template separately.

I create my-template.g8/src/main/g8/src/main/$package$/Main.scala and I want
to create IDEA project inside my-template.g8/src/main/g8 to be able to edit and compile it.

But if I want to write:

package $package$

object Main extends App {
  val foo = "foo"
  val bar = "bar"
  println(s"\$foo\$bar") // I need to escape this
}

inner project won't work properly. Same thing with .gitignore the .idea folder.

@eed3si9n
Copy link
Member

@foxmk I think that's a whole different issue from what's being reported here. Eric reported "action parse error; template context is [anonymous]".

@foxmk foxmk self-assigned this Mar 11, 2017
@djmittens
Copy link

Hi, i was recently also trying to use giter8 to create a template which has amongst other things, sh files which would provide example commands to start docker containers with the right parameters, and therefore have a lot of env vars defined with $VAR which break giter8, this is a pretty big issue for me, so getting a work around or something like that would be of huge help.

@Tvaroh
Copy link

Tvaroh commented May 9, 2017

$"$"$ doesn't work anymore, other solutions above neither. It's pity to see such a child problems in a project like this.

@kknd22
Copy link

kknd22 commented May 31, 2017

I think #3 is the right solution:

Look for existing properties and ignore unknown variables. But we should at least notify user about them.

It make no sense to pollute the whole source code with all kinds of escape while the ONLY replacements are limited to a very small set...

This assumes that the file default.properties is mandatory and it contains ALL replaceable names and their default values.

@foxmk foxmk removed their assignment Jun 30, 2017
@laughedelic
Copy link

Escaping with \$ works for me with sbt 0.13.15 which should be using Giter8 0.7.2

@eed3si9n eed3si9n removed this from the 0.7.3 milestone Sep 24, 2017
@metasim
Copy link

metasim commented Oct 24, 2017

FWIW: I'm having this problem with sbt 0.13.16 sbt-giter8 0.9.0; can't figure out how to escape '$'

@djmittens
Copy link

It seems like there was an identified correct solution (eg. if there is no token specified in build.properties, then dont try to find the closing dollar) is it a difficult change? and if so where would one look to make it?

@ryan-williams
Copy link

FWIW, I believe I'm seeing both \$ and $"$"$ working atm, SBT 1.1.0 and Giter8 0.11.0-M2

@salim7
Copy link

salim7 commented Mar 6, 2019

It would be great to have a more intelligent behavior, like auto-escaping for undefined vars or when a closing $ sign is missing.

@TonioGela
Copy link
Member

Hi, i was recently also trying to use giter8 to create a template which has amongst other things, sh files which would provide example commands to start docker containers with the right parameters, and therefore have a lot of env vars defined with $VAR which break giter8, this is a pretty big issue for me, so getting a work around or something like that would be of huge help.

I think that the verbatim could solve here.

Since issue title is Parsing issue when having template files including interpolated strings and, like in the above example, escaping dollars like \$ works perfectly with the current version 0.13.1 (see screenshots), I think the only thing left is adding this detail to the documentation. I'll write a PR asap.

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.