1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
//! A syntactic for loop.
//!
//! For example, the following takes the sum of the bit-length of four integer
//! types:
//! ```
//! # use syntactic_for::syntactic_for;
//! let sum = syntactic_for!{ ty in [ u8, u16, u32, u64 ] {
//! [$( <$ty>::BITS ),*].into_iter().sum::<u32>()
//! }};
//! assert_eq!(sum, 120);
//! ```
//!
//! # Usage
//!
//! The syntax is as follows:
//! ```ignore
//! syntactic_for!{ IDENTIFIER in [ EXPRESSION, EXPRESSION, ... ] {
//! BODY
//! }}
//! ```
//! where `BODY` works similarly to `macro_rules!`, that is:
//! `$($IDENTIFIER)SEPARATOR*` will expand and substitute `IDENTIFIER` with
//! each `EXPRESSION`, separating the expansions with `SEPARATOR`.
//!
//! `SEPARATOR` can be any non-`*` punctuation. Hence, the example from above
//! could also be written without an iterator:
//! ```
//! # use syntactic_for::syntactic_for;
//! # let sum =
//! # syntactic_for!{ ty in [ u8, u16, u32, u64 ] {
//! $( <$ty>::BITS )+*
//! # }};
//! # assert_eq!(sum, 120);
//! ```
//!
//! # Examples
//!
//! ## Loop unrolling
//!
//! Sum the elements of an array with
//! [loop unrolling](https://en.wikipedia.org/wiki/Loop_unrolling):
//! ```
//! # use syntactic_for::syntactic_for;
//! let array = b"oh my, I am getting summed!";
//! let mut acc = 0u32;
//! let mut i = 0;
//! while i <= array.len()-4 {
//! syntactic_for!{ offset in [ 0, 1, 2, 3 ] {$(
//! acc += array[i + $offset] as u32;
//! )*}}
//! i += 4;
//! }
//! for j in i..array.len() {
//! acc += array[j] as u32;
//! }
//! assert_eq!(acc, 2366);
//! ```
//!
//! ## Matching
//!
//! Find the maximum value of an integer type of the given bit size:
//! ```
//! # use syntactic_for::syntactic_for;
//! # let bit_size = 16;
//! let max_size = syntactic_for!{ ty in [ u8, u16, u32, u64, u128 ] {
//! match bit_size {
//! $(<$ty>::BITS => <$ty>::MAX as u128,)*
//! other => panic!("No integer of size {other}"),
//! }
//! }};
//! # assert_eq!(max_size, u16::MAX as u128)
//! ```
//!
//! ## `impl` blocks
//!
//! Implement a trait for a set of types:
//! ```
//! # use syntactic_for::syntactic_for;
//! # trait MyTrait {}
//! syntactic_for!{ ty in [ u8, u16, u32, u64, u128 ] {$(
//! impl MyTrait for $ty {
//! // snip.
//! }
//! )*}}
//! ```
//!
//! ## Custom syntactic loop
//!
//! A useful design pattern is to define a custom macro that expands to a
//! syntactic loop over a given set of expressions:
//! ```
//! # struct CustomType1;
//! # struct CustomType2;
//! #[doc(hidden)]
//! pub extern crate syntactic_for;
//!
//! #[macro_export]
//! macro_rules! for_each_custom_type {
//! ($ident:ident { $($tt:tt)* }) => {
//! $crate::syntactic_for::syntactic_for! { $ident in [
//! $crate::CustomType1,
//! $crate::CustomType2,
//! // etc.
//! ] { $($tt)* } }
//! }
//! }
//! ```
//!
//! For example, a library could expose `for_each_custom_type` as a way of
//! letting its users write syntactic loops over a set of types defined in the
//! library. Then, it becomes possible to add types to that loop inside the
//! library, whithout requiring any change on the user's end:
//!
//! ```
//! # struct CustomType1;
//! # impl CustomType1 { fn parse(i: &str) -> Result<(), ()> { Err(()) }}
//! # struct CustomType2;
//! # impl CustomType2 { fn parse(i: &str) -> Result<(), ()> { Ok(()) }}
//! # pub extern crate syntactic_for;
//! # #[macro_export]
//! # macro_rules! for_each_custom_type {
//! # ($ident:ident { $($tt:tt)* }) => {
//! # ::syntactic_for::syntactic_for! { $ident in [
//! # CustomType1,
//! # CustomType2,
//! # // etc.
//! # ] { $($tt)* } }
//! # }
//! # }
//! # mod my_library { pub use for_each_custom_type; }
//! // Try and parse each library type in succession, stopping at the first
//! // success:
//! fn can_parse(input: &str) -> bool {
//! my_library::for_each_custom_type! { ty {
//! $(if let Ok(parsed) = <$ty>::parse(input) {
//! return true;
//! })*
//! }}
//! return false;
//! }
//! # assert_eq!(can_parse("foo"), true);
//! ```
use proc_macro2::{TokenStream, TokenTree};
use quote::{ToTokens, TokenStreamExt};
use syn::{
braced, bracketed,
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
token, Expr, Ident, Token,
};
struct SyntacticFor {
ident: Ident,
_in_token: Token![in],
_bracket_token: token::Bracket,
exprs: Punctuated<Expr, Token![,]>,
_brace_token: token::Brace,
body: TokenStream,
}
impl Parse for SyntacticFor {
fn parse(input: ParseStream) -> syn::Result<Self> {
let brackets;
let braces;
Ok(SyntacticFor {
ident: input.parse()?,
_in_token: input.parse()?,
_bracket_token: bracketed!(brackets in input),
exprs: Punctuated::parse_terminated(&brackets)?,
_brace_token: braced!(braces in input),
body: braces.parse()?,
})
}
}
fn subs_group<'a, S, IntoIter>(
pattern: &Ident,
subs: &'a S,
tokens: TokenStream,
) -> syn::Result<TokenStream>
where
&'a S: IntoIterator<IntoIter = IntoIter> + Clone + 'a,
IntoIter: ExactSizeIterator,
<IntoIter as Iterator>::Item: ToTokens,
{
let mut output = TokenStream::new();
let mut tokens = tokens.into_iter().peekable();
while let Some(token) = tokens.next() {
match token {
TokenTree::Punct(punct) if punct.as_char() == '$' => match tokens.next() {
Some(TokenTree::Group(group)) => {
let separator = match tokens.peek() {
Some(TokenTree::Punct(punct)) if punct.as_char() == '*' => None,
Some(TokenTree::Punct(_)) => {
if let TokenTree::Punct(punct) = tokens.next().unwrap() {
Some(punct)
} else {
unreachable!()
}
}
Some(token) => {
return Err(syn::Error::new_spanned(
token,
format!("expected punctuation or `*`, found {}", token),
))
}
None => panic!("unexpected end of stream after group"),
};
match tokens.next() {
Some(TokenTree::Punct(punct)) if punct.as_char() == '*' => {}
Some(token) => {
return Err(syn::Error::new_spanned(
&token,
format!("expected `*`, found {}", token),
))
}
None => panic!("unexpected end of stream after group"),
}
let subs = subs.into_iter();
let len = subs.len();
for (i, sub) in subs.enumerate() {
output.extend(subs_ident(pattern, &sub, group.stream())?);
if i + 1 < len {
if let Some(separator) = &separator {
output.append(separator.clone());
}
}
}
}
Some(token) => {
return Err(syn::Error::new_spanned(
&token,
format!("expected group after `$`, found `{}`", token),
))
}
None => {
panic!("unexpected end of stream after `$`")
}
},
TokenTree::Group(group) => {
output.append(proc_macro2::Group::new(
group.delimiter(),
subs_group(pattern, subs, group.stream())?,
));
}
token => output.append(token),
}
}
Ok(output)
}
fn subs_ident<'a, I>(pattern: &Ident, sub: &'a I, tokens: TokenStream) -> syn::Result<TokenStream>
where
&'a I: ToTokens,
{
let mut output = TokenStream::new();
let mut tokens = tokens.into_iter();
while let Some(token) = tokens.next() {
match token {
TokenTree::Punct(punct) if punct.as_char() == '$' => match tokens.next() {
Some(TokenTree::Ident(ident)) if &ident == pattern => {
sub.to_tokens(&mut output);
}
Some(token) => {
return Err(syn::Error::new_spanned(
&token,
format!("expected `{}` after `$`, found `{}`", pattern, token),
))
}
None => {
panic!("unexpected end of stream after `$`")
}
},
TokenTree::Group(group) => {
output.append(proc_macro2::Group::new(
group.delimiter(),
subs_ident(pattern, sub, group.stream())?,
));
}
token => output.append(token),
}
}
Ok(output)
}
/// Iterate over a list of (syntactic) expressions.
///
/// For details, see [top level documentation][crate].
#[proc_macro]
pub fn syntactic_for(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let SyntacticFor {
ident, exprs, body, ..
} = parse_macro_input!(input as SyntacticFor);
subs_group(&ident, &exprs, body)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}