41 releases (7 breaking)
new 0.8.28 | Jun 19, 2025 |
---|---|
0.8.16 | May 24, 2025 |
#152 in Procedural macros
1,655 downloads per month
Used in 5 crates
41KB
1K
SLoC
Some common simple tool for proc-macro impl
There are many methods available for chain calls
What can do:
- Quickly create processors in the style of
#foo(...)
- Common methods,
such as
add
,split_puncts
,is_ident
,as_ident
,into_punct
,is_joint
,as_punct_char
andset_spaned
etc span
,set_span
traitsParseIter
, can peek n, and peek jointed puncts
Examples
extern crate proc_macro;
use proc_macro::*;;
use proc_macro_tool::*;
fn index_tt<I>(mut tt: TokenTree, iter: &mut ParseIter<I>) -> Result<TokenTree, TokenStream>
where I: Iterator<Item = TokenTree>,
{
while let Some((mut span, mut param)) = iter
.next_if(|tt| tt.is_delimiter_bracket())
.map(|tt| tt.into_group().unwrap())
.map(|g| (g.span_close(), g.stream().into_iter()))
{
let i = param.next()
.ok_or_else(|| err!("unexpected token, expected literal", span))?
.span_as(&mut span)
.into_literal()
.map_err(|_| err!("unexpected token, expected literal", span))?
.to_string()
.parse()
.map_err(|e| err!(@("parse number {e}"), span))?;
let g = tt.into_group()
.map_err(|t| err!(@("cannot index {t}, e.g [...]"), span))?;
tt = g.stream().into_iter().nth(i)
.ok_or_else(|| err!(@("index {i} out of range, of {}", g), span))?
};
Ok(tt)
}