lib.rs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. //! [![github]](https://github.com/dtolnay/quote) [![crates-io]](https://crates.io/crates/quote) [![docs-rs]](https://docs.rs/quote)
  3. //!
  4. //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
  5. //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
  6. //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
  7. //!
  8. //! <br>
  9. //!
  10. //! This crate provides the [`quote!`] macro for turning Rust syntax tree data
  11. //! structures into tokens of source code.
  12. //!
  13. //! Procedural macros in Rust receive a stream of tokens as input, execute
  14. //! arbitrary Rust code to determine how to manipulate those tokens, and produce
  15. //! a stream of tokens to hand back to the compiler to compile into the caller's
  16. //! crate. Quasi-quoting is a solution to one piece of that &mdash; producing
  17. //! tokens to return to the compiler.
  18. //!
  19. //! The idea of quasi-quoting is that we write *code* that we treat as *data*.
  20. //! Within the `quote!` macro, we can write what looks like code to our text
  21. //! editor or IDE. We get all the benefits of the editor's brace matching,
  22. //! syntax highlighting, indentation, and maybe autocompletion. But rather than
  23. //! compiling that as code into the current crate, we can treat it as data, pass
  24. //! it around, mutate it, and eventually hand it back to the compiler as tokens
  25. //! to compile into the macro caller's crate.
  26. //!
  27. //! This crate is motivated by the procedural macro use case, but is a
  28. //! general-purpose Rust quasi-quoting library and is not specific to procedural
  29. //! macros.
  30. //!
  31. //! ```toml
  32. //! [dependencies]
  33. //! quote = "1.0"
  34. //! ```
  35. //!
  36. //! <br>
  37. //!
  38. //! # Example
  39. //!
  40. //! The following quasi-quoted block of code is something you might find in [a]
  41. //! procedural macro having to do with data structure serialization. The `#var`
  42. //! syntax performs interpolation of runtime variables into the quoted tokens.
  43. //! Check out the documentation of the [`quote!`] macro for more detail about
  44. //! the syntax. See also the [`quote_spanned!`] macro which is important for
  45. //! implementing hygienic procedural macros.
  46. //!
  47. //! [a]: https://serde.rs/
  48. //!
  49. //! ```
  50. //! # use quote::quote;
  51. //! #
  52. //! # let generics = "";
  53. //! # let where_clause = "";
  54. //! # let field_ty = "";
  55. //! # let item_ty = "";
  56. //! # let path = "";
  57. //! # let value = "";
  58. //! #
  59. //! let tokens = quote! {
  60. //! struct SerializeWith #generics #where_clause {
  61. //! value: &'a #field_ty,
  62. //! phantom: core::marker::PhantomData<#item_ty>,
  63. //! }
  64. //!
  65. //! impl #generics serde::Serialize for SerializeWith #generics #where_clause {
  66. //! fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  67. //! where
  68. //! S: serde::Serializer,
  69. //! {
  70. //! #path(self.value, serializer)
  71. //! }
  72. //! }
  73. //!
  74. //! SerializeWith {
  75. //! value: #value,
  76. //! phantom: core::marker::PhantomData::<#item_ty>,
  77. //! }
  78. //! };
  79. //! ```
  80. //!
  81. //! <br>
  82. //!
  83. //! # Non-macro code generators
  84. //!
  85. //! When using `quote` in a build.rs or main.rs and writing the output out to a
  86. //! file, consider having the code generator pass the tokens through
  87. //! [prettyplease] before writing. This way if an error occurs in the generated
  88. //! code it is convenient for a human to read and debug.
  89. //!
  90. //! [prettyplease]: https://github.com/dtolnay/prettyplease
  91. // Quote types in rustdoc of other crates get linked to here.
  92. #![doc(html_root_url = "https://docs.rs/quote/1.0.40")]
  93. #![allow(
  94. clippy::doc_markdown,
  95. clippy::elidable_lifetime_names,
  96. clippy::missing_errors_doc,
  97. clippy::missing_panics_doc,
  98. clippy::module_name_repetitions,
  99. clippy::needless_lifetimes,
  100. // false positive https://github.com/rust-lang/rust-clippy/issues/6983
  101. clippy::wrong_self_convention,
  102. )]
  103. extern crate alloc;
  104. #[cfg(feature = "proc-macro")]
  105. extern crate proc_macro;
  106. mod ext;
  107. mod format;
  108. mod ident_fragment;
  109. mod to_tokens;
  110. // Not public API.
  111. #[doc(hidden)]
  112. #[path = "runtime.rs"]
  113. pub mod __private;
  114. pub use crate::ext::TokenStreamExt;
  115. pub use crate::ident_fragment::IdentFragment;
  116. pub use crate::to_tokens::ToTokens;
  117. // Not public API.
  118. #[doc(hidden)]
  119. pub mod spanned;
  120. macro_rules! __quote {
  121. ($quote:item) => {
  122. /// The whole point.
  123. ///
  124. /// Performs variable interpolation against the input and produces it as
  125. /// [`proc_macro2::TokenStream`].
  126. ///
  127. /// Note: for returning tokens to the compiler in a procedural macro, use
  128. /// `.into()` on the result to convert to [`proc_macro::TokenStream`].
  129. ///
  130. /// <br>
  131. ///
  132. /// # Interpolation
  133. ///
  134. /// Variable interpolation is done with `#var` (similar to `$var` in
  135. /// `macro_rules!` macros). This grabs the `var` variable that is currently in
  136. /// scope and inserts it in that location in the output tokens. Any type
  137. /// implementing the [`ToTokens`] trait can be interpolated. This includes most
  138. /// Rust primitive types as well as most of the syntax tree types from the [Syn]
  139. /// crate.
  140. ///
  141. /// [Syn]: https://github.com/dtolnay/syn
  142. ///
  143. /// Repetition is done using `#(...)*` or `#(...),*` again similar to
  144. /// `macro_rules!`. This iterates through the elements of any variable
  145. /// interpolated within the repetition and inserts a copy of the repetition body
  146. /// for each one. The variables in an interpolation may be a `Vec`, slice,
  147. /// `BTreeSet`, or any `Iterator`.
  148. ///
  149. /// - `#(#var)*` — no separators
  150. /// - `#(#var),*` — the character before the asterisk is used as a separator
  151. /// - `#( struct #var; )*` — the repetition can contain other tokens
  152. /// - `#( #k => println!("{}", #v), )*` — even multiple interpolations
  153. ///
  154. /// <br>
  155. ///
  156. /// # Hygiene
  157. ///
  158. /// Any interpolated tokens preserve the `Span` information provided by their
  159. /// `ToTokens` implementation. Tokens that originate within the `quote!`
  160. /// invocation are spanned with [`Span::call_site()`].
  161. ///
  162. /// [`Span::call_site()`]: proc_macro2::Span::call_site
  163. ///
  164. /// A different span can be provided through the [`quote_spanned!`] macro.
  165. ///
  166. /// <br>
  167. ///
  168. /// # Return type
  169. ///
  170. /// The macro evaluates to an expression of type `proc_macro2::TokenStream`.
  171. /// Meanwhile Rust procedural macros are expected to return the type
  172. /// `proc_macro::TokenStream`.
  173. ///
  174. /// The difference between the two types is that `proc_macro` types are entirely
  175. /// specific to procedural macros and cannot ever exist in code outside of a
  176. /// procedural macro, while `proc_macro2` types may exist anywhere including
  177. /// tests and non-macro code like main.rs and build.rs. This is why even the
  178. /// procedural macro ecosystem is largely built around `proc_macro2`, because
  179. /// that ensures the libraries are unit testable and accessible in non-macro
  180. /// contexts.
  181. ///
  182. /// There is a [`From`]-conversion in both directions so returning the output of
  183. /// `quote!` from a procedural macro usually looks like `tokens.into()` or
  184. /// `proc_macro::TokenStream::from(tokens)`.
  185. ///
  186. /// <br>
  187. ///
  188. /// # Examples
  189. ///
  190. /// ### Procedural macro
  191. ///
  192. /// The structure of a basic procedural macro is as follows. Refer to the [Syn]
  193. /// crate for further useful guidance on using `quote!` as part of a procedural
  194. /// macro.
  195. ///
  196. /// [Syn]: https://github.com/dtolnay/syn
  197. ///
  198. /// ```
  199. /// # #[cfg(any())]
  200. /// extern crate proc_macro;
  201. /// # extern crate proc_macro2;
  202. ///
  203. /// # #[cfg(any())]
  204. /// use proc_macro::TokenStream;
  205. /// # use proc_macro2::TokenStream;
  206. /// use quote::quote;
  207. ///
  208. /// # const IGNORE_TOKENS: &'static str = stringify! {
  209. /// #[proc_macro_derive(HeapSize)]
  210. /// # };
  211. /// pub fn derive_heap_size(input: TokenStream) -> TokenStream {
  212. /// // Parse the input and figure out what implementation to generate...
  213. /// # const IGNORE_TOKENS: &'static str = stringify! {
  214. /// let name = /* ... */;
  215. /// let expr = /* ... */;
  216. /// # };
  217. /// #
  218. /// # let name = 0;
  219. /// # let expr = 0;
  220. ///
  221. /// let expanded = quote! {
  222. /// // The generated impl.
  223. /// impl heapsize::HeapSize for #name {
  224. /// fn heap_size_of_children(&self) -> usize {
  225. /// #expr
  226. /// }
  227. /// }
  228. /// };
  229. ///
  230. /// // Hand the output tokens back to the compiler.
  231. /// TokenStream::from(expanded)
  232. /// }
  233. /// ```
  234. ///
  235. /// <p><br></p>
  236. ///
  237. /// ### Combining quoted fragments
  238. ///
  239. /// Usually you don't end up constructing an entire final `TokenStream` in one
  240. /// piece. Different parts may come from different helper functions. The tokens
  241. /// produced by `quote!` themselves implement `ToTokens` and so can be
  242. /// interpolated into later `quote!` invocations to build up a final result.
  243. ///
  244. /// ```
  245. /// # use quote::quote;
  246. /// #
  247. /// let type_definition = quote! {...};
  248. /// let methods = quote! {...};
  249. ///
  250. /// let tokens = quote! {
  251. /// #type_definition
  252. /// #methods
  253. /// };
  254. /// ```
  255. ///
  256. /// <p><br></p>
  257. ///
  258. /// ### Constructing identifiers
  259. ///
  260. /// Suppose we have an identifier `ident` which came from somewhere in a macro
  261. /// input and we need to modify it in some way for the macro output. Let's
  262. /// consider prepending the identifier with an underscore.
  263. ///
  264. /// Simply interpolating the identifier next to an underscore will not have the
  265. /// behavior of concatenating them. The underscore and the identifier will
  266. /// continue to be two separate tokens as if you had written `_ x`.
  267. ///
  268. /// ```
  269. /// # use proc_macro2::{self as syn, Span};
  270. /// # use quote::quote;
  271. /// #
  272. /// # let ident = syn::Ident::new("i", Span::call_site());
  273. /// #
  274. /// // incorrect
  275. /// quote! {
  276. /// let mut _#ident = 0;
  277. /// }
  278. /// # ;
  279. /// ```
  280. ///
  281. /// The solution is to build a new identifier token with the correct value. As
  282. /// this is such a common case, the [`format_ident!`] macro provides a
  283. /// convenient utility for doing so correctly.
  284. ///
  285. /// ```
  286. /// # use proc_macro2::{Ident, Span};
  287. /// # use quote::{format_ident, quote};
  288. /// #
  289. /// # let ident = Ident::new("i", Span::call_site());
  290. /// #
  291. /// let varname = format_ident!("_{}", ident);
  292. /// quote! {
  293. /// let mut #varname = 0;
  294. /// }
  295. /// # ;
  296. /// ```
  297. ///
  298. /// Alternatively, the APIs provided by Syn and proc-macro2 can be used to
  299. /// directly build the identifier. This is roughly equivalent to the above, but
  300. /// will not handle `ident` being a raw identifier.
  301. ///
  302. /// ```
  303. /// # use proc_macro2::{self as syn, Span};
  304. /// # use quote::quote;
  305. /// #
  306. /// # let ident = syn::Ident::new("i", Span::call_site());
  307. /// #
  308. /// let concatenated = format!("_{}", ident);
  309. /// let varname = syn::Ident::new(&concatenated, ident.span());
  310. /// quote! {
  311. /// let mut #varname = 0;
  312. /// }
  313. /// # ;
  314. /// ```
  315. ///
  316. /// <p><br></p>
  317. ///
  318. /// ### Making method calls
  319. ///
  320. /// Let's say our macro requires some type specified in the macro input to have
  321. /// a constructor called `new`. We have the type in a variable called
  322. /// `field_type` of type `syn::Type` and want to invoke the constructor.
  323. ///
  324. /// ```
  325. /// # use quote::quote;
  326. /// #
  327. /// # let field_type = quote!(...);
  328. /// #
  329. /// // incorrect
  330. /// quote! {
  331. /// let value = #field_type::new();
  332. /// }
  333. /// # ;
  334. /// ```
  335. ///
  336. /// This works only sometimes. If `field_type` is `String`, the expanded code
  337. /// contains `String::new()` which is fine. But if `field_type` is something
  338. /// like `Vec<i32>` then the expanded code is `Vec<i32>::new()` which is invalid
  339. /// syntax. Ordinarily in handwritten Rust we would write `Vec::<i32>::new()`
  340. /// but for macros often the following is more convenient.
  341. ///
  342. /// ```
  343. /// # use quote::quote;
  344. /// #
  345. /// # let field_type = quote!(...);
  346. /// #
  347. /// quote! {
  348. /// let value = <#field_type>::new();
  349. /// }
  350. /// # ;
  351. /// ```
  352. ///
  353. /// This expands to `<Vec<i32>>::new()` which behaves correctly.
  354. ///
  355. /// A similar pattern is appropriate for trait methods.
  356. ///
  357. /// ```
  358. /// # use quote::quote;
  359. /// #
  360. /// # let field_type = quote!(...);
  361. /// #
  362. /// quote! {
  363. /// let value = <#field_type as core::default::Default>::default();
  364. /// }
  365. /// # ;
  366. /// ```
  367. ///
  368. /// <p><br></p>
  369. ///
  370. /// ### Interpolating text inside of doc comments
  371. ///
  372. /// Neither doc comments nor string literals get interpolation behavior in
  373. /// quote:
  374. ///
  375. /// ```compile_fail
  376. /// quote! {
  377. /// /// try to interpolate: #ident
  378. /// ///
  379. /// /// ...
  380. /// }
  381. /// ```
  382. ///
  383. /// ```compile_fail
  384. /// quote! {
  385. /// #[doc = "try to interpolate: #ident"]
  386. /// }
  387. /// ```
  388. ///
  389. /// Instead the best way to build doc comments that involve variables is by
  390. /// formatting the doc string literal outside of quote.
  391. ///
  392. /// ```rust
  393. /// # use proc_macro2::{Ident, Span};
  394. /// # use quote::quote;
  395. /// #
  396. /// # const IGNORE: &str = stringify! {
  397. /// let msg = format!(...);
  398. /// # };
  399. /// #
  400. /// # let ident = Ident::new("var", Span::call_site());
  401. /// # let msg = format!("try to interpolate: {}", ident);
  402. /// quote! {
  403. /// #[doc = #msg]
  404. /// ///
  405. /// /// ...
  406. /// }
  407. /// # ;
  408. /// ```
  409. ///
  410. /// <p><br></p>
  411. ///
  412. /// ### Indexing into a tuple struct
  413. ///
  414. /// When interpolating indices of a tuple or tuple struct, we need them not to
  415. /// appears suffixed as integer literals by interpolating them as [`syn::Index`]
  416. /// instead.
  417. ///
  418. /// [`syn::Index`]: https://docs.rs/syn/2.0/syn/struct.Index.html
  419. ///
  420. /// ```compile_fail
  421. /// let i = 0usize..self.fields.len();
  422. ///
  423. /// // expands to 0 + self.0usize.heap_size() + self.1usize.heap_size() + ...
  424. /// // which is not valid syntax
  425. /// quote! {
  426. /// 0 #( + self.#i.heap_size() )*
  427. /// }
  428. /// ```
  429. ///
  430. /// ```
  431. /// # use proc_macro2::{Ident, TokenStream};
  432. /// # use quote::quote;
  433. /// #
  434. /// # mod syn {
  435. /// # use proc_macro2::{Literal, TokenStream};
  436. /// # use quote::{ToTokens, TokenStreamExt};
  437. /// #
  438. /// # pub struct Index(usize);
  439. /// #
  440. /// # impl From<usize> for Index {
  441. /// # fn from(i: usize) -> Self {
  442. /// # Index(i)
  443. /// # }
  444. /// # }
  445. /// #
  446. /// # impl ToTokens for Index {
  447. /// # fn to_tokens(&self, tokens: &mut TokenStream) {
  448. /// # tokens.append(Literal::usize_unsuffixed(self.0));
  449. /// # }
  450. /// # }
  451. /// # }
  452. /// #
  453. /// # struct Struct {
  454. /// # fields: Vec<Ident>,
  455. /// # }
  456. /// #
  457. /// # impl Struct {
  458. /// # fn example(&self) -> TokenStream {
  459. /// let i = (0..self.fields.len()).map(syn::Index::from);
  460. ///
  461. /// // expands to 0 + self.0.heap_size() + self.1.heap_size() + ...
  462. /// quote! {
  463. /// 0 #( + self.#i.heap_size() )*
  464. /// }
  465. /// # }
  466. /// # }
  467. /// ```
  468. $quote
  469. };
  470. }
  471. #[cfg(doc)]
  472. __quote![
  473. #[macro_export]
  474. macro_rules! quote {
  475. ($($tt:tt)*) => {
  476. ...
  477. };
  478. }
  479. ];
  480. #[cfg(not(doc))]
  481. __quote![
  482. #[macro_export]
  483. macro_rules! quote {
  484. () => {
  485. $crate::__private::TokenStream::new()
  486. };
  487. // Special case rule for a single tt, for performance.
  488. ($tt:tt) => {{
  489. let mut _s = $crate::__private::TokenStream::new();
  490. $crate::quote_token!{$tt _s}
  491. _s
  492. }};
  493. // Special case rules for two tts, for performance.
  494. (# $var:ident) => {{
  495. let mut _s = $crate::__private::TokenStream::new();
  496. $crate::ToTokens::to_tokens(&$var, &mut _s);
  497. _s
  498. }};
  499. ($tt1:tt $tt2:tt) => {{
  500. let mut _s = $crate::__private::TokenStream::new();
  501. $crate::quote_token!{$tt1 _s}
  502. $crate::quote_token!{$tt2 _s}
  503. _s
  504. }};
  505. // Rule for any other number of tokens.
  506. ($($tt:tt)*) => {{
  507. let mut _s = $crate::__private::TokenStream::new();
  508. $crate::quote_each_token!{_s $($tt)*}
  509. _s
  510. }};
  511. }
  512. ];
  513. macro_rules! __quote_spanned {
  514. ($quote_spanned:item) => {
  515. /// Same as `quote!`, but applies a given span to all tokens originating within
  516. /// the macro invocation.
  517. ///
  518. /// <br>
  519. ///
  520. /// # Syntax
  521. ///
  522. /// A span expression of type [`Span`], followed by `=>`, followed by the tokens
  523. /// to quote. The span expression should be brief &mdash; use a variable for
  524. /// anything more than a few characters. There should be no space before the
  525. /// `=>` token.
  526. ///
  527. /// [`Span`]: proc_macro2::Span
  528. ///
  529. /// ```
  530. /// # use proc_macro2::Span;
  531. /// # use quote::quote_spanned;
  532. /// #
  533. /// # const IGNORE_TOKENS: &'static str = stringify! {
  534. /// let span = /* ... */;
  535. /// # };
  536. /// # let span = Span::call_site();
  537. /// # let init = 0;
  538. ///
  539. /// // On one line, use parentheses.
  540. /// let tokens = quote_spanned!(span=> Box::into_raw(Box::new(#init)));
  541. ///
  542. /// // On multiple lines, place the span at the top and use braces.
  543. /// let tokens = quote_spanned! {span=>
  544. /// Box::into_raw(Box::new(#init))
  545. /// };
  546. /// ```
  547. ///
  548. /// The lack of space before the `=>` should look jarring to Rust programmers
  549. /// and this is intentional. The formatting is designed to be visibly
  550. /// off-balance and draw the eye a particular way, due to the span expression
  551. /// being evaluated in the context of the procedural macro and the remaining
  552. /// tokens being evaluated in the generated code.
  553. ///
  554. /// <br>
  555. ///
  556. /// # Hygiene
  557. ///
  558. /// Any interpolated tokens preserve the `Span` information provided by their
  559. /// `ToTokens` implementation. Tokens that originate within the `quote_spanned!`
  560. /// invocation are spanned with the given span argument.
  561. ///
  562. /// <br>
  563. ///
  564. /// # Example
  565. ///
  566. /// The following procedural macro code uses `quote_spanned!` to assert that a
  567. /// particular Rust type implements the [`Sync`] trait so that references can be
  568. /// safely shared between threads.
  569. ///
  570. /// ```
  571. /// # use quote::{quote_spanned, TokenStreamExt, ToTokens};
  572. /// # use proc_macro2::{Span, TokenStream};
  573. /// #
  574. /// # struct Type;
  575. /// #
  576. /// # impl Type {
  577. /// # fn span(&self) -> Span {
  578. /// # Span::call_site()
  579. /// # }
  580. /// # }
  581. /// #
  582. /// # impl ToTokens for Type {
  583. /// # fn to_tokens(&self, _tokens: &mut TokenStream) {}
  584. /// # }
  585. /// #
  586. /// # let ty = Type;
  587. /// # let call_site = Span::call_site();
  588. /// #
  589. /// let ty_span = ty.span();
  590. /// let assert_sync = quote_spanned! {ty_span=>
  591. /// struct _AssertSync where #ty: Sync;
  592. /// };
  593. /// ```
  594. ///
  595. /// If the assertion fails, the user will see an error like the following. The
  596. /// input span of their type is highlighted in the error.
  597. ///
  598. /// ```text
  599. /// error[E0277]: the trait bound `*const (): std::marker::Sync` is not satisfied
  600. /// --> src/main.rs:10:21
  601. /// |
  602. /// 10 | static ref PTR: *const () = &();
  603. /// | ^^^^^^^^^ `*const ()` cannot be shared between threads safely
  604. /// ```
  605. ///
  606. /// In this example it is important for the where-clause to be spanned with the
  607. /// line/column information of the user's input type so that error messages are
  608. /// placed appropriately by the compiler.
  609. $quote_spanned
  610. };
  611. }
  612. #[cfg(doc)]
  613. __quote_spanned![
  614. #[macro_export]
  615. macro_rules! quote_spanned {
  616. ($span:expr=> $($tt:tt)*) => {
  617. ...
  618. };
  619. }
  620. ];
  621. #[cfg(not(doc))]
  622. __quote_spanned![
  623. #[macro_export]
  624. macro_rules! quote_spanned {
  625. ($span:expr=>) => {{
  626. let _: $crate::__private::Span = $crate::__private::get_span($span).__into_span();
  627. $crate::__private::TokenStream::new()
  628. }};
  629. // Special case rule for a single tt, for performance.
  630. ($span:expr=> $tt:tt) => {{
  631. let mut _s = $crate::__private::TokenStream::new();
  632. let _span: $crate::__private::Span = $crate::__private::get_span($span).__into_span();
  633. $crate::quote_token_spanned!{$tt _s _span}
  634. _s
  635. }};
  636. // Special case rules for two tts, for performance.
  637. ($span:expr=> # $var:ident) => {{
  638. let mut _s = $crate::__private::TokenStream::new();
  639. let _: $crate::__private::Span = $crate::__private::get_span($span).__into_span();
  640. $crate::ToTokens::to_tokens(&$var, &mut _s);
  641. _s
  642. }};
  643. ($span:expr=> $tt1:tt $tt2:tt) => {{
  644. let mut _s = $crate::__private::TokenStream::new();
  645. let _span: $crate::__private::Span = $crate::__private::get_span($span).__into_span();
  646. $crate::quote_token_spanned!{$tt1 _s _span}
  647. $crate::quote_token_spanned!{$tt2 _s _span}
  648. _s
  649. }};
  650. // Rule for any other number of tokens.
  651. ($span:expr=> $($tt:tt)*) => {{
  652. let mut _s = $crate::__private::TokenStream::new();
  653. let _span: $crate::__private::Span = $crate::__private::get_span($span).__into_span();
  654. $crate::quote_each_token_spanned!{_s _span $($tt)*}
  655. _s
  656. }};
  657. }
  658. ];
  659. // Extract the names of all #metavariables and pass them to the $call macro.
  660. //
  661. // in: pounded_var_names!(then!(...) a #b c #( #d )* #e)
  662. // out: then!(... b);
  663. // then!(... d);
  664. // then!(... e);
  665. #[macro_export]
  666. #[doc(hidden)]
  667. macro_rules! pounded_var_names {
  668. ($call:ident! $extra:tt $($tts:tt)*) => {
  669. $crate::pounded_var_names_with_context!{$call! $extra
  670. (@ $($tts)*)
  671. ($($tts)* @)
  672. }
  673. };
  674. }
  675. #[macro_export]
  676. #[doc(hidden)]
  677. macro_rules! pounded_var_names_with_context {
  678. ($call:ident! $extra:tt ($($b1:tt)*) ($($curr:tt)*)) => {
  679. $(
  680. $crate::pounded_var_with_context!{$call! $extra $b1 $curr}
  681. )*
  682. };
  683. }
  684. #[macro_export]
  685. #[doc(hidden)]
  686. macro_rules! pounded_var_with_context {
  687. ($call:ident! $extra:tt $b1:tt ( $($inner:tt)* )) => {
  688. $crate::pounded_var_names!{$call! $extra $($inner)*}
  689. };
  690. ($call:ident! $extra:tt $b1:tt [ $($inner:tt)* ]) => {
  691. $crate::pounded_var_names!{$call! $extra $($inner)*}
  692. };
  693. ($call:ident! $extra:tt $b1:tt { $($inner:tt)* }) => {
  694. $crate::pounded_var_names!{$call! $extra $($inner)*}
  695. };
  696. ($call:ident!($($extra:tt)*) # $var:ident) => {
  697. $crate::$call!($($extra)* $var);
  698. };
  699. ($call:ident! $extra:tt $b1:tt $curr:tt) => {};
  700. }
  701. #[macro_export]
  702. #[doc(hidden)]
  703. macro_rules! quote_bind_into_iter {
  704. ($has_iter:ident $var:ident) => {
  705. // `mut` may be unused if $var occurs multiple times in the list.
  706. #[allow(unused_mut)]
  707. let (mut $var, i) = $var.quote_into_iter();
  708. let $has_iter = $has_iter | i;
  709. };
  710. }
  711. #[macro_export]
  712. #[doc(hidden)]
  713. macro_rules! quote_bind_next_or_break {
  714. ($var:ident) => {
  715. let $var = match $var.next() {
  716. Some(_x) => $crate::__private::RepInterp(_x),
  717. None => break,
  718. };
  719. };
  720. }
  721. // The obvious way to write this macro is as a tt muncher. This implementation
  722. // does something more complex for two reasons.
  723. //
  724. // - With a tt muncher it's easy to hit Rust's built-in recursion_limit, which
  725. // this implementation avoids because it isn't tail recursive.
  726. //
  727. // - Compile times for a tt muncher are quadratic relative to the length of
  728. // the input. This implementation is linear, so it will be faster
  729. // (potentially much faster) for big inputs. However, the constant factors
  730. // of this implementation are higher than that of a tt muncher, so it is
  731. // somewhat slower than a tt muncher if there are many invocations with
  732. // short inputs.
  733. //
  734. // An invocation like this:
  735. //
  736. // quote_each_token!(_s a b c d e f g h i j);
  737. //
  738. // expands to this:
  739. //
  740. // quote_tokens_with_context!(_s
  741. // (@ @ @ @ @ @ a b c d e f g h i j)
  742. // (@ @ @ @ @ a b c d e f g h i j @)
  743. // (@ @ @ @ a b c d e f g h i j @ @)
  744. // (@ @ @ (a) (b) (c) (d) (e) (f) (g) (h) (i) (j) @ @ @)
  745. // (@ @ a b c d e f g h i j @ @ @ @)
  746. // (@ a b c d e f g h i j @ @ @ @ @)
  747. // (a b c d e f g h i j @ @ @ @ @ @)
  748. // );
  749. //
  750. // which gets transposed and expanded to this:
  751. //
  752. // quote_token_with_context!(_s @ @ @ @ @ @ a);
  753. // quote_token_with_context!(_s @ @ @ @ @ a b);
  754. // quote_token_with_context!(_s @ @ @ @ a b c);
  755. // quote_token_with_context!(_s @ @ @ (a) b c d);
  756. // quote_token_with_context!(_s @ @ a (b) c d e);
  757. // quote_token_with_context!(_s @ a b (c) d e f);
  758. // quote_token_with_context!(_s a b c (d) e f g);
  759. // quote_token_with_context!(_s b c d (e) f g h);
  760. // quote_token_with_context!(_s c d e (f) g h i);
  761. // quote_token_with_context!(_s d e f (g) h i j);
  762. // quote_token_with_context!(_s e f g (h) i j @);
  763. // quote_token_with_context!(_s f g h (i) j @ @);
  764. // quote_token_with_context!(_s g h i (j) @ @ @);
  765. // quote_token_with_context!(_s h i j @ @ @ @);
  766. // quote_token_with_context!(_s i j @ @ @ @ @);
  767. // quote_token_with_context!(_s j @ @ @ @ @ @);
  768. //
  769. // Without having used muncher-style recursion, we get one invocation of
  770. // quote_token_with_context for each original tt, with three tts of context on
  771. // either side. This is enough for the longest possible interpolation form (a
  772. // repetition with separator, as in `# (#var) , *`) to be fully represented with
  773. // the first or last tt in the middle.
  774. //
  775. // The middle tt (surrounded by parentheses) is the tt being processed.
  776. //
  777. // - When it is a `#`, quote_token_with_context can do an interpolation. The
  778. // interpolation kind will depend on the three subsequent tts.
  779. //
  780. // - When it is within a later part of an interpolation, it can be ignored
  781. // because the interpolation has already been done.
  782. //
  783. // - When it is not part of an interpolation it can be pushed as a single
  784. // token into the output.
  785. //
  786. // - When the middle token is an unparenthesized `@`, that call is one of the
  787. // first 3 or last 3 calls of quote_token_with_context and does not
  788. // correspond to one of the original input tokens, so turns into nothing.
  789. #[macro_export]
  790. #[doc(hidden)]
  791. macro_rules! quote_each_token {
  792. ($tokens:ident $($tts:tt)*) => {
  793. $crate::quote_tokens_with_context!{$tokens
  794. (@ @ @ @ @ @ $($tts)*)
  795. (@ @ @ @ @ $($tts)* @)
  796. (@ @ @ @ $($tts)* @ @)
  797. (@ @ @ $(($tts))* @ @ @)
  798. (@ @ $($tts)* @ @ @ @)
  799. (@ $($tts)* @ @ @ @ @)
  800. ($($tts)* @ @ @ @ @ @)
  801. }
  802. };
  803. }
  804. // See the explanation on quote_each_token.
  805. #[macro_export]
  806. #[doc(hidden)]
  807. macro_rules! quote_each_token_spanned {
  808. ($tokens:ident $span:ident $($tts:tt)*) => {
  809. $crate::quote_tokens_with_context_spanned!{$tokens $span
  810. (@ @ @ @ @ @ $($tts)*)
  811. (@ @ @ @ @ $($tts)* @)
  812. (@ @ @ @ $($tts)* @ @)
  813. (@ @ @ $(($tts))* @ @ @)
  814. (@ @ $($tts)* @ @ @ @)
  815. (@ $($tts)* @ @ @ @ @)
  816. ($($tts)* @ @ @ @ @ @)
  817. }
  818. };
  819. }
  820. // See the explanation on quote_each_token.
  821. #[macro_export]
  822. #[doc(hidden)]
  823. macro_rules! quote_tokens_with_context {
  824. ($tokens:ident
  825. ($($b3:tt)*) ($($b2:tt)*) ($($b1:tt)*)
  826. ($($curr:tt)*)
  827. ($($a1:tt)*) ($($a2:tt)*) ($($a3:tt)*)
  828. ) => {
  829. $(
  830. $crate::quote_token_with_context!{$tokens $b3 $b2 $b1 $curr $a1 $a2 $a3}
  831. )*
  832. };
  833. }
  834. // See the explanation on quote_each_token.
  835. #[macro_export]
  836. #[doc(hidden)]
  837. macro_rules! quote_tokens_with_context_spanned {
  838. ($tokens:ident $span:ident
  839. ($($b3:tt)*) ($($b2:tt)*) ($($b1:tt)*)
  840. ($($curr:tt)*)
  841. ($($a1:tt)*) ($($a2:tt)*) ($($a3:tt)*)
  842. ) => {
  843. $(
  844. $crate::quote_token_with_context_spanned!{$tokens $span $b3 $b2 $b1 $curr $a1 $a2 $a3}
  845. )*
  846. };
  847. }
  848. // See the explanation on quote_each_token.
  849. #[macro_export]
  850. #[doc(hidden)]
  851. macro_rules! quote_token_with_context {
  852. // Unparenthesized `@` indicates this call does not correspond to one of the
  853. // original input tokens. Ignore it.
  854. ($tokens:ident $b3:tt $b2:tt $b1:tt @ $a1:tt $a2:tt $a3:tt) => {};
  855. // A repetition with no separator.
  856. ($tokens:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) * $a3:tt) => {{
  857. use $crate::__private::ext::*;
  858. let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
  859. $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*}
  860. let _: $crate::__private::HasIterator = has_iter;
  861. // This is `while true` instead of `loop` because if there are no
  862. // iterators used inside of this repetition then the body would not
  863. // contain any `break`, so the compiler would emit unreachable code
  864. // warnings on anything below the loop. We use has_iter to detect and
  865. // fail to compile when there are no iterators, so here we just work
  866. // around the unneeded extra warning.
  867. while true {
  868. $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*}
  869. $crate::quote_each_token!{$tokens $($inner)*}
  870. }
  871. }};
  872. // ... and one step later.
  873. ($tokens:ident $b3:tt $b2:tt # (( $($inner:tt)* )) * $a2:tt $a3:tt) => {};
  874. // ... and one step later.
  875. ($tokens:ident $b3:tt # ( $($inner:tt)* ) (*) $a1:tt $a2:tt $a3:tt) => {};
  876. // A repetition with separator.
  877. ($tokens:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) $sep:tt *) => {{
  878. use $crate::__private::ext::*;
  879. let mut _i = 0usize;
  880. let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
  881. $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*}
  882. let _: $crate::__private::HasIterator = has_iter;
  883. while true {
  884. $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*}
  885. if _i > 0 {
  886. $crate::quote_token!{$sep $tokens}
  887. }
  888. _i += 1;
  889. $crate::quote_each_token!{$tokens $($inner)*}
  890. }
  891. }};
  892. // ... and one step later.
  893. ($tokens:ident $b3:tt $b2:tt # (( $($inner:tt)* )) $sep:tt * $a3:tt) => {};
  894. // ... and one step later.
  895. ($tokens:ident $b3:tt # ( $($inner:tt)* ) ($sep:tt) * $a2:tt $a3:tt) => {};
  896. // (A special case for `#(var)**`, where the first `*` is treated as the
  897. // repetition symbol and the second `*` is treated as an ordinary token.)
  898. ($tokens:ident # ( $($inner:tt)* ) * (*) $a1:tt $a2:tt $a3:tt) => {
  899. // https://github.com/dtolnay/quote/issues/130
  900. $crate::quote_token!{* $tokens}
  901. };
  902. // ... and one step later.
  903. ($tokens:ident # ( $($inner:tt)* ) $sep:tt (*) $a1:tt $a2:tt $a3:tt) => {};
  904. // A non-repetition interpolation.
  905. ($tokens:ident $b3:tt $b2:tt $b1:tt (#) $var:ident $a2:tt $a3:tt) => {
  906. $crate::ToTokens::to_tokens(&$var, &mut $tokens);
  907. };
  908. // ... and one step later.
  909. ($tokens:ident $b3:tt $b2:tt # ($var:ident) $a1:tt $a2:tt $a3:tt) => {};
  910. // An ordinary token, not part of any interpolation.
  911. ($tokens:ident $b3:tt $b2:tt $b1:tt ($curr:tt) $a1:tt $a2:tt $a3:tt) => {
  912. $crate::quote_token!{$curr $tokens}
  913. };
  914. }
  915. // See the explanation on quote_each_token, and on the individual rules of
  916. // quote_token_with_context.
  917. #[macro_export]
  918. #[doc(hidden)]
  919. macro_rules! quote_token_with_context_spanned {
  920. ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt @ $a1:tt $a2:tt $a3:tt) => {};
  921. ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) * $a3:tt) => {{
  922. use $crate::__private::ext::*;
  923. let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
  924. $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*}
  925. let _: $crate::__private::HasIterator = has_iter;
  926. while true {
  927. $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*}
  928. $crate::quote_each_token_spanned!{$tokens $span $($inner)*}
  929. }
  930. }};
  931. ($tokens:ident $span:ident $b3:tt $b2:tt # (( $($inner:tt)* )) * $a2:tt $a3:tt) => {};
  932. ($tokens:ident $span:ident $b3:tt # ( $($inner:tt)* ) (*) $a1:tt $a2:tt $a3:tt) => {};
  933. ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) ( $($inner:tt)* ) $sep:tt *) => {{
  934. use $crate::__private::ext::*;
  935. let mut _i = 0usize;
  936. let has_iter = $crate::__private::ThereIsNoIteratorInRepetition;
  937. $crate::pounded_var_names!{quote_bind_into_iter!(has_iter) () $($inner)*}
  938. let _: $crate::__private::HasIterator = has_iter;
  939. while true {
  940. $crate::pounded_var_names!{quote_bind_next_or_break!() () $($inner)*}
  941. if _i > 0 {
  942. $crate::quote_token_spanned!{$sep $tokens $span}
  943. }
  944. _i += 1;
  945. $crate::quote_each_token_spanned!{$tokens $span $($inner)*}
  946. }
  947. }};
  948. ($tokens:ident $span:ident $b3:tt $b2:tt # (( $($inner:tt)* )) $sep:tt * $a3:tt) => {};
  949. ($tokens:ident $span:ident $b3:tt # ( $($inner:tt)* ) ($sep:tt) * $a2:tt $a3:tt) => {};
  950. ($tokens:ident $span:ident # ( $($inner:tt)* ) * (*) $a1:tt $a2:tt $a3:tt) => {
  951. // https://github.com/dtolnay/quote/issues/130
  952. $crate::quote_token_spanned!{* $tokens $span}
  953. };
  954. ($tokens:ident $span:ident # ( $($inner:tt)* ) $sep:tt (*) $a1:tt $a2:tt $a3:tt) => {};
  955. ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt (#) $var:ident $a2:tt $a3:tt) => {
  956. $crate::ToTokens::to_tokens(&$var, &mut $tokens);
  957. };
  958. ($tokens:ident $span:ident $b3:tt $b2:tt # ($var:ident) $a1:tt $a2:tt $a3:tt) => {};
  959. ($tokens:ident $span:ident $b3:tt $b2:tt $b1:tt ($curr:tt) $a1:tt $a2:tt $a3:tt) => {
  960. $crate::quote_token_spanned!{$curr $tokens $span}
  961. };
  962. }
  963. // These rules are ordered by approximate token frequency, at least for the
  964. // first 10 or so, to improve compile times. Having `ident` first is by far the
  965. // most important because it's typically 2-3x more common than the next most
  966. // common token.
  967. //
  968. // Separately, we put the token being matched in the very front so that failing
  969. // rules may fail to match as quickly as possible.
  970. #[macro_export]
  971. #[doc(hidden)]
  972. macro_rules! quote_token {
  973. ($ident:ident $tokens:ident) => {
  974. $crate::__private::push_ident(&mut $tokens, stringify!($ident));
  975. };
  976. (:: $tokens:ident) => {
  977. $crate::__private::push_colon2(&mut $tokens);
  978. };
  979. (( $($inner:tt)* ) $tokens:ident) => {
  980. $crate::__private::push_group(
  981. &mut $tokens,
  982. $crate::__private::Delimiter::Parenthesis,
  983. $crate::quote!($($inner)*),
  984. );
  985. };
  986. ([ $($inner:tt)* ] $tokens:ident) => {
  987. $crate::__private::push_group(
  988. &mut $tokens,
  989. $crate::__private::Delimiter::Bracket,
  990. $crate::quote!($($inner)*),
  991. );
  992. };
  993. ({ $($inner:tt)* } $tokens:ident) => {
  994. $crate::__private::push_group(
  995. &mut $tokens,
  996. $crate::__private::Delimiter::Brace,
  997. $crate::quote!($($inner)*),
  998. );
  999. };
  1000. (# $tokens:ident) => {
  1001. $crate::__private::push_pound(&mut $tokens);
  1002. };
  1003. (, $tokens:ident) => {
  1004. $crate::__private::push_comma(&mut $tokens);
  1005. };
  1006. (. $tokens:ident) => {
  1007. $crate::__private::push_dot(&mut $tokens);
  1008. };
  1009. (; $tokens:ident) => {
  1010. $crate::__private::push_semi(&mut $tokens);
  1011. };
  1012. (: $tokens:ident) => {
  1013. $crate::__private::push_colon(&mut $tokens);
  1014. };
  1015. (+ $tokens:ident) => {
  1016. $crate::__private::push_add(&mut $tokens);
  1017. };
  1018. (+= $tokens:ident) => {
  1019. $crate::__private::push_add_eq(&mut $tokens);
  1020. };
  1021. (& $tokens:ident) => {
  1022. $crate::__private::push_and(&mut $tokens);
  1023. };
  1024. (&& $tokens:ident) => {
  1025. $crate::__private::push_and_and(&mut $tokens);
  1026. };
  1027. (&= $tokens:ident) => {
  1028. $crate::__private::push_and_eq(&mut $tokens);
  1029. };
  1030. (@ $tokens:ident) => {
  1031. $crate::__private::push_at(&mut $tokens);
  1032. };
  1033. (! $tokens:ident) => {
  1034. $crate::__private::push_bang(&mut $tokens);
  1035. };
  1036. (^ $tokens:ident) => {
  1037. $crate::__private::push_caret(&mut $tokens);
  1038. };
  1039. (^= $tokens:ident) => {
  1040. $crate::__private::push_caret_eq(&mut $tokens);
  1041. };
  1042. (/ $tokens:ident) => {
  1043. $crate::__private::push_div(&mut $tokens);
  1044. };
  1045. (/= $tokens:ident) => {
  1046. $crate::__private::push_div_eq(&mut $tokens);
  1047. };
  1048. (.. $tokens:ident) => {
  1049. $crate::__private::push_dot2(&mut $tokens);
  1050. };
  1051. (... $tokens:ident) => {
  1052. $crate::__private::push_dot3(&mut $tokens);
  1053. };
  1054. (..= $tokens:ident) => {
  1055. $crate::__private::push_dot_dot_eq(&mut $tokens);
  1056. };
  1057. (= $tokens:ident) => {
  1058. $crate::__private::push_eq(&mut $tokens);
  1059. };
  1060. (== $tokens:ident) => {
  1061. $crate::__private::push_eq_eq(&mut $tokens);
  1062. };
  1063. (>= $tokens:ident) => {
  1064. $crate::__private::push_ge(&mut $tokens);
  1065. };
  1066. (> $tokens:ident) => {
  1067. $crate::__private::push_gt(&mut $tokens);
  1068. };
  1069. (<= $tokens:ident) => {
  1070. $crate::__private::push_le(&mut $tokens);
  1071. };
  1072. (< $tokens:ident) => {
  1073. $crate::__private::push_lt(&mut $tokens);
  1074. };
  1075. (*= $tokens:ident) => {
  1076. $crate::__private::push_mul_eq(&mut $tokens);
  1077. };
  1078. (!= $tokens:ident) => {
  1079. $crate::__private::push_ne(&mut $tokens);
  1080. };
  1081. (| $tokens:ident) => {
  1082. $crate::__private::push_or(&mut $tokens);
  1083. };
  1084. (|= $tokens:ident) => {
  1085. $crate::__private::push_or_eq(&mut $tokens);
  1086. };
  1087. (|| $tokens:ident) => {
  1088. $crate::__private::push_or_or(&mut $tokens);
  1089. };
  1090. (? $tokens:ident) => {
  1091. $crate::__private::push_question(&mut $tokens);
  1092. };
  1093. (-> $tokens:ident) => {
  1094. $crate::__private::push_rarrow(&mut $tokens);
  1095. };
  1096. (<- $tokens:ident) => {
  1097. $crate::__private::push_larrow(&mut $tokens);
  1098. };
  1099. (% $tokens:ident) => {
  1100. $crate::__private::push_rem(&mut $tokens);
  1101. };
  1102. (%= $tokens:ident) => {
  1103. $crate::__private::push_rem_eq(&mut $tokens);
  1104. };
  1105. (=> $tokens:ident) => {
  1106. $crate::__private::push_fat_arrow(&mut $tokens);
  1107. };
  1108. (<< $tokens:ident) => {
  1109. $crate::__private::push_shl(&mut $tokens);
  1110. };
  1111. (<<= $tokens:ident) => {
  1112. $crate::__private::push_shl_eq(&mut $tokens);
  1113. };
  1114. (>> $tokens:ident) => {
  1115. $crate::__private::push_shr(&mut $tokens);
  1116. };
  1117. (>>= $tokens:ident) => {
  1118. $crate::__private::push_shr_eq(&mut $tokens);
  1119. };
  1120. (* $tokens:ident) => {
  1121. $crate::__private::push_star(&mut $tokens);
  1122. };
  1123. (- $tokens:ident) => {
  1124. $crate::__private::push_sub(&mut $tokens);
  1125. };
  1126. (-= $tokens:ident) => {
  1127. $crate::__private::push_sub_eq(&mut $tokens);
  1128. };
  1129. ($lifetime:lifetime $tokens:ident) => {
  1130. $crate::__private::push_lifetime(&mut $tokens, stringify!($lifetime));
  1131. };
  1132. (_ $tokens:ident) => {
  1133. $crate::__private::push_underscore(&mut $tokens);
  1134. };
  1135. ($other:tt $tokens:ident) => {
  1136. $crate::__private::parse(&mut $tokens, stringify!($other));
  1137. };
  1138. }
  1139. // See the comment above `quote_token!` about the rule ordering.
  1140. #[macro_export]
  1141. #[doc(hidden)]
  1142. macro_rules! quote_token_spanned {
  1143. ($ident:ident $tokens:ident $span:ident) => {
  1144. $crate::__private::push_ident_spanned(&mut $tokens, $span, stringify!($ident));
  1145. };
  1146. (:: $tokens:ident $span:ident) => {
  1147. $crate::__private::push_colon2_spanned(&mut $tokens, $span);
  1148. };
  1149. (( $($inner:tt)* ) $tokens:ident $span:ident) => {
  1150. $crate::__private::push_group_spanned(
  1151. &mut $tokens,
  1152. $span,
  1153. $crate::__private::Delimiter::Parenthesis,
  1154. $crate::quote_spanned!($span=> $($inner)*),
  1155. );
  1156. };
  1157. ([ $($inner:tt)* ] $tokens:ident $span:ident) => {
  1158. $crate::__private::push_group_spanned(
  1159. &mut $tokens,
  1160. $span,
  1161. $crate::__private::Delimiter::Bracket,
  1162. $crate::quote_spanned!($span=> $($inner)*),
  1163. );
  1164. };
  1165. ({ $($inner:tt)* } $tokens:ident $span:ident) => {
  1166. $crate::__private::push_group_spanned(
  1167. &mut $tokens,
  1168. $span,
  1169. $crate::__private::Delimiter::Brace,
  1170. $crate::quote_spanned!($span=> $($inner)*),
  1171. );
  1172. };
  1173. (# $tokens:ident $span:ident) => {
  1174. $crate::__private::push_pound_spanned(&mut $tokens, $span);
  1175. };
  1176. (, $tokens:ident $span:ident) => {
  1177. $crate::__private::push_comma_spanned(&mut $tokens, $span);
  1178. };
  1179. (. $tokens:ident $span:ident) => {
  1180. $crate::__private::push_dot_spanned(&mut $tokens, $span);
  1181. };
  1182. (; $tokens:ident $span:ident) => {
  1183. $crate::__private::push_semi_spanned(&mut $tokens, $span);
  1184. };
  1185. (: $tokens:ident $span:ident) => {
  1186. $crate::__private::push_colon_spanned(&mut $tokens, $span);
  1187. };
  1188. (+ $tokens:ident $span:ident) => {
  1189. $crate::__private::push_add_spanned(&mut $tokens, $span);
  1190. };
  1191. (+= $tokens:ident $span:ident) => {
  1192. $crate::__private::push_add_eq_spanned(&mut $tokens, $span);
  1193. };
  1194. (& $tokens:ident $span:ident) => {
  1195. $crate::__private::push_and_spanned(&mut $tokens, $span);
  1196. };
  1197. (&& $tokens:ident $span:ident) => {
  1198. $crate::__private::push_and_and_spanned(&mut $tokens, $span);
  1199. };
  1200. (&= $tokens:ident $span:ident) => {
  1201. $crate::__private::push_and_eq_spanned(&mut $tokens, $span);
  1202. };
  1203. (@ $tokens:ident $span:ident) => {
  1204. $crate::__private::push_at_spanned(&mut $tokens, $span);
  1205. };
  1206. (! $tokens:ident $span:ident) => {
  1207. $crate::__private::push_bang_spanned(&mut $tokens, $span);
  1208. };
  1209. (^ $tokens:ident $span:ident) => {
  1210. $crate::__private::push_caret_spanned(&mut $tokens, $span);
  1211. };
  1212. (^= $tokens:ident $span:ident) => {
  1213. $crate::__private::push_caret_eq_spanned(&mut $tokens, $span);
  1214. };
  1215. (/ $tokens:ident $span:ident) => {
  1216. $crate::__private::push_div_spanned(&mut $tokens, $span);
  1217. };
  1218. (/= $tokens:ident $span:ident) => {
  1219. $crate::__private::push_div_eq_spanned(&mut $tokens, $span);
  1220. };
  1221. (.. $tokens:ident $span:ident) => {
  1222. $crate::__private::push_dot2_spanned(&mut $tokens, $span);
  1223. };
  1224. (... $tokens:ident $span:ident) => {
  1225. $crate::__private::push_dot3_spanned(&mut $tokens, $span);
  1226. };
  1227. (..= $tokens:ident $span:ident) => {
  1228. $crate::__private::push_dot_dot_eq_spanned(&mut $tokens, $span);
  1229. };
  1230. (= $tokens:ident $span:ident) => {
  1231. $crate::__private::push_eq_spanned(&mut $tokens, $span);
  1232. };
  1233. (== $tokens:ident $span:ident) => {
  1234. $crate::__private::push_eq_eq_spanned(&mut $tokens, $span);
  1235. };
  1236. (>= $tokens:ident $span:ident) => {
  1237. $crate::__private::push_ge_spanned(&mut $tokens, $span);
  1238. };
  1239. (> $tokens:ident $span:ident) => {
  1240. $crate::__private::push_gt_spanned(&mut $tokens, $span);
  1241. };
  1242. (<= $tokens:ident $span:ident) => {
  1243. $crate::__private::push_le_spanned(&mut $tokens, $span);
  1244. };
  1245. (< $tokens:ident $span:ident) => {
  1246. $crate::__private::push_lt_spanned(&mut $tokens, $span);
  1247. };
  1248. (*= $tokens:ident $span:ident) => {
  1249. $crate::__private::push_mul_eq_spanned(&mut $tokens, $span);
  1250. };
  1251. (!= $tokens:ident $span:ident) => {
  1252. $crate::__private::push_ne_spanned(&mut $tokens, $span);
  1253. };
  1254. (| $tokens:ident $span:ident) => {
  1255. $crate::__private::push_or_spanned(&mut $tokens, $span);
  1256. };
  1257. (|= $tokens:ident $span:ident) => {
  1258. $crate::__private::push_or_eq_spanned(&mut $tokens, $span);
  1259. };
  1260. (|| $tokens:ident $span:ident) => {
  1261. $crate::__private::push_or_or_spanned(&mut $tokens, $span);
  1262. };
  1263. (? $tokens:ident $span:ident) => {
  1264. $crate::__private::push_question_spanned(&mut $tokens, $span);
  1265. };
  1266. (-> $tokens:ident $span:ident) => {
  1267. $crate::__private::push_rarrow_spanned(&mut $tokens, $span);
  1268. };
  1269. (<- $tokens:ident $span:ident) => {
  1270. $crate::__private::push_larrow_spanned(&mut $tokens, $span);
  1271. };
  1272. (% $tokens:ident $span:ident) => {
  1273. $crate::__private::push_rem_spanned(&mut $tokens, $span);
  1274. };
  1275. (%= $tokens:ident $span:ident) => {
  1276. $crate::__private::push_rem_eq_spanned(&mut $tokens, $span);
  1277. };
  1278. (=> $tokens:ident $span:ident) => {
  1279. $crate::__private::push_fat_arrow_spanned(&mut $tokens, $span);
  1280. };
  1281. (<< $tokens:ident $span:ident) => {
  1282. $crate::__private::push_shl_spanned(&mut $tokens, $span);
  1283. };
  1284. (<<= $tokens:ident $span:ident) => {
  1285. $crate::__private::push_shl_eq_spanned(&mut $tokens, $span);
  1286. };
  1287. (>> $tokens:ident $span:ident) => {
  1288. $crate::__private::push_shr_spanned(&mut $tokens, $span);
  1289. };
  1290. (>>= $tokens:ident $span:ident) => {
  1291. $crate::__private::push_shr_eq_spanned(&mut $tokens, $span);
  1292. };
  1293. (* $tokens:ident $span:ident) => {
  1294. $crate::__private::push_star_spanned(&mut $tokens, $span);
  1295. };
  1296. (- $tokens:ident $span:ident) => {
  1297. $crate::__private::push_sub_spanned(&mut $tokens, $span);
  1298. };
  1299. (-= $tokens:ident $span:ident) => {
  1300. $crate::__private::push_sub_eq_spanned(&mut $tokens, $span);
  1301. };
  1302. ($lifetime:lifetime $tokens:ident $span:ident) => {
  1303. $crate::__private::push_lifetime_spanned(&mut $tokens, $span, stringify!($lifetime));
  1304. };
  1305. (_ $tokens:ident $span:ident) => {
  1306. $crate::__private::push_underscore_spanned(&mut $tokens, $span);
  1307. };
  1308. ($other:tt $tokens:ident $span:ident) => {
  1309. $crate::__private::parse_spanned(&mut $tokens, $span, stringify!($other));
  1310. };
  1311. }