gpt4 book ai didi

python - 在枚举(pypeg)中使用美元符号?

转载 作者:行者123 更新时间:2023-11-28 17:18:39 24 4
gpt4 key购买 nike

我想使用 pypeg 匹配 $f, $c, ..., $d 形式的类型,所以我尝试将其放入 Enum 中,如下所示:

class StatementType(Keyword):
grammar = Enum( K("$f"), K("$c"),
K("$v"), K("$e"),
K("$a"), K("$p"),
K("$d"))

然而,这失败了:

>>> k = parse("$d", StatementType)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/pypeg2/__init__.py", line 667, in parse
t, r = parser.parse(text, thing)
File "/usr/local/lib/python3.6/site-packages/pypeg2/__init__.py", line 794, in parse
raise r
File "<string>", line 1
$d
^
SyntaxError: expecting StatementType

我还尝试用 \$x 替换 $x 来转义 $ 字符。我还尝试在 r"\$x" 之前添加,希望它将其视为正则表达式对象。这些组合似乎都不起作用并给出相同的错误消息。如何让它与我给出的示例相匹配?

最佳答案

default regex for Keywords\w+。您可以通过设置 Keyword.regex 类变量来更改它:

class StatementType(Keyword):
grammar = Enum( K("$f"), K("$c"),
K("$v"), K("$e"),
K("$a"), K("$p"),
K("$d"))

Keyword.regex = re.compile(r"\$\w") # e.g. $a, $2, $_
k = parse("$d", StatementType)

关于python - 在枚举(pypeg)中使用美元符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42659136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com