Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 1 | #![cfg_attr(not(super_unstable), allow(dead_code))] |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 2 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 3 | use std::fmt; |
| 4 | use std::iter; |
David Tolnay | 78ef773 | 2018-09-01 19:20:23 -0700 | [diff] [blame] | 5 | use std::panic::{self, PanicInfo}; |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 6 | #[cfg(super_unstable)] |
| 7 | use std::path::PathBuf; |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 8 | use std::str::FromStr; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 9 | |
| 10 | use proc_macro; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 11 | use stable; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 12 | |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 13 | use {Delimiter, Punct, Spacing, TokenTree}; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 14 | |
| 15 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 16 | pub enum TokenStream { |
| 17 | Nightly(proc_macro::TokenStream), |
| 18 | Stable(stable::TokenStream), |
| 19 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 20 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 21 | pub enum LexError { |
| 22 | Nightly(proc_macro::LexError), |
| 23 | Stable(stable::LexError), |
| 24 | } |
| 25 | |
| 26 | fn nightly_works() -> bool { |
| 27 | use std::sync::atomic::*; |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 28 | use std::sync::Once; |
| 29 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 30 | static WORKS: AtomicUsize = ATOMIC_USIZE_INIT; |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 31 | static INIT: Once = Once::new(); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 32 | |
| 33 | match WORKS.load(Ordering::SeqCst) { |
| 34 | 1 => return false, |
| 35 | 2 => return true, |
| 36 | _ => {} |
| 37 | } |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 38 | |
| 39 | // Swap in a null panic hook to avoid printing "thread panicked" to stderr, |
| 40 | // then use catch_unwind to determine whether the compiler's proc_macro is |
| 41 | // working. When proc-macro2 is used from outside of a procedural macro all |
| 42 | // of the proc_macro crate's APIs currently panic. |
| 43 | // |
| 44 | // The Once is to prevent the possibility of this ordering: |
| 45 | // |
| 46 | // thread 1 calls take_hook, gets the user's original hook |
| 47 | // thread 1 calls set_hook with the null hook |
| 48 | // thread 2 calls take_hook, thinks null hook is the original hook |
| 49 | // thread 2 calls set_hook with the null hook |
| 50 | // thread 1 calls set_hook with the actual original hook |
| 51 | // thread 2 calls set_hook with what it thinks is the original hook |
| 52 | // |
| 53 | // in which the user's hook has been lost. |
| 54 | // |
| 55 | // There is still a race condition where a panic in a different thread can |
| 56 | // happen during the interval that the user's original panic hook is |
| 57 | // unregistered such that their hook is incorrectly not called. This is |
| 58 | // sufficiently unlikely and less bad than printing panic messages to stderr |
| 59 | // on correct use of this crate. Maybe there is a libstd feature request |
| 60 | // here. For now, if a user needs to guarantee that this failure mode does |
| 61 | // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from |
| 62 | // the main thread before launching any other threads. |
| 63 | INIT.call_once(|| { |
David Tolnay | 78ef773 | 2018-09-01 19:20:23 -0700 | [diff] [blame] | 64 | type PanicHook = Fn(&PanicInfo) + Sync + Send + 'static; |
| 65 | |
| 66 | let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ }); |
| 67 | let sanity_check = &*null_hook as *const PanicHook; |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 68 | let original_hook = panic::take_hook(); |
David Tolnay | 78ef773 | 2018-09-01 19:20:23 -0700 | [diff] [blame] | 69 | panic::set_hook(null_hook); |
| 70 | |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 71 | let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok(); |
| 72 | WORKS.store(works as usize + 1, Ordering::SeqCst); |
David Tolnay | 78ef773 | 2018-09-01 19:20:23 -0700 | [diff] [blame] | 73 | |
| 74 | let hopefully_null_hook = panic::take_hook(); |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 75 | panic::set_hook(original_hook); |
David Tolnay | 78ef773 | 2018-09-01 19:20:23 -0700 | [diff] [blame] | 76 | if sanity_check != &*hopefully_null_hook { |
| 77 | panic!("observed race condition in proc_macro2::nightly_works"); |
| 78 | } |
David Tolnay | 2ed15d5 | 2018-09-01 08:46:12 -0700 | [diff] [blame] | 79 | }); |
| 80 | nightly_works() |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | fn mismatch() -> ! { |
| 84 | panic!("stable/nightly mismatch") |
| 85 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 86 | |
| 87 | impl TokenStream { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 88 | pub fn new() -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 89 | if nightly_works() { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 90 | TokenStream::Nightly(proc_macro::TokenStream::new()) |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 91 | } else { |
David Tolnay | c3bb459 | 2018-05-28 20:09:44 -0700 | [diff] [blame] | 92 | TokenStream::Stable(stable::TokenStream::new()) |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 93 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | pub fn is_empty(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 97 | match self { |
| 98 | TokenStream::Nightly(tts) => tts.is_empty(), |
| 99 | TokenStream::Stable(tts) => tts.is_empty(), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn unwrap_nightly(self) -> proc_macro::TokenStream { |
| 104 | match self { |
| 105 | TokenStream::Nightly(s) => s, |
| 106 | TokenStream::Stable(_) => mismatch(), |
| 107 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 108 | } |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 109 | |
| 110 | fn unwrap_stable(self) -> stable::TokenStream { |
| 111 | match self { |
| 112 | TokenStream::Nightly(_) => mismatch(), |
| 113 | TokenStream::Stable(s) => s, |
| 114 | } |
| 115 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | impl FromStr for TokenStream { |
| 119 | type Err = LexError; |
| 120 | |
| 121 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 122 | if nightly_works() { |
| 123 | Ok(TokenStream::Nightly(src.parse()?)) |
| 124 | } else { |
| 125 | Ok(TokenStream::Stable(src.parse()?)) |
| 126 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | impl fmt::Display for TokenStream { |
| 131 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 132 | match self { |
| 133 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 134 | TokenStream::Stable(tts) => tts.fmt(f), |
| 135 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | impl From<proc_macro::TokenStream> for TokenStream { |
| 140 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 141 | TokenStream::Nightly(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | impl From<TokenStream> for proc_macro::TokenStream { |
| 146 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 147 | match inner { |
| 148 | TokenStream::Nightly(inner) => inner, |
| 149 | TokenStream::Stable(inner) => inner.to_string().parse().unwrap(), |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl From<stable::TokenStream> for TokenStream { |
| 155 | fn from(inner: stable::TokenStream) -> TokenStream { |
| 156 | TokenStream::Stable(inner) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 160 | impl From<TokenTree> for TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 161 | fn from(token: TokenTree) -> TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 162 | if !nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 163 | return TokenStream::Stable(token.into()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 164 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 165 | let tt: proc_macro::TokenTree = match token { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 166 | TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 167 | TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 168 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 169 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 170 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 171 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 172 | let mut op = proc_macro::Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 173 | op.set_span(tt.span().inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 174 | op.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 175 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 176 | TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 177 | TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 178 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 179 | TokenStream::Nightly(tt.into()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 183 | impl iter::FromIterator<TokenTree> for TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 184 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { |
| 185 | if nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 186 | let trees = trees |
| 187 | .into_iter() |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 188 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 189 | .flat_map(|t| match t { |
| 190 | TokenStream::Nightly(s) => s, |
| 191 | TokenStream::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 192 | }); |
| 193 | TokenStream::Nightly(trees.collect()) |
| 194 | } else { |
| 195 | TokenStream::Stable(trees.into_iter().collect()) |
| 196 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Alex Crichton | 53b0067 | 2018-09-06 17:16:10 -0700 | [diff] [blame] | 200 | impl iter::FromIterator<TokenStream> for TokenStream { |
| 201 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { |
| 202 | let mut streams = streams.into_iter(); |
| 203 | match streams.next() { |
| 204 | #[cfg(slow_extend)] |
| 205 | Some(TokenStream::Nightly(first)) => { |
| 206 | let stream = iter::once(first).chain(streams.map(|s| { |
| 207 | match s { |
| 208 | TokenStream::Nightly(s) => s, |
| 209 | TokenStream::Stable(_) => mismatch(), |
| 210 | } |
| 211 | })).collect(); |
| 212 | TokenStream::Nightly(stream) |
| 213 | } |
| 214 | #[cfg(not(slow_extend))] |
| 215 | Some(TokenStream::Nightly(mut first)) => { |
| 216 | first.extend(streams.map(|s| { |
| 217 | match s { |
| 218 | TokenStream::Nightly(s) => s, |
| 219 | TokenStream::Stable(_) => mismatch(), |
| 220 | } |
| 221 | })); |
| 222 | TokenStream::Nightly(first) |
| 223 | } |
| 224 | Some(TokenStream::Stable(mut first)) => { |
| 225 | first.extend(streams.map(|s| { |
| 226 | match s { |
| 227 | TokenStream::Stable(s) => s, |
| 228 | TokenStream::Nightly(_) => mismatch(), |
| 229 | } |
| 230 | })); |
| 231 | TokenStream::Stable(first) |
| 232 | } |
| 233 | None => TokenStream::new(), |
| 234 | |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 239 | impl Extend<TokenTree> for TokenStream { |
| 240 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) { |
| 241 | match self { |
| 242 | TokenStream::Nightly(tts) => { |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 243 | #[cfg(not(slow_extend))] |
| 244 | { |
| 245 | tts.extend( |
| 246 | streams |
| 247 | .into_iter() |
| 248 | .map(|t| TokenStream::from(t).unwrap_nightly()), |
| 249 | ); |
| 250 | } |
| 251 | #[cfg(slow_extend)] |
| 252 | { |
| 253 | *tts = tts |
| 254 | .clone() |
David Tolnay | 40d4ffd | 2018-08-12 13:49:09 -0700 | [diff] [blame] | 255 | .into_iter() |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 256 | .chain( |
| 257 | streams |
| 258 | .into_iter() |
| 259 | .map(TokenStream::from) |
| 260 | .flat_map(|t| match t { |
| 261 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 262 | _ => mismatch(), |
| 263 | }), |
| 264 | ).collect(); |
| 265 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 266 | } |
| 267 | TokenStream::Stable(tts) => tts.extend(streams), |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 272 | impl Extend<TokenStream> for TokenStream { |
| 273 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { |
| 274 | match self { |
| 275 | TokenStream::Nightly(tts) => { |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 276 | #[cfg(not(slow_extend))] |
| 277 | { |
| 278 | tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly())); |
| 279 | } |
| 280 | #[cfg(slow_extend)] |
| 281 | { |
| 282 | *tts = tts |
| 283 | .clone() |
| 284 | .into_iter() |
| 285 | .chain( |
| 286 | streams |
| 287 | .into_iter() |
| 288 | .flat_map(|t| match t { |
| 289 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 290 | _ => mismatch(), |
| 291 | }), |
| 292 | ).collect(); |
| 293 | } |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 294 | } |
| 295 | TokenStream::Stable(tts) => { |
| 296 | tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable())) |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 302 | impl fmt::Debug for TokenStream { |
| 303 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 304 | match self { |
| 305 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 306 | TokenStream::Stable(tts) => tts.fmt(f), |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | impl From<proc_macro::LexError> for LexError { |
| 312 | fn from(e: proc_macro::LexError) -> LexError { |
| 313 | LexError::Nightly(e) |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | impl From<stable::LexError> for LexError { |
| 318 | fn from(e: stable::LexError) -> LexError { |
| 319 | LexError::Stable(e) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | impl fmt::Debug for LexError { |
| 324 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 325 | match self { |
| 326 | LexError::Nightly(e) => e.fmt(f), |
| 327 | LexError::Stable(e) => e.fmt(f), |
| 328 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 332 | pub enum TokenTreeIter { |
| 333 | Nightly(proc_macro::token_stream::IntoIter), |
| 334 | Stable(stable::TokenTreeIter), |
| 335 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 336 | |
| 337 | impl IntoIterator for TokenStream { |
| 338 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 339 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 340 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 341 | fn into_iter(self) -> TokenTreeIter { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 342 | match self { |
| 343 | TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()), |
| 344 | TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()), |
| 345 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 349 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 350 | type Item = TokenTree; |
| 351 | |
| 352 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 353 | let token = match self { |
| 354 | TokenTreeIter::Nightly(iter) => iter.next()?, |
| 355 | TokenTreeIter::Stable(iter) => return iter.next(), |
| 356 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 357 | Some(match token { |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 358 | proc_macro::TokenTree::Group(tt) => ::Group::_new(Group::Nightly(tt)).into(), |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 359 | proc_macro::TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 360 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 361 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 362 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 363 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 364 | let mut o = Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 365 | o.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 366 | o.into() |
| 367 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 368 | proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 369 | proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 370 | }) |
| 371 | } |
| 372 | |
| 373 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 374 | match self { |
| 375 | TokenTreeIter::Nightly(tts) => tts.size_hint(), |
| 376 | TokenTreeIter::Stable(tts) => tts.size_hint(), |
| 377 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 381 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 382 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 383 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 387 | #[derive(Clone, PartialEq, Eq)] |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 388 | #[cfg(super_unstable)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 389 | pub enum SourceFile { |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 390 | Nightly(proc_macro::SourceFile), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 391 | Stable(stable::SourceFile), |
| 392 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 393 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 394 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 395 | impl SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 396 | fn nightly(sf: proc_macro::SourceFile) -> Self { |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 397 | SourceFile::Nightly(sf) |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 400 | /// Get the path to this source file as a string. |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 401 | pub fn path(&self) -> PathBuf { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 402 | match self { |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 403 | SourceFile::Nightly(a) => a.path(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 404 | SourceFile::Stable(a) => a.path(), |
| 405 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | pub fn is_real(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 409 | match self { |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 410 | SourceFile::Nightly(a) => a.is_real(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 411 | SourceFile::Stable(a) => a.is_real(), |
| 412 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 416 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 417 | impl fmt::Debug for SourceFile { |
| 418 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 419 | match self { |
David Tolnay | 9cd3b4c | 2018-11-11 16:47:32 -0800 | [diff] [blame] | 420 | SourceFile::Nightly(a) => a.fmt(f), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 421 | SourceFile::Stable(a) => a.fmt(f), |
| 422 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 426 | pub struct LineColumn { |
| 427 | pub line: usize, |
| 428 | pub column: usize, |
| 429 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 430 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 431 | #[derive(Copy, Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 432 | pub enum Span { |
| 433 | Nightly(proc_macro::Span), |
| 434 | Stable(stable::Span), |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 437 | impl Span { |
| 438 | pub fn call_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 439 | if nightly_works() { |
| 440 | Span::Nightly(proc_macro::Span::call_site()) |
| 441 | } else { |
| 442 | Span::Stable(stable::Span::call_site()) |
| 443 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 444 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 445 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 446 | #[cfg(super_unstable)] |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 447 | pub fn def_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 448 | if nightly_works() { |
| 449 | Span::Nightly(proc_macro::Span::def_site()) |
| 450 | } else { |
| 451 | Span::Stable(stable::Span::def_site()) |
| 452 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 453 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 454 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 455 | #[cfg(super_unstable)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 456 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 457 | match (self, other) { |
| 458 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)), |
| 459 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)), |
| 460 | _ => mismatch(), |
| 461 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 464 | #[cfg(super_unstable)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 465 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 466 | match (self, other) { |
| 467 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)), |
| 468 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)), |
| 469 | _ => mismatch(), |
| 470 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 471 | } |
| 472 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 473 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 474 | match self { |
| 475 | Span::Nightly(s) => s, |
David Tolnay | c0425cd | 2019-01-16 12:13:15 -0800 | [diff] [blame] | 476 | Span::Stable(_) => { |
| 477 | panic!("proc_macro::Span is only available in procedural macros") |
| 478 | } |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 479 | } |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 480 | } |
| 481 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 482 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 483 | pub fn source_file(&self) -> SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 484 | match self { |
| 485 | Span::Nightly(s) => SourceFile::nightly(s.source_file()), |
| 486 | Span::Stable(s) => SourceFile::Stable(s.source_file()), |
| 487 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 488 | } |
| 489 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 490 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 491 | pub fn start(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 492 | match self { |
| 493 | Span::Nightly(s) => { |
| 494 | let proc_macro::LineColumn { line, column } = s.start(); |
| 495 | LineColumn { line, column } |
| 496 | } |
| 497 | Span::Stable(s) => { |
| 498 | let stable::LineColumn { line, column } = s.start(); |
| 499 | LineColumn { line, column } |
| 500 | } |
| 501 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 502 | } |
| 503 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 504 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 505 | pub fn end(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 506 | match self { |
| 507 | Span::Nightly(s) => { |
| 508 | let proc_macro::LineColumn { line, column } = s.end(); |
| 509 | LineColumn { line, column } |
| 510 | } |
| 511 | Span::Stable(s) => { |
| 512 | let stable::LineColumn { line, column } = s.end(); |
| 513 | LineColumn { line, column } |
| 514 | } |
| 515 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 518 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 519 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 520 | let ret = match (self, other) { |
| 521 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?), |
| 522 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?), |
| 523 | _ => return None, |
| 524 | }; |
| 525 | Some(ret) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 526 | } |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 527 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 528 | #[cfg(super_unstable)] |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 529 | pub fn eq(&self, other: &Span) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 530 | match (self, other) { |
| 531 | (Span::Nightly(a), Span::Nightly(b)) => a.eq(b), |
| 532 | (Span::Stable(a), Span::Stable(b)) => a.eq(b), |
| 533 | _ => false, |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | fn unwrap_nightly(self) -> proc_macro::Span { |
| 538 | match self { |
| 539 | Span::Nightly(s) => s, |
| 540 | Span::Stable(_) => mismatch(), |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | impl From<proc_macro::Span> for ::Span { |
| 546 | fn from(proc_span: proc_macro::Span) -> ::Span { |
| 547 | ::Span::_new(Span::Nightly(proc_span)) |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | impl From<stable::Span> for Span { |
| 552 | fn from(inner: stable::Span) -> Span { |
| 553 | Span::Stable(inner) |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 554 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 557 | impl fmt::Debug for Span { |
| 558 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 559 | match self { |
| 560 | Span::Nightly(s) => s.fmt(f), |
| 561 | Span::Stable(s) => s.fmt(f), |
| 562 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 566 | #[derive(Clone)] |
David Tolnay | f14813f | 2018-09-08 17:14:07 -0700 | [diff] [blame] | 567 | pub enum Group { |
| 568 | Nightly(proc_macro::Group), |
| 569 | Stable(stable::Group), |
| 570 | } |
| 571 | |
| 572 | impl Group { |
| 573 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { |
| 574 | match stream { |
| 575 | TokenStream::Nightly(stream) => { |
| 576 | let delimiter = match delimiter { |
| 577 | Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis, |
| 578 | Delimiter::Bracket => proc_macro::Delimiter::Bracket, |
| 579 | Delimiter::Brace => proc_macro::Delimiter::Brace, |
| 580 | Delimiter::None => proc_macro::Delimiter::None, |
| 581 | }; |
| 582 | Group::Nightly(proc_macro::Group::new(delimiter, stream)) |
| 583 | } |
| 584 | TokenStream::Stable(stream) => { |
| 585 | Group::Stable(stable::Group::new(delimiter, stream)) |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | pub fn delimiter(&self) -> Delimiter { |
| 591 | match self { |
| 592 | Group::Nightly(g) => match g.delimiter() { |
| 593 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
| 594 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
| 595 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
| 596 | proc_macro::Delimiter::None => Delimiter::None, |
| 597 | } |
| 598 | Group::Stable(g) => g.delimiter(), |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | pub fn stream(&self) -> TokenStream { |
| 603 | match self { |
| 604 | Group::Nightly(g) => TokenStream::Nightly(g.stream()), |
| 605 | Group::Stable(g) => TokenStream::Stable(g.stream()), |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | pub fn span(&self) -> Span { |
| 610 | match self { |
| 611 | Group::Nightly(g) => Span::Nightly(g.span()), |
| 612 | Group::Stable(g) => Span::Stable(g.span()), |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | #[cfg(super_unstable)] |
| 617 | pub fn span_open(&self) -> Span { |
| 618 | match self { |
| 619 | Group::Nightly(g) => Span::Nightly(g.span_open()), |
| 620 | Group::Stable(g) => Span::Stable(g.span_open()), |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | #[cfg(super_unstable)] |
| 625 | pub fn span_close(&self) -> Span { |
| 626 | match self { |
| 627 | Group::Nightly(g) => Span::Nightly(g.span_close()), |
| 628 | Group::Stable(g) => Span::Stable(g.span_close()), |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | pub fn set_span(&mut self, span: Span) { |
| 633 | match (self, span) { |
| 634 | (Group::Nightly(g), Span::Nightly(s)) => g.set_span(s), |
| 635 | (Group::Stable(g), Span::Stable(s)) => g.set_span(s), |
| 636 | _ => mismatch(), |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | fn unwrap_nightly(self) -> proc_macro::Group { |
| 641 | match self { |
| 642 | Group::Nightly(g) => g, |
| 643 | Group::Stable(_) => mismatch(), |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | impl From<stable::Group> for Group { |
| 649 | fn from(g: stable::Group) -> Self { |
| 650 | Group::Stable(g) |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | impl fmt::Display for Group { |
| 655 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 656 | match self { |
| 657 | Group::Nightly(group) => group.fmt(formatter), |
| 658 | Group::Stable(group) => group.fmt(formatter), |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | impl fmt::Debug for Group { |
| 664 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 665 | match self { |
| 666 | Group::Nightly(group) => group.fmt(formatter), |
| 667 | Group::Stable(group) => group.fmt(formatter), |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | #[derive(Clone)] |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 673 | pub enum Ident { |
| 674 | Nightly(proc_macro::Ident), |
| 675 | Stable(stable::Ident), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 676 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 677 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 678 | impl Ident { |
| 679 | pub fn new(string: &str, span: Span) -> Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 680 | match span { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 681 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)), |
| 682 | Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 683 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 684 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 685 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 686 | pub fn new_raw(string: &str, span: Span) -> Ident { |
| 687 | match span { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 688 | Span::Nightly(s) => { |
| 689 | let p: proc_macro::TokenStream = string.parse().unwrap(); |
| 690 | let ident = match p.into_iter().next() { |
| 691 | Some(proc_macro::TokenTree::Ident(mut i)) => { |
| 692 | i.set_span(s); |
| 693 | i |
| 694 | } |
| 695 | _ => panic!(), |
| 696 | }; |
| 697 | Ident::Nightly(ident) |
| 698 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 699 | Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 700 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 704 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 705 | Ident::Nightly(t) => Span::Nightly(t.span()), |
| 706 | Ident::Stable(t) => Span::Stable(t.span()), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 707 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 711 | match (self, span) { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 712 | (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s), |
| 713 | (Ident::Stable(t), Span::Stable(s)) => t.set_span(s), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 714 | _ => mismatch(), |
| 715 | } |
| 716 | } |
| 717 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 718 | fn unwrap_nightly(self) -> proc_macro::Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 719 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 720 | Ident::Nightly(s) => s, |
| 721 | Ident::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 722 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
David Tolnay | c0b0f2e | 2018-09-02 17:56:08 -0700 | [diff] [blame] | 726 | impl PartialEq for Ident { |
| 727 | fn eq(&self, other: &Ident) -> bool { |
| 728 | match (self, other) { |
| 729 | (Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(), |
| 730 | (Ident::Stable(t), Ident::Stable(o)) => t == o, |
| 731 | _ => mismatch(), |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | impl<T> PartialEq<T> for Ident |
| 737 | where |
| 738 | T: ?Sized + AsRef<str>, |
| 739 | { |
| 740 | fn eq(&self, other: &T) -> bool { |
| 741 | let other = other.as_ref(); |
| 742 | match self { |
| 743 | Ident::Nightly(t) => t.to_string() == other, |
| 744 | Ident::Stable(t) => t == other, |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 749 | impl fmt::Display for Ident { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 750 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 751 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 752 | Ident::Nightly(t) => t.fmt(f), |
| 753 | Ident::Stable(t) => t.fmt(f), |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | impl fmt::Debug for Ident { |
| 759 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 760 | match self { |
| 761 | Ident::Nightly(t) => t.fmt(f), |
| 762 | Ident::Stable(t) => t.fmt(f), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 763 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 768 | pub enum Literal { |
| 769 | Nightly(proc_macro::Literal), |
| 770 | Stable(stable::Literal), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 771 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 772 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 773 | macro_rules! suffixed_numbers { |
| 774 | ($($name:ident => $kind:ident,)*) => ($( |
| 775 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 776 | if nightly_works() { |
| 777 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 778 | } else { |
| 779 | Literal::Stable(stable::Literal::$name(n)) |
| 780 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 781 | } |
| 782 | )*) |
| 783 | } |
| 784 | |
| 785 | macro_rules! unsuffixed_integers { |
| 786 | ($($name:ident => $kind:ident,)*) => ($( |
| 787 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 788 | if nightly_works() { |
| 789 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 790 | } else { |
| 791 | Literal::Stable(stable::Literal::$name(n)) |
| 792 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 793 | } |
| 794 | )*) |
| 795 | } |
| 796 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 797 | impl Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 798 | suffixed_numbers! { |
| 799 | u8_suffixed => u8, |
| 800 | u16_suffixed => u16, |
| 801 | u32_suffixed => u32, |
| 802 | u64_suffixed => u64, |
| 803 | usize_suffixed => usize, |
| 804 | i8_suffixed => i8, |
| 805 | i16_suffixed => i16, |
| 806 | i32_suffixed => i32, |
| 807 | i64_suffixed => i64, |
| 808 | isize_suffixed => isize, |
| 809 | |
| 810 | f32_suffixed => f32, |
| 811 | f64_suffixed => f64, |
| 812 | } |
| 813 | |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 814 | #[cfg(u128)] |
| 815 | suffixed_numbers! { |
| 816 | i128_suffixed => i128, |
| 817 | u128_suffixed => u128, |
| 818 | } |
| 819 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 820 | unsuffixed_integers! { |
| 821 | u8_unsuffixed => u8, |
| 822 | u16_unsuffixed => u16, |
| 823 | u32_unsuffixed => u32, |
| 824 | u64_unsuffixed => u64, |
| 825 | usize_unsuffixed => usize, |
| 826 | i8_unsuffixed => i8, |
| 827 | i16_unsuffixed => i16, |
| 828 | i32_unsuffixed => i32, |
| 829 | i64_unsuffixed => i64, |
| 830 | isize_unsuffixed => isize, |
| 831 | } |
| 832 | |
Alex Crichton | 6938566 | 2018-11-08 06:30:04 -0800 | [diff] [blame] | 833 | #[cfg(u128)] |
| 834 | unsuffixed_integers! { |
| 835 | i128_unsuffixed => i128, |
| 836 | u128_unsuffixed => u128, |
| 837 | } |
| 838 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 839 | pub fn f32_unsuffixed(f: f32) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 840 | if nightly_works() { |
| 841 | Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f)) |
| 842 | } else { |
| 843 | Literal::Stable(stable::Literal::f32_unsuffixed(f)) |
| 844 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 848 | if nightly_works() { |
| 849 | Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f)) |
| 850 | } else { |
| 851 | Literal::Stable(stable::Literal::f64_unsuffixed(f)) |
| 852 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 855 | pub fn string(t: &str) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 856 | if nightly_works() { |
| 857 | Literal::Nightly(proc_macro::Literal::string(t)) |
| 858 | } else { |
| 859 | Literal::Stable(stable::Literal::string(t)) |
| 860 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | pub fn character(t: char) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 864 | if nightly_works() { |
| 865 | Literal::Nightly(proc_macro::Literal::character(t)) |
| 866 | } else { |
| 867 | Literal::Stable(stable::Literal::character(t)) |
| 868 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 872 | if nightly_works() { |
| 873 | Literal::Nightly(proc_macro::Literal::byte_string(bytes)) |
| 874 | } else { |
| 875 | Literal::Stable(stable::Literal::byte_string(bytes)) |
| 876 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 879 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 880 | match self { |
| 881 | Literal::Nightly(lit) => Span::Nightly(lit.span()), |
| 882 | Literal::Stable(lit) => Span::Stable(lit.span()), |
| 883 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 886 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 887 | match (self, span) { |
| 888 | (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s), |
| 889 | (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s), |
| 890 | _ => mismatch(), |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | fn unwrap_nightly(self) -> proc_macro::Literal { |
| 895 | match self { |
| 896 | Literal::Nightly(s) => s, |
| 897 | Literal::Stable(_) => mismatch(), |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | impl From<stable::Literal> for Literal { |
| 903 | fn from(s: stable::Literal) -> Literal { |
| 904 | Literal::Stable(s) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
| 908 | impl fmt::Display for Literal { |
| 909 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 910 | match self { |
| 911 | Literal::Nightly(t) => t.fmt(f), |
| 912 | Literal::Stable(t) => t.fmt(f), |
| 913 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
| 917 | impl fmt::Debug for Literal { |
| 918 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 919 | match self { |
| 920 | Literal::Nightly(t) => t.fmt(f), |
| 921 | Literal::Stable(t) => t.fmt(f), |
| 922 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 923 | } |
| 924 | } |