0

I'm need of help parsing a custom file structured file. As you can see below is the structure. The issue is that I can't seem to parse the structure correctly, namely myOriginalFormula & myBonusType in the same group when I want them seperated for example.

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
            }
        }
    }
}

My try is blow. Could someone help me parse this structure correctly?

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

1 Answers1

0

Your definition of value is too greedy:

value = CharsNotIn("{};,")

I don't know how this works with the format you are parsing, but I get better results with this:

value = quotedString | CharsNotIn("{};,\n")
PaulMcG
  • 62,419
  • 16
  • 94
  • 130