]> git.proxmox.com Git - rustc.git/blob - vendor/time-macros/src/quote.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / vendor / time-macros / src / quote.rs
1 macro_rules! quote {
2 () => (::proc_macro::TokenStream::new());
3 ($($x:tt)*) => {{
4 let mut ts = ::proc_macro::TokenStream::new();
5 let ts_mut = &mut ts;
6 quote_inner!(ts_mut $($x)*);
7 ts
8 }};
9 }
10
11 #[cfg(any(feature = "formatting", feature = "parsing"))]
12 macro_rules! quote_append {
13 ($ts:ident $($x:tt)*) => {{
14 quote_inner!($ts $($x)*);
15 }};
16 }
17
18 macro_rules! quote_group {
19 ({ $($x:tt)* }) => {
20 ::proc_macro::TokenTree::Group(::proc_macro::Group::new(
21 ::proc_macro::Delimiter::Brace,
22 quote!($($x)*)
23 ))
24 };
25 }
26
27 macro_rules! sym {
28 ($ts:ident $x:tt $y:tt) => {
29 $ts.extend([
30 ::proc_macro::TokenTree::from(::proc_macro::Punct::new(
31 $x,
32 ::proc_macro::Spacing::Joint,
33 )),
34 ::proc_macro::TokenTree::from(::proc_macro::Punct::new(
35 $y,
36 ::proc_macro::Spacing::Alone,
37 )),
38 ]);
39 };
40 ($ts:ident $x:tt) => {
41 $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Punct::new(
42 $x,
43 ::proc_macro::Spacing::Alone,
44 ))]);
45 };
46 }
47
48 macro_rules! quote_inner {
49 // Base case
50 ($ts:ident) => {};
51
52 // Single or double symbols
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 ($ts:ident >> $($tail:tt)*) => { sym!($ts '>' '>'); quote_inner!($ts $($tail)*); };
64 ($ts:ident > $($tail:tt)*) => { sym!($ts '>'); quote_inner!($ts $($tail)*); };
65 ($ts:ident -> $($tail:tt)*) => { sym!($ts '-' '>'); quote_inner!($ts $($tail)*); };
66 ($ts:ident ? $($tail:tt)*) => { sym!($ts '?'); quote_inner!($ts $($tail)*); };
67 ($ts:ident ! $($tail:tt)*) => { sym!($ts '!'); quote_inner!($ts $($tail)*); };
68 ($ts:ident | $($tail:tt)*) => { sym!($ts '|'); quote_inner!($ts $($tail)*); };
69 ($ts:ident * $($tail:tt)*) => { sym!($ts '*'); quote_inner!($ts $($tail)*); };
70
71 // Identifier
72 ($ts:ident $i:ident $($tail:tt)*) => {
73 $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Ident::new(
74 &stringify!($i),
75 ::proc_macro::Span::mixed_site(),
76 ))]);
77 quote_inner!($ts $($tail)*);
78 };
79
80 // Literal
81 ($ts:ident $l:literal $($tail:tt)*) => {
82 $ts.extend([::proc_macro::TokenTree::from(::proc_macro::Literal::string(&$l))]);
83 quote_inner!($ts $($tail)*);
84 };
85
86 // Lifetime
87 ($ts:ident $l:lifetime $($tail:tt)*) => {
88 $ts.extend([
89 ::proc_macro::TokenTree::from(
90 ::proc_macro::Punct::new('\'', ::proc_macro::Spacing::Joint)
91 ),
92 ::proc_macro::TokenTree::from(::proc_macro::Ident::new(
93 stringify!($l).trim_start_matches(|c| c == '\''),
94 ::proc_macro::Span::mixed_site(),
95 )),
96 ]);
97 quote_inner!($ts $($tail)*);
98 };
99
100 // Groups
101 ($ts:ident ($($inner:tt)*) $($tail:tt)*) => {
102 $ts.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new(
103 ::proc_macro::Delimiter::Parenthesis,
104 quote!($($inner)*)
105 ))]);
106 quote_inner!($ts $($tail)*);
107 };
108 ($ts:ident [$($inner:tt)*] $($tail:tt)*) => {
109 $ts.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new(
110 ::proc_macro::Delimiter::Bracket,
111 quote!($($inner)*)
112 ))]);
113 quote_inner!($ts $($tail)*);
114 };
115 ($ts:ident {$($inner:tt)*} $($tail:tt)*) => {
116 $ts.extend([::proc_macro::TokenTree::Group(::proc_macro::Group::new(
117 ::proc_macro::Delimiter::Brace,
118 quote!($($inner)*)
119 ))]);
120 quote_inner!($ts $($tail)*);
121 };
122
123 // Interpolated values
124 // TokenTree by default
125 ($ts:ident #($e:expr) $($tail:tt)*) => {
126 $ts.extend([$crate::to_tokens::ToTokenTree::into_token_tree($e)]);
127 quote_inner!($ts $($tail)*);
128 };
129 // Allow a TokenStream by request. It's more expensive, so avoid if possible.
130 ($ts:ident #S($e:expr) $($tail:tt)*) => {
131 $crate::to_tokens::ToTokenStream::append_to($e, $ts);
132 quote_inner!($ts $($tail)*);
133 };
134 }