3 releases (breaking)

0.3.0 Mar 21, 2024
0.2.0 Feb 17, 2024
0.1.0 Feb 16, 2024

#168 in Procedural macros

Download history 49/week @ 2024-02-10 286/week @ 2024-02-17 66/week @ 2024-02-24 5/week @ 2024-03-02 2/week @ 2024-03-09 96/week @ 2024-03-16 18/week @ 2024-03-23 23/week @ 2024-03-30 1/week @ 2024-04-06

69 downloads per month
Used in 3 crates

MIT OR Apache-2.0 OR Zlib

145KB
3K SLoC

proclet: proc macros made easy

⚠️ proclet is still in early development. It's missing some basic features and may get major design changes, and documentation is a work in progress.

proclet can be used with either proc-macro or proc-macro2, or both. Most of the types of the proc-macro crates are abstracted into traits, and proclet's types are generic over these traits. If you run into type inference issues, there's proc-macro specific aliases for the proclet types in the pm1 and pm2 modules.

Here's how you'd make a proc macro that takes a set of comma separated strings as arguments (last comma optional):

#[proc_macro]
pub fn my_proc_macro(input: TokenStream) -> TokenStream {
    proclet(input, |input| {
        let args = punctuated(StringLiteral::parser(), op(",")).parse_all(input)?;
        // ...
    })
}

The proclet function is an optional wrapper that converts the input to a TokenBuf ready for parsing, converts the output back into a TokenStream, and handles errors by making them nice spanned compiler errors instead of panics.

parse_all returns an error if there's tokens left in the buffer after parsing. To leave the rest of the buffer for the next parser to parse, use the parse method instead.

You can combine parsers to parse more complex objects like punctuated does in the example above. Types that implement the Parse trait can be parsed directly:

let string = StringLiteral::parse(input)?;

The input is automatically advanced to point past the parsed object on success.

Dependencies