Expand description
§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.
Modules§
- pm1
proc-macro
- Type aliases for use with proc-macro.
- pm2
proc-macro2
- Type aliases for use with proc-macro2.
- prelude
- The prelude imports all the crate’s traits with
use ... as _
.
Structs§
- Byte
Character Literal literal-value
- A byte character literal. This can be converted to and from
LiteralValue
. - Byte
String Literal literal-value
- A byte string literal. This can be converted to and from
LiteralValue
. - CString
Literal literal-value
- A C string literal. This can be converted to and from
LiteralValue
. - Character
Literal literal-value
- A character literal. This can be converted to and from
LiteralValue
. - Error
- An error.
- F32Literal
literal-value
f32
suffixed floating point literal. This can be converted to and fromLiteralValue
.- F64Literal
literal-value
f64
suffixed floating point literal. This can be converted to and fromLiteralValue
.- Float
Literal literal-value
- Unsuffixed floating point literal. This can be converted to and from
LiteralValue
. - I8Literal
literal-value
i8
suffixed integer literal. This can be converted to and fromLiteralValue
.- I16Literal
literal-value
i16
suffixed integer literal. This can be converted to and fromLiteralValue
.- I32Literal
literal-value
i32
suffixed integer literal. This can be converted to and fromLiteralValue
.- I64Literal
literal-value
i64
suffixed integer literal. This can be converted to and fromLiteralValue
.- I128
Literal literal-value
i128
suffixed integer literal. This can be converted to and fromLiteralValue
.- IntLiteral
literal-value
- Unsuffixed integer literal. This can be converted to and from
LiteralValue
. - Isize
Literal literal-value
isize
suffixed integer literal. This can be converted to and fromLiteralValue
.- Op
- An operator. These can be parsed from tokens, and can also be used as parsers to parse one specific operator.
- OpParser
- Parser for specific sets of operators. Ops can be parsed via
Parser
or manually fromPunct
s. - OpParser
Instance - An instance for doing manual parsing of operators.
- Optional
- Wrap a parser in this to make it always succeed and return an
Option
. - PM1
proc-macro
- Marker type for the
proc-macro
crate. - PM2
proc-macro2
- Marker type for the
proc-macro2
crate. - Puncts
- Iterator over
Punct
s. - Punctuated
- Parsed punctuated values.
- Punctuated
Parser - Parser for punctuated values.
- String
Literal literal-value
- A string literal. This can be converted to and from
LiteralValue
. - Token
Buf - Borrowed version of
TokenBuffer
. - Token
Buffer - An owned buffer of tokens.
- U8Literal
literal-value
u8
suffixed integer literal. This can be converted to and fromLiteralValue
.- U16Literal
literal-value
u16
suffixed integer literal. This can be converted to and fromLiteralValue
.- U32Literal
literal-value
u32
suffixed integer literal. This can be converted to and fromLiteralValue
.- U64Literal
literal-value
u64
suffixed integer literal. This can be converted to and fromLiteralValue
.- U128
Literal literal-value
u128
suffixed integer literal. This can be converted to and fromLiteralValue
.- Usize
Literal literal-value
usize
suffixed integer literal. This can be converted to and fromLiteralValue
.
Enums§
- Delimiter
Kind - Delimiter. This is exactly like
proc_macro*::Delimiter
, but doesn’t depend on which proc-macro crate you use. - Literal
Value literal-value
- A literal token. This is like
Literal
fromproc-macro*
, except that the value has already been parsed and is available at no cost. You can convert it to and fromLiteral
withinto
. - Match
- Match results.
- Token
Tree Kind - 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§
- AsToken
Buf - Automatically implemented for types that implement
Into<&TokenBuf>
for&Type
. - AsToken
BufMut - Automatically implemented for types that implement
Into<&mut TokenBuf>
for&mut Type
. - Default
Parser - Trait for making a default parser. This is automatically implemented for objects
that implement the
Parse
trait. - Delimiter
Delimiter
API trait. Seeproc_macro::Delimiter
.- Delimiter
Ext - Extensions for
Delimiter
. - Group
Group
API trait. Seeproc_macro::Group
.- Group
Ext - Extensions for
Group
. - Ident
Ident
API trait. Seeproc_macro::Ident
.- Ident
Ext - Extensions for
Ident
. - Into
Tokens - Trait for converting an object into its token representation.
- Literal
Literal
API trait. Seeproc_macro::Literal
.- Literal
Ext - Extensions for
Literal
. - Match
OpFn - Function for OpParser to use to match operators.
- PM
- Proc-macro selector for when there isn’t one specific proc-macro type to use.
- PMExt
- Enable extension traits for
PM
. - Parse
- Parse from a
TokenBuf
. - Parser
- A parser for parsing values from a
TokenBuf
. - Proc
Macro - Base trait with associated type aliases for types from
proc-macro
/proc-macro2
. See alsoProcMacroExt
. - Proc
Macro Ext - Adds extra bounds to the associated types of
ProcMacro
to enable extension traits. - Punct
Punct
API trait. Seeproc_macro::Punct
.- Punct
Ext - Extensions for
Punct
. - Spacing
Spacing
API trait. Seeproc_macro::Spacing
.- Spacing
Ext - Extensions for
Spacing
. - Span
Span
API trait. Seeproc_macro::Span
.- SpanExt
- Extensions for
Span
. - ToToken
Buffer - Methods for making or extending a
TokenBuffer
with tokens representing this object. This is automatically implemented for types that implement theIntoTokens
trait. - ToToken
Stream - Methods for making or extending a
TokenStream
with a representation of this object. - ToTokens
- Trait for getting the token representation of an object.
- Token
Stream TokenStream
API trait. Seeproc_macro::TokenStream
.- Token
Stream Ext - Extensions for
TokenStream
. - Token
Tree TokenTree
API trait. Seeproc_macro::TokenTree
.- Token
Tree Ext - Extensions for
TokenTree
.
Functions§
- op
- Convenience function for calling
Op::new_static
. It can be used for parsing a specific op. - proclet
- Optional wrapper for proc macros.
- punctuated
- Create a new parser for parsing things with
main
punctuated bydelim
. Convenience function for callingPunctuatedParser::new
.