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 | 2c1dac5 | 2018-04-23 19:18:07 -0700 | [diff] [blame] | 46 | #![doc(html_root_url = "https://docs.rs/proc-macro2/0.3.7")] |
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 | |
| 53 | use std::fmt; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 54 | use std::iter::FromIterator; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 55 | use std::marker; |
| 56 | use std::rc::Rc; |
| 57 | use std::str::FromStr; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 58 | |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 59 | #[macro_use] |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 60 | mod strnom; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 61 | mod stable; |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 62 | |
David Tolnay | d66ecf6 | 2018-01-02 20:05:42 -0800 | [diff] [blame] | 63 | #[cfg(not(feature = "nightly"))] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 64 | use stable as imp; |
Alex Crichton | b15c635 | 2017-05-19 19:36:36 -0700 | [diff] [blame] | 65 | #[path = "unstable.rs"] |
David Tolnay | d66ecf6 | 2018-01-02 20:05:42 -0800 | [diff] [blame] | 66 | #[cfg(feature = "nightly")] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 67 | mod imp; |
| 68 | |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 69 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 70 | pub struct TokenStream { |
| 71 | inner: imp::TokenStream, |
| 72 | _marker: marker::PhantomData<Rc<()>>, |
| 73 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 74 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 75 | pub struct LexError { |
| 76 | inner: imp::LexError, |
| 77 | _marker: marker::PhantomData<Rc<()>>, |
| 78 | } |
| 79 | |
| 80 | impl TokenStream { |
| 81 | fn _new(inner: imp::TokenStream) -> TokenStream { |
| 82 | TokenStream { |
| 83 | inner: inner, |
| 84 | _marker: marker::PhantomData, |
| 85 | } |
| 86 | } |
| 87 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 88 | fn _new_stable(inner: stable::TokenStream) -> TokenStream { |
| 89 | TokenStream { |
| 90 | inner: inner.into(), |
| 91 | _marker: marker::PhantomData, |
| 92 | } |
| 93 | } |
| 94 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 95 | pub fn empty() -> TokenStream { |
| 96 | TokenStream::_new(imp::TokenStream::empty()) |
| 97 | } |
| 98 | |
| 99 | pub fn is_empty(&self) -> bool { |
| 100 | self.inner.is_empty() |
| 101 | } |
| 102 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 103 | |
| 104 | impl FromStr for TokenStream { |
| 105 | type Err = LexError; |
| 106 | |
| 107 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 108 | let e = src.parse().map_err(|e| LexError { |
| 109 | inner: e, |
| 110 | _marker: marker::PhantomData, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 111 | })?; |
| 112 | Ok(TokenStream::_new(e)) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Alex Crichton | 0e8e7f4 | 2018-02-22 06:15:13 -0800 | [diff] [blame] | 116 | #[cfg(feature = "proc-macro")] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 117 | impl From<proc_macro::TokenStream> for TokenStream { |
| 118 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 119 | TokenStream::_new(inner.into()) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Alex Crichton | 0e8e7f4 | 2018-02-22 06:15:13 -0800 | [diff] [blame] | 123 | #[cfg(feature = "proc-macro")] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 124 | impl From<TokenStream> for proc_macro::TokenStream { |
| 125 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 126 | inner.inner.into() |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 130 | impl FromIterator<TokenTree> for TokenStream { |
| 131 | fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self { |
| 132 | TokenStream::_new(streams.into_iter().collect()) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 136 | impl fmt::Display for TokenStream { |
| 137 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 138 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 142 | impl fmt::Debug for TokenStream { |
| 143 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 144 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 148 | impl fmt::Debug for LexError { |
| 149 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 150 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 154 | // Returned by reference, so we can't easily wrap it. |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 155 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 156 | pub use imp::FileName; |
| 157 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 158 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 159 | #[derive(Clone, PartialEq, Eq)] |
| 160 | pub struct SourceFile(imp::SourceFile); |
| 161 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 162 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 163 | impl SourceFile { |
| 164 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 165 | pub fn path(&self) -> &FileName { |
| 166 | self.0.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | pub fn is_real(&self) -> bool { |
| 170 | self.0.is_real() |
| 171 | } |
| 172 | } |
| 173 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 174 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 175 | impl AsRef<FileName> for SourceFile { |
| 176 | fn as_ref(&self) -> &FileName { |
| 177 | self.0.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 181 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 182 | impl fmt::Debug for SourceFile { |
| 183 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 184 | self.0.fmt(f) |
| 185 | } |
| 186 | } |
| 187 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 188 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 189 | pub struct LineColumn { |
| 190 | pub line: usize, |
| 191 | pub column: usize, |
| 192 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 193 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 194 | #[derive(Copy, Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 195 | pub struct Span { |
| 196 | inner: imp::Span, |
| 197 | _marker: marker::PhantomData<Rc<()>>, |
| 198 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 199 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 200 | impl Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 201 | fn _new(inner: imp::Span) -> Span { |
| 202 | Span { |
| 203 | inner: inner, |
| 204 | _marker: marker::PhantomData, |
| 205 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 206 | } |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 207 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 208 | fn _new_stable(inner: stable::Span) -> Span { |
| 209 | Span { |
| 210 | inner: inner.into(), |
| 211 | _marker: marker::PhantomData, |
| 212 | } |
| 213 | } |
| 214 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 215 | pub fn call_site() -> Span { |
| 216 | Span::_new(imp::Span::call_site()) |
| 217 | } |
| 218 | |
| 219 | #[cfg(procmacro2_semver_exempt)] |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 220 | pub fn def_site() -> Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 221 | Span::_new(imp::Span::def_site()) |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 222 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 223 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 224 | /// Creates a new span with the same line/column information as `self` but |
| 225 | /// that resolves symbols as though it were at `other`. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 226 | #[cfg(procmacro2_semver_exempt)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 227 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 228 | Span::_new(self.inner.resolved_at(other.inner)) |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /// Creates a new span with the same name resolution behavior as `self` but |
| 232 | /// with the line/column information of `other`. |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 233 | #[cfg(procmacro2_semver_exempt)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 234 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 235 | Span::_new(self.inner.located_at(other.inner)) |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 236 | } |
| 237 | |
David Tolnay | d66ecf6 | 2018-01-02 20:05:42 -0800 | [diff] [blame] | 238 | /// This method is only available when the `"nightly"` feature is enabled. |
Alex Crichton | 0e8e7f4 | 2018-02-22 06:15:13 -0800 | [diff] [blame] | 239 | #[cfg(all(feature = "nightly", feature = "proc-macro"))] |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 240 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 241 | self.inner.unstable() |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 242 | } |
| 243 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 244 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 245 | pub fn source_file(&self) -> SourceFile { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 246 | SourceFile(self.inner.source_file()) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 247 | } |
| 248 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 249 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 250 | pub fn start(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 251 | let imp::LineColumn { line, column } = self.inner.start(); |
| 252 | LineColumn { |
| 253 | line: line, |
| 254 | column: column, |
| 255 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 256 | } |
| 257 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 258 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 259 | pub fn end(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 260 | let imp::LineColumn { line, column } = self.inner.end(); |
| 261 | LineColumn { |
| 262 | line: line, |
| 263 | column: column, |
| 264 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 265 | } |
| 266 | |
David Tolnay | 1ebe397 | 2018-01-02 20:14:20 -0800 | [diff] [blame] | 267 | #[cfg(procmacro2_semver_exempt)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 268 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 269 | self.inner.join(other.inner).map(Span::_new) |
| 270 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 271 | |
| 272 | #[cfg(procmacro2_semver_exempt)] |
| 273 | pub fn eq(&self, other: &Span) -> bool { |
| 274 | self.inner.eq(&other.inner) |
| 275 | } |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | impl fmt::Debug for Span { |
| 279 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 280 | self.inner.fmt(f) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 281 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 282 | } |
| 283 | |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 284 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 285 | pub enum TokenTree { |
| 286 | Group(Group), |
| 287 | Term(Term), |
| 288 | Op(Op), |
| 289 | Literal(Literal), |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 292 | impl TokenTree { |
| 293 | pub fn span(&self) -> Span { |
| 294 | match *self { |
| 295 | TokenTree::Group(ref t) => t.span(), |
| 296 | TokenTree::Term(ref t) => t.span(), |
| 297 | TokenTree::Op(ref t) => t.span(), |
| 298 | TokenTree::Literal(ref t) => t.span(), |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | pub fn set_span(&mut self, span: Span) { |
| 303 | match *self { |
| 304 | TokenTree::Group(ref mut t) => t.set_span(span), |
| 305 | TokenTree::Term(ref mut t) => t.set_span(span), |
| 306 | TokenTree::Op(ref mut t) => t.set_span(span), |
| 307 | TokenTree::Literal(ref mut t) => t.set_span(span), |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | impl From<Group> for TokenTree { |
| 313 | fn from(g: Group) -> TokenTree { |
| 314 | TokenTree::Group(g) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | impl From<Term> for TokenTree { |
| 319 | fn from(g: Term) -> TokenTree { |
| 320 | TokenTree::Term(g) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | impl From<Op> for TokenTree { |
| 325 | fn from(g: Op) -> TokenTree { |
| 326 | TokenTree::Op(g) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | impl From<Literal> for TokenTree { |
| 331 | fn from(g: Literal) -> TokenTree { |
| 332 | TokenTree::Literal(g) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 333 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 336 | impl fmt::Display for TokenTree { |
| 337 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 338 | match *self { |
| 339 | TokenTree::Group(ref t) => t.fmt(f), |
| 340 | TokenTree::Term(ref t) => t.fmt(f), |
| 341 | TokenTree::Op(ref t) => t.fmt(f), |
| 342 | TokenTree::Literal(ref t) => t.fmt(f), |
| 343 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 347 | impl fmt::Debug for TokenTree { |
| 348 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 349 | // Each of these has the name in the struct type in the derived debug, |
| 350 | // so don't bother with an extra layer of indirection |
| 351 | match *self { |
| 352 | TokenTree::Group(ref t) => t.fmt(f), |
| 353 | TokenTree::Term(ref t) => t.fmt(f), |
| 354 | TokenTree::Op(ref t) => t.fmt(f), |
| 355 | TokenTree::Literal(ref t) => t.fmt(f), |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | #[derive(Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 361 | pub struct Group { |
| 362 | delimiter: Delimiter, |
| 363 | stream: TokenStream, |
| 364 | span: Span, |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Michael Layzell | 5372f4b | 2017-06-02 10:29:31 -0400 | [diff] [blame] | 367 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 368 | pub enum Delimiter { |
| 369 | Parenthesis, |
| 370 | Brace, |
| 371 | Bracket, |
| 372 | None, |
| 373 | } |
| 374 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 375 | impl Group { |
| 376 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { |
| 377 | Group { |
| 378 | delimiter: delimiter, |
| 379 | stream: stream, |
| 380 | span: Span::call_site(), |
| 381 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 382 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 383 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 384 | pub fn delimiter(&self) -> Delimiter { |
| 385 | self.delimiter |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 386 | } |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 387 | |
| 388 | pub fn stream(&self) -> TokenStream { |
| 389 | self.stream.clone() |
| 390 | } |
| 391 | |
| 392 | pub fn span(&self) -> Span { |
| 393 | self.span |
| 394 | } |
| 395 | |
| 396 | pub fn set_span(&mut self, span: Span) { |
| 397 | self.span = span; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | impl fmt::Display for Group { |
| 402 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 403 | self.stream.fmt(f) |
| 404 | } |
| 405 | } |
| 406 | |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 407 | impl fmt::Debug for Group { |
| 408 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 409 | let mut debug = fmt.debug_struct("Group"); |
| 410 | debug.field("delimiter", &self.delimiter); |
| 411 | debug.field("stream", &self.stream); |
| 412 | #[cfg(procmacro2_semver_exempt)] |
| 413 | debug.field("span", &self.span); |
| 414 | debug.finish() |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | #[derive(Copy, Clone)] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 419 | pub struct Op { |
| 420 | op: char, |
| 421 | spacing: Spacing, |
| 422 | span: Span, |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Lukas Kalbertodt | eb3f930 | 2017-08-20 18:58:41 +0200 | [diff] [blame] | 425 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 426 | pub enum Spacing { |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 427 | Alone, |
| 428 | Joint, |
| 429 | } |
| 430 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 431 | impl Op { |
| 432 | pub fn new(op: char, spacing: Spacing) -> Op { |
| 433 | Op { |
| 434 | op: op, |
| 435 | spacing: spacing, |
| 436 | span: Span::call_site(), |
| 437 | } |
| 438 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 439 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 440 | pub fn op(&self) -> char { |
| 441 | self.op |
| 442 | } |
| 443 | |
| 444 | pub fn spacing(&self) -> Spacing { |
| 445 | self.spacing |
| 446 | } |
| 447 | |
| 448 | pub fn span(&self) -> Span { |
| 449 | self.span |
| 450 | } |
| 451 | |
| 452 | pub fn set_span(&mut self, span: Span) { |
| 453 | self.span = span; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | impl fmt::Display for Op { |
| 458 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 459 | self.op.fmt(f) |
| 460 | } |
| 461 | } |
| 462 | |
David Tolnay | 034205f | 2018-04-22 16:45:28 -0700 | [diff] [blame] | 463 | impl fmt::Debug for Op { |
| 464 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 465 | let mut debug = fmt.debug_struct("Op"); |
| 466 | debug.field("op", &self.op); |
| 467 | debug.field("spacing", &self.spacing); |
| 468 | #[cfg(procmacro2_semver_exempt)] |
| 469 | debug.field("span", &self.span); |
| 470 | debug.finish() |
| 471 | } |
| 472 | } |
| 473 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 474 | #[derive(Copy, Clone)] |
| 475 | pub struct Term { |
| 476 | inner: imp::Term, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 477 | _marker: marker::PhantomData<Rc<()>>, |
| 478 | } |
| 479 | |
| 480 | impl Term { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 481 | fn _new(inner: imp::Term) -> Term { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 482 | Term { |
| 483 | inner: inner, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 484 | _marker: marker::PhantomData, |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | pub fn new(string: &str, span: Span) -> Term { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 489 | Term::_new(imp::Term::new(string, span.inner)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | pub fn as_str(&self) -> &str { |
| 493 | self.inner.as_str() |
| 494 | } |
| 495 | |
| 496 | pub fn span(&self) -> Span { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 497 | Span::_new(self.inner.span()) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 501 | self.inner.set_span(span.inner); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | impl fmt::Display for Term { |
| 506 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 507 | self.as_str().fmt(f) |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | impl fmt::Debug for Term { |
| 512 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 513 | self.inner.fmt(f) |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | #[derive(Clone)] |
| 518 | pub struct Literal { |
| 519 | inner: imp::Literal, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 520 | _marker: marker::PhantomData<Rc<()>>, |
| 521 | } |
| 522 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 523 | macro_rules! int_literals { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 524 | ($($name:ident => $kind:ident,)*) => ($( |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 525 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 526 | Literal::_new(imp::Literal::$name(n)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 527 | } |
| 528 | )*) |
| 529 | } |
| 530 | |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 531 | impl Literal { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 532 | fn _new(inner: imp::Literal) -> Literal { |
| 533 | Literal { |
| 534 | inner: inner, |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 535 | _marker: marker::PhantomData, |
| 536 | } |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 539 | fn _new_stable(inner: stable::Literal) -> Literal { |
| 540 | Literal { |
| 541 | inner: inner.into(), |
| 542 | _marker: marker::PhantomData, |
| 543 | } |
| 544 | } |
| 545 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 546 | int_literals! { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 547 | u8_suffixed => u8, |
| 548 | u16_suffixed => u16, |
| 549 | u32_suffixed => u32, |
| 550 | u64_suffixed => u64, |
| 551 | usize_suffixed => usize, |
| 552 | i8_suffixed => i8, |
| 553 | i16_suffixed => i16, |
| 554 | i32_suffixed => i32, |
| 555 | i64_suffixed => i64, |
| 556 | isize_suffixed => isize, |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 557 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 558 | u8_unsuffixed => u8, |
| 559 | u16_unsuffixed => u16, |
| 560 | u32_unsuffixed => u32, |
| 561 | u64_unsuffixed => u64, |
| 562 | usize_unsuffixed => usize, |
| 563 | i8_unsuffixed => i8, |
| 564 | i16_unsuffixed => i16, |
| 565 | i32_unsuffixed => i32, |
| 566 | i64_unsuffixed => i64, |
| 567 | isize_unsuffixed => isize, |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 570 | pub fn f64_unsuffixed(f: f64) -> Literal { |
| 571 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 572 | Literal::_new(imp::Literal::f64_unsuffixed(f)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 575 | pub fn f64_suffixed(f: f64) -> Literal { |
| 576 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 577 | Literal::_new(imp::Literal::f64_suffixed(f)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | pub fn f32_unsuffixed(f: f32) -> Literal { |
| 581 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 582 | Literal::_new(imp::Literal::f32_unsuffixed(f)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | pub fn f32_suffixed(f: f32) -> Literal { |
| 586 | assert!(f.is_finite()); |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 587 | Literal::_new(imp::Literal::f32_suffixed(f)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | pub fn string(string: &str) -> Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 591 | Literal::_new(imp::Literal::string(string)) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | pub fn character(ch: char) -> Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 595 | Literal::_new(imp::Literal::character(ch)) |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Alex Crichton | 9c2fb0a | 2017-05-26 08:49:31 -0700 | [diff] [blame] | 598 | pub fn byte_string(s: &[u8]) -> Literal { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 599 | Literal::_new(imp::Literal::byte_string(s)) |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 600 | } |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 601 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 602 | pub fn span(&self) -> Span { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 603 | Span::_new(self.inner.span()) |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 606 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 607 | self.inner.set_span(span.inner); |
Alex Crichton | 3131662 | 2017-05-26 12:54:47 -0700 | [diff] [blame] | 608 | } |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 611 | impl fmt::Debug for Literal { |
| 612 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 613 | self.inner.fmt(f) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 616 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 617 | impl fmt::Display for Literal { |
| 618 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 619 | self.inner.fmt(f) |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | pub mod token_stream { |
| 624 | use std::fmt; |
| 625 | use std::marker; |
| 626 | use std::rc::Rc; |
| 627 | |
David Tolnay | 48ea504 | 2018-04-23 19:17:35 -0700 | [diff] [blame] | 628 | use imp; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 629 | pub use TokenStream; |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 630 | use TokenTree; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 631 | |
| 632 | pub struct IntoIter { |
| 633 | inner: imp::TokenTreeIter, |
| 634 | _marker: marker::PhantomData<Rc<()>>, |
| 635 | } |
| 636 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 637 | impl Iterator for IntoIter { |
| 638 | type Item = TokenTree; |
| 639 | |
| 640 | fn next(&mut self) -> Option<TokenTree> { |
| 641 | self.inner.next() |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | impl fmt::Debug for IntoIter { |
| 646 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 647 | self.inner.fmt(f) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | impl IntoIterator for TokenStream { |
| 652 | type Item = TokenTree; |
| 653 | type IntoIter = IntoIter; |
| 654 | |
| 655 | fn into_iter(self) -> IntoIter { |
| 656 | IntoIter { |
| 657 | inner: self.inner.into_iter(), |
| 658 | _marker: marker::PhantomData, |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |