Skip to content

parsing failure context label #95

Closed
@jamesdbrock

Description

@jamesdbrock

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions