I'm parsing strings with dotted.strings and infix math operators. The parsing itself seems to work fine, but the name returned by getName is not what I expect.
from pyparsing import (Word, alphas, opAssoc, infixNotation,
Suppress, Group, delimitedList, oneOf)
LPAR, RPAR = map(Suppress, "()")
chars = Word(alphas + "_")
colstr = Group(delimitedList(chars, delim=".")).setResultsName("colstr*")
infix_expr = infixNotation(colstr, [
("-", 1, opAssoc.RIGHT),
(oneOf("* /"), 2, opAssoc.LEFT),
(oneOf("+ -"), 2, opAssoc.LEFT),
])("infix")
Now testing it out...
>>> infix_ex = infix_expr.parseString('a.b + x.y')
>>> print(infix_ex.dump())
[[['a', 'b'], '+', ['x', 'y']]]
- infix: [['a', 'b'], '+', ['x', 'y']]
- colstr: [['a', 'b'], ['x', 'y']]
[0]:
['a', 'b']
[1]:
['x', 'y']
This is what I expect, but getName seems to return the name from the wrong level of the parsing tree.
>>> infix_ex.getName()
'infix'
>>> infix_ex[0].getName()
'colstr'
>>> infix_ex[0].asList()
[['a', 'b'], '+', ['x', 'y']]
As you can see, getName returns 'infix' for the root level and 'colstr' for the 'infix' level of the tree.