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