# Parse to AST, update, emit new Rust code?

**URL:** <https://internals.rust-lang.org/t/parse-to-ast-update-emit-new-rust-code/9120>\
**Category:** compiler\
**Created:** [December 29, 2018, 1:50am UTC](https://internals.rust-lang.org/t/parse-to-ast-update-emit-new-rust-code/9120 "2018-12-29T01:50:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamuelMarks](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/samuelmarks/32/5031_2.png) [@SamuelMarks](https://internals.rust-lang.org/u/SamuelMarks)\
**Post date:** [December 29, 2018, 1:50am UTC](https://internals.rust-lang.org/t/parse-to-ast-update-emit-new-rust-code/9120/1 "2018-12-29T01:50:43Z")

</div>

With TypeScript, there are tools built into its library for parsing and emitting TypeScript code. There are similar libraries built into Python (ast / 2to3), C++ (libclang), and others.

In Rust, I want to read Rust code in, modify the code, then output the newly modified code.

How do I do this?

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [December 29, 2018, 2:27am UTC](https://internals.rust-lang.org/t/parse-to-ast-update-emit-new-rust-code/9120/2 "2018-12-29T02:27:32Z")

</div>

I’m not sure if there is a stable way to do this. I know there have been a few efforts to right IDE-friendly stable Rust parsers, but I’m not sure if any of them is complete… here is one of them: [https://github.com/rust-analyzer/rowan](https://github.com/rust-analyzer/rowan)

Another thing you could do is look at what [`rustfmt`](https://github.com/rust-lang/rustfmt) does. I think it uses the compiler’s unstable interface.

---

<div class="post-metadata">

**Author:** ![vakaras](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vakaras/32/2156_2.png) [@vakaras](https://internals.rust-lang.org/u/vakaras)\
**Post date:** [December 29, 2018, 8:47am UTC](https://internals.rust-lang.org/t/parse-to-ast-update-emit-new-rust-code/9120/3 "2018-12-29T08:47:18Z")

</div>

Maybe you could use the [syn](https://docs.rs/crate/syn/0.15.23) and [quote](https://docs.rs/quote/0.6.10/quote/) crates? The intended use of these crates is for writing procedural macros that transform Rust ASTs. However, you should be able to use them in a standalone program. For example, for parsing Rust source code you could try to use [parse\_str](https://docs.rs/syn/0.15/syn/fn.parse_str.html) function.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 29, 2019, 8:47am UTC](https://internals.rust-lang.org/t/parse-to-ast-update-emit-new-rust-code/9120/4 "2019-03-29T08:47:20Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
