.NET 中最接近 pyparsing 的东西是什么?

发布于 2024-08-05 13:24:25 字数 1397 浏览 8 评论 0原文

我特别感兴趣的是能够将代码中的语法定义为普通代码,而没有任何不必要的麻烦。

我知道我可以使用 IronPython。我不想。

更新:

为了进一步解释我正在寻找的内容,我提供了一些示例 pyparsing 代码。这是一个不完整的解析器,用于将 emacs 快捷键转换为更常规的符号。当然,这个例子足够小,字符串函数就足够了,但这只是为了显示 pyparsing 的干净和简洁。

from pyparsing import Literal, OneOrMore, Optional, Word, printables, replaceWith

CTRL_MODIFIER = Literal('C').setParseAction(replaceWith('Ctrl'))
META_MODIFIER = Literal('M').setParseAction(replaceWith('Alt'))
MODIFIER = CTRL_MODIFIER | META_MODIFIER # Note operator overloading

SEPARATOR = Literal('-').setParseAction(replaceWith('+'))

MODIFIER_LIST = OneOrMore(MODIFIER + SEPARATOR)

KEY = Word(printables) # This is a "word" composed of any number of printable characters.

# The lambda functions here just join the tokens with the literal string 
# on which .join is called.
STROKE = (Optional(MODIFIER_LIST) + KEY).setParseAction(
    lambda tokens: ' '.join([str(token) for token in tokens]))
BINDING = OneOrMore(STROKE).setParseAction(
    lambda tokens: ', '.join([str(token) for token in tokens]))

# Example usage:
# >>> BINDING.transformString('M-/')
# Alt + /
# >>> BINDING.transformString('C-x C-f')
# Ctrl + x, Ctrl + f
# >>> BINDING.transformString('C-x f')
# Ctrl + x, f
# >>> BINDING.transformString('C-x M-c M-butterfly')
# Ctrl + x, Alt + c, Alt + butterfly

我希望能够在 .NET 中以尽可能少的行轻松编写语法。

What I'm especially interested in is the ability to define the grammar in the code as ordinary code without any unnecessary cruft.

I'm aware I could use IronPython. I don't want to.

UPDATE:

To further explain what I'm looking for, I'm including some sample pyparsing code. This is an incomplete parser to convert emacs shortcut keys to more conventional notation. This example is, of course, small enough that string functions would suffice, but it's just to show the cleanness and conciseness of pyparsing.

from pyparsing import Literal, OneOrMore, Optional, Word, printables, replaceWith

CTRL_MODIFIER = Literal('C').setParseAction(replaceWith('Ctrl'))
META_MODIFIER = Literal('M').setParseAction(replaceWith('Alt'))
MODIFIER = CTRL_MODIFIER | META_MODIFIER # Note operator overloading

SEPARATOR = Literal('-').setParseAction(replaceWith('+'))

MODIFIER_LIST = OneOrMore(MODIFIER + SEPARATOR)

KEY = Word(printables) # This is a "word" composed of any number of printable characters.

# The lambda functions here just join the tokens with the literal string 
# on which .join is called.
STROKE = (Optional(MODIFIER_LIST) + KEY).setParseAction(
    lambda tokens: ' '.join([str(token) for token in tokens]))
BINDING = OneOrMore(STROKE).setParseAction(
    lambda tokens: ', '.join([str(token) for token in tokens]))

# Example usage:
# >>> BINDING.transformString('M-/')
# Alt + /
# >>> BINDING.transformString('C-x C-f')
# Ctrl + x, Ctrl + f
# >>> BINDING.transformString('C-x f')
# Ctrl + x, f
# >>> BINDING.transformString('C-x M-c M-butterfly')
# Ctrl + x, Alt + c, Alt + butterfly

I would like to be able to write grammars in .NET with as much ease in as few lines.

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

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

发布评论

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

评论(3

万人眼中万个我 2024-08-12 13:24:25

看一下: Irony 它允许您在 C# 代码中定义语法

Take a look at: Irony It allows you to define your grammar in your c# code

離殇 2024-08-12 13:24:25

您可以尝试 NParsec,但它似乎不再被积极开发。

You could try NParsec, but it seems not to be actively developed any more.

深居我梦 2024-08-12 13:24:25

OSLO 项目将在几年后发布,并且将成为 pyparsing 的过度设计版本。

Project OSLO, which won't be released for another few years, and is going to be an over-engineered version of pyparsing.

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