Loading...
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 | // SPDX-License-Identifier: Apache-2.0 OR MIT use proc_macro::{TokenStream, TokenTree}; pub(crate) trait ToTokens { fn to_tokens(&self, tokens: &mut TokenStream); } impl<T: ToTokens> ToTokens for Option<T> { fn to_tokens(&self, tokens: &mut TokenStream) { if let Some(v) = self { v.to_tokens(tokens); } } } impl ToTokens for proc_macro::Group { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend([TokenTree::from(self.clone())]); } } impl ToTokens for TokenTree { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend([self.clone()]); } } impl ToTokens for TokenStream { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(self.clone()); } } /// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with /// the given span. /// /// This is a similar to the /// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the /// `quote` crate but provides only just enough functionality needed by the current `macros` crate. macro_rules! quote_spanned { ($span:expr => $($tt:tt)*) => { #[allow(clippy::vec_init_then_push)] { let mut tokens = ::std::vec::Vec::new(); let span = $span; quote_spanned!(@proc tokens span $($tt)*); ::proc_macro::TokenStream::from_iter(tokens) }}; (@proc $v:ident $span:ident) => {}; (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => { let mut ts = ::proc_macro::TokenStream::new(); $crate::quote::ToTokens::to_tokens(&$id, &mut ts); $v.extend(ts); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => { for token in $id { let mut ts = ::proc_macro::TokenStream::new(); $crate::quote::ToTokens::to_tokens(&token, &mut ts); $v.extend(ts); } quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => { let mut tokens = ::std::vec::Vec::new(); quote_spanned!(@proc tokens $span $($inner)*); $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( ::proc_macro::Delimiter::Parenthesis, ::proc_macro::TokenStream::from_iter(tokens) ))); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => { let mut tokens = ::std::vec::Vec::new(); quote_spanned!(@proc tokens $span $($inner)*); $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( ::proc_macro::Delimiter::Bracket, ::proc_macro::TokenStream::from_iter(tokens) ))); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => { let mut tokens = ::std::vec::Vec::new(); quote_spanned!(@proc tokens $span $($inner)*); $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new( ::proc_macro::Delimiter::Brace, ::proc_macro::TokenStream::from_iter(tokens) ))); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident :: $($tt:tt)*) => { $v.push(::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Joint) )); $v.push(::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone) )); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident : $($tt:tt)*) => { $v.push(::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone) )); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident , $($tt:tt)*) => { $v.push(::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new(',', ::proc_macro::Spacing::Alone) )); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident @ $($tt:tt)*) => { $v.push(::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('@', ::proc_macro::Spacing::Alone) )); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident ! $($tt:tt)*) => { $v.push(::proc_macro::TokenTree::Punct( ::proc_macro::Punct::new('!', ::proc_macro::Spacing::Alone) )); quote_spanned!(@proc $v $span $($tt)*); }; (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => { $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(stringify!($id), $span))); quote_spanned!(@proc $v $span $($tt)*); }; } /// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with /// mixed site span ([`Span::mixed_site()`]). /// /// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro /// from the `quote` crate but provides only just enough functionality needed by the current /// `macros` crate. /// /// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site macro_rules! quote { ($($tt:tt)*) => { quote_spanned!(::proc_macro::Span::mixed_site() => $($tt)*) } } |