使用 PyParsing 解析自定义结构化文件

Parsing custom structured file with PyParsing

我需要帮助来解析自定义文件结构化文件。正如您在下面看到的结构。问题是我似乎无法正确解析结构,即 myOriginalFormulamyBonusType 在同一组中,例如当我希望将它们分开时。

AttributeDictionary SomeDictName
{
    myAttributeDefinitionCategories
    {
        AttributeDefinitionList SomeList
        {
            AttributeDefinition SomeDefinitioName < uid=8972789HHDUAI7 >
            {
                myOriginalFormula "(A_Variable) * 10"
                myBonusType FlatValue
            }
            AttributeDefinition UIBlankAttribute < uid=JIAIODJ7899 >
            {
            }
            AttributeDefinition SomeOtherDefinitionName < uid=17837HHJAJ7788 >
            {
                myOriginalFormula 1
                mySpecializations
                {
                    Specialization "Some_Specialization 1"
                    {
                        myCondition "Something.MustEqual = 1"
                        myFormula 0
                    }
                    Specialization "SomeSpecialization 2"
                    {
                        myCondition "Something.MustEqual = 2"
                        myFormula 0.026
                    }

                }
                myBonusType FlatValue
            }
            AttributeDefinition SomeReal_Other_definition < uid=6768GYAG//() >
            {
                myOriginalFormula "(Some_Formula / Other_Variable) - 1"
                myBonusType Percentage
                myUINumDecimals 1
                myHasAddSignUI FALSE
            }
        }
    }
}

我的尝试失败了。谁能帮我正确解析这个结构?

def syntaxParser():

    # constants
    left_bracket = Literal("{").suppress()
    right_bracket = Literal("}").suppress()
    semicolon = Literal(";").suppress()
    space = White().suppress()
    key = Word(alphanums + '_/"')
    value = CharsNotIn("{};,")

    # rules
    assignment = Group(key + Optional(space + value))
    block = Forward()
    specialblock = Forward()
    subblock = ZeroOrMore(Group(assignment) | specialblock)
    specialblock = (
        Keyword("Specialization")
        + key
        + left_bracket
        + subblock
        + right_bracket)

    block << Group(
        Optional(Group(key + Optional(space + value)))
        + left_bracket
        + Group(ZeroOrMore(assignment | Group(block) | Group(specialblock)))
        + right_bracket)

    script = block

    return script

你对value的定义太贪心了:

value = CharsNotIn("{};,")

我不知道这如何与您正在解析的格式一起工作,但我得到了更好的结果:

value = quotedString | CharsNotIn("{};,\n")