Closed x23n5902y closed 2 years ago
I think the best way is to create a transformer with v_args(tree=True)
, like this:
@v_args(tree=True)
class T(Transformer):
def my_rule(self, tree):
return tree if random() > 0.5 else "removed"
(not tested but should work)
thanks a lot. I achieved my point by converting a tree to a python dict(), then parsing it again by json grammar and then reconstructing back
What did you think of the reconstructor?
It's still written in the docs as an experimental feature, but I wonder maybe it's stable enough by now.
I had to change the grammar a bit because the reconstructor didn't accept regular expressions in TERMINALS. In general, I did not get any problems. The text after the reconstructor is completely equal to the original text
Nice to hear!
Yes, it could be improved. Maybe always resolve regexps to their shortest match. But that doesn't sound like a simple implementation.
What is your question?
Hello. I'm making a parser that works with an INI-like config file. I also want to change the value of a parameter in a specific section. I don't understand how to conditionally change the tree using the Visitor, Transformer or Interpreter classes. I would appreciate any help.
If you're having trouble with your code or grammar
If I add the Transformer class to my code, then it changes all branches that correspond to the terminal.
The result is
I would like to understand how you can make a condition for replacing text