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