使用Haskell Parsec自定义whiteSpace [英] Custom whiteSpace using Haskell Parsec

查看:108
本文介绍了使用Haskell Parsec自定义whiteSpace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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屋!

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