[Haskell-cafe] parsec: parserFail & multiple error messages

Nick Vanderweit nick.vanderweit at gmail.com
Wed Aug 8 22:09:45 CEST 2012


I found a similar question asked in June 2009 on the haskell-beginners 
archives, titled "Clearing Parsec error messages." A hack that was proposed 
(http://www.haskell.org/pipermail/beginners/2009-June/001809.html) was to 
insert a dummy character into the stream, consume it, and then fail. Still, 
I'd like to see if there is a cleaner way to modify the error state in the 
Parsec monad.


NIck

On Wednesday, August 08, 2012 03:24:31 PM silly8888 wrote:
> I am trying to create a parsec parser that parses an integer and then
> checks if that integer has the right size. If not, it generates an
> error.
> I tried the following:
> 
> 8<---------------------------------------------------------------
> import Text.Parsec
> import Text.Parsec.String
> 
> integer :: Parser Int
> integer  = do s <- many1 digit
>               let n = read s
>               if n > 65535 then
>                   parserFail "integer overflow"
>               else
>                   return n
> 8<---------------------------------------------------------------
> 
> The problem is that when I try this
> 
> parse integer "" "70000"
> 
> I get the following error:
> 
> Left (line 1, column 6):
> unexpected end of input
> expecting digit
> integer overflow
> 
> ie there are three error messages but I only want the last one. Is
> there something I can do about this?
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list