I’m trying to create a template where you would enter this:
ID: {{ my_func($.project.id, "arg") }}, NAME: {{ $.project.id }}
or
{{ my_func($.project.id, "arg") }}, NAME: {{ $.project.id }}
I want to write a parser with NimbleParsec
that gives me something like this:
{:ok, ["ID: ", [expr: [function: [my_func: ["$.project.id", "arg"]]]], ", NAME: ", [expr: "$.project.id"]]}
Basically, I need to identify opening and closing tags {{
and }}
, and then inside them detect if there is a function call. If yes, parse the function call too for the name and arguments.
Function calls can also be nested my_other_func(my_func($.project.id, "arg"))
and also sometimes there is no function call, but only a value.
The output doesn’t need to be exactly like mine, I just need to be able to tell things apart.
This is my first ever serious usage on NimbleParsec, and I’m not really sure how to approach it.
I get stuck on things like:
- how to collect a string, until I detect a starting tag. But also how to make it optional.
- how do I capture things between open and close tags, and then tag it as
:expr
. - how do I repeat it, until I reach the end of the string.
And I haven’t even got to parsing nested functions.
Can anyone be so kind and write down how do you start to tackle the problem?