parse_generics

Macro parse_generics 

Source
macro_rules! parse_generics {
    ($($args:tt)*) => { ... };
}
Expand description

Parse generics

Input should be zero or many generics separated by , with an optional trailing , and end with EOF or >.

macro_rules! expect_output {
    (
        parsed_generics {
            generics {$($generics:tt)*} // the original generics with a trailing comma
            impl_generics {$($impl_generics:tt)*} // generics with a trailing comma but without default types and default exprs
            type_generics {$($type_generics:tt)*} // generics with a trailing comma that can be used as type parameters of a generic path
            generics_info {$($generics_info:tt)*} // info of all generics
        }
        rest { > }
    ) => {
        // ..
    };
}

syn_lite::parse_generics! {
    on_finish { expect_output! }
    input { 'a, 'b: 'c, T: ?Sized + FnOnce(), > }
}

Here is a full example:

input {
    /// lifetime
    'a: 'b + 'c,
    /// type param
    T: FnOnce() -> u8 + 'static + ?Sized = dyn FnOnce() -> u8,
    /// const
    const N: &'static str = "default expr"
}
/// output
parsed_generics {
    // the original generics with a trailing comma
    generics      { #[doc = r" lifetime"] 'a: 'b + 'c, #[doc = r" type param"] T: FnOnce() -> u8 + 'static + ?Sized = $DefaultType:ty, #[doc = r" const"] const N: $ConstType:ty = $const_default_expr:expr, }
    // generics with a trailing comma but without default types and default exprs
    impl_generics { #[doc = r" lifetime"] 'a: 'b + 'c, #[doc = r" type param"] T: FnOnce() -> u8 + 'static + ?Sized                  , #[doc = r" const"] const N: $ConstTyp_:ty                           , }
    // generics with a trailing comma that can be used as type parameters of a generic path
    type_generics { #[doc = r" lifetime"] 'a         , #[doc = r" type param"] T                                                     , #[doc = r" const"]       N                                          , }
    // info of all generics
    generics_info {
        {
            lifetime_attrs {#[doc = r" lifetime"]} // present if there are attributes
            lifetime { 'a }
            bounds { 'b + 'c } // present if there is a colon. This might be empty
        }
        {
            type_attrs {#[doc = r" type param"]} // present if there are attributes
            name { T }
            bounds { FnOnce() -> u8 + 'static + ?Sized } // present if there is a colon. This might be empty
            default_ty { $DefaultTypeOfT:ty } // present if there is a `= $default_ty:ty`
        }
        {
            const_attrs {#[doc = r" const"]} // present if there are attributes
            const { const }
            name { N }
            bounds { $TypeOfN:ty }
            default_expr { $DefaultExprOfN:expr } // present if there is a `= $default_expr:expr`
        }
    }
}
rest {}