Skip to content

parsing failure context label #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamesdbrock opened this issue Sep 18, 2020 · 0 comments · Fixed by #96
Closed

parsing failure context label #95

jamesdbrock opened this issue Sep 18, 2020 · 0 comments · Fixed by #96

Comments

@jamesdbrock
Copy link
Member

It would be nice if we could get some context with parse failures, like Megaparsec label

In ParserT, fail is implemented as throwError

-- | Fail with a message.
fail :: forall m s a. Monad m => String -> ParserT s m a
fail message = failWithPosition message =<< position
-- | Fail with a message and a position.
failWithPosition :: forall m s a. Monad m => String -> Position -> ParserT s m a
failWithPosition message pos = throwError (ParseError message pos)

This suggests a cheap implementation strategy for label, using catchError :: forall a. m a -> (e -> m a) -> m a.

-- | If parsing fails inside this context, then prepend the String to the error String.
label :: forall m s a. Monad m => String -> ParserT s m a -> ParserT s m a
label preerror p = catchError p go
 where go (ParseError err pos) = failWithPosition (preerror <> err) pos

Actually come to think of it, anyone can use this label function without library support. Maybe I'll just do that.

I suppose if we wanted to make this more official and support it in the library, then we could change the error type to ParseError (NonEmptyList String) Position and cons the errors up as we fail out of the parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant