使用 Haskell Parsec 自定义空白

发布于 2024-08-29 02:52:48 字数 626 浏览 2 评论 0原文

我想使用 Parsec 的 makeTokenParser 来构建我的解析器,但我想使用我自己的 whiteSpace 定义。执行以下操作将 whiteSpace 替换为我的定义,但所有 lexeme 解析器仍然使用旧的定义(例如 P.identifier lexer 将使用旧的定义)空白)。

...
lexer :: P.TokenParser ()
lexer      = l { P.whiteSpace = myWhiteSpace }
   where l = P.makeTokenParser myLanguageDef
...

查看 makeTokenParser 我想我明白为什么它会这样工作。我想知道是否有任何解决方法可以避免完全重复 makeTokenParser 的代码?

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
...

Looking at the code for makeTokenParser I think I understand why it works this way. I want to know if there are any workarounds to avoid completely duplicating the code for makeTokenParser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-09-05 02:52:48

可悲的是,我认为没有办法。 makeTokenParser 中使用的本地定义递归地引用自身,因此,正如您所指出的,lexeme 使用其中定义的 whiteSpace,而不是您在 lexer 对象中替换的 whiteSpace 记录成员。

该代码令人嘲讽,因为它使用与 makeTokenParser 中的本地函数以及 TokenParser 构造函数的记录成员相同的名称。它们实际上是完全不同的实体。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文