Tree Rewriting

Sometimes you wanna convert certain nodes to other nodes. This happens often when you lower language constructs to other constructs like lowering a for loop to a while loop. You could traverser the tree by your own using recursion and try build a new tree or you can use a custom visitor.

To make it easier to rewrite trees Silverfly introduces the Rewriter visitor that has already visitors registered for all common node types from the core. You can add your own visitor methods for your own node types or you can replace the default to get different behavior.

Some nodes require a token like the BinaryOperatorNode. But if you wanna replace a node with an operator call you need to get a temporary token to to make a valid tree. You could create a token by your self but you have to take care that all properties are set correctly. Especially the range to prevent errors in further processing like displaying error messages. Every parselet gives you a token that you can use to rewire to your own needs. It creates a copy and changes the token text.

Last updated