I am trying to create a parser for condition statements. Something like [X=2 OR Y contains A]
Here is what I came up with so far:
parameter = pp.Word(pp.alphanums).setResultsName('parameter')
operator = pp.oneOf(['=', '!=', 'contains', 'CONTAINS']).setResultsName('operator')
value = pp.Word(pp.alphanums).setResultsName('value')
condition = pp.Group(parameter + operator + value).setResultsName('condition')
expr = pp.operatorPrecedence(condition,[
("AND", 2, pp.opAssoc.LEFT, ),
("OR", 2, pp.opAssoc.LEFT, ),
]).setResultsName('expr')
parse_result = expr.parseString('X=2 OR Y contains A')
print(parse_result.dump()
which prints the following
[[['X', '=', '2'], 'OR', ['Y', 'contains', 'A']]]
- expr: [['X', '=', '2'], 'OR', ['Y', 'contains', 'A']]
- condition: ['Y', 'contains', 'A']
- operator: 'contains'
- parameter: 'Y'
- value: 'A'
And dump result has only the last condition and the first condition is missing in the result. How can I access to all of the conditions in parse result?