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 {
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)* }
@detectDelim
And these tests are passing.
# Basic
{{"foo"}}
==>
Templates(Object(Stuff))
# Multiple Templates
{{"foo"}}{{"foo"}}
==>
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 -> "{{" · "}}"
and
Stuff ->
With input:
"{{" · "}}" …
Shared origin: value -> · Object
via Object -> "{{" · Stuff+ "}}"
via Stuff+ -> · Stuff
Stuff -> ·