I do not understand how to use the lexeme function

From Text.Parsec.Token:

lexeme p = do { x <- p; whiteSpace; return x }

It looks like lexeme accepts the parser p and delivers a parser that has the same behavior as p, except that it also skips all trailing spaces. Correctly?

Then why the following does not work:

constant :: Parser Int
constant = do
    digits <- many1 digit
    return (read digits)

lexConst :: Parser Int
lexConst = lexeme constant

The following error message appears on the last line:

Couldn't match expected type `ParsecT
                                String () Data.Functor.Identity.Identity Int'
            with actual type `ParsecT s0 u0 m0 a0 -> ParsecT s0 u0 m0 a0'
Expected type: Parser Int
  Actual type: ParsecT s0 u0 m0 a0 -> ParsecT s0 u0 m0 a0
In the return type of a call of `lexeme'
In the expression: lexeme constant

What am I doing wrong?

+1
source share
2 answers

You misunderstood the documentation, lexemeexported from Text.Parsec.Tokenis a field GenTokenParser s u m, so the type

lexeme :: GenTokenParser s u m -> ParsecT s u m a -> ParsecT s u m a

and you did not submit an argument GenTokenParserto lexeme constant.

GenTokenParser GenLanguageDef ( makeTokenParser) lexeme.

+6

lexeme GenTokenParser , makeTokenParser, , . - , .

{-# LANGUAGE RecordWildCards #-}

import qualified Text.Parsec.Token as Tok

Tok.TokenParser { .. } = Tok.makeTokenParser {- language definition -}

, lexeme , , , .

+2

All Articles