Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upRe-using nested parse elements with different parse actions #189
Comments
It's a bit nasty, but I suppose one solution to this might be creating a function like I'm envisaging that For example, it would work like this for the above example: def python2Eval(expr):
py2Expression = expression.copy().alterChild(op, lambda oldOp: oldOp.copy().setParseAction(lambda s, lok, toks: {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv
}[toks[0]])
return py2Expression.parseString(expr) |
What if you use a Forward for the binary operator, as a late-binding expression to be defined just before parsing:
Would this work for you? |
That's certainly an interesting solution. It's a tad ugly because I have to define the |
Lets say I want to make a library of parser elements that are not tied to a particular processing action, and then I want to use those elements for two different use-cases. In this case, evaluating math as if it's in python 2 vs python 3:
See how, even though the actual token descriptions are correct for both languages, the parsing behaviour must be different. But there's no neat way of saying "I want to re-use the entire expression tree, but setting the operator parse action to something different". I'm encountering this in another non-trivial situation I'm using the parser for, as well.