使用Haskell Parsec自定义whiteSpace [英] Custom whiteSpace using Haskell Parsec
问题描述
我想使用Parsec的makeTokenParser
来构建解析器,但是我想使用自己的whiteSpace
定义.执行以下操作将whiteSpace
替换为我的定义,但是所有lexeme
解析器仍使用旧的定义(例如P.identifier lexer
将使用旧的空白).
I would like to use Parsec's makeTokenParser
to build my parser, but I want to use my own definition of whiteSpace
. Doing the following replaces whiteSpace
with my definition, but all the lexeme
parsers still use the old definition (e.g. P.identifier lexer
will use the old whiteSpace).
...
lexer :: P.TokenParser ()
lexer = l { P.whiteSpace = myWhiteSpace }
where l = P.makeTokenParser myLanguageDef
...
查看 makeTokenParser
我想我理解它为什么这样工作.我想知道是否有任何解决方法来避免完全复制makeTokenParser
的代码?
推荐答案
遗憾的是,我认为没有办法.在makeTokenParser
中使用的局部定义是递归地指向它们自己,因此,如您所注意到的,lexeme
使用的是在那里定义的whiteSpace
,而不是在lexer
对象中替换的whiteSpace
记录成员.
Sadly, I don't think there is a way. The local definitions used in makeTokenParser
refer recursively to themselves, and so, as you've noted, lexeme
uses whiteSpace
as defined there, rather than the whiteSpace
record member you replace in your lexer
object.
代码之所以嘲讽,是因为它使用与makeTokenParser
中的本地函数和TokenParser
构造函数的记录成员相同的名称.它们实际上是完全不同的实体.
The code is taunting because it uses the same names as both local functions in makeTokenParser
and as record members of the TokenParser
constructor. They are in fact totally distinct entities.
这篇关于使用Haskell Parsec自定义whiteSpace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!