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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews