Eventually in NimbleParsec returning wrong result?

If I run the following code, I get

{:ok, [“ohno”, “matchme”, 1], “”, %{}, {3, 15}, 15}

But why does I match on ohno, also if I uncomment tag, it works as expected,

{:ok, [{“record”, [“matchme”, 1]}], “”, %{}, {3, 15}, 15}

defmodule MyParser.Helpers do
  import NimbleParsec

  def license do
    ascii_string([?a..?z], min: 1)
    |> ignore(ascii_string([?\s], min: 1))
    |> integer(min: 1)
    |> ignore(string("\n"))
    #|> tag("record")
  end

  def eventually_license do
    eventually(license())
  end
end

defmodule MyParser do
  import NimbleParsec
  import MyParser.Helpers

  defparsec(:license, eventually_license())
end

defmodule MyParser.Run do
  @data """
  ohno
  matchme 1
  """

  def license do
    MyParser.license(@data)
  end
end

1 Like

That does seem unexpected. It appears that the accumulator isn’t being cleared on the partial combinator match. I tested with this simple variation on your code:

defmodule MyParser do
  import NimbleParsec

  parse_incorrect =
    ascii_string([?a..?z], min: 1)
    |> ignore(ascii_char([?\s]))
    |> integer(min: 1)
    |> ignore(ascii_char([?\n]))

  parse_correct =
    ascii_string([?a..?z], min: 1)
    |> ignore(ascii_char([?\s]))
    |> integer(min: 1)
    |> ignore(ascii_char([?\n]))
    |> tag("record")

  defparsec :parse_correct, eventually(parse_correct)
  defparsec :parse_incorrect, eventually(parse_incorrect)

  @data """
  ohno
  matchme 1
  """

  def correct do
    parse_correct(@data)
  end

  def incorrect do
    parse_incorrect(@data)
  end
end

Perhaps file an issue?

1 Like

Done.

And thanks for having a look, I am an Elixir noob, so i just needed a second opinion.

1 Like