如何使标识符解析器停止在 FParsec 中的 OperationPrecedenceParser 运算符上?
我正在实现一个会使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
isAsciiIdStart
和isAsciiIdContinue
仅用于指定标识符中有效的 ASCII 字符。identifier
解析器接受的非 ASCII 字符是那些通过预检查且有效的 Unicode XID 字符。由于符号运算符不是有效的 Unicode XID 标识符字符,因此您可以简单地使用
IdentifierOptions(normalization = System.Text.NormalizationForm.FormKC)
。isAsciiIdStart
andisAsciiIdContinue
are only meant to specify the ASCII chars valid in an identifier. The non-ASCII chars accepted by theidentifier
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)
.