Expand description
§proclet: proc macros made easy
⚠️
procletis 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.
Modules§
- pm1
proc-macroType aliases for use with proc-macro. - pm2
proc-macro2Type aliases for use with proc-macro2. - The prelude imports all the crate’s traits with
use ... as _.
Structs§
- ByteCharacterLiteral
literal-valueA byte character literal. This can be converted to and fromLiteralValue. - ByteStringLiteral
literal-valueA byte string literal. This can be converted to and fromLiteralValue. - CharacterLiteral
literal-valueA character literal. This can be converted to and fromLiteralValue. - An error.
- F32Literal
literal-valuef32suffixed floating point literal. This can be converted to and fromLiteralValue. - F64Literal
literal-valuef64suffixed floating point literal. This can be converted to and fromLiteralValue. - FloatLiteral
literal-valueUnsuffixed floating point literal. This can be converted to and fromLiteralValue. - I8Literal
literal-valuei8suffixed integer literal. This can be converted to and fromLiteralValue. - I16Literal
literal-valuei16suffixed integer literal. This can be converted to and fromLiteralValue. - I32Literal
literal-valuei32suffixed integer literal. This can be converted to and fromLiteralValue. - I64Literal
literal-valuei64suffixed integer literal. This can be converted to and fromLiteralValue. - I128Literal
literal-valuei128suffixed integer literal. This can be converted to and fromLiteralValue. - IntLiteral
literal-valueUnsuffixed integer literal. This can be converted to and fromLiteralValue. - IsizeLiteral
literal-valueisizesuffixed integer literal. This can be converted to and fromLiteralValue. - An operator. These can be parsed from tokens, and can also be used as parsers to parse one specific operator.
- Parser for specific sets of operators. Ops can be parsed via
Parseror manually fromPuncts. - An instance for doing manual parsing of operators.
- Wrap a parser in this to make it always succeed and return an
Option. - PM1
proc-macroMarker type for theproc-macrocrate. - PM2
proc-macro2Marker type for theproc-macro2crate. - Iterator over
Puncts. - Parsed punctuated values.
- Parser for punctuated values.
- StringLiteral
literal-valueA string literal. This can be converted to and fromLiteralValue. - Borrowed version of
TokenBuffer. - An owned buffer of tokens.
- U8Literal
literal-valueu8suffixed integer literal. This can be converted to and fromLiteralValue. - U16Literal
literal-valueu16suffixed integer literal. This can be converted to and fromLiteralValue. - U32Literal
literal-valueu32suffixed integer literal. This can be converted to and fromLiteralValue. - U64Literal
literal-valueu64suffixed integer literal. This can be converted to and fromLiteralValue. - U128Literal
literal-valueu128suffixed integer literal. This can be converted to and fromLiteralValue. - UsizeLiteral
literal-valueusizesuffixed integer literal. This can be converted to and fromLiteralValue.
Enums§
- Delimiter. This is exactly like
proc_macro*::Delimiter, but doesn’t depend on which proc-macro crate you use. - LiteralValue
literal-valueA literal token. This is likeLiteralfromproc-macro*, except that the value has already been parsed and is available at no cost. You can convert it to and fromLiteralwithinto. - Match results.
- The kind of a
TokenTree. This is like the enum inproc_macro*::TokenTree, but without any contained data. It doesn’t depend on which proc-macro crate you use.
Traits§
- Automatically implemented for types that implement
Into<&TokenBuf>for&Type. - Automatically implemented for types that implement
Into<&mut TokenBuf>for&mut Type. - Trait for making a default parser. This is automatically implemented for objects that implement the
Parsetrait. DelimiterAPI trait. Seeproc_macro::Delimiter.- Extensions for
Delimiter. GroupAPI trait. Seeproc_macro::Group.- Extensions for
Group. IdentAPI trait. Seeproc_macro::Ident.- Extensions for
Ident. - Trait for converting an object into its token representation.
LiteralAPI trait. Seeproc_macro::Literal.- Extensions for
Literal. - Function for OpParser to use to match operators.
- Proc-macro selector for when there isn’t one specific proc-macro type to use.
- Enable extension traits for
PM. - Parse from a
TokenBuf. - A parser for parsing values from a
TokenBuf. - Base trait with associated type aliases for types from
proc-macro/proc-macro2. See alsoProcMacroExt. - Adds extra bounds to the associated types of
ProcMacroto enable extension traits. PunctAPI trait. Seeproc_macro::Punct.- Extensions for
Punct. SpacingAPI trait. Seeproc_macro::Spacing.- Extensions for
Spacing. SpanAPI trait. Seeproc_macro::Span.- Extensions for
Span. - Methods for making or extending a
TokenBufferwith tokens representing this object. This is automatically implemented for types that implement theIntoTokenstrait. - Methods for making or extending a
TokenStreamwith a representation of this object. - Trait for getting the token representation of an object.
TokenStreamAPI trait. Seeproc_macro::TokenStream.- Extensions for
TokenStream. TokenTreeAPI trait. Seeproc_macro::TokenTree.- Extensions for
TokenTree.
Functions§
- Convenience function for calling
Op::new_static. It can be used for parsing a specific op. - Optional wrapper for proc macros.
- Create a new parser for parsing things with
mainpunctuated bydelim. Convenience function for callingPunctuatedParser::new.