使用 FParsec 解析字符串文字?

发布于 2025-01-03 15:26:40 字数 286 浏览 5 评论 0原文

我想使用 FParsec 解析字符串文字。我所说的“字符串文字”是指在开始和结束引号之间的东西(在我的例子中

'Please, switch off your mobile phone'

是单引号):我当前正在做的事情如下:

let string = between (pstring "'") (pstring "'") (manySatisfy isLetter)

但这在第一个字母被消耗后停止。有什么办法让它贪心吗?

I would like to parse string literals using FParsec. By "string literals" I mean something between opening and closing quote (in my case -- single quote):

'Please, switch off your mobile phone'

What I am currently doing is the following:

let string = between (pstring "'") (pstring "'") (manySatisfy isLetter)

But this stops after the first letter consumed. Is there any way to make it greedy?

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

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

发布评论

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

评论(1

烟燃烟灭 2025-01-10 15:26:40

它已经很贪婪了; manySatisfy isLetter 解析输入流中的字母序列。

问题是解析器因 或空格而失败,因为它们不是字母。 来修复

manyChars (noneOf "'")

它可以通过使用:或更明确地使用:

manySatisfy ((<>) '\'')

It's already greedy; manySatisfy isLetter parses a sequence of letters from the input stream.

The problem is that the parser fails with , or spaces since they are not letters. It could be fixed by using:

manyChars (noneOf "'")

or more explicitly using:

manySatisfy ((<>) '\'')

instead.

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