David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 1 | //! A wrapper around the procedural macro API of the compiler's [`proc_macro`] |
| 2 | //! crate. This library serves three purposes: |
Alex Crichton | babc99e | 2017-07-05 18:00:29 -0700 | [diff] [blame] | 3 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 4 | //! [`proc_macro`]: https://doc.rust-lang.org/proc_macro/ |
Alex Crichton | babc99e | 2017-07-05 18:00:29 -0700 | [diff] [blame] | 5 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 6 | //! - **Bring proc-macro-like functionality to other contexts like build.rs and |
| 7 | //! main.rs.** Types from `proc_macro` are entirely specific to procedural |
| 8 | //! macros and cannot ever exist in code outside of a procedural macro. |
| 9 | //! Meanwhile `proc_macro2` types may exist anywhere including non-macro code. |
| 10 | //! By developing foundational libraries like [syn] and [quote] against |
| 11 | //! `proc_macro2` rather than `proc_macro`, the procedural macro ecosystem |
| 12 | //! becomes easily applicable to many other use cases and we avoid |
| 13 | //! reimplementing non-macro equivalents of those libraries. |
Alex Crichton | babc99e | 2017-07-05 18:00:29 -0700 | [diff] [blame] | 14 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 15 | //! - **Make procedural macros unit testable.** As a consequence of being |
| 16 | //! specific to procedural macros, nothing that uses `proc_macro` can be |
| 17 | //! executed from a unit test. In order for helper libraries or components of |
| 18 | //! a macro to be testable in isolation, they must be implemented using |
| 19 | //! `proc_macro2`. |
Alex Crichton | 1fd0e8a | 2018-02-04 21:29:13 -0800 | [diff] [blame] | 20 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 21 | //! - **Provide the latest and greatest APIs across all compiler versions.** |
| 22 | //! Procedural macros were first introduced to Rust in 1.15.0 with an |
| 23 | //! extremely minimal interface. Since then, many improvements have landed to |
| 24 | //! make macros more flexible and easier to write. This library tracks the |
| 25 | //! procedural macro API of the most recent stable compiler but employs a |
| 26 | //! polyfill to provide that API consistently across any compiler since |
| 27 | //! 1.15.0. |
David Tolnay | 6b46deb | 2018-04-25 21:22:46 -0700 | [diff] [blame] | 28 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 29 | //! [syn]: https://github.com/dtolnay/syn |
| 30 | //! [quote]: https://github.com/dtolnay/quote |
David Tolnay | 6b46deb | 2018-04-25 21:22:46 -0700 | [diff] [blame] | 31 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 32 | //! # Usage |
| 33 | //! |
| 34 | //! The skeleton of a typical procedural macro typically looks like this: |
| 35 | //! |
| 36 | //! ```edition2018 |
| 37 | //! extern crate proc_macro; |
| 38 | //! |
| 39 | //! # const IGNORE: &str = stringify! { |
| 40 | //! #[proc_macro_derive(MyDerive)] |
| 41 | //! # }; |
| 42 | //! pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 43 | //! let input = proc_macro2::TokenStream::from(input); |
| 44 | //! |
| 45 | //! let output: proc_macro2::TokenStream = { |
| 46 | //! /* transform input */ |
| 47 | //! # input |
| 48 | //! }; |
| 49 | //! |
| 50 | //! proc_macro::TokenStream::from(output) |
| 51 | //! } |
| 52 | //! ``` |
| 53 | //! |
| 54 | //! If parsing with [Syn], you'll use [`parse_macro_input!`] instead to |
| 55 | //! propagate parse errors correctly back to the compiler when parsing fails. |
| 56 | //! |
| 57 | //! [`parse_macro_input!`]: https://docs.rs/syn/0.15/syn/macro.parse_macro_input.html |
| 58 | //! |
| 59 | //! # Unstable features |
| 60 | //! |
| 61 | //! The default feature set of proc-macro2 tracks the most recent stable |
| 62 | //! compiler API. Functionality in `proc_macro` that is not yet stable is not |
| 63 | //! exposed by proc-macro2 by default. |
| 64 | //! |
| 65 | //! To opt into the additional APIs available in the most recent nightly |
| 66 | //! compiler, the `procmacro2_semver_exempt` config flag must be passed to |
| 67 | //! rustc. As usual, we will polyfill those nightly-only APIs all the way back |
| 68 | //! to Rust 1.15.0. As these are unstable APIs that track the nightly compiler, |
| 69 | //! minor versions of proc-macro2 may make breaking changes to them at any time. |
David Tolnay | 6b46deb | 2018-04-25 21:22:46 -0700 | [diff] [blame] | 70 | //! |
| 71 | //! ```sh |
| 72 | //! RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build |
| 73 | //! ``` |
| 74 | //! |
| 75 | //! Note that this must not only be done for your crate, but for any crate that |
| 76 | //! depends on your crate. This infectious nature is intentional, as it serves |
| 77 | //! as a reminder that you are outside of the normal semver guarantees. |
| 78 | //! |
David Tolnay | 7aa1c9c | 2019-01-19 19:53:10 -0800 | [diff] [blame] | 79 | //! Semver exempt methods are marked as such in the proc-macro2 documentation. |
Michael Bryan | 4333996 | 2019-06-19 09:04:22 +0800 | [diff] [blame] | 80 | //! |
| 81 | //! # Thread-Safety |
| 82 | //! |
| 83 | //! Most types in this crate are `!Sync` because the underlying compiler |
| 84 | //! types make use of thread-local memory, meaning they cannot be accessed from |
| 85 | //! a different thread. |
Alex Crichton | babc99e | 2017-07-05 18:00:29 -0700 | [diff] [blame] | 86 | |
David Tolnay | 15cc498 | 2018-01-08 08:03:27 -0800 | [diff] [blame] | 87 | // Proc-macro2 types in rustdoc of other crates get linked to here. |
David Tolnay | f514921 | 2019-05-08 13:39:08 -0700 | [diff] [blame] | 88 | #![doc(html_root_url = "https://docs.rs/proc-macro2/0.4.30")] |
David Tolnay | b455dd7 | 2019-04-28 13:50:51 -0700 | [diff] [blame] | 89 | #![cfg_attr(any(proc_macro_span, super_unstable), feature(proc_macro_span))] |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 90 | #![cfg_attr(super_unstable, feature(proc_macro_raw_ident, proc_macro_def_site))] |
David Tolnay | eac2b75 | 2019-07-19 11:55:12 -0700 | [diff] [blame^] | 91 | #![allow(ellipsis_inclusive_range_patterns, unknown_lints)] |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 92 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 93 | #[cfg(use_proc_macro)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 94 | extern crate proc_macro; |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 95 | extern crate unicode_xid; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 96 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 97 | use std::cmp::Ordering; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 98 | use std::fmt; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 99 | use std::hash::{Hash, Hasher}; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 100 | use std::iter::FromIterator; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 101 | use std::marker; |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 102 | #[cfg(procmacro2_semver_exempt)] |
| 103 | use std::path::PathBuf; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 104 | use std::rc::Rc; |
| 105 | use std::str::FromStr; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 106 | |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 107 | #[macro_use] |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 108 | mod strnom; |
David Tolnay | aef075b | 2019-01-16 16:29:18 -0800 | [diff] [blame] | 109 | mod fallback; |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 110 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 111 | #[cfg(not(wrap_proc_macro))] |
David Tolnay | aef075b | 2019-01-16 16:29:18 -0800 | [diff] [blame] | 112 | use fallback as imp; |
| 113 | #[path = "wrapper.rs"] |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 114 | #[cfg(wrap_proc_macro)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 115 | mod imp; |
| 116 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 117 | /// An abstract stream of tokens, or more concretely a sequence of token trees. |
| 118 | /// |
| 119 | /// This type provides interfaces for iterating over token trees and for |
| 120 | /// collecting token trees into one stream. |
| 121 | /// |
| 122 | /// Token stream is both the input and output of `#[proc_macro]`, |
| 123 | /// `#[proc_macro_attribute]` and `#[proc_macro_derive]` definitions. |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 124 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 125 | pub struct TokenStream { |
| 126 | inner: imp::TokenStream, |
| 127 | _marker: marker::PhantomData<Rc<()>>, |
| 128 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 129 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 130 | /// Error returned from `TokenStream::from_str`. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 131 | pub struct LexError { |
| 132 | inner: imp::LexError, |
| 133 | _marker: marker::PhantomData<Rc<()>>, |
| 134 | } |
| 135 | |
| 136 | impl TokenStream { |
| 137 | fn _new(inner: imp::TokenStream) -> TokenStream { |
| 138 | TokenStream { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 139 | inner, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 140 | _marker: marker::PhantomData, |
| 141 | } |
| 142 | } |
| 143 | |
David Tolnay | aef075b | 2019-01-16 16:29:18 -0800 | [diff] [blame] | 144 | fn _new_stable(inner: fallback::TokenStream) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 145 | TokenStream { |
| 146 | inner: inner.into(), |
| 147 | _marker: marker::PhantomData, |
| 148 | } |
| 149 | } |
| 150 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 151 | /// Returns an empty `TokenStream` containing no token trees. |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 152 | pub fn new() -> TokenStream { |
| 153 | TokenStream::_new(imp::TokenStream::new()) |
| 154 | } |
| 155 | |
| 156 | #[deprecated(since = "0.4.4", note = "please use TokenStream::new")] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 157 | pub fn empty() -> TokenStream { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 158 | TokenStream::new() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 159 | } |
| 160 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 161 | /// Checks if this `TokenStream` is empty. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 162 | pub fn is_empty(&self) -> bool { |
| 163 | self.inner.is_empty() |
| 164 | } |
| 165 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 166 | |
Árpád Goretity | 4f74b68 | 2018-07-14 00:47:51 +0200 | [diff] [blame] | 167 | /// `TokenStream::default()` returns an empty stream, |
| 168 | /// i.e. this is equivalent with `TokenStream::new()`. |
| 169 | impl Default for TokenStream { |
| 170 | fn default() -> Self { |
| 171 | TokenStream::new() |
| 172 | } |
| 173 | } |
| 174 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 175 | /// Attempts to break the string into tokens and parse those tokens into a token |
| 176 | /// stream. |
| 177 | /// |
| 178 | /// May fail for a number of reasons, for example, if the string contains |
| 179 | /// unbalanced delimiters or characters not existing in the language. |
| 180 | /// |
| 181 | /// NOTE: Some errors may cause panics instead of returning `LexError`. We |
| 182 | /// reserve the right to change these errors into `LexError`s later. |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 183 | impl FromStr for TokenStream { |
| 184 | type Err = LexError; |
| 185 | |
| 186 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 187 | let e = src.parse().map_err(|e| LexError { |
| 188 | inner: e, |
| 189 | _marker: marker::PhantomData, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 190 | })?; |
| 191 | Ok(TokenStream::_new(e)) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 195 | #[cfg(use_proc_macro)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 196 | impl From<proc_macro::TokenStream> for TokenStream { |
| 197 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 198 | TokenStream::_new(inner.into()) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Alex Crichton | 5354848 | 2018-08-11 21:54:05 -0700 | [diff] [blame] | 202 | #[cfg(use_proc_macro)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 203 | impl From<TokenStream> for proc_macro::TokenStream { |
| 204 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 205 | inner.inner.into() |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 209 | impl Extend<TokenTree> for TokenStream { |
| 210 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) { |
| 211 | self.inner.extend(streams) |
| 212 | } |
| 213 | } |
| 214 | |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 215 | impl Extend<TokenStream> for TokenStream { |
| 216 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { |
| 217 | self.inner |
| 218 | .extend(streams.into_iter().map(|stream| stream.inner)) |
| 219 | } |
| 220 | } |
| 221 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 222 | /// Collects a number of token trees into a single stream. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 223 | impl FromIterator<TokenTree> for TokenStream { |
| 224 | fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self { |
| 225 | TokenStream::_new(streams.into_iter().collect()) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
Alex Crichton | 53b0067 | 2018-09-06 17:16:10 -0700 | [diff] [blame] | 228 | impl FromIterator<TokenStream> for TokenStream { |
| 229 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { |
| 230 | TokenStream::_new(streams.into_iter().map(|i| i.inner).collect()) |
| 231 | } |
| 232 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 233 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 234 | /// Prints the token stream as a string that is supposed to be losslessly |
| 235 | /// convertible back into the same token stream (modulo spans), except for |
| 236 | /// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative |
| 237 | /// numeric literals. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 238 | impl fmt::Display for TokenStream { |
| 239 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 240 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 244 | /// Prints token in a form convenient for debugging. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 245 | impl fmt::Debug for TokenStream { |
| 246 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 247 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 251 | impl fmt::Debug for LexError { |
| 252 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 253 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 257 | /// The source file of a given `Span`. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 258 | /// |
| 259 | /// This type is semver exempt and not exposed by default. |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 260 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 261 | #[derive(Clone, PartialEq, Eq)] |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 262 | pub struct SourceFile { |
| 263 | inner: imp::SourceFile, |
| 264 | _marker: marker::PhantomData<Rc<()>>, |
| 265 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 266 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 267 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 268 | impl SourceFile { |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 269 | fn _new(inner: imp::SourceFile) -> Self { |
| 270 | SourceFile { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 271 | inner, |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 272 | _marker: marker::PhantomData, |
| 273 | } |
| 274 | } |
| 275 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 276 | /// Get the path to this source file. |
| 277 | /// |
| 278 | /// ### Note |
| 279 | /// |
| 280 | /// If the code span associated with this `SourceFile` was generated by an |
| 281 | /// external macro, this may not be an actual path on the filesystem. Use |
| 282 | /// [`is_real`] to check. |
| 283 | /// |
| 284 | /// Also note that even if `is_real` returns `true`, if |
| 285 | /// `--remap-path-prefix` was passed on the command line, the path as given |
| 286 | /// may not actually be valid. |
| 287 | /// |
| 288 | /// [`is_real`]: #method.is_real |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 289 | pub fn path(&self) -> PathBuf { |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 290 | self.inner.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 291 | } |
| 292 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 293 | /// Returns `true` if this source file is a real source file, and not |
| 294 | /// generated by an external macro's expansion. |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 295 | pub fn is_real(&self) -> bool { |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 296 | self.inner.is_real() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 300 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 301 | impl fmt::Debug for SourceFile { |
| 302 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 303 | self.inner.fmt(f) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 307 | /// A line-column pair representing the start or end of a `Span`. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 308 | /// |
| 309 | /// This type is semver exempt and not exposed by default. |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 310 | #[cfg(span_locations)] |
David Tolnay | 9d4fb44 | 2019-04-22 16:42:41 -0700 | [diff] [blame] | 311 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 312 | pub struct LineColumn { |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 313 | /// The 1-indexed line in the source file on which the span starts or ends |
| 314 | /// (inclusive). |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 315 | pub line: usize, |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 316 | /// The 0-indexed column (in UTF-8 characters) in the source file on which |
| 317 | /// the span starts or ends (inclusive). |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 318 | pub column: usize, |
| 319 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 320 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 321 | /// A region of source code, along with macro expansion information. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 322 | #[derive(Copy, Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 323 | pub struct Span { |
| 324 | inner: imp::Span, |
| 325 | _marker: marker::PhantomData<Rc<()>>, |
| 326 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 327 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 328 | impl Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 329 | fn _new(inner: imp::Span) -> Span { |
| 330 | Span { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 331 | inner, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 332 | _marker: marker::PhantomData, |
| 333 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 334 | } |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 335 | |
David Tolnay | aef075b | 2019-01-16 16:29:18 -0800 | [diff] [blame] | 336 | fn _new_stable(inner: fallback::Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 337 | Span { |
| 338 | inner: inner.into(), |
| 339 | _marker: marker::PhantomData, |
| 340 | } |
| 341 | } |
| 342 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 343 | /// The span of the invocation of the current procedural macro. |
| 344 | /// |
| 345 | /// Identifiers created with this span will be resolved as if they were |
| 346 | /// written directly at the macro call location (call-site hygiene) and |
| 347 | /// other code at the macro call site will be able to refer to them as well. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 348 | pub fn call_site() -> Span { |
| 349 | Span::_new(imp::Span::call_site()) |
| 350 | } |
| 351 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 352 | /// A span that resolves at the macro definition site. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 353 | /// |
| 354 | /// This method is semver exempt and not exposed by default. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 355 | #[cfg(procmacro2_semver_exempt)] |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 356 | pub fn def_site() -> Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 357 | Span::_new(imp::Span::def_site()) |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 358 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 359 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 360 | /// Creates a new span with the same line/column information as `self` but |
| 361 | /// that resolves symbols as though it were at `other`. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 362 | /// |
| 363 | /// This method is semver exempt and not exposed by default. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 364 | #[cfg(procmacro2_semver_exempt)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 365 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 366 | Span::_new(self.inner.resolved_at(other.inner)) |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /// Creates a new span with the same name resolution behavior as `self` but |
| 370 | /// with the line/column information of `other`. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 371 | /// |
| 372 | /// This method is semver exempt and not exposed by default. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 373 | #[cfg(procmacro2_semver_exempt)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 374 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 375 | Span::_new(self.inner.located_at(other.inner)) |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 376 | } |
| 377 | |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 378 | /// Convert `proc_macro2::Span` to `proc_macro::Span`. |
| 379 | /// |
| 380 | /// This method is available when building with a nightly compiler, or when |
| 381 | /// building with rustc 1.29+ *without* semver exempt features. |
David Tolnay | c0425cd | 2019-01-16 12:13:15 -0800 | [diff] [blame] | 382 | /// |
| 383 | /// # Panics |
| 384 | /// |
| 385 | /// Panics if called from outside of a procedural macro. Unlike |
| 386 | /// `proc_macro2::Span`, the `proc_macro::Span` type can only exist within |
| 387 | /// the context of a procedural macro invocation. |
David Tolnay | 17eb070 | 2019-01-05 12:23:17 -0800 | [diff] [blame] | 388 | #[cfg(wrap_proc_macro)] |
David Tolnay | 40bbb1c | 2019-01-19 19:43:55 -0800 | [diff] [blame] | 389 | pub fn unwrap(self) -> proc_macro::Span { |
| 390 | self.inner.unwrap() |
| 391 | } |
| 392 | |
| 393 | // Soft deprecated. Please use Span::unwrap. |
| 394 | #[cfg(wrap_proc_macro)] |
| 395 | #[doc(hidden)] |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 396 | pub fn unstable(self) -> proc_macro::Span { |
David Tolnay | 40bbb1c | 2019-01-19 19:43:55 -0800 | [diff] [blame] | 397 | self.unwrap() |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 398 | } |
| 399 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 400 | /// The original source file into which this span points. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 401 | /// |
| 402 | /// This method is semver exempt and not exposed by default. |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 403 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 404 | pub fn source_file(&self) -> SourceFile { |
David Tolnay | 7e654a8 | 2018-11-11 13:33:18 -0800 | [diff] [blame] | 405 | SourceFile::_new(self.inner.source_file()) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 406 | } |
| 407 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 408 | /// Get the starting line/column in the source file for this span. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 409 | /// |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 410 | /// This method requires the `"span-locations"` feature to be enabled. |
| 411 | #[cfg(span_locations)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 412 | pub fn start(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 413 | let imp::LineColumn { line, column } = self.inner.start(); |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 414 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 415 | } |
| 416 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 417 | /// Get the ending line/column in the source file for this span. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 418 | /// |
David Tolnay | 3b1f7d2 | 2019-01-28 12:22:11 -0800 | [diff] [blame] | 419 | /// This method requires the `"span-locations"` feature to be enabled. |
| 420 | #[cfg(span_locations)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 421 | pub fn end(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 422 | let imp::LineColumn { line, column } = self.inner.end(); |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 423 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 424 | } |
| 425 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 426 | /// Create a new span encompassing `self` and `other`. |
| 427 | /// |
| 428 | /// Returns `None` if `self` and `other` are from different files. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 429 | /// |
| 430 | /// This method is semver exempt and not exposed by default. |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 431 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 432 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 433 | self.inner.join(other.inner).map(Span::_new) |
| 434 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 435 | |
David Tolnay | 0360cf8 | 2019-06-30 13:09:49 -0700 | [diff] [blame] | 436 | /// Compares two spans to see if they're equal. |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 437 | /// |
| 438 | /// This method is semver exempt and not exposed by default. |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 439 | #[cfg(procmacro2_semver_exempt)] |
| 440 | pub fn eq(&self, other: &Span) -> bool { |
| 441 | self.inner.eq(&other.inner) |
| 442 | } |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 443 | } |
| 444 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 445 | /// Prints a span in a form convenient for debugging. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 446 | impl fmt::Debug for Span { |
| 447 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 448 | self.inner.fmt(f) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 449 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 450 | } |
| 451 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 452 | /// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`). |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 453 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 454 | pub enum TokenTree { |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 455 | /// A token stream surrounded by bracket delimiters. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 456 | Group(Group), |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 457 | /// An identifier. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 458 | Ident(Ident), |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 459 | /// A single punctuation character (`+`, `,`, `$`, etc.). |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 460 | Punct(Punct), |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 461 | /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 462 | Literal(Literal), |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 465 | impl TokenTree { |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 466 | /// Returns the span of this tree, delegating to the `span` method of |
| 467 | /// the contained token or a delimited stream. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 468 | pub fn span(&self) -> Span { |
| 469 | match *self { |
| 470 | TokenTree::Group(ref t) => t.span(), |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 471 | TokenTree::Ident(ref t) => t.span(), |
| 472 | TokenTree::Punct(ref t) => t.span(), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 473 | TokenTree::Literal(ref t) => t.span(), |
| 474 | } |
| 475 | } |
| 476 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 477 | /// Configures the span for *only this token*. |
| 478 | /// |
| 479 | /// Note that if this token is a `Group` then this method will not configure |
| 480 | /// the span of each of the internal tokens, this will simply delegate to |
| 481 | /// the `set_span` method of each variant. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 482 | pub fn set_span(&mut self, span: Span) { |
| 483 | match *self { |
| 484 | TokenTree::Group(ref mut t) => t.set_span(span), |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 485 | TokenTree::Ident(ref mut t) => t.set_span(span), |
| 486 | TokenTree::Punct(ref mut t) => t.set_span(span), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 487 | TokenTree::Literal(ref mut t) => t.set_span(span), |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | impl From<Group> for TokenTree { |
| 493 | fn from(g: Group) -> TokenTree { |
| 494 | TokenTree::Group(g) |
| 495 | } |
| 496 | } |
| 497 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 498 | impl From<Ident> for TokenTree { |
| 499 | fn from(g: Ident) -> TokenTree { |
| 500 | TokenTree::Ident(g) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 504 | impl From<Punct> for TokenTree { |
| 505 | fn from(g: Punct) -> TokenTree { |
| 506 | TokenTree::Punct(g) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | impl From<Literal> for TokenTree { |
| 511 | fn from(g: Literal) -> TokenTree { |
| 512 | TokenTree::Literal(g) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 513 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 514 | } |
| 515 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 516 | /// Prints the token tree as a string that is supposed to be losslessly |
| 517 | /// convertible back into the same token tree (modulo spans), except for |
| 518 | /// possibly `TokenTree::Group`s with `Delimiter::None` delimiters and negative |
| 519 | /// numeric literals. |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 520 | impl fmt::Display for TokenTree { |
| 521 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 522 | match *self { |
| 523 | TokenTree::Group(ref t) => t.fmt(f), |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 524 | TokenTree::Ident(ref t) => t.fmt(f), |
| 525 | TokenTree::Punct(ref t) => t.fmt(f), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 526 | TokenTree::Literal(ref t) => t.fmt(f), |
| 527 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 531 | /// Prints token tree in a form convenient for debugging. |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 532 | impl fmt::Debug for TokenTree { |
| 533 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 534 | // Each of these has the name in the struct type in the derived debug, |
| 535 | // so don't bother with an extra layer of indirection |
| 536 | match *self { |
| 537 | TokenTree::Group(ref t) => t.fmt(f), |
David Tolnay | d8fcdb8 | 2018-06-02 15:43:53 -0700 | [diff] [blame] | 538 | TokenTree::Ident(ref t) => { |
| 539 | let mut debug = f.debug_struct("Ident"); |
| 540 | debug.field("sym", &format_args!("{}", t)); |
David Tolnay | fd8cdc8 | 2019-01-19 19:23:59 -0800 | [diff] [blame] | 541 | imp::debug_span_field_if_nontrivial(&mut debug, t.span().inner); |
David Tolnay | d8fcdb8 | 2018-06-02 15:43:53 -0700 | [diff] [blame] | 542 | debug.finish() |
| 543 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 544 | TokenTree::Punct(ref t) => t.fmt(f), |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 545 | TokenTree::Literal(ref t) => t.fmt(f), |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 550 | /// A delimited token stream. |
| 551 | /// |
| 552 | /// A `Group` internally contains a `TokenStream` which is surrounded by |
| 553 | /// `Delimiter`s. |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 554 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 555 | pub struct Group { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 556 | inner: imp::Group, |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 557 | } |
| 558 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 559 | /// Describes how a sequence of token trees is delimited. |
Michael Layzell | 5372f4b | 2017-06-02 10:29:31 -0400 | [diff] [blame] | 560 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 561 | pub enum Delimiter { |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 562 | /// `( ... )` |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 563 | Parenthesis, |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 564 | /// `{ ... }` |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 565 | Brace, |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 566 | /// `[ ... ]` |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 567 | Bracket, |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 568 | /// `Ø ... Ø` |
| 569 | /// |
| 570 | /// An implicit delimiter, that may, for example, appear around tokens |
| 571 | /// coming from a "macro variable" `$var`. It is important to preserve |
| 572 | /// operator priorities in cases like `$var * 3` where `$var` is `1 + 2`. |
| 573 | /// Implicit delimiters may not survive roundtrip of a token stream through |
| 574 | /// a string. |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 575 | None, |
| 576 | } |
| 577 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 578 | impl Group { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 579 | fn _new(inner: imp::Group) -> Self { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 580 | Group { inner } |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 581 | } |
| 582 | |
David Tolnay | aef075b | 2019-01-16 16:29:18 -0800 | [diff] [blame] | 583 | fn _new_stable(inner: fallback::Group) -> Self { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 584 | Group { |
| 585 | inner: inner.into(), |
| 586 | } |
| 587 | } |
| 588 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 589 | /// Creates a new `Group` with the given delimiter and token stream. |
| 590 | /// |
| 591 | /// This constructor will set the span for this group to |
| 592 | /// `Span::call_site()`. To change the span you can use the `set_span` |
| 593 | /// method below. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 594 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { |
| 595 | Group { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 596 | inner: imp::Group::new(delimiter, stream.inner), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 597 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 598 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 599 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 600 | /// Returns the delimiter of this `Group` |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 601 | pub fn delimiter(&self) -> Delimiter { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 602 | self.inner.delimiter() |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 603 | } |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 604 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 605 | /// Returns the `TokenStream` of tokens that are delimited in this `Group`. |
| 606 | /// |
| 607 | /// Note that the returned token stream does not include the delimiter |
| 608 | /// returned above. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 609 | pub fn stream(&self) -> TokenStream { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 610 | TokenStream::_new(self.inner.stream()) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 611 | } |
| 612 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 613 | /// Returns the span for the delimiters of this token stream, spanning the |
| 614 | /// entire `Group`. |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 615 | /// |
| 616 | /// ```text |
| 617 | /// pub fn span(&self) -> Span { |
| 618 | /// ^^^^^^^ |
| 619 | /// ``` |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 620 | pub fn span(&self) -> Span { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 621 | Span::_new(self.inner.span()) |
| 622 | } |
| 623 | |
| 624 | /// Returns the span pointing to the opening delimiter of this group. |
| 625 | /// |
| 626 | /// ```text |
| 627 | /// pub fn span_open(&self) -> Span { |
| 628 | /// ^ |
| 629 | /// ``` |
| 630 | #[cfg(procmacro2_semver_exempt)] |
| 631 | pub fn span_open(&self) -> Span { |
| 632 | Span::_new(self.inner.span_open()) |
| 633 | } |
| 634 | |
| 635 | /// Returns the span pointing to the closing delimiter of this group. |
| 636 | /// |
| 637 | /// ```text |
| 638 | /// pub fn span_close(&self) -> Span { |
| 639 | /// ^ |
| 640 | /// ``` |
| 641 | #[cfg(procmacro2_semver_exempt)] |
| 642 | pub fn span_close(&self) -> Span { |
| 643 | Span::_new(self.inner.span_close()) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 644 | } |
| 645 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 646 | /// Configures the span for this `Group`'s delimiters, but not its internal |
| 647 | /// tokens. |
| 648 | /// |
| 649 | /// This method will **not** set the span of all the internal tokens spanned |
| 650 | /// by this group, but rather it will only set the span of the delimiter |
| 651 | /// tokens at the level of the `Group`. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 652 | pub fn set_span(&mut self, span: Span) { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 653 | self.inner.set_span(span.inner) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 657 | /// Prints the group as a string that should be losslessly convertible back |
| 658 | /// into the same group (modulo spans), except for possibly `TokenTree::Group`s |
| 659 | /// with `Delimiter::None` delimiters. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 660 | impl fmt::Display for Group { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 661 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 662 | fmt::Display::fmt(&self.inner, formatter) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 666 | impl fmt::Debug for Group { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 667 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 668 | fmt::Debug::fmt(&self.inner, formatter) |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 672 | /// An `Punct` is an single punctuation character like `+`, `-` or `#`. |
| 673 | /// |
| 674 | /// Multicharacter operators like `+=` are represented as two instances of |
| 675 | /// `Punct` with different forms of `Spacing` returned. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 676 | #[derive(Clone)] |
| 677 | pub struct Punct { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 678 | op: char, |
| 679 | spacing: Spacing, |
| 680 | span: Span, |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 681 | } |
| 682 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 683 | /// Whether an `Punct` is followed immediately by another `Punct` or followed by |
| 684 | /// another token or whitespace. |
Lukas Kalbertodt | eb3f930 | 2017-08-20 18:58:41 +0200 | [diff] [blame] | 685 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 686 | pub enum Spacing { |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 687 | /// E.g. `+` is `Alone` in `+ =`, `+ident` or `+()`. |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 688 | Alone, |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 689 | /// E.g. `+` is `Joint` in `+=` or `'#`. |
| 690 | /// |
| 691 | /// Additionally, single quote `'` can join with identifiers to form |
| 692 | /// lifetimes `'ident`. |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 693 | Joint, |
| 694 | } |
| 695 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 696 | impl Punct { |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 697 | /// Creates a new `Punct` from the given character and spacing. |
| 698 | /// |
| 699 | /// The `ch` argument must be a valid punctuation character permitted by the |
| 700 | /// language, otherwise the function will panic. |
| 701 | /// |
| 702 | /// The returned `Punct` will have the default span of `Span::call_site()` |
| 703 | /// which can be further configured with the `set_span` method below. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 704 | pub fn new(op: char, spacing: Spacing) -> Punct { |
| 705 | Punct { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 706 | op, |
| 707 | spacing, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 708 | span: Span::call_site(), |
| 709 | } |
| 710 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 711 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 712 | /// Returns the value of this punctuation character as `char`. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 713 | pub fn as_char(&self) -> char { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 714 | self.op |
| 715 | } |
| 716 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 717 | /// Returns the spacing of this punctuation character, indicating whether |
| 718 | /// it's immediately followed by another `Punct` in the token stream, so |
| 719 | /// they can potentially be combined into a multicharacter operator |
| 720 | /// (`Joint`), or it's followed by some other token or whitespace (`Alone`) |
| 721 | /// so the operator has certainly ended. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 722 | pub fn spacing(&self) -> Spacing { |
| 723 | self.spacing |
| 724 | } |
| 725 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 726 | /// Returns the span for this punctuation character. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 727 | pub fn span(&self) -> Span { |
| 728 | self.span |
| 729 | } |
| 730 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 731 | /// Configure the span for this punctuation character. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 732 | pub fn set_span(&mut self, span: Span) { |
| 733 | self.span = span; |
| 734 | } |
| 735 | } |
| 736 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 737 | /// Prints the punctuation character as a string that should be losslessly |
| 738 | /// convertible back into the same character. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 739 | impl fmt::Display for Punct { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 740 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 741 | self.op.fmt(f) |
| 742 | } |
| 743 | } |
| 744 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 745 | impl fmt::Debug for Punct { |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 746 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 747 | let mut debug = fmt.debug_struct("Punct"); |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 748 | debug.field("op", &self.op); |
| 749 | debug.field("spacing", &self.spacing); |
David Tolnay | fd8cdc8 | 2019-01-19 19:23:59 -0800 | [diff] [blame] | 750 | imp::debug_span_field_if_nontrivial(&mut debug, self.span.inner); |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 751 | debug.finish() |
| 752 | } |
| 753 | } |
| 754 | |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 755 | /// A word of Rust code, which may be a keyword or legal variable name. |
| 756 | /// |
| 757 | /// An identifier consists of at least one Unicode code point, the first of |
| 758 | /// which has the XID_Start property and the rest of which have the XID_Continue |
| 759 | /// property. |
| 760 | /// |
| 761 | /// - The empty string is not an identifier. Use `Option<Ident>`. |
| 762 | /// - A lifetime is not an identifier. Use `syn::Lifetime` instead. |
| 763 | /// |
| 764 | /// An identifier constructed with `Ident::new` is permitted to be a Rust |
David Tolnay | c239f03 | 2018-11-11 12:57:09 -0800 | [diff] [blame] | 765 | /// keyword, though parsing one through its [`Parse`] implementation rejects |
| 766 | /// Rust keywords. Use `input.call(Ident::parse_any)` when parsing to match the |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 767 | /// behaviour of `Ident::new`. |
| 768 | /// |
David Tolnay | c239f03 | 2018-11-11 12:57:09 -0800 | [diff] [blame] | 769 | /// [`Parse`]: https://docs.rs/syn/0.15/syn/parse/trait.Parse.html |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 770 | /// |
| 771 | /// # Examples |
| 772 | /// |
| 773 | /// A new ident can be created from a string using the `Ident::new` function. |
| 774 | /// A span must be provided explicitly which governs the name resolution |
| 775 | /// behavior of the resulting identifier. |
| 776 | /// |
David Tolnay | 7a96473 | 2019-01-19 19:03:20 -0800 | [diff] [blame] | 777 | /// ```edition2018 |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 778 | /// use proc_macro2::{Ident, Span}; |
| 779 | /// |
| 780 | /// fn main() { |
| 781 | /// let call_ident = Ident::new("calligraphy", Span::call_site()); |
| 782 | /// |
| 783 | /// println!("{}", call_ident); |
| 784 | /// } |
| 785 | /// ``` |
| 786 | /// |
| 787 | /// An ident can be interpolated into a token stream using the `quote!` macro. |
| 788 | /// |
David Tolnay | 7a96473 | 2019-01-19 19:03:20 -0800 | [diff] [blame] | 789 | /// ```edition2018 |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 790 | /// use proc_macro2::{Ident, Span}; |
David Tolnay | 7a96473 | 2019-01-19 19:03:20 -0800 | [diff] [blame] | 791 | /// use quote::quote; |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 792 | /// |
| 793 | /// fn main() { |
| 794 | /// let ident = Ident::new("demo", Span::call_site()); |
| 795 | /// |
| 796 | /// // Create a variable binding whose name is this ident. |
| 797 | /// let expanded = quote! { let #ident = 10; }; |
| 798 | /// |
| 799 | /// // Create a variable binding with a slightly different name. |
| 800 | /// let temp_ident = Ident::new(&format!("new_{}", ident), Span::call_site()); |
| 801 | /// let expanded = quote! { let #temp_ident = 10; }; |
| 802 | /// } |
| 803 | /// ``` |
| 804 | /// |
| 805 | /// A string representation of the ident is available through the `to_string()` |
| 806 | /// method. |
| 807 | /// |
David Tolnay | 7a96473 | 2019-01-19 19:03:20 -0800 | [diff] [blame] | 808 | /// ```edition2018 |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 809 | /// # use proc_macro2::{Ident, Span}; |
| 810 | /// # |
| 811 | /// # let ident = Ident::new("another_identifier", Span::call_site()); |
| 812 | /// # |
| 813 | /// // Examine the ident as a string. |
| 814 | /// let ident_string = ident.to_string(); |
| 815 | /// if ident_string.len() > 60 { |
| 816 | /// println!("Very long identifier: {}", ident_string) |
| 817 | /// } |
| 818 | /// ``` |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 819 | #[derive(Clone)] |
| 820 | pub struct Ident { |
| 821 | inner: imp::Ident, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 822 | _marker: marker::PhantomData<Rc<()>>, |
| 823 | } |
| 824 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 825 | impl Ident { |
| 826 | fn _new(inner: imp::Ident) -> Ident { |
| 827 | Ident { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 828 | inner, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 829 | _marker: marker::PhantomData, |
| 830 | } |
| 831 | } |
| 832 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 833 | /// Creates a new `Ident` with the given `string` as well as the specified |
| 834 | /// `span`. |
| 835 | /// |
| 836 | /// The `string` argument must be a valid identifier permitted by the |
| 837 | /// language, otherwise the function will panic. |
| 838 | /// |
| 839 | /// Note that `span`, currently in rustc, configures the hygiene information |
| 840 | /// for this identifier. |
| 841 | /// |
| 842 | /// As of this time `Span::call_site()` explicitly opts-in to "call-site" |
| 843 | /// hygiene meaning that identifiers created with this span will be resolved |
| 844 | /// as if they were written directly at the location of the macro call, and |
| 845 | /// other code at the macro call site will be able to refer to them as well. |
| 846 | /// |
| 847 | /// Later spans like `Span::def_site()` will allow to opt-in to |
| 848 | /// "definition-site" hygiene meaning that identifiers created with this |
| 849 | /// span will be resolved at the location of the macro definition and other |
| 850 | /// code at the macro call site will not be able to refer to them. |
| 851 | /// |
| 852 | /// Due to the current importance of hygiene this constructor, unlike other |
| 853 | /// tokens, requires a `Span` to be specified at construction. |
David Tolnay | 8b71dac | 2018-05-20 17:07:47 -0700 | [diff] [blame] | 854 | /// |
| 855 | /// # Panics |
| 856 | /// |
| 857 | /// Panics if the input string is neither a keyword nor a legal variable |
David Tolnay | 219b1d3 | 2019-04-22 16:04:11 -0700 | [diff] [blame] | 858 | /// name. If you are not sure whether the string contains an identifier and |
| 859 | /// need to handle an error case, use |
| 860 | /// <a href="https://docs.rs/syn/0.15/syn/fn.parse_str.html"><code |
| 861 | /// style="padding-right:0;">syn::parse_str</code></a><code |
| 862 | /// style="padding-left:0;">::<Ident></code> |
| 863 | /// rather than `Ident::new`. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 864 | pub fn new(string: &str, span: Span) -> Ident { |
| 865 | Ident::_new(imp::Ident::new(string, span.inner)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 866 | } |
| 867 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 868 | /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). |
David Tolnay | a01ca8e | 2018-06-04 00:55:28 -0700 | [diff] [blame] | 869 | /// |
| 870 | /// This method is semver exempt and not exposed by default. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 871 | #[cfg(procmacro2_semver_exempt)] |
| 872 | pub fn new_raw(string: &str, span: Span) -> Ident { |
| 873 | Ident::_new_raw(string, span) |
| 874 | } |
| 875 | |
| 876 | fn _new_raw(string: &str, span: Span) -> Ident { |
| 877 | Ident::_new(imp::Ident::new_raw(string, span.inner)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 878 | } |
| 879 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 880 | /// Returns the span of this `Ident`. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 881 | pub fn span(&self) -> Span { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 882 | Span::_new(self.inner.span()) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 883 | } |
| 884 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 885 | /// Configures the span of this `Ident`, possibly changing its hygiene |
| 886 | /// context. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 887 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 888 | self.inner.set_span(span.inner); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 892 | impl PartialEq for Ident { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 893 | fn eq(&self, other: &Ident) -> bool { |
David Tolnay | c0b0f2e | 2018-09-02 17:56:08 -0700 | [diff] [blame] | 894 | self.inner == other.inner |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
David Tolnay | c0bbcc5 | 2018-05-18 10:51:04 -0700 | [diff] [blame] | 898 | impl<T> PartialEq<T> for Ident |
| 899 | where |
| 900 | T: ?Sized + AsRef<str>, |
| 901 | { |
| 902 | fn eq(&self, other: &T) -> bool { |
David Tolnay | c0b0f2e | 2018-09-02 17:56:08 -0700 | [diff] [blame] | 903 | self.inner == other |
David Tolnay | c0bbcc5 | 2018-05-18 10:51:04 -0700 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 907 | impl Eq for Ident {} |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 908 | |
| 909 | impl PartialOrd for Ident { |
| 910 | fn partial_cmp(&self, other: &Ident) -> Option<Ordering> { |
| 911 | Some(self.cmp(other)) |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | impl Ord for Ident { |
| 916 | fn cmp(&self, other: &Ident) -> Ordering { |
| 917 | self.to_string().cmp(&other.to_string()) |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | impl Hash for Ident { |
| 922 | fn hash<H: Hasher>(&self, hasher: &mut H) { |
| 923 | self.to_string().hash(hasher) |
| 924 | } |
| 925 | } |
| 926 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 927 | /// Prints the identifier as a string that should be losslessly convertible back |
| 928 | /// into the same identifier. |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 929 | impl fmt::Display for Ident { |
| 930 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 931 | self.inner.fmt(f) |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | impl fmt::Debug for Ident { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 936 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 937 | self.inner.fmt(f) |
| 938 | } |
| 939 | } |
| 940 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 941 | /// A literal string (`"hello"`), byte string (`b"hello"`), character (`'a'`), |
| 942 | /// byte character (`b'a'`), an integer or floating point number with or without |
| 943 | /// a suffix (`1`, `1u8`, `2.3`, `2.3f32`). |
| 944 | /// |
| 945 | /// Boolean literals like `true` and `false` do not belong here, they are |
| 946 | /// `Ident`s. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 947 | #[derive(Clone)] |
| 948 | pub struct Literal { |
| 949 | inner: imp::Literal, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 950 | _marker: marker::PhantomData<Rc<()>>, |
| 951 | } |
| 952 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 953 | macro_rules! suffixed_int_literals { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 954 | ($($name:ident => $kind:ident,)*) => ($( |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 955 | /// Creates a new suffixed integer literal with the specified value. |
| 956 | /// |
| 957 | /// This function will create an integer like `1u32` where the integer |
| 958 | /// value specified is the first part of the token and the integral is |
| 959 | /// also suffixed at the end. Literals created from negative numbers may |
| 960 | /// not survive rountrips through `TokenStream` or strings and may be |
| 961 | /// broken into two tokens (`-` and positive literal). |
| 962 | /// |
| 963 | /// Literals created through this method have the `Span::call_site()` |
| 964 | /// span by default, which can be configured with the `set_span` method |
| 965 | /// below. |
| 966 | pub fn $name(n: $kind) -> Literal { |
| 967 | Literal::_new(imp::Literal::$name(n)) |
| 968 | } |
| 969 | )*) |
| 970 | } |
| 971 | |
| 972 | macro_rules! unsuffixed_int_literals { |
| 973 | ($($name:ident => $kind:ident,)*) => ($( |
| 974 | /// Creates a new unsuffixed integer literal with the specified value. |
| 975 | /// |
| 976 | /// This function will create an integer like `1` where the integer |
| 977 | /// value specified is the first part of the token. No suffix is |
| 978 | /// specified on this token, meaning that invocations like |
| 979 | /// `Literal::i8_unsuffixed(1)` are equivalent to |
| 980 | /// `Literal::u32_unsuffixed(1)`. Literals created from negative numbers |
| 981 | /// may not survive rountrips through `TokenStream` or strings and may |
| 982 | /// be broken into two tokens (`-` and positive literal). |
| 983 | /// |
| 984 | /// Literals created through this method have the `Span::call_site()` |
| 985 | /// span by default, which can be configured with the `set_span` method |
| 986 | /// below. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 987 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 988 | Literal::_new(imp::Literal::$name(n)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 989 | } |
| 990 | )*) |
| 991 | } |
| 992 | |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 993 | impl Literal { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 994 | fn _new(inner: imp::Literal) -> Literal { |
| 995 | Literal { |
David Tolnay | f081412 | 2019-07-19 11:53:55 -0700 | [diff] [blame] | 996 | inner, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 997 | _marker: marker::PhantomData, |
| 998 | } |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
David Tolnay | aef075b | 2019-01-16 16:29:18 -0800 | [diff] [blame] | 1001 | fn _new_stable(inner: fallback::Literal) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 1002 | Literal { |
| 1003 | inner: inner.into(), |
| 1004 | _marker: marker::PhantomData, |
| 1005 | } |
| 1006 | } |
| 1007 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 1008 | suffixed_int_literals! { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1009 | u8_suffixed => u8, |
| 1010 | u16_suffixed => u16, |
| 1011 | u32_suffixed => u32, |
| 1012 | u64_suffixed => u64, |
David Tolnay | 1596a8c | 2019-07-19 11:45:26 -0700 | [diff] [blame] | 1013 | u128_suffixed => u128, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1014 | usize_suffixed => usize, |
| 1015 | i8_suffixed => i8, |
| 1016 | i16_suffixed => i16, |
| 1017 | i32_suffixed => i32, |
| 1018 | i64_suffixed => i64, |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 1019 | i128_suffixed => i128, |
David Tolnay | 1596a8c | 2019-07-19 11:45:26 -0700 | [diff] [blame] | 1020 | isize_suffixed => isize, |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 1021 | } |
| 1022 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 1023 | unsuffixed_int_literals! { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1024 | u8_unsuffixed => u8, |
| 1025 | u16_unsuffixed => u16, |
| 1026 | u32_unsuffixed => u32, |
| 1027 | u64_unsuffixed => u64, |
David Tolnay | 1596a8c | 2019-07-19 11:45:26 -0700 | [diff] [blame] | 1028 | u128_unsuffixed => u128, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1029 | usize_unsuffixed => usize, |
| 1030 | i8_unsuffixed => i8, |
| 1031 | i16_unsuffixed => i16, |
| 1032 | i32_unsuffixed => i32, |
| 1033 | i64_unsuffixed => i64, |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 1034 | i128_unsuffixed => i128, |
David Tolnay | 1596a8c | 2019-07-19 11:45:26 -0700 | [diff] [blame] | 1035 | isize_unsuffixed => isize, |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1038 | /// Creates a new unsuffixed floating-point literal. |
| 1039 | /// |
| 1040 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where |
| 1041 | /// the float's value is emitted directly into the token but no suffix is |
| 1042 | /// used, so it may be inferred to be a `f64` later in the compiler. |
| 1043 | /// Literals created from negative numbers may not survive rountrips through |
| 1044 | /// `TokenStream` or strings and may be broken into two tokens (`-` and |
| 1045 | /// positive literal). |
| 1046 | /// |
| 1047 | /// # Panics |
| 1048 | /// |
| 1049 | /// This function requires that the specified float is finite, for example |
| 1050 | /// if it is infinity or NaN this function will panic. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1051 | pub fn f64_unsuffixed(f: f64) -> Literal { |
| 1052 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1053 | Literal::_new(imp::Literal::f64_unsuffixed(f)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1056 | /// Creates a new suffixed floating-point literal. |
| 1057 | /// |
| 1058 | /// This constructor will create a literal like `1.0f64` where the value |
| 1059 | /// specified is the preceding part of the token and `f64` is the suffix of |
| 1060 | /// the token. This token will always be inferred to be an `f64` in the |
| 1061 | /// compiler. Literals created from negative numbers may not survive |
| 1062 | /// rountrips through `TokenStream` or strings and may be broken into two |
| 1063 | /// tokens (`-` and positive literal). |
| 1064 | /// |
| 1065 | /// # Panics |
| 1066 | /// |
| 1067 | /// This function requires that the specified float is finite, for example |
| 1068 | /// if it is infinity or NaN this function will panic. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1069 | pub fn f64_suffixed(f: f64) -> Literal { |
| 1070 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1071 | Literal::_new(imp::Literal::f64_suffixed(f)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 1074 | /// Creates a new unsuffixed floating-point literal. |
| 1075 | /// |
| 1076 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where |
| 1077 | /// the float's value is emitted directly into the token but no suffix is |
| 1078 | /// used, so it may be inferred to be a `f64` later in the compiler. |
| 1079 | /// Literals created from negative numbers may not survive rountrips through |
| 1080 | /// `TokenStream` or strings and may be broken into two tokens (`-` and |
| 1081 | /// positive literal). |
| 1082 | /// |
| 1083 | /// # Panics |
| 1084 | /// |
| 1085 | /// This function requires that the specified float is finite, for example |
| 1086 | /// if it is infinity or NaN this function will panic. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1087 | pub fn f32_unsuffixed(f: f32) -> Literal { |
| 1088 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1089 | Literal::_new(imp::Literal::f32_unsuffixed(f)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1092 | /// Creates a new suffixed floating-point literal. |
| 1093 | /// |
| 1094 | /// This constructor will create a literal like `1.0f32` where the value |
| 1095 | /// specified is the preceding part of the token and `f32` is the suffix of |
| 1096 | /// the token. This token will always be inferred to be an `f32` in the |
| 1097 | /// compiler. Literals created from negative numbers may not survive |
| 1098 | /// rountrips through `TokenStream` or strings and may be broken into two |
| 1099 | /// tokens (`-` and positive literal). |
| 1100 | /// |
| 1101 | /// # Panics |
| 1102 | /// |
| 1103 | /// This function requires that the specified float is finite, for example |
| 1104 | /// if it is infinity or NaN this function will panic. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1105 | pub fn f32_suffixed(f: f32) -> Literal { |
| 1106 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1107 | Literal::_new(imp::Literal::f32_suffixed(f)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1110 | /// String literal. |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 1111 | pub fn string(string: &str) -> Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1112 | Literal::_new(imp::Literal::string(string)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1115 | /// Character literal. |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 1116 | pub fn character(ch: char) -> Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 1117 | Literal::_new(imp::Literal::character(ch)) |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1120 | /// Byte string literal. |
Alex Crichton | 9c2fb0a | 2017-05-26 08:49:31 -0700 | [diff] [blame] | 1121 | pub fn byte_string(s: &[u8]) -> Literal { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1122 | Literal::_new(imp::Literal::byte_string(s)) |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 1123 | } |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 1124 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1125 | /// Returns the span encompassing this literal. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1126 | pub fn span(&self) -> Span { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 1127 | Span::_new(self.inner.span()) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
David Tolnay | bc3bd3f | 2019-06-30 13:03:56 -0700 | [diff] [blame] | 1130 | /// Configures the span associated for this literal. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1131 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 1132 | self.inner.set_span(span.inner); |
Alex Crichton | 3131662 | 2017-05-26 12:54:47 -0700 | [diff] [blame] | 1133 | } |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1136 | impl fmt::Debug for Literal { |
| 1137 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1138 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 1141 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1142 | impl fmt::Display for Literal { |
| 1143 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1144 | self.inner.fmt(f) |
| 1145 | } |
| 1146 | } |
| 1147 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 1148 | /// Public implementation details for the `TokenStream` type, such as iterators. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1149 | pub mod token_stream { |
| 1150 | use std::fmt; |
| 1151 | use std::marker; |
| 1152 | use std::rc::Rc; |
| 1153 | |
David Tolnay | 48ea504 | 2018-04-23 19:17:35 -0700 | [diff] [blame] | 1154 | use imp; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1155 | pub use TokenStream; |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 1156 | use TokenTree; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1157 | |
David Tolnay | 82ba02d | 2018-05-20 16:22:43 -0700 | [diff] [blame] | 1158 | /// An iterator over `TokenStream`'s `TokenTree`s. |
| 1159 | /// |
| 1160 | /// The iteration is "shallow", e.g. the iterator doesn't recurse into |
| 1161 | /// delimited groups, and returns whole groups as token trees. |
Isaac van Bakel | f6754d3 | 2019-05-08 20:21:06 +0100 | [diff] [blame] | 1162 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1163 | pub struct IntoIter { |
| 1164 | inner: imp::TokenTreeIter, |
| 1165 | _marker: marker::PhantomData<Rc<()>>, |
| 1166 | } |
| 1167 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1168 | impl Iterator for IntoIter { |
| 1169 | type Item = TokenTree; |
| 1170 | |
| 1171 | fn next(&mut self) -> Option<TokenTree> { |
| 1172 | self.inner.next() |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | impl fmt::Debug for IntoIter { |
| 1177 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1178 | self.inner.fmt(f) |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | impl IntoIterator for TokenStream { |
| 1183 | type Item = TokenTree; |
| 1184 | type IntoIter = IntoIter; |
| 1185 | |
| 1186 | fn into_iter(self) -> IntoIter { |
| 1187 | IntoIter { |
| 1188 | inner: self.inner.into_iter(), |
| 1189 | _marker: marker::PhantomData, |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | } |