egze
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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
fuelen
Hello @edze
That’s a good exercise for my evening
Here is my parser:
and result:
egze
Wow, a full working solution! Thank you so much!
Can you please comment on how did you start working on this parser? Like, do you just start from left to right, first try to extract the text and then just go along? Or you prepare smaller building pieces first? How do you plan it, in other words?
fuelen
Yes, I’ve started from left to right. While working on parser, I find it easier not to limit anything on the right side. Like, put
eos()only when parser is ready. When working onfunction(...)put expectation for closing)only when parsing arguments is ready.Also, I have this helper for inspecting errors:
usage:
error messages are not so good as they could be, and writing a parser with good error messages is another art. But this helper allows to visually find the place where something goes wrong. I mean, try to remove
"from the template near the"arg"and inspect the error.egze
Really appreciate the answer and the snippet. I’m sure it will be useful to a lot of folks.
egze
@fuelen How would I go about working with unfinished templates? For example
{{ $that only has a start tag and a beginning of a variable. What I want to do is to see that we’re now in the interpolation->variable part, and offer autocomplete.Would you suggest to have 2 separate parsecs, 1 for valid templates and 1 for incomplete where most of the rules are relaxed?
fuelen
I’m not sure if nimble parsec is the best tool for autocomplete on possibly invalid templates. I think you need a lexer (
leex) and then using list of tokens and position of a cursor try to analyze what you can suggest for autocompleteegze
Awesome. I was thinking that it would go in this direction
Challenge accepted. Thanks again.