我不明白如何使用lexeme函数 [英] I don't understand how to use the lexeme function

查看:156
本文介绍了我不明白如何使用lexeme函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Text.Parsec.Token

  lexeme p = do {x < -  p;空白符;返回x} 

似乎lexeme采用解析器p并提供了一个解析器, p,除了它也跳过所有的尾部空格。正确?



那么下面怎么不起作用:

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

lexConst :: Parser Int
lexConst = lexeme常量

最后一行产生以下错误消息:

 无法匹配预期的类型`ParsecT 
String()Data.Functor.Identity.Identity Int'
,实际类型为`ParsecT s0 u0 m0 a0 - > ParsecT s0 u0 m0 a0'
预期类型:解析器Int
实际类型:ParsecT s0 u0 m0 a0 - > parsecT s0 u0 m0 a0
在`lexeme'调用的返回类型中
在表达式中:lexeme常量

我做错了什么?

解决方案

您误解了文档,lexeme> Text.Parsec.Token 导出的 GenTokenParser sum ,因此类型为

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

并且您没有提供 GenTokenParser lexeme constant



您需要创建 GenTokenParser 来自 GenLanguageDef (通常使用 makeTokenParser )首先使用它的 lexeme 字段。

From Text.Parsec.Token:

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

It appears that lexeme takes a parser p and delivers a parser that has the same behavior as p, except that it also skips all the trailing whitespace. Correct?

Then how come the following does not work:

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

lexConst :: Parser Int
lexConst = lexeme constant

The last line results in the following error message:

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?

解决方案

You misunderstood the documentation, the lexeme exported from Text.Parsec.Token is a field of a GenTokenParser s u m, so the type is

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

and you haven't supplied the GenTokenParser argument in lexeme constant.

You need to create a GenTokenParser from a GenLanguageDef (typically with makeTokenParser) first to use its lexeme field.

这篇关于我不明白如何使用lexeme函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆