简体   繁体   中英

Searching inside scala 2.10 ASTs

What's the best way to recursively search for an element in scala 2.10 ASTs?

The trees might be a result of power.trees(code) or mirror.mkToolBox().parseExpr(code) Edit . In 2.10.0-RC1 parseExpr has been renamed to parse .

The concrete use-case that I have is extracting the code of a method from a given class/object code by method name, but I assume that the question would be more relevant for others if formulated in a more generic way.

Maybe you should have a look at https://github.com/scala/scala/blob/2.10.x/src/reflect/scala/reflect/api/Trees.scala#L606 , especially at the classes Traverser, Transformer and the methods for substitution ( Tree.substituteSymbols , Tree.substituteTypes or Tree.substituteThis ). If you want to extract a method from a tree, you can use a Traverser and override the traverse method. In the traverse method, you check whether the node matches the method you want. If so, you are done. If not, you call super.traverse .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM