Template style language

Ultimately I want a user’s input into the codemirror editor to be a string which concatenates consectutive parts where a part is either a template or not and where the template will contain a javascript expression to be evaluated.

For example (if x is number 2),

“abc {{ x + 2 }} zep”

Would evaluate to “abc 4 zep”.

As you probably gathered “{{” and “}}” are used to determine the javascript expression.

From what I’ve researched I will need to use the “mixed parser”.

However I’m struggling to jump over the first hurdle of building the outside parser.

Based on “lang-json” I’ve got something close but need a little help pointing me in the right direction or telling me what I’m aiming for isn’t even a go-er.

This is the grammar.

@top Templates { value* }

value { Object | String | whitespace }

Object { "{{" Stuff* "}}" }

String {

Stuff {
  string | whitespace

@tokens {

  string { '"' char* '"' }
  char { $[\u{20}\u{21}\u{23}-\u{5b}\u{5d}-\u{10ffff}] | "\\" esc }
  esc  { $["\\\/bfnrt] | "u" hex hex hex hex }
  hex  { $[0-9a-fA-F] }

  whitespace { $[ \n\r\t] }

  "{{" "}}"

list<item> { item ("," item)* }


And these tests are passing.

# Basic




# Multiple Templates



Templates(Object(Stuff), Object(Stuff))

# Spaced out multiple templates

{{"foo"}} {{"foo"}}


Templates(Object(Stuff), Object(Stuff))

# Strings outside templates

"abc" {{"foo"}} "mno" {{"foo"}} "xwz"


Templates(String, Object(Stuff), String, Object(Stuff), String)

All I need is to change it so plain text, instead of strings, are permitted inside and outside the templates.

However if I make this slighty change

Stuff {
  char* | whitespace

I’m getting a conflict which needs “reducing”.

shift/reduce conflict between
  Object -> "{{" · "}}"
  Stuff -> 
With input:
  "{{" · "}}" …
Shared origin: value -> · Object
  via Object -> "{{" · Stuff+ "}}"
    via Stuff+ -> · Stuff
      Stuff -> ·

Have you seen the Mixed-Language parsing example?

Thanks for the quick response.

I’ve had a look again at the example. It seems though that this takes for granted an outside parser already and only explains how to then mix it with another. An example of a templating language like what I’m trying to achieve would be helpful. Mine though is perhaps a tad trickier than many because “{{” could be interpreted as plain text or the beginning of a template. On that note I did just try adding in a “precedence” list inside the tokens and that’s now got something working. Whether it’s “right” though I’m not sure. I guess so long as I have a token which contains each template I should be able to get started with now mixing this in with the javascript parser.


The example links the grammar it uses, which might be useful.