Is it possible somehow to get parse error of some custom type? It would be cool to get more information about parsing context from error for example. And it seems not very convenient to have error info just in the form of text message.
As Rhymoid observes, it is not possible directly, unfortunately.
Combining Parsec
with your own Either
-like monad won't help, too — it will exit too soon (ParsecT
over Either
) or too late (EitherT
over ParsecT
).
If you want it badly, you can do it like this: use ParsecT
over State (SourcePos, YourErrorType)
. (You can't use Parsec's user state because then the error will be backtracked.)
Every time you'd like to emit a structured error value, record it in the state with the current location, but only if the current location is farther than the already recorded one. (If locations are equal, you may want to merge the errors somehow. Maybe keep a list of them.)
Finally, when you run your monad stack, you'll be given the final state and a ParseError
that contains a SourcePos
. Just check that the two locations coincide. If they don't (i.e. the Parsec's SourcePos
is farther), then you don't have an error value for this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With