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