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