chumsky icon indicating copy to clipboard operation
chumsky copied to clipboard

RichReason Many

Open faassen opened this issue 11 months ago • 3 comments

I'm using 1.0.0a4, and I'm returning a Rich::custom error from a try_map. I was surprised to find an error message like this:

multiple errors found at 7..8

That's not very informative at all. Plus the range is off - the custom error is actually returned for the token just before the "multiple errors" message.

Digging into it I found that this is happens because apparently multiple errors are combined into one using RichReason::Many: first my custom error happens, and then because that causes a token not to be parsed (I think?), the parser adds an ExpectedFound error. That's not what I intended; I just wanted to see the custom error for the correct span.

I'm not sure how to proceed. I see a TODO comment in the source code:

   // TODO: Should we really do this? Possibly better to just unify the unrelated reasons. It's not like consumers
    // probably care about reporting 5 different errors for the same location anyway!

Perhaps an improvement would be to show the custom error if Many contains any custom, because that's likely the intention of the user.

But I'm also puzzled why try_map continues parsing even though I've returned an error, as the docs say this:

After a successful parse, apply a fallible function to the output. If the function produces an error, treat it as a parsing error.

If you wish parsing of this pattern to continue when an error is generated instead of halting, consider using [Parser::validate](https://docs.rs/chumsky/1.0.0-alpha.4/chumsky/trait.Parser.html#method.validate) instead.

But parsing is apparently continuing when my custom error was generated, so I don't understand the behavior. Perhaps the docs are just obscure and the intention for try_map is to continue parsing other rules if it fails. If so, that warrants a documentation improvement.

faassen avatar Jul 13 '23 10:07 faassen