Skip to content

Latest commit

 

History

History

syn

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

syn crate for syntax analysis

https://crates.io/crates/syn

The Rust syn crate is a library that enables parsing, analyzing, and processing of Rust source code syntax. It is designed to allow developers to build tools like code generators, linters, and syntax highlighters.

The syn crate provides an abstract syntax tree (AST) of Rust code, which represents the structure and meaning of the code without including all the details and syntax of the source code. This makes it easier for developers to work with Rust code programmatically, as they do not need to parse and analyze the code manually.

The syn crate also provides support for parsing Rust code in different contexts, such as macros, attributes, and expressions, making it a versatile tool for Rust developers.

Example of a derive macro:

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(MyMacro)]
pub fn my_macro(input: TokenStream) -> TokenStream {
    // Parse the input tokens into a syntax tree
    let input = parse_macro_input!(input as DeriveInput);

    // Build the output, possibly using quasi-quotation
    let expanded = quote! {
        // ...
    };

    // Hand the output tokens back to the compiler
    TokenStream::from(expanded)
}