I am trying to write a lark grammar for a dsl, but having trouble with this string interpolation syntax:
" abc " <- normal string
" xyz~{expression}abc " <- string with interpolation
so a ~{ switches from string to expression, and a } terminates that expression. I think this is close:
string : "\"" (string_interp|not_string_interp)* "\""
string_interp: "~{" expression "}"
not_string_interp: /([^~][^{])+/
But the regex will only match even numbers of characters, and if the ~{ straddles an even boundary, it will be missed.
not_string_interp: /(.?|([^~][^{])+)/
This is about as far as I could get, but still seems wrong. Can I use lookaheads? I also want to keep %ignore WS on, as it keeps the noise down massively, so a solution accounting for that would be great!
Thanks
Test cases:
""
"a"
"~{1}"
" ~{1} "
"a bc~{1}c d"
"a b~{1}c d"