GavinJoyce

GavinJoyce

Hi,

I’m trying to write a parser using NimbleParsec v1.4.2 — Documentation which parses arbitrary text wrapped in a character like an underscore.

Here are some examples of what I’m hoping the parser will do:

"hello there" => ["hello there"]
"hello _Alex_!" => ["hello ", "_Alex_", "!"]
"Ũnicode is _allŏwed everywhere_ _ŏk?_" => ["Ũnicode is ", "_allŏwed everywhere_", "_ŏk?_"]

Here’s my attempt:

defmodule Txtparser do
  import NimbleParsec

  defcombinatorp :plain_string,
    utf8_string([], min: 1)
    |> lookahead_not(parsec(:wrapped_string))

  defcombinatorp :wrapped_string,
    string("_")
    |> parsec(:plain_string)
    |> string("_")

  defparsec(:parse,
    repeat(
      choice(
        [parsec(:wrapped_string), parsec(:plain_string)]
      )
    )
  )
end

But I think I’m misundertanding how lookahead_not works, as the following is happening

"hi _there_" => ["hi _there_"]

instead of this desired result:

"hi _there_" => ["hi ", "_there_"]

Showing Posts 1 to 4

ityonemo

ityonemo

maybe I’m missing something. how are you getting them split into separate chunks? When I run your code on "foo _bar_ foo" I get

{:ok, ["foo _bar_ foo"], "", %{}, {1, 0}, 13}
GavinJoyce

GavinJoyce OP

I want them to split into chunk, they aren’t currently though

ityonemo

ityonemo

a few NimbleParsec tips:

  1. try not to use defcombinatorp
  2. don’t be afraid to break up into smaller testable chunks
  3. try to limit circular references.
  4. test test test, I like having the Mix.env == :test defparsec block pattern
  5. don’t be afraid to set variables in you “module-building-script”
  6. use post_traverse

here is what I got:

defmodule Np do
  import NimbleParsec

  basic_chars =
    utf8_string([not: ?_], min: 1)

  wrapped_string =
    string("_")
    |> concat(basic_chars)
    |> string("_")

  plain_string =
    times(
      utf8_char([])
      |> lookahead_not(wrapped_string),
      min: 1
    )
    |> choice([eos(), utf8_char([])])
    |> post_traverse(:post_plain)

  parser =
    repeat(
      choice([
        wrapped_string
        |> post_traverse(:post_wrapped),
        plain_string
      ])
    )

  defparsec(:parse, parser)

  defp post_wrapped(_rest, parsed, context, _line, _offset) do
    {[parsed |> Enum.reverse |> List.to_string], context}
  end

  defp post_plain(_rest, parsed, context, _line, _offset) do
    {[parsed |> Enum.reverse |> List.to_string], context}
  end


  if Mix.env() == :test do
    defparsec(:basic_chars, basic_chars)
    defparsec(:wrapped_string, wrapped_string)
    defparsec(:plain_string, plain_string)
  end
end

np_test.exs

defmodule NpTest do
  use ExUnit.Case

  defmacrop assert_parses(result, what) do
    quote bind_quoted: [result: result, what: what] do
      assert {:ok, ^result, _, _, _, _} = what
    end
  end

  defmacrop assert_leaves(result, leftover, what) do
    quote bind_quoted: [result: result, leftover: leftover, what: what] do
      assert {:ok, ^result, ^leftover, _, _, _} = what
    end
  end

  defmacrop assert_fails(what) do
    quote bind_quoted: [what: what] do
      assert {:error, _, _, _, _, _} = what
    end
  end

  test "basic_chars" do
    assert_parses ["foo"], Np.basic_chars("foo")
    assert_leaves ["foo "], "_foo_", Np.basic_chars("foo _foo_")
    assert_fails Np.basic_chars("_foo_")
  end

  test "wrapped_string" do
    assert_parses ["_", "foo", "_"], Np.wrapped_string("_foo_")
    assert_leaves ["_", "foo", "_"], " bar", Np.wrapped_string("_foo_ bar")
  end

  test "plain string" do
    assert_parses ["foo"], Np.plain_string("foo")
    assert_leaves ["foo "], "_bar_", Np.plain_string("foo _bar_")
  end
end
GavinJoyce

GavinJoyce OP

Amazing, thanks so much!. I’ll dig into this and play around with it now - thank you!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews