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