Syn is a parsing library for Rust source code that converts token streams into syntax trees, primarily designed for use in procedural macros.
Syn solves the problem of parsing Rust tokens into structured syntax trees that procedural macros can inspect and manipulate. It provides data structures representing Rust syntax at multiple levels of granularity, from complete files down to individual expressions and types. The library includes specialized entry points like DeriveInput for derive macros, along with generic parser functions that accept a ParseStream and return a Result, allowing both use of predefined syntax tree types and construction of custom syntaxes. Every parsed token carries span information tracking line and column positions, enabling procedural macros to generate error messages that point to exact locations in user code.
Syn is the natural choice for anyone writing procedural macros in Rust. It suits projects that need to parse and analyze Rust code at compile time, whether implementing derive macros, attribute macros, or function-like macros. The library's aggressive feature gating means you only pay compile-time costs for the syntax tree nodes and functionality your macro actually uses. The README positions Syn as the standard tool for this task, referencing a procedural macro workshop as the canonical learning resource.
The project maintains a comprehensive test suite covering the syntax tree implementations. Development activity shows consistent attention to correctness and compatibility with Rust's evolving syntax, including support for both stable and unstable language features. The library receives regular updates to track changes in the Rust language and maintain compatibility with the procedural macro ecosystem.