Expand description
Better UX for proc-macro.
Inspired by crabtime.
Allows to receiving inputs and producing outputs in non TokenStream way.
This is the boring goblin craft: fewer raw token piles, more typed little bundles.
E.g. instead of:
fn foo(input: TokenStream) -> TokenStream {
let parser = syn::punctuated::Punctuated::<syn::LitStr, syn::Token![,]>::parse_terminated;
let lit_components = parser.parse2(input).unwrap();
let components = lit_components.iter().map(|c| c.value()).collect::<Vec<_>>();
// Handling of `components`
}One could write:
fn foo(components: CommaSeparated<Token>) -> TokenStream {
// Handling of `components`
}Since extending syn::parse::Parse with std types is not possible due to orphan rule.
We use macro parse_into!, that hardcodes checks for specific types.
Note: having String and Vec<String> in input params remove span information, and reduce IDE/diagnostics quality.
Output is a little bit more simple, it expected in three forms:
String- For strings that should be converted toTokenStreamwithout input span informationTokenStream- as basic case.- and in empty form - for cases where output is already emitted as
output_str!,output!macros.
So we have a trait IntoTokenStream that is solely focused on converting specific types into TokenStream.
The user can extend it as well, to support custom types in output.
Structs§
- Comma
Separated - Represents a comma separated list of parsable values.
- Snifed
Entries - Represents a group of
snif!entries. - Snifed
Entry - Represents a single entry in
snif!route.
Enums§
- Token
- Represents either
IdentorLitStrtoken.
Traits§
- Into
Token Stream - Convert specific type into
TokenStream.
Functions§
- push_
output - For some usages, user might want to emit output streamingly, like
println!orwrite!macros.