Is that not pretty much what Rust's procedural macros are? [0]
Except for the fact that they do not allow emitting invalid token streams (which is something you don't want anyways), it can do any arbitrary manipulation of source code, but in a way that is integrated with the compiler and less prone to breaking the program.
[0] https://doc.rust-lang.org/reference/procedural-macros.html