如何使标识符解析器停止在 FParsec 中的 OperationPrecedenceParser 运算符上?

发布于 2025-01-04 11:14:17 字数 964 浏览 5 评论 0原文

我正在实现一个会使用 unicode 符号的标识符名称解析器。我面临的问题是我有一些运算符也用 unicode 符号编写,这些运算符可能直接放在标识符之后,例如:

time→sleep(7);

这里箭头符号是中缀运算符,我将其添加到运算符优先级解析器中

opp.AddOperator(InfixOperator("→", ws, 10, Associativity.Right, 
      fun left right -> BinaryOperation(Arrow, left, right)))

:如果我可以自动排除作为运算符添加到 OPP 的所有符号组合,那就太好了。目前,我使用以下标识符手动执行此操作:

let variable =
    let isAsciiIdContinue = isNoneOf "→*/+-<>=≠≤≥' ,();"

    identifier (IdentifierOptions(
                    isAsciiIdContinue = isAsciiIdContinue,
                    normalization = System.Text.NormalizationForm.FormKC,
                    allowAllNonAsciiCharsInPreCheck = true))

但是,这似乎不起作用。我在尝试解析代码时收到以下错误消息:

  time→sleep(7);
      ^
The identifier contains an invalid character at the indicated position.

How can I make my variable parser stop on infix Operators?

I am implementing a parser for identifier names that would consume unicode symbols. The problem I am facing is what I have some operators that are also written with unicode symbols and these might be placed directly after the identifier, for example:

time→sleep(7);

Here the arrow sign is an infix operator, which I add to my operator precedence parser:

opp.AddOperator(InfixOperator("→", ws, 10, Associativity.Right, 
      fun left right -> BinaryOperation(Arrow, left, right)))

It would be nice if I could just exclude all sign combinations added as operators to the OPP automatically. At the moment I do it manually using the following implementation for my identifier:

let variable =
    let isAsciiIdContinue = isNoneOf "→*/+-<>=≠≤≥' ,();"

    identifier (IdentifierOptions(
                    isAsciiIdContinue = isAsciiIdContinue,
                    normalization = System.Text.NormalizationForm.FormKC,
                    allowAllNonAsciiCharsInPreCheck = true))

However, this doesn't seem to work. I get the following error message trying to parse my code:

  time→sleep(7);
      ^
The identifier contains an invalid character at the indicated position.

How can I make my variable parser stop on infix operators?

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

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

发布评论

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

评论(1

东北女汉子 2025-01-11 11:14:17

isAsciiIdStartisAsciiIdContinue 仅用于指定标识符中有效的 ASCII 字符。 identifier 解析器接受的非 ASCII 字符是那些通过预检查且有效的 Unicode XID 字符。

由于符号运算符不是有效的 Unicode XID 标识符字符,因此您可以简单地使用 IdentifierOptions(normalization = System.Text.NormalizationForm.FormKC)

isAsciiIdStart and isAsciiIdContinue are only meant to specify the ASCII chars valid in an identifier. The non-ASCII chars accepted by the identifier parser are those that pass the pre-check and are valid Unicode XID chars.

Since the symbolic operators aren't valid Unicode XID identifier chars, you could simply use IdentifierOptions(normalization = System.Text.NormalizationForm.FormKC).

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