I am working on a simple Xquery processor and using Antlr4 to parse the grammar. I use the visitor pattern to walk through the parse tree. Now I want to rewrite a query if the query meet the some condition. The processor now can process a query if the query directly use the keyword like "join" and meet the "join" grammar.
I want to first rewrite the parse tree if the query can be changed to a join query or do nothing if not. Is there a way to manually manipulate the parse tree? Like adding a rule context or construct a new parse tree?