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