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