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