ReadP & Error Reporting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

ReadP & Error Reporting

Hilco Wijbenga
Hi all,

I recently discovered ReadP (Text.ParserCombinators.ReadP) and I
wondered if I could use it to write simple parsers _with decent error
reporting_.

Let's say I want to parse "[ab]   [12]" (i.e. either 'a', or 'b',
followed by whitespace, followed by '1', or '2'). I believe something
like this does the trick:

parse = do
    name <- ReadP.choice [ReadP.char 'a', ReadP.char 'b']
    ReadP.skipMany1 (ReadP.satisfy (\ch -> ch == ' ' || ch == '\t'))
    value <- ReadP.choice [ReadP.char '1', ReadP.char '2']
    return (name, value)

How do you add error handling to this? Obviously, I could change the
signature to return an Either but I don't see how I "return" anything
other than Right.

E.g., I would want to give a warning like "Expected 'a', or 'b'" if
anything other than 'a', or 'b' is used as first character. Similarly
for '1' and '2'. The obvious(?) way would be to add an error result as
the last option. The best I can come up with is adding ReadP.pfail to
the list of choices but that doesn't allow me to add a useful error.
Is ReadP simply not powerful enough for this? Or am I overlooking
something obvious?

I did some googling and there is plenty about creating parsers with
parser combinators but I could not find anything about error handling.

Cheers,
Hilco
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: ReadP & Error Reporting

David McBride
My first instinct is that no, there's not much you can do with this.  You could wrap every combinator to return more detailed error messages, which might be *okay*.  Or you could use the heavier parsing libraries and get all the error reporting for free.

On Sat, Mar 10, 2018 at 2:25 AM, Hilco Wijbenga <[hidden email]> wrote:
Hi all,

I recently discovered ReadP (Text.ParserCombinators.ReadP) and I
wondered if I could use it to write simple parsers _with decent error
reporting_.

Let's say I want to parse "[ab]   [12]" (i.e. either 'a', or 'b',
followed by whitespace, followed by '1', or '2'). I believe something
like this does the trick:

parse = do
    name <- ReadP.choice [ReadP.char 'a', ReadP.char 'b']
    ReadP.skipMany1 (ReadP.satisfy (\ch -> ch == ' ' || ch == '\t'))
    value <- ReadP.choice [ReadP.char '1', ReadP.char '2']
    return (name, value)

How do you add error handling to this? Obviously, I could change the
signature to return an Either but I don't see how I "return" anything
other than Right.

E.g., I would want to give a warning like "Expected 'a', or 'b'" if
anything other than 'a', or 'b' is used as first character. Similarly
for '1' and '2'. The obvious(?) way would be to add an error result as
the last option. The best I can come up with is adding ReadP.pfail to
the list of choices but that doesn't allow me to add a useful error.
Is ReadP simply not powerful enough for this? Or am I overlooking
something obvious?

I did some googling and there is plenty about creating parsers with
parser combinators but I could not find anything about error handling.

Cheers,
Hilco
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners