• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! [![github]](https://github.com/dtolnay/paste) [![crates-io]](https://crates.io/crates/paste) [![docs-rs]](https://docs.rs/paste)
2 //!
3 //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4 //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5 //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
6 //!
7 //! <br>
8 //!
9 //! The nightly-only [`concat_idents!`] macro in the Rust standard library is
10 //! notoriously underpowered in that its concatenated identifiers can only refer to
11 //! existing items, they can never be used to define something new.
12 //!
13 //! [`concat_idents!`]: https://doc.rust-lang.org/std/macro.concat_idents.html
14 //!
15 //! This crate provides a flexible way to paste together identifiers in a macro,
16 //! including using pasted identifiers to define new items.
17 //!
18 //! This approach works with any Rust compiler 1.31+.
19 //!
20 //! <br>
21 //!
22 //! # Pasting identifiers
23 //!
24 //! Within the `paste!` macro, identifiers inside `[<`...`>]` are pasted
25 //! together to form a single identifier.
26 //!
27 //! ```
28 //! use paste::paste;
29 //!
30 //! paste! {
31 //!     // Defines a const called `QRST`.
32 //!     const [<Q R S T>]: &str = "success!";
33 //! }
34 //!
35 //! fn main() {
36 //!     assert_eq!(
37 //!         paste! { [<Q R S T>].len() },
38 //!         8,
39 //!     );
40 //! }
41 //! ```
42 //!
43 //! <br><br>
44 //!
45 //! # More elaborate example
46 //!
47 //! The next example shows a macro that generates accessor methods for some
48 //! struct fields. It demonstrates how you might find it useful to bundle a
49 //! paste invocation inside of a macro\_rules macro.
50 //!
51 //! ```
52 //! use paste::paste;
53 //!
54 //! macro_rules! make_a_struct_and_getters {
55 //!     ($name:ident { $($field:ident),* }) => {
56 //!         // Define a struct. This expands to:
57 //!         //
58 //!         //     pub struct S {
59 //!         //         a: String,
60 //!         //         b: String,
61 //!         //         c: String,
62 //!         //     }
63 //!         pub struct $name {
64 //!             $(
65 //!                 $field: String,
66 //!             )*
67 //!         }
68 //!
69 //!         // Build an impl block with getters. This expands to:
70 //!         //
71 //!         //     impl S {
72 //!         //         pub fn get_a(&self) -> &str { &self.a }
73 //!         //         pub fn get_b(&self) -> &str { &self.b }
74 //!         //         pub fn get_c(&self) -> &str { &self.c }
75 //!         //     }
76 //!         paste! {
77 //!             impl $name {
78 //!                 $(
79 //!                     pub fn [<get_ $field>](&self) -> &str {
80 //!                         &self.$field
81 //!                     }
82 //!                 )*
83 //!             }
84 //!         }
85 //!     }
86 //! }
87 //!
88 //! make_a_struct_and_getters!(S { a, b, c });
89 //!
90 //! fn call_some_getters(s: &S) -> bool {
91 //!     s.get_a() == s.get_b() && s.get_c().is_empty()
92 //! }
93 //! #
94 //! # fn main() {}
95 //! ```
96 //!
97 //! <br><br>
98 //!
99 //! # Case conversion
100 //!
101 //! Use `$var:lower` or `$var:upper` in the segment list to convert an
102 //! interpolated segment to lower- or uppercase as part of the paste. For
103 //! example, `[<ld_ $reg:lower _expr>]` would paste to `ld_bc_expr` if invoked
104 //! with $reg=`Bc`.
105 //!
106 //! Use `$var:snake` to convert CamelCase input to snake\_case.
107 //! Use `$var:camel` to convert snake\_case to CamelCase.
108 //! These compose, so for example `$var:snake:upper` would give you SCREAMING\_CASE.
109 //!
110 //! The precise Unicode conversions are as defined by [`str::to_lowercase`] and
111 //! [`str::to_uppercase`].
112 //!
113 //! [`str::to_lowercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase
114 //! [`str::to_uppercase`]: https://doc.rust-lang.org/std/primitive.str.html#method.to_uppercase
115 //!
116 //! <br>
117 //!
118 //! # Pasting documentation strings
119 //!
120 //! Within the `paste!` macro, arguments to a #\[doc ...\] attribute are
121 //! implicitly concatenated together to form a coherent documentation string.
122 //!
123 //! ```
124 //! use paste::paste;
125 //!
126 //! macro_rules! method_new {
127 //!     ($ret:ident) => {
128 //!         paste! {
129 //!             #[doc = "Create a new `" $ret "` object."]
130 //!             pub fn new() -> $ret { todo!() }
131 //!         }
132 //!     };
133 //! }
134 //!
135 //! pub struct Paste {}
136 //!
137 //! method_new!(Paste);  // expands to #[doc = "Create a new `Paste` object"]
138 //! ```
139 
140 #![allow(
141     clippy::derive_partial_eq_without_eq,
142     clippy::doc_markdown,
143     clippy::match_same_arms,
144     clippy::module_name_repetitions,
145     clippy::needless_doctest_main,
146     clippy::too_many_lines
147 )]
148 
149 extern crate proc_macro;
150 
151 mod attr;
152 mod error;
153 mod segment;
154 
155 use crate::attr::expand_attr;
156 use crate::error::{Error, Result};
157 use crate::segment::Segment;
158 use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
159 use std::char;
160 use std::iter;
161 use std::panic;
162 
163 #[proc_macro]
paste(input: TokenStream) -> TokenStream164 pub fn paste(input: TokenStream) -> TokenStream {
165     let mut contains_paste = false;
166     let flatten_single_interpolation = true;
167     match expand(
168         input.clone(),
169         &mut contains_paste,
170         flatten_single_interpolation,
171     ) {
172         Ok(expanded) => {
173             if contains_paste {
174                 expanded
175             } else {
176                 input
177             }
178         }
179         Err(err) => err.to_compile_error(),
180     }
181 }
182 
183 #[doc(hidden)]
184 #[proc_macro]
item(input: TokenStream) -> TokenStream185 pub fn item(input: TokenStream) -> TokenStream {
186     paste(input)
187 }
188 
189 #[doc(hidden)]
190 #[proc_macro]
expr(input: TokenStream) -> TokenStream191 pub fn expr(input: TokenStream) -> TokenStream {
192     paste(input)
193 }
194 
expand( input: TokenStream, contains_paste: &mut bool, flatten_single_interpolation: bool, ) -> Result<TokenStream>195 fn expand(
196     input: TokenStream,
197     contains_paste: &mut bool,
198     flatten_single_interpolation: bool,
199 ) -> Result<TokenStream> {
200     let mut expanded = TokenStream::new();
201     let mut lookbehind = Lookbehind::Other;
202     let mut prev_none_group = None::<Group>;
203     let mut tokens = input.into_iter().peekable();
204     loop {
205         let token = tokens.next();
206         if let Some(group) = prev_none_group.take() {
207             if match (&token, tokens.peek()) {
208                 (Some(TokenTree::Punct(fst)), Some(TokenTree::Punct(snd))) => {
209                     fst.as_char() == ':' && snd.as_char() == ':' && fst.spacing() == Spacing::Joint
210                 }
211                 _ => false,
212             } {
213                 expanded.extend(group.stream());
214                 *contains_paste = true;
215             } else {
216                 expanded.extend(iter::once(TokenTree::Group(group)));
217             }
218         }
219         match token {
220             Some(TokenTree::Group(group)) => {
221                 let delimiter = group.delimiter();
222                 let content = group.stream();
223                 let span = group.span();
224                 if delimiter == Delimiter::Bracket && is_paste_operation(&content) {
225                     let segments = parse_bracket_as_segments(content, span)?;
226                     let pasted = segment::paste(&segments)?;
227                     let tokens = pasted_to_tokens(pasted, span)?;
228                     expanded.extend(tokens);
229                     *contains_paste = true;
230                 } else if flatten_single_interpolation
231                     && delimiter == Delimiter::None
232                     && is_single_interpolation_group(&content)
233                 {
234                     expanded.extend(content);
235                     *contains_paste = true;
236                 } else {
237                     let mut group_contains_paste = false;
238                     let is_attribute = delimiter == Delimiter::Bracket
239                         && (lookbehind == Lookbehind::Pound || lookbehind == Lookbehind::PoundBang);
240                     let mut nested = expand(
241                         content,
242                         &mut group_contains_paste,
243                         flatten_single_interpolation && !is_attribute,
244                     )?;
245                     if is_attribute {
246                         nested = expand_attr(nested, span, &mut group_contains_paste)?;
247                     }
248                     let group = if group_contains_paste {
249                         let mut group = Group::new(delimiter, nested);
250                         group.set_span(span);
251                         *contains_paste = true;
252                         group
253                     } else {
254                         group.clone()
255                     };
256                     if delimiter != Delimiter::None {
257                         expanded.extend(iter::once(TokenTree::Group(group)));
258                     } else if lookbehind == Lookbehind::DoubleColon {
259                         expanded.extend(group.stream());
260                         *contains_paste = true;
261                     } else {
262                         prev_none_group = Some(group);
263                     }
264                 }
265                 lookbehind = Lookbehind::Other;
266             }
267             Some(TokenTree::Punct(punct)) => {
268                 lookbehind = match punct.as_char() {
269                     ':' if lookbehind == Lookbehind::JointColon => Lookbehind::DoubleColon,
270                     ':' if punct.spacing() == Spacing::Joint => Lookbehind::JointColon,
271                     '#' => Lookbehind::Pound,
272                     '!' if lookbehind == Lookbehind::Pound => Lookbehind::PoundBang,
273                     _ => Lookbehind::Other,
274                 };
275                 expanded.extend(iter::once(TokenTree::Punct(punct)));
276             }
277             Some(other) => {
278                 lookbehind = Lookbehind::Other;
279                 expanded.extend(iter::once(other));
280             }
281             None => return Ok(expanded),
282         }
283     }
284 }
285 
286 #[derive(PartialEq)]
287 enum Lookbehind {
288     JointColon,
289     DoubleColon,
290     Pound,
291     PoundBang,
292     Other,
293 }
294 
295 // https://github.com/dtolnay/paste/issues/26
is_single_interpolation_group(input: &TokenStream) -> bool296 fn is_single_interpolation_group(input: &TokenStream) -> bool {
297     #[derive(PartialEq)]
298     enum State {
299         Init,
300         Ident,
301         Literal,
302         Apostrophe,
303         Lifetime,
304         Colon1,
305         Colon2,
306     }
307 
308     let mut state = State::Init;
309     for tt in input.clone() {
310         state = match (state, &tt) {
311             (State::Init, TokenTree::Ident(_)) => State::Ident,
312             (State::Init, TokenTree::Literal(_)) => State::Literal,
313             (State::Init, TokenTree::Punct(punct)) if punct.as_char() == '\'' => State::Apostrophe,
314             (State::Apostrophe, TokenTree::Ident(_)) => State::Lifetime,
315             (State::Ident, TokenTree::Punct(punct))
316                 if punct.as_char() == ':' && punct.spacing() == Spacing::Joint =>
317             {
318                 State::Colon1
319             }
320             (State::Colon1, TokenTree::Punct(punct))
321                 if punct.as_char() == ':' && punct.spacing() == Spacing::Alone =>
322             {
323                 State::Colon2
324             }
325             (State::Colon2, TokenTree::Ident(_)) => State::Ident,
326             _ => return false,
327         };
328     }
329 
330     state == State::Ident || state == State::Literal || state == State::Lifetime
331 }
332 
is_paste_operation(input: &TokenStream) -> bool333 fn is_paste_operation(input: &TokenStream) -> bool {
334     let mut tokens = input.clone().into_iter();
335 
336     match &tokens.next() {
337         Some(TokenTree::Punct(punct)) if punct.as_char() == '<' => {}
338         _ => return false,
339     }
340 
341     let mut has_token = false;
342     loop {
343         match &tokens.next() {
344             Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => {
345                 return has_token && tokens.next().is_none();
346             }
347             Some(_) => has_token = true,
348             None => return false,
349         }
350     }
351 }
352 
parse_bracket_as_segments(input: TokenStream, scope: Span) -> Result<Vec<Segment>>353 fn parse_bracket_as_segments(input: TokenStream, scope: Span) -> Result<Vec<Segment>> {
354     let mut tokens = input.into_iter().peekable();
355 
356     match &tokens.next() {
357         Some(TokenTree::Punct(punct)) if punct.as_char() == '<' => {}
358         Some(wrong) => return Err(Error::new(wrong.span(), "expected `<`")),
359         None => return Err(Error::new(scope, "expected `[< ... >]`")),
360     }
361 
362     let mut segments = segment::parse(&mut tokens)?;
363 
364     match &tokens.next() {
365         Some(TokenTree::Punct(punct)) if punct.as_char() == '>' => {}
366         Some(wrong) => return Err(Error::new(wrong.span(), "expected `>`")),
367         None => return Err(Error::new(scope, "expected `[< ... >]`")),
368     }
369 
370     if let Some(unexpected) = tokens.next() {
371         return Err(Error::new(
372             unexpected.span(),
373             "unexpected input, expected `[< ... >]`",
374         ));
375     }
376 
377     for segment in &mut segments {
378         if let Segment::String(string) = segment {
379             if string.value.starts_with("'\\u{") {
380                 let hex = &string.value[4..string.value.len() - 2];
381                 if let Ok(unsigned) = u32::from_str_radix(hex, 16) {
382                     if let Some(ch) = char::from_u32(unsigned) {
383                         string.value.clear();
384                         string.value.push(ch);
385                         continue;
386                     }
387                 }
388             }
389             if string.value.contains(&['#', '\\', '.', '+'][..])
390                 || string.value.starts_with("b'")
391                 || string.value.starts_with("b\"")
392                 || string.value.starts_with("br\"")
393             {
394                 return Err(Error::new(string.span, "unsupported literal"));
395             }
396             let mut range = 0..string.value.len();
397             if string.value.starts_with("r\"") {
398                 range.start += 2;
399                 range.end -= 1;
400             } else if string.value.starts_with(&['"', '\''][..]) {
401                 range.start += 1;
402                 range.end -= 1;
403             }
404             string.value = string.value[range].replace('-', "_");
405         }
406     }
407 
408     Ok(segments)
409 }
410 
pasted_to_tokens(mut pasted: String, span: Span) -> Result<TokenStream>411 fn pasted_to_tokens(mut pasted: String, span: Span) -> Result<TokenStream> {
412     let mut tokens = TokenStream::new();
413 
414     #[cfg(not(no_literal_fromstr))]
415     {
416         use proc_macro::{LexError, Literal};
417         use std::str::FromStr;
418 
419         if pasted.starts_with(|ch: char| ch.is_ascii_digit()) {
420             let literal = match panic::catch_unwind(|| Literal::from_str(&pasted)) {
421                 Ok(Ok(literal)) => TokenTree::Literal(literal),
422                 Ok(Err(LexError { .. })) | Err(_) => {
423                     return Err(Error::new(
424                         span,
425                         &format!("`{:?}` is not a valid literal", pasted),
426                     ));
427                 }
428             };
429             tokens.extend(iter::once(literal));
430             return Ok(tokens);
431         }
432     }
433 
434     if pasted.starts_with('\'') {
435         let mut apostrophe = TokenTree::Punct(Punct::new('\'', Spacing::Joint));
436         apostrophe.set_span(span);
437         tokens.extend(iter::once(apostrophe));
438         pasted.remove(0);
439     }
440 
441     let ident = match panic::catch_unwind(|| Ident::new(&pasted, span)) {
442         Ok(ident) => TokenTree::Ident(ident),
443         Err(_) => {
444             return Err(Error::new(
445                 span,
446                 &format!("`{:?}` is not a valid identifier", pasted),
447             ));
448         }
449     };
450 
451     tokens.extend(iter::once(ident));
452     Ok(tokens)
453 }
454