[Haskell-cafe] Parsing of bytestrings with non-String errors?

Magnus Therning magnus at therning.org
Mon Feb 22 17:38:59 EST 2010

On 22/02/10 18:44, Bryan O'Sullivan wrote:
> On Sun, Feb 21, 2010 at 4:36 AM, Magnus Therning <magnus at therning.org
> <mailto:magnus at therning.org>> wrote:
>     I've looked at polyparse and attoparsec and they seem to have in common
>     that the error always is a String.  My current ideas for a project would
>     be a lot easier if I could just return some other type, something that I
>     can pattern match on.
> It would be easy enough to add this, but you'd end up with a slightly
> convoluted API. Because of the presence of fail in all monadic APIs, you'd
> have to support only-a-string as a failure result in some form, so your
> failure type would have to be something like Either String a.

My thoughts went more like a parser type like

    data Parser e a = ...

Possibly with the addition that 'e' implements a class that goes something

    class ParserError e where
        baseError :: e
        addError :: e -> e -> e

(At first I thought that maybe Monoid would do, but both a identity and
associativity feels awkward in this case. :-)

With 'String' implemented something like

    instance ParserError String where
        baseError = "Parser error, expected:\n"
        addError = (++)

Then I believe 'Parser String' would be equivalent to the existing attoparsec
parser (as found in the 0.7 series).

I still haven't convinced myself that this will work though.  Also, I had a
look at attoparsec on bitbucket, and there are some major changes between 0.7
and 0.8.  I realised I'll have to spend a lot more time understanding the code
than I initially hoped.  Right now that is unlikely to happen any time soon :(

> There's no support for this in attoparsec simply because I haven't
> needed it. I suspect the same is true of other libraries, nothing deeper.

Yeah, that's what I thought.  In a current project I just have a need to
differentiate between errors in different parts of the parser.  And handling
those errors would just be simple if I could use pattern matching rather than
inspect strings.


Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100222/d65eecbd/signature.bin

More information about the Haskell-Cafe mailing list