简体   繁体   English

用pyparsing解析递归模板

[英]Parsing recursive templates with pyparsing

I'm currently trying to parse recursive templates with pyparsing. 我目前正在尝试使用pyparsing解析递归模板。 A template can look like this: 模板如下所示:

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

The template has a name (Attribute) and defines some variables (name = attr1, description = First attribute.). 模板有一个名称(属性),并定义了一些变量(名称= attr1,描述=第一个属性。)。 However, there are also templates which can contain zero or more templates: 但是,还有一些模板可以包含零个或多个模板:

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

To parse these templates I came up with the following: 为了解析这些模板,我提出了以下内容:

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)

This works quite well, but it does not allow me to use "|{}=" within a value which is problematic if I want to use them. 这很好用,但是不允许我在一个值中使用“ | {} =”,如果我想使用它们的话。 Eg: 例如:

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

So, how can I change my code so that it allows these characters, too? 那么,如何更改我的代码,使其也允许这些字符? Unforunately, I have no idea how I can archieve this. 不幸的是,我不知道该如何归档。

I hope someone can give me some tips! 希望有人能给我一些提示!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM