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