revati

revati

How to parse strings with NimbleParsec?

Hello.
i would like to parse strings (with nimble parsec i assume) that look something like this:

x(some.dot.split.values) → [x: [“some”, “dot”, “split”, “value”]]
y(some.x(dot).split.values) → [y: [“some”, [x: [“dot”]], “split”, “value”]]

maybe if it is easier to implement

x some.dot.split.values → [x: [“some”, “dot”, “split”, “value”]]
y some.(x dot).split.values → [y: [“some”, [x: [“dot”]], “split”, “value”]]

Idea is to have nested patterns, and dot split strings that should represent array

I found this, but i do not understand how perentacies are implemented there How do you write nested and recursive NimbleParsec parsers? - #4 by jakemorrison

Maybe someone knows. Thanks.

Marked As Solved

NduatiK

NduatiK

defmodule Parser do
  import NimbleParsec

  # Step 1: Define your smallest units
  # I've assumed here that the a word is a list of
  # more than one lowercase character
  # 
  # Match abc
  defparsecp(
    :word,
    times(ascii_char([?a..?z]), min: 1)
    |> reduce({List, :to_string, []})
  )

  # Step 2: Define a function as a a thing
  # that has a word followed by:
  # - an opening bracket
  # - a "chain of stuff"
  # - a closing bracket
  #
  # Match abc(___)
  defparsec(
    :function,
    parsec(:word)
    |> map({String, :to_atom, []})
    |> concat(
      ignore(string("("))
      |> concat(parsec(:chain))
      |> concat(ignore(string(")")))
      # Put the function arguments in a list
      |> wrap()
    )
  )

  # Match abc or abc(___)
  defparsecp(
    :function_or_word,
    choice([
      parsec(:function),
      parsec(:word)
    ])
  )

  # Step 3: Define the "chain" as a
  # word or function followed by a "."
  # delimited list of words or functions
  # 
  # Match abc.def or abc(___).def or abc(___).def
  defparsecp(
    :chain,
    parsec(:function_or_word)
    |> times(
      ignore(string("."))
      |> concat(parsec(:function_or_word)),
      min: 0
    )
  )

  def parse!(str) do
    # Only parse successfully if the entire string is consumed
    {:ok, result, "" = _unconsumed, %{}, {_, _}, _} =
      function(str)

    result
  end
end

Parser.parse!("x(some.dot.split.values)")
Parser.parse!("y(some.x(dot).split.values)")

Also Liked

jakemorrison

jakemorrison

Here is a library that supports parsing nested lists with parentheses: GitHub - cogini/http_structured_field: Elixir library to parse HTTP Structured Fields (RFC 8941) · GitHub

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement