benjamin-hodgson/Pidgin

Add ParseError as a context field to ParseException

Closed this issue · 1 comments

When constructing ParseException, it would be very useful to include the context from the ParseError:

private static T GetValueOrThrow<TToken, T>(Result<TToken, T> result)
        => result.Success ? result.Value : throw new ParseException(result.Error!.RenderErrorMessage(). <add result.Error here>);

This will allow code catching the ParseException to query the exception in a structured way about what went wrong

Fixed in c2a2b2e. I had to add a subclass of ParseException with a TToken parameter in order to type the ParseError field correctly. So you can catch the more specific subclass and have access to the Error, or you can continue to catch the non-parameterised ParseException as before.

try
{
    parser.ParseOrThrow("");
}
catch (ParseException<char> e)
{
    DoSomething(e.Error);
}