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