rkr

rkr

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.

Showing Posts 1 to 6

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

rkr OP

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?

jordiee

jordiee

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

dimitarvp

dimitarvp

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!

jordiee

jordiee

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews