rkr

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

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

jordiee

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

rkr

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

dimitarvp

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?

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement