Crate syn_codegen[][src]

Expand description

Data structures that describe Syn’s syntax tree.

The Syn syntax tree is made up of more than 200 types. Occasionally it can come up that you need to implement some behavior across them all.

  • For example the Rust integration for AST Explorer wants to turn a syntax tree from Syn into a JavaScript value understood by the platform’s existing cross-language syntax tree visualization code.

  • As another example from within Syn itself, the traits and implementations of the visit, visit_mut, and fold modules can be generated programmatically from a description of the syntax tree.

To make this type of code as easy as possible to implement in any language, every Syn release comes with a machine-readable description of that version of the syntax tree as a JSON file syn.json. This syn-codegen crate provides the canonical data structures for parsing and making use of the representation in syn.json from Rust code.


use syn_codegen::Definitions;

const SYN: &str = include_str!("syn.json");

fn main() {
    let defs: Definitions = serde_json::from_str(SYN).unwrap();

    for node in &defs.types {
        println!("syn::{}", node.ident);



Top-level content of the syntax tree description.


Features behind which a syntax tree type is cfg gated.


Syntax tree type defined by Syn.


Type of a punctuated list.



Content of a syntax tree data structure.


Type of a struct field or tuple variant field in the syntax tree.

Type Definitions


Fields of a braced struct syntax tree node with named fields.


Variants of an enum syntax tree node.