在枚举(pypeg)中使用美元符号?
Using a dollar sign in enum (pypeg)?
我想使用 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 变量来更改它:
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, , $_
k = parse("$d", StatementType)
我想使用 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 变量来更改它:
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, , $_
k = parse("$d", StatementType)