I want to learn how to create refactor tools.

As an example: I want to create migration scripts  

for when some library removes deprecated function  

and we want to transform code to use the newer adequate functionality.

My idea was to use ANTLR to parse the code into AST,  

use some pattern matching on this tree to modify contents,  

and output the modified contents.

However, from what I read ANTLR isn't preserving formatting in AST tree,  

therefore it would be hard to get unbroken content back.

Do you have a solution that would comply with:

* allows me to modify code with preserving formatting
* (optionally) allows me to use AST transformations for code transformation
* (optionally) can transform variety languages like ANTLR

Question is not limited to one particular language,  

I'd be happy to heard solutions created for different languages.