dashbitco / nimble_parsec

A simple and fast library for text-based parser combinators

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

return of the partial accumulator when returning an error

jtarchie opened this issue · comments

commented

When using nimble, the current error returns the error message, leftover of the string, and the offset.
For some grammars, it would be nice to the thus-far parsed accumalator.

For example,

defmodule Parser do
  include NumbleParsec
  defparsec(:machine, integer(1) |> tag(:first) |> integer(1) |> tag(:second))
end

With Parser.machine("1") the error returned would be error message, leftover, and offset.
Having the already processed accumulator would be useful to, as the tagging is still structured information.

Is there a way of doing this?

I've discussed with some that using choice everywhere would help.

@jtarchie the context (acc) should be part of the error message. Here is the spec:

{:ok, [term], rest, context, line, byte_offset}
      | {:error, reason, rest, context, line, byte_offset}

It is the 4th element.

commented

I might be confusing the accumulator with the [term] here.
I was not doing this by types but by looking at generated code, my apologies.
Let me take a longer look here.

commented

Alright, it is the [term], I'm looking for.
I was relying on the example here.
This says that argument is acc, which is not returned in the {:error, ...} return value.

I see. It is pretty unreliable to rely on that when the parsing fails which is why we don’t expose it and we have no plans for now either, as that would be a breaking change.
If you need something here and there, you try to store it in the context.

Alternatively, if you language supports partial expressions, you can use the offset to find the part that could be parsed and feed that into the parser once again.