rkr

rkr OP

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.

First 6 of 6 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement