haskellparsec

How to parse an Integer with parsec


I was expecting to find a function

integer :: Stream s m Char => ParsecT s u m Integer

or maybe even

natural :: Stream s m Char => ParsecT s u m Integer

in the standard libraries, but I did not find one.

What is the standard way of parsing plain natural numbers directly to an Integer?


Solution

  • Here is what I often do is to use the expression

    read <$> many1 digit
    

    which can have type Stream s m Char => ParsecT s u m Integer (or simply Parser Integer).

    I don’t like the use of the the partial function read, but when the parser succeeds I know that the read will succeed, and it is somewhat readable.