fre*_*low 1 whitespace haskell parsec lexer parser-combinators
来自Text.Parsec.Token
:
lexeme p = do { x <- p; whiteSpace; return x }
Run Code Online (Sandbox Code Playgroud)
看来,语义需要一个解析器P和提供具有相同行为为p,但它也跳过所有尾随空格的分析器.正确?
那怎么来以下不起作用:
constant :: Parser Int
constant = do
digits <- many1 digit
return (read digits)
lexConst :: Parser Int
lexConst = lexeme constant
Run Code Online (Sandbox Code Playgroud)
最后一行导致以下错误消息:
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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
你误解了文档,lexeme
导出的Text.Parsec.Token
是一个字段GenTokenParser s u m
,所以类型是
lexeme :: GenTokenParser s u m -> ParsecT s u m a -> ParsecT s u m a
Run Code Online (Sandbox Code Playgroud)
你没有提供GenTokenParser
参数lexeme constant
.
您需要创建一个GenTokenParser
从GenLanguageDef
(通常makeTokenParser
)首先使用它的lexeme
领域.