1 // SPDX-License-Identifier: Apache-2.0 OR MIT
3 use proc_macro::{TokenStream, TokenTree};
5 pub(crate) trait ToTokens {
6 fn to_tokens(&self, tokens: &mut TokenStream);
9 impl<T: ToTokens> ToTokens for Option<T> {
10 fn to_tokens(&self, tokens: &mut TokenStream) {
11 if let Some(v) = self {
17 impl ToTokens for proc_macro::Group {
18 fn to_tokens(&self, tokens: &mut TokenStream) {
19 tokens.extend([TokenTree::from(self.clone())]);
23 impl ToTokens for TokenTree {
24 fn to_tokens(&self, tokens: &mut TokenStream) {
25 tokens.extend([self.clone()]);
29 impl ToTokens for TokenStream {
30 fn to_tokens(&self, tokens: &mut TokenStream) {
31 tokens.extend(self.clone());
35 /// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
38 /// This is a similar to the
39 /// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
40 /// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
41 macro_rules! quote_spanned {
42 ($span:expr => $($tt:tt)*) => {{
44 #[allow(clippy::vec_init_then_push)]
46 tokens = ::std::vec::Vec::new();
48 quote_spanned!(@proc tokens span $($tt)*);
50 ::proc_macro::TokenStream::from_iter(tokens)
52 (@proc $v:ident $span:ident) => {};
53 (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
54 let mut ts = ::proc_macro::TokenStream::new();
55 $crate::quote::ToTokens::to_tokens(&$id, &mut ts);
57 quote_spanned!(@proc $v $span $($tt)*);
59 (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
61 let mut ts = ::proc_macro::TokenStream::new();
62 $crate::quote::ToTokens::to_tokens(&token, &mut ts);
65 quote_spanned!(@proc $v $span $($tt)*);
67 (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
68 let mut tokens = ::std::vec::Vec::new();
69 quote_spanned!(@proc tokens $span $($inner)*);
70 $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
71 ::proc_macro::Delimiter::Parenthesis,
72 ::proc_macro::TokenStream::from_iter(tokens)
74 quote_spanned!(@proc $v $span $($tt)*);
76 (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
77 let mut tokens = ::std::vec::Vec::new();
78 quote_spanned!(@proc tokens $span $($inner)*);
79 $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
80 ::proc_macro::Delimiter::Bracket,
81 ::proc_macro::TokenStream::from_iter(tokens)
83 quote_spanned!(@proc $v $span $($tt)*);
85 (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
86 let mut tokens = ::std::vec::Vec::new();
87 quote_spanned!(@proc tokens $span $($inner)*);
88 $v.push(::proc_macro::TokenTree::Group(::proc_macro::Group::new(
89 ::proc_macro::Delimiter::Brace,
90 ::proc_macro::TokenStream::from_iter(tokens)
92 quote_spanned!(@proc $v $span $($tt)*);
94 (@proc $v:ident $span:ident :: $($tt:tt)*) => {
95 $v.push(::proc_macro::TokenTree::Punct(
96 ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Joint)
98 $v.push(::proc_macro::TokenTree::Punct(
99 ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone)
101 quote_spanned!(@proc $v $span $($tt)*);
103 (@proc $v:ident $span:ident : $($tt:tt)*) => {
104 $v.push(::proc_macro::TokenTree::Punct(
105 ::proc_macro::Punct::new(':', ::proc_macro::Spacing::Alone)
107 quote_spanned!(@proc $v $span $($tt)*);
109 (@proc $v:ident $span:ident , $($tt:tt)*) => {
110 $v.push(::proc_macro::TokenTree::Punct(
111 ::proc_macro::Punct::new(',', ::proc_macro::Spacing::Alone)
113 quote_spanned!(@proc $v $span $($tt)*);
115 (@proc $v:ident $span:ident @ $($tt:tt)*) => {
116 $v.push(::proc_macro::TokenTree::Punct(
117 ::proc_macro::Punct::new('@', ::proc_macro::Spacing::Alone)
119 quote_spanned!(@proc $v $span $($tt)*);
121 (@proc $v:ident $span:ident ! $($tt:tt)*) => {
122 $v.push(::proc_macro::TokenTree::Punct(
123 ::proc_macro::Punct::new('!', ::proc_macro::Spacing::Alone)
125 quote_spanned!(@proc $v $span $($tt)*);
127 (@proc $v:ident $span:ident ; $($tt:tt)*) => {
128 $v.push(::proc_macro::TokenTree::Punct(
129 ::proc_macro::Punct::new(';', ::proc_macro::Spacing::Alone)
131 quote_spanned!(@proc $v $span $($tt)*);
133 (@proc $v:ident $span:ident + $($tt:tt)*) => {
134 $v.push(::proc_macro::TokenTree::Punct(
135 ::proc_macro::Punct::new('+', ::proc_macro::Spacing::Alone)
137 quote_spanned!(@proc $v $span $($tt)*);
139 (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
140 $v.push(::proc_macro::TokenTree::Ident(::proc_macro::Ident::new(stringify!($id), $span)));
141 quote_spanned!(@proc $v $span $($tt)*);
145 /// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
146 /// mixed site span ([`Span::mixed_site()`]).
148 /// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
149 /// from the `quote` crate but provides only just enough functionality needed by the current
152 /// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
155 quote_spanned!(::proc_macro::Span::mixed_site() => $($tt)*)