How do I get Parsec to let me call `read` :: Int?

前端 未结 3 1843
感情败类
感情败类 2020-12-21 15:50

I\'ve got the following, which type-checks:

p_int = liftA read (many (char \' \') *> many1 digit <* many (char \' \'))

Now, as the fu

3条回答
  •  感情败类
    2020-12-21 16:19

    You may find

    Text-Megaparsec-Lexer.integer :: MonadParsec s m Char => m Integer
    

    does what you want.

    The vanilla parsec library seems to be missing a number of obvious parsers, which has led to the rise of "batteries included" parsec derivative packages. I suppose the parsec maintainers will get around to betteries eventually.

    https://hackage.haskell.org/package/megaparsec-4.2.0/docs/Text-Megaparsec-Lexer.html

    UPDATE

    or with vanilla parsec:

    Prelude Text.Parsec Text.Parsec.Language Text.Parsec.Token> parse ( integer . makeTokenParser $ haskellStyle ) "integer" "-1234"
    Right (-1234)
    

提交回复
热议问题