用pyparsing解析递归模板

时间:2014-10-20 19:48:32

标签: python regex pyparsing

我目前正在尝试使用pyparsing解析递归模板。模板可以如下所示:

{{Attribute
| name=attr1
| description=First attribute.}}

模板有一个名称(Attribute)并定义了一些变量(name = attr1,description = First attribute。)。但是,还有一些模板可以包含零个或多个模板:

{{Enum
| name=MyEnum
| description=Just a test enum.
| example=Not given...
| attributes={{Attribute
| name=attr1
| description=First attribute.}}
{{Attribute
| name=attr2
| description=Second attribute.}}}}

为解析这些模板,我想出了以下内容:

template = Forward()
lb = '{{'
rb = '}}'
template_name = Word(alphas)
variable = Word(alphas)
value = CharsNotIn('|{}=') | Group(ZeroOrMore(template))
member = Group(Suppress('|') + variable + Suppress('=') + value)
members = Group(OneOrMore(member))
template << Suppress(lb) + Group(template_name + members) + Suppress(rb)

这很有效,但它不允许我使用&#34; | {} =&#34;如果我想使用它们,在一个有问题的值内。 E.g:

{{Enum
| name=MyEnum
| description=Just a test enum.
| example=<python>x = 1</python>
| attributes=}}

那么,我怎样才能更改我的代码以便允许这些字符呢?不幸的是,我不知道如何能够实现这一目标。

我希望有人可以给我一些提示!

1 个答案:

答案 0 :(得分:0)

我找到了我要找的东西:https://github.com/earwig/mwparserfromhell

相关问题