|
| 1 | +// force-host |
| 2 | +// no-prefer-dynamic |
| 3 | + |
| 4 | +#![crate_type = "proc-macro"] |
| 5 | + |
| 6 | +extern crate proc_macro; |
| 7 | +use proc_macro::{Ident, Group, TokenStream, TokenTree as Tt}; |
| 8 | + |
| 9 | +// This constant has to be above the ALLOCATING_ALGO_THRESHOLD |
| 10 | +// constant in inherent_impls_overlap.rs |
| 11 | +const REPEAT_COUNT: u32 = 501; |
| 12 | + |
| 13 | +#[proc_macro] |
| 14 | +/// Repeats the input many times, while replacing idents |
| 15 | +/// named "IDENT" with "id_$v", where v is a counter. |
| 16 | +pub fn repeat_with_idents(input: TokenStream) -> TokenStream { |
| 17 | + let mut res = Vec::new(); |
| 18 | + fn visit_stream(res: &mut Vec<Tt>, stream :TokenStream, v: u32) { |
| 19 | + let mut stream_iter = stream.into_iter(); |
| 20 | + while let Some(tt) = stream_iter.next() { |
| 21 | + match tt { |
| 22 | + Tt::Group(group) => { |
| 23 | + let tt = Tt::Group(visit_group(group, v)); |
| 24 | + res.push(tt); |
| 25 | + }, |
| 26 | + Tt::Ident(id) => { |
| 27 | + let id = if &id.to_string() == "IDENT" { |
| 28 | + Ident::new(&format!("id_{}", v), id.span()) |
| 29 | + } else { |
| 30 | + id |
| 31 | + }; |
| 32 | + res.push(Tt::Ident(id)); |
| 33 | + }, |
| 34 | + Tt::Punct(p) => { |
| 35 | + res.push(Tt::Punct(p)); |
| 36 | + }, |
| 37 | + Tt::Literal(lit) => { |
| 38 | + res.push(Tt::Literal(lit)); |
| 39 | + }, |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + fn visit_group(group :Group, v: u32) -> Group { |
| 44 | + let mut res = Vec::new(); |
| 45 | + visit_stream(&mut res, group.stream(), v); |
| 46 | + let stream = res.into_iter().collect(); |
| 47 | + let delim = group.delimiter(); |
| 48 | + Group::new(delim, stream) |
| 49 | + } |
| 50 | + for v in 0 .. REPEAT_COUNT { |
| 51 | + visit_stream(&mut res, input.clone(), v) |
| 52 | + } |
| 53 | + res.into_iter().collect() |
| 54 | +} |
0 commit comments