Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying a node in a Scala AST

I understand that it is possible to obtain a Tree corresponding to a Scala expression, and also to traverse the resulting tree. Suppose that a given tree node matches some criterion, what method calls do I need to be able to modify it in place?, i.e. replace it with some other Tree?

object traverser extends Traverser {
    var applies = List[Apply]()

    override def traverse(tree: Tree): Unit = tree match {
        case someCriterion => modifyNodeSomehow // How to do this?
        // Otherwise...
        case app @ Apply(fun, args) =>
            applies = app :: applies
            super.traverse(fun)
            super.traverseTrees(args)
        case _ => super.traverse(tree)
    }
}
like image 660
NietzscheanAI Avatar asked Apr 29 '26 23:04

NietzscheanAI


1 Answers

In another question I was suggested to use Transformer to do something similar. AFAIK it is not possible to modify the tree in place, however.

like image 166
ghik Avatar answered May 01 '26 21:05

ghik