.NET 中最接近 pyparsing 的东西是什么?
我特别感兴趣的是能够将代码中的语法定义为普通代码,而没有任何不必要的麻烦。
我知道我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下: Irony 它允许您在 C# 代码中定义语法
Take a look at: Irony It allows you to define your grammar in your c# code
您可以尝试 NParsec,但它似乎不再被积极开发。
You could try NParsec, but it seems not to be actively developed any more.
OSLO 项目将在几年后发布,并且将成为 pyparsing 的过度设计版本。
Project OSLO, which won't be released for another few years, and is going to be an over-engineered version of pyparsing.