Syn is a parsing library for parsing a stream of Rust tokens into a syntax tree of Rust source code.
This release features a redesigned AST that is more consistent and easier to understand than the previous one, and supports more comfortable idioms for traversing and building the syntax tree.
In addition, unlike all past Syn releases which focused on parsing strings of source code as input, this release is built around parsing the Rust compiler’s token representation used by 2.0 procedural macros. We provide a polyfill so that code written against the token-based API of tomorrow can work just as well on Rust compilers all the way back to the first stable support for procedural macros in Rust 1.15.0.
Whether you have used Syn extensively already or are looking at it for the first time, I encourage you to browse through our brand new examples directory featuring runnable working procedural macros of all sorts. Check out
heapsize2 for a 2.0-style custom derive with high quality error messages,
lazy-static for a function-like procedural macro that demonstrates the custom diagnostic API, and
trace-var for a procedural macro attribute that mutates a function body.
Error mesage from a 2.0-style custom derive
error[E0277]: the trait bound `std::thread::Thread: HeapSize` is not satisfied --> src/main.rs:7:5 | 7 | bad: std::thread::Thread, | ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HeapSize` is not implemented for `std::thread::Thread`
Custom warning from a procedural macro
warning: come on, pick a more creative name --> src/main.rs:10:16 | 10 | static ref FOO: String = "lazy_static".to_owned(); | ^^^
The 0.12 release represents many person-months of design iteration and documentation. Thanks in particular to @mystor for being a force multiplier and shouldering a lot of both the tricky and the tedious implementation work, @alexcrichton for the key idea of polyfilling Macros 2.0 support atop a Macros 1.1 shim and for outstanding rapid prototyping work in the futures-await library, and @jseyfried and @DroidLogician both for driving all of this procedural macro stuff forward on the compiler side with implementation, insightful discussion, and responsiveness.