time_macros/
quote.rs

1macro_rules! quote_ {
2    () => (proc_macro::TokenStream::new());
3    ($($x:tt)*) => {{
4        use proc_macro::*;
5        let mut ts = TokenStream::new();
6        let ts_mut = &mut ts;
7        quote_inner!(ts_mut $($x)*);
8        ts
9    }};
10}
11
12macro_rules! quote_append {
13    ($ts:ident $($x:tt)*) => {{
14        use proc_macro::*;
15        quote_inner!($ts $($x)*);
16    }};
17}
18
19macro_rules! quote_group {
20    ({ $($x:tt)* }) => {{
21        use proc_macro::*;
22        TokenTree::Group(Group::new(
23            Delimiter::Brace,
24            quote_!($($x)*)
25        ))
26    }};
27}
28
29macro_rules! sym {
30    ($ts:ident $x:tt $y:tt) => {
31        $ts.extend([
32            TokenTree::from(Punct::new($x, Spacing::Joint)),
33            TokenTree::from(Punct::new($y, Spacing::Alone)),
34        ]);
35    };
36    ($ts:ident $x:tt) => {
37        $ts.extend([TokenTree::from(Punct::new($x, Spacing::Alone))]);
38    };
39}
40
41#[allow(unused_macro_rules)] // Varies by feature flag combination.
42macro_rules! quote_inner {
43    // Base case
44    ($ts:ident) => {};
45
46    // Single or double symbols
47    ($ts:ident :: $($tail:tt)*) => { sym!($ts ':' ':'); quote_inner!($ts $($tail)*); };
48    ($ts:ident : $($tail:tt)*) => { sym!($ts ':'); quote_inner!($ts $($tail)*); };
49    ($ts:ident = $($tail:tt)*) => { sym!($ts '='); quote_inner!($ts $($tail)*); };
50    ($ts:ident ; $($tail:tt)*) => { sym!($ts ';'); quote_inner!($ts $($tail)*); };
51    ($ts:ident , $($tail:tt)*) => { sym!($ts ','); quote_inner!($ts $($tail)*); };
52    ($ts:ident . $($tail:tt)*) => { sym!($ts '.'); quote_inner!($ts $($tail)*); };
53    ($ts:ident & $($tail:tt)*) => { sym!($ts '&'); quote_inner!($ts $($tail)*); };
54    ($ts:ident < $($tail:tt)*) => { sym!($ts '<'); quote_inner!($ts $($tail)*); };
55    ($ts:ident >> $($tail:tt)*) => { sym!($ts '>' '>'); quote_inner!($ts $($tail)*); };
56    ($ts:ident > $($tail:tt)*) => { sym!($ts '>'); quote_inner!($ts $($tail)*); };
57    ($ts:ident -> $($tail:tt)*) => { sym!($ts '-' '>'); quote_inner!($ts $($tail)*); };
58    ($ts:ident ? $($tail:tt)*) => { sym!($ts '?'); quote_inner!($ts $($tail)*); };
59    ($ts:ident ! $($tail:tt)*) => { sym!($ts '!'); quote_inner!($ts $($tail)*); };
60    ($ts:ident | $($tail:tt)*) => { sym!($ts '|'); quote_inner!($ts $($tail)*); };
61    ($ts:ident * $($tail:tt)*) => { sym!($ts '*'); quote_inner!($ts $($tail)*); };
62    ($ts:ident + $($tail:tt)*) => { sym!($ts '+'); quote_inner!($ts $($tail)*); };
63
64    // Identifier
65    ($ts:ident $i:ident $($tail:tt)*) => {
66        $ts.extend([TokenTree::from(Ident::new(
67            &stringify!($i),
68            Span::mixed_site(),
69        ))]);
70        quote_inner!($ts $($tail)*);
71    };
72
73    // Literal
74    ($ts:ident 0 $($tail:tt)*) => {
75        $ts.extend([TokenTree::from(Literal::usize_unsuffixed(0))]);
76        quote_inner!($ts $($tail)*);
77    };
78    ($ts:ident $l:literal $($tail:tt)*) => {
79        $ts.extend([TokenTree::from(Literal::string(&$l))]);
80        quote_inner!($ts $($tail)*);
81    };
82
83    // Lifetime
84    ($ts:ident $l:lifetime $($tail:tt)*) => {
85        $ts.extend([
86            TokenTree::from(
87                Punct::new('\'', Spacing::Joint)
88            ),
89            TokenTree::from(Ident::new(
90                stringify!($l).trim_start_matches(|c| c == '\''),
91                Span::mixed_site(),
92            )),
93        ]);
94        quote_inner!($ts $($tail)*);
95    };
96
97    // Attribute
98    ($ts:ident #[$($inner:tt)*] $($tail:tt)*) => {
99        $ts.extend([
100            TokenTree::from(
101                Punct::new('#', Spacing::Alone)
102            ),
103            TokenTree::Group(Group::new(
104                Delimiter::Bracket,
105                quote_!($($inner)*)
106            )),
107        ]);
108        quote_inner!($ts $($tail)*);
109    };
110
111    // Groups
112    ($ts:ident ($($inner:tt)*) $($tail:tt)*) => {
113        $ts.extend([TokenTree::Group(Group::new(
114            Delimiter::Parenthesis,
115            quote_!($($inner)*)
116        ))]);
117        quote_inner!($ts $($tail)*);
118    };
119    ($ts:ident [$($inner:tt)*] $($tail:tt)*) => {
120        $ts.extend([TokenTree::Group(Group::new(
121            Delimiter::Bracket,
122            quote_!($($inner)*)
123        ))]);
124        quote_inner!($ts $($tail)*);
125    };
126    ($ts:ident {$($inner:tt)*} $($tail:tt)*) => {
127        $ts.extend([TokenTree::Group(Group::new(
128            Delimiter::Brace,
129            quote_!($($inner)*)
130        ))]);
131        quote_inner!($ts $($tail)*);
132    };
133
134    // Interpolated values
135    // TokenTree by default
136    ($ts:ident #($e:expr) $($tail:tt)*) => {
137        $ts.extend([$crate::to_tokens::ToTokenTree::into_token_tree($e)]);
138        quote_inner!($ts $($tail)*);
139    };
140    // Allow a TokenStream by request. It's more expensive, so avoid if possible.
141    ($ts:ident #S($e:expr) $($tail:tt)*) => {
142        $crate::to_tokens::ToTokenStream::append_to($e, $ts);
143        quote_inner!($ts $($tail)*);
144    };
145}