Pattern-matching char-lists


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 ->

but this isn’t working.

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 {{ }} 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.

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}

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 :slight_smile:

  {: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.

1 Like

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?

1 Like

Under the The pin operator section it talks about a variable being used more then once in a pattern match.


Indeed. I had no idea you can use the same variable in more than one pattern matching clause though. :open_mouth: In this case you basically force string sub-matching by using this mechanism. Damn, I feel like I missed something extremely basic when learning Elixir!

1 Like

Its easy to overlook. I only ever saw it mentioned there and very briefly in the Programming Elixir books.