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

时间:2017-03-07 21:59:13

标签: python python-3.x pypeg

我希望使用pypeg匹配表单的类型public class MyRole : IdentityRole { public string CustomProperty { get; set; } } // What do I derive from here? // Or do I/can I override the Roles property? public ApplicationDbContext : IdentityDbContext<ApplicationUser> { public DbSet<MyRole> Roles { get; set; } } $f,...,$c,因此我尝试将其放入{{1}如下:

$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,希望它将它视为正则表达式对象。这些组合似乎都不起作用并给出相同的错误消息。如何让它与我给出的例子相符?

1 个答案:

答案 0 :(得分:2)

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)