<div dir="ltr">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.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Mar 10, 2018 at 2:25 AM, Hilco Wijbenga <span dir="ltr"><<a href="mailto:hilco.wijbenga@gmail.com" target="_blank">hilco.wijbenga@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I recently discovered ReadP (Text.ParserCombinators.ReadP) and I<br>
wondered if I could use it to write simple parsers _with decent error<br>
reporting_.<br>
<br>
Let's say I want to parse "[ab] [12]" (i.e. either 'a', or 'b',<br>
followed by whitespace, followed by '1', or '2'). I believe something<br>
like this does the trick:<br>
<br>
parse = do<br>
name <- ReadP.choice [ReadP.char 'a', ReadP.char 'b']<br>
ReadP.skipMany1 (ReadP.satisfy (\ch -> ch == ' ' || ch == '\t'))<br>
value <- ReadP.choice [ReadP.char '1', ReadP.char '2']<br>
return (name, value)<br>
<br>
How do you add error handling to this? Obviously, I could change the<br>
signature to return an Either but I don't see how I "return" anything<br>
other than Right.<br>
<br>
E.g., I would want to give a warning like "Expected 'a', or 'b'" if<br>
anything other than 'a', or 'b' is used as first character. Similarly<br>
for '1' and '2'. The obvious(?) way would be to add an error result as<br>
the last option. The best I can come up with is adding ReadP.pfail to<br>
the list of choices but that doesn't allow me to add a useful error.<br>
Is ReadP simply not powerful enough for this? Or am I overlooking<br>
something obvious?<br>
<br>
I did some googling and there is plenty about creating parsers with<br>
parser combinators but I could not find anything about error handling.<br>
<br>
Cheers,<br>
Hilco<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>