rkr
Pattern-matching char-lists
Hi,
whats the correct way to match a char-list against another charlist, but as a variable? Like so:
input = 'Hello World! {{ }} {% %}'
pattern = '{{'
...
case input do
^pattern ++ remaining ->
...
end
but this isn’t working.
Context:
I currently try to build a parser for twig-like templates to reuse code I already have, but mostly for learning purposes as I’ve never came in touch with elixir or erlang before. Erlang already comes with leex and yecc, but they (or I) struggle with context-switching (programs embedded in text-content). So I try to build a very light-weight parser to tell the content-context and program-context apart and then use leex/yecc to parse the program-islands.
Twig-templates might look like this:
Hi, my name is {{ person.name }} and I am {{ person.years }} year{% person.years != 1 ? ‘s’ : ‘’ %} old.
{% include ‘@common/details.twig’ with person %}
Since the opening-tags are configurable, I have a special case, where I’m stuck at and I dont manage to find help online.
At the most fundamental level, I have a function that should tell me, whats the first occurence of a list of char-sequences (char-list).
Like template |> MyScanner.get_next_position_of({'{{', '{%', '{#'}) to find the nearst opening-tag in my template to toggle the twig-context.
Now inside the get_next_position_of-function, I thought I could simply use recursion to step through all chars in the template and use pattern-matching to check, if the upcoming sequence of characters matches one of the patterns, I gave as the argument.
Marked As Solved
michalmuskala
I think the only way to do this with charlists is to go element-by-element in a loop. Something like:
input = 'Hello World! {{ }} {% %}'
pattern = '{{'
consume_pattern(input, pattern)
defp consume_pattern([c | input], [c | pattern]), do: consume_pattern(input, pattern)
defp consume_pattern(input, []), do: {:ok, input}
defp consume_pattern(_input, pattern), do: {:unmatched, pattern}
Also Liked
jordiee
Under the The pin operator section it talks about a variable being used more then once in a pattern match.
rkr
Thank you!
Would have been great if I could express it the way I wanted to, but this also works and I found a working solution.
Now even templates like Twig.PreParser.parse('<html>{{ fn({test: {}}) }}</html>', my_tags) could be parsed correctly. I have no idea how inefficient my solution is, but I will eventually find out soon ![]()
[
{:content, 0, '<html>'},
{:expr, 8, ' fn({test: {}}) '},
{:content, 24, '</html>'}
]
Now I finally have to find a solution for strings like "Hello \"World\"!", but I think I already now what to do.
dimitarvp
Didn’t know about that particular way of duplicating parameters in a function head. Could you please explain this particular line of code or point me to the proper docs?
Popular in Questions
Other popular 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
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









