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 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 11 | use {Delimiter, Group, 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 { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 164 | TokenTree::Group(tt) => { |
| 165 | let delim = match tt.delimiter() { |
| 166 | Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis, |
| 167 | Delimiter::Bracket => proc_macro::Delimiter::Bracket, |
| 168 | Delimiter::Brace => proc_macro::Delimiter::Brace, |
| 169 | Delimiter::None => proc_macro::Delimiter::None, |
| 170 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 171 | let span = tt.span(); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 172 | let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly()); |
| 173 | group.set_span(span.inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 174 | group.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::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 177 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 178 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 179 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 180 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 181 | let mut op = proc_macro::Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 182 | op.set_span(tt.span().inner.unwrap_nightly()); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 183 | op.into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 184 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 185 | TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 186 | TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 187 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 188 | TokenStream::Nightly(tt.into()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 192 | impl iter::FromIterator<TokenTree> for TokenStream { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 193 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { |
| 194 | if nightly_works() { |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 195 | let trees = trees |
| 196 | .into_iter() |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 197 | .map(TokenStream::from) |
David Tolnay | 3d9d6ad | 2018-05-18 10:51:55 -0700 | [diff] [blame] | 198 | .flat_map(|t| match t { |
| 199 | TokenStream::Nightly(s) => s, |
| 200 | TokenStream::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 201 | }); |
| 202 | TokenStream::Nightly(trees.collect()) |
| 203 | } else { |
| 204 | TokenStream::Stable(trees.into_iter().collect()) |
| 205 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Alex Crichton | 53b0067 | 2018-09-06 17:16:10 -0700 | [diff] [blame] | 209 | impl iter::FromIterator<TokenStream> for TokenStream { |
| 210 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { |
| 211 | let mut streams = streams.into_iter(); |
| 212 | match streams.next() { |
| 213 | #[cfg(slow_extend)] |
| 214 | Some(TokenStream::Nightly(first)) => { |
| 215 | let stream = iter::once(first).chain(streams.map(|s| { |
| 216 | match s { |
| 217 | TokenStream::Nightly(s) => s, |
| 218 | TokenStream::Stable(_) => mismatch(), |
| 219 | } |
| 220 | })).collect(); |
| 221 | TokenStream::Nightly(stream) |
| 222 | } |
| 223 | #[cfg(not(slow_extend))] |
| 224 | Some(TokenStream::Nightly(mut first)) => { |
| 225 | first.extend(streams.map(|s| { |
| 226 | match s { |
| 227 | TokenStream::Nightly(s) => s, |
| 228 | TokenStream::Stable(_) => mismatch(), |
| 229 | } |
| 230 | })); |
| 231 | TokenStream::Nightly(first) |
| 232 | } |
| 233 | Some(TokenStream::Stable(mut first)) => { |
| 234 | first.extend(streams.map(|s| { |
| 235 | match s { |
| 236 | TokenStream::Stable(s) => s, |
| 237 | TokenStream::Nightly(_) => mismatch(), |
| 238 | } |
| 239 | })); |
| 240 | TokenStream::Stable(first) |
| 241 | } |
| 242 | None => TokenStream::new(), |
| 243 | |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 248 | impl Extend<TokenTree> for TokenStream { |
| 249 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) { |
| 250 | match self { |
| 251 | TokenStream::Nightly(tts) => { |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 252 | #[cfg(not(slow_extend))] |
| 253 | { |
| 254 | tts.extend( |
| 255 | streams |
| 256 | .into_iter() |
| 257 | .map(|t| TokenStream::from(t).unwrap_nightly()), |
| 258 | ); |
| 259 | } |
| 260 | #[cfg(slow_extend)] |
| 261 | { |
| 262 | *tts = tts |
| 263 | .clone() |
David Tolnay | 40d4ffd | 2018-08-12 13:49:09 -0700 | [diff] [blame] | 264 | .into_iter() |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 265 | .chain( |
| 266 | streams |
| 267 | .into_iter() |
| 268 | .map(TokenStream::from) |
| 269 | .flat_map(|t| match t { |
| 270 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 271 | _ => mismatch(), |
| 272 | }), |
| 273 | ).collect(); |
| 274 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 275 | } |
| 276 | TokenStream::Stable(tts) => tts.extend(streams), |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 281 | impl Extend<TokenStream> for TokenStream { |
| 282 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { |
| 283 | match self { |
| 284 | TokenStream::Nightly(tts) => { |
David Tolnay | e839e4f | 2018-09-06 09:36:43 -0700 | [diff] [blame] | 285 | #[cfg(not(slow_extend))] |
| 286 | { |
| 287 | tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly())); |
| 288 | } |
| 289 | #[cfg(slow_extend)] |
| 290 | { |
| 291 | *tts = tts |
| 292 | .clone() |
| 293 | .into_iter() |
| 294 | .chain( |
| 295 | streams |
| 296 | .into_iter() |
| 297 | .flat_map(|t| match t { |
| 298 | TokenStream::Nightly(tts) => tts.into_iter(), |
| 299 | _ => mismatch(), |
| 300 | }), |
| 301 | ).collect(); |
| 302 | } |
David Tolnay | 5c58c53 | 2018-08-13 11:33:51 -0700 | [diff] [blame] | 303 | } |
| 304 | TokenStream::Stable(tts) => { |
| 305 | tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable())) |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 311 | impl fmt::Debug for TokenStream { |
| 312 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 313 | match self { |
| 314 | TokenStream::Nightly(tts) => tts.fmt(f), |
| 315 | TokenStream::Stable(tts) => tts.fmt(f), |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | impl From<proc_macro::LexError> for LexError { |
| 321 | fn from(e: proc_macro::LexError) -> LexError { |
| 322 | LexError::Nightly(e) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | impl From<stable::LexError> for LexError { |
| 327 | fn from(e: stable::LexError) -> LexError { |
| 328 | LexError::Stable(e) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | impl fmt::Debug for LexError { |
| 333 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 334 | match self { |
| 335 | LexError::Nightly(e) => e.fmt(f), |
| 336 | LexError::Stable(e) => e.fmt(f), |
| 337 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 341 | pub enum TokenTreeIter { |
| 342 | Nightly(proc_macro::token_stream::IntoIter), |
| 343 | Stable(stable::TokenTreeIter), |
| 344 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 345 | |
| 346 | impl IntoIterator for TokenStream { |
| 347 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 348 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 349 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 350 | fn into_iter(self) -> TokenTreeIter { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 351 | match self { |
| 352 | TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()), |
| 353 | TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()), |
| 354 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 358 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 359 | type Item = TokenTree; |
| 360 | |
| 361 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 362 | let token = match self { |
| 363 | TokenTreeIter::Nightly(iter) => iter.next()?, |
| 364 | TokenTreeIter::Stable(iter) => return iter.next(), |
| 365 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 366 | Some(match token { |
| 367 | proc_macro::TokenTree::Group(tt) => { |
| 368 | let delim = match tt.delimiter() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 369 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
| 370 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
| 371 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
| 372 | proc_macro::Delimiter::None => Delimiter::None, |
| 373 | }; |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 374 | let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream())); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 375 | let mut g = Group::new(delim, stream); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 376 | g.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 377 | g.into() |
| 378 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 379 | proc_macro::TokenTree::Punct(tt) => { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 380 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 381 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 382 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 383 | }; |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 384 | let mut o = Punct::new(tt.as_char(), spacing); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 385 | o.set_span(::Span::_new(Span::Nightly(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 386 | o.into() |
| 387 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 388 | proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 389 | proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 390 | }) |
| 391 | } |
| 392 | |
| 393 | fn size_hint(&self) -> (usize, Option<usize>) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 394 | match self { |
| 395 | TokenTreeIter::Nightly(tts) => tts.size_hint(), |
| 396 | TokenTreeIter::Stable(tts) => tts.size_hint(), |
| 397 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 401 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 402 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 403 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 407 | pub use stable::FileName; |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 408 | |
| 409 | // NOTE: We have to generate our own filename object here because we can't wrap |
| 410 | // the one provided by proc_macro. |
| 411 | #[derive(Clone, PartialEq, Eq)] |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 412 | #[cfg(super_unstable)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 413 | pub enum SourceFile { |
| 414 | Nightly(proc_macro::SourceFile, FileName), |
| 415 | Stable(stable::SourceFile), |
| 416 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 417 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 418 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 419 | impl SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 420 | fn nightly(sf: proc_macro::SourceFile) -> Self { |
Alex Crichton | 9f0a28a | 2018-07-21 18:53:35 -0700 | [diff] [blame] | 421 | let filename = stable::file_name(sf.path().display().to_string()); |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 422 | SourceFile::Nightly(sf, filename) |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 423 | } |
| 424 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 425 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 426 | pub fn path(&self) -> &FileName { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 427 | match self { |
| 428 | SourceFile::Nightly(_, f) => f, |
| 429 | SourceFile::Stable(a) => a.path(), |
| 430 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | pub fn is_real(&self) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 434 | match self { |
| 435 | SourceFile::Nightly(a, _) => a.is_real(), |
| 436 | SourceFile::Stable(a) => a.is_real(), |
| 437 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 441 | #[cfg(super_unstable)] |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 442 | impl AsRef<FileName> for SourceFile { |
| 443 | fn as_ref(&self) -> &FileName { |
| 444 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 448 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 449 | impl fmt::Debug for SourceFile { |
| 450 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 451 | match self { |
| 452 | SourceFile::Nightly(a, _) => a.fmt(f), |
| 453 | SourceFile::Stable(a) => a.fmt(f), |
| 454 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 458 | pub struct LineColumn { |
| 459 | pub line: usize, |
| 460 | pub column: usize, |
| 461 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 462 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame] | 463 | #[derive(Copy, Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 464 | pub enum Span { |
| 465 | Nightly(proc_macro::Span), |
| 466 | Stable(stable::Span), |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 469 | impl Span { |
| 470 | pub fn call_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 471 | if nightly_works() { |
| 472 | Span::Nightly(proc_macro::Span::call_site()) |
| 473 | } else { |
| 474 | Span::Stable(stable::Span::call_site()) |
| 475 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 476 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 477 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 478 | #[cfg(super_unstable)] |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 479 | pub fn def_site() -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 480 | if nightly_works() { |
| 481 | Span::Nightly(proc_macro::Span::def_site()) |
| 482 | } else { |
| 483 | Span::Stable(stable::Span::def_site()) |
| 484 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 485 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 486 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 487 | #[cfg(super_unstable)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 488 | pub fn resolved_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 489 | match (self, other) { |
| 490 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)), |
| 491 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)), |
| 492 | _ => mismatch(), |
| 493 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 496 | #[cfg(super_unstable)] |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 497 | pub fn located_at(&self, other: Span) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 498 | match (self, other) { |
| 499 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)), |
| 500 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)), |
| 501 | _ => mismatch(), |
| 502 | } |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 503 | } |
| 504 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 505 | pub fn unstable(self) -> proc_macro::Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 506 | match self { |
| 507 | Span::Nightly(s) => s, |
| 508 | Span::Stable(_) => mismatch(), |
| 509 | } |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -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 source_file(&self) -> SourceFile { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 514 | match self { |
| 515 | Span::Nightly(s) => SourceFile::nightly(s.source_file()), |
| 516 | Span::Stable(s) => SourceFile::Stable(s.source_file()), |
| 517 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 518 | } |
| 519 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 520 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 521 | pub fn start(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 522 | match self { |
| 523 | Span::Nightly(s) => { |
| 524 | let proc_macro::LineColumn { line, column } = s.start(); |
| 525 | LineColumn { line, column } |
| 526 | } |
| 527 | Span::Stable(s) => { |
| 528 | let stable::LineColumn { line, column } = s.start(); |
| 529 | LineColumn { line, column } |
| 530 | } |
| 531 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 532 | } |
| 533 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 534 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 535 | pub fn end(&self) -> LineColumn { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 536 | match self { |
| 537 | Span::Nightly(s) => { |
| 538 | let proc_macro::LineColumn { line, column } = s.end(); |
| 539 | LineColumn { line, column } |
| 540 | } |
| 541 | Span::Stable(s) => { |
| 542 | let stable::LineColumn { line, column } = s.end(); |
| 543 | LineColumn { line, column } |
| 544 | } |
| 545 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 548 | #[cfg(super_unstable)] |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 549 | pub fn join(&self, other: Span) -> Option<Span> { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 550 | let ret = match (self, other) { |
| 551 | (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?), |
| 552 | (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?), |
| 553 | _ => return None, |
| 554 | }; |
| 555 | Some(ret) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 556 | } |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 557 | |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 558 | #[cfg(super_unstable)] |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 559 | pub fn eq(&self, other: &Span) -> bool { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 560 | match (self, other) { |
| 561 | (Span::Nightly(a), Span::Nightly(b)) => a.eq(b), |
| 562 | (Span::Stable(a), Span::Stable(b)) => a.eq(b), |
| 563 | _ => false, |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | fn unwrap_nightly(self) -> proc_macro::Span { |
| 568 | match self { |
| 569 | Span::Nightly(s) => s, |
| 570 | Span::Stable(_) => mismatch(), |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | impl From<proc_macro::Span> for ::Span { |
| 576 | fn from(proc_span: proc_macro::Span) -> ::Span { |
| 577 | ::Span::_new(Span::Nightly(proc_span)) |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | impl From<stable::Span> for Span { |
| 582 | fn from(inner: stable::Span) -> Span { |
| 583 | Span::Stable(inner) |
Alex Crichton | e24f734 | 2018-04-05 17:58:11 -0700 | [diff] [blame] | 584 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 587 | impl fmt::Debug for Span { |
| 588 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 589 | match self { |
| 590 | Span::Nightly(s) => s.fmt(f), |
| 591 | Span::Stable(s) => s.fmt(f), |
| 592 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 596 | #[derive(Clone)] |
| 597 | pub enum Ident { |
| 598 | Nightly(proc_macro::Ident), |
| 599 | Stable(stable::Ident), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 600 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 601 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 602 | impl Ident { |
| 603 | pub fn new(string: &str, span: Span) -> Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 604 | match span { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 605 | Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)), |
| 606 | Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 607 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 608 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 609 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 610 | pub fn new_raw(string: &str, span: Span) -> Ident { |
| 611 | match span { |
Alex Crichton | ce0904d | 2018-08-27 17:29:49 -0700 | [diff] [blame] | 612 | Span::Nightly(s) => { |
| 613 | let p: proc_macro::TokenStream = string.parse().unwrap(); |
| 614 | let ident = match p.into_iter().next() { |
| 615 | Some(proc_macro::TokenTree::Ident(mut i)) => { |
| 616 | i.set_span(s); |
| 617 | i |
| 618 | } |
| 619 | _ => panic!(), |
| 620 | }; |
| 621 | Ident::Nightly(ident) |
| 622 | } |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 623 | Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 624 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 628 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 629 | Ident::Nightly(t) => Span::Nightly(t.span()), |
| 630 | Ident::Stable(t) => Span::Stable(t.span()), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 631 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 635 | match (self, span) { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 636 | (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s), |
| 637 | (Ident::Stable(t), Span::Stable(s)) => t.set_span(s), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 638 | _ => mismatch(), |
| 639 | } |
| 640 | } |
| 641 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 642 | fn unwrap_nightly(self) -> proc_macro::Ident { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 643 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 644 | Ident::Nightly(s) => s, |
| 645 | Ident::Stable(_) => mismatch(), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 646 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
David Tolnay | c0b0f2e | 2018-09-02 17:56:08 -0700 | [diff] [blame] | 650 | impl PartialEq for Ident { |
| 651 | fn eq(&self, other: &Ident) -> bool { |
| 652 | match (self, other) { |
| 653 | (Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(), |
| 654 | (Ident::Stable(t), Ident::Stable(o)) => t == o, |
| 655 | _ => mismatch(), |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | impl<T> PartialEq<T> for Ident |
| 661 | where |
| 662 | T: ?Sized + AsRef<str>, |
| 663 | { |
| 664 | fn eq(&self, other: &T) -> bool { |
| 665 | let other = other.as_ref(); |
| 666 | match self { |
| 667 | Ident::Nightly(t) => t.to_string() == other, |
| 668 | Ident::Stable(t) => t == other, |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 673 | impl fmt::Display for Ident { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 674 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 675 | match self { |
Alex Crichton | f388843 | 2018-05-16 09:11:05 -0700 | [diff] [blame] | 676 | Ident::Nightly(t) => t.fmt(f), |
| 677 | Ident::Stable(t) => t.fmt(f), |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | impl fmt::Debug for Ident { |
| 683 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 684 | match self { |
| 685 | Ident::Nightly(t) => t.fmt(f), |
| 686 | Ident::Stable(t) => t.fmt(f), |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 687 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | #[derive(Clone)] |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 692 | pub enum Literal { |
| 693 | Nightly(proc_macro::Literal), |
| 694 | Stable(stable::Literal), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 695 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 696 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 697 | macro_rules! suffixed_numbers { |
| 698 | ($($name:ident => $kind:ident,)*) => ($( |
| 699 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 700 | if nightly_works() { |
| 701 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 702 | } else { |
| 703 | Literal::Stable(stable::Literal::$name(n)) |
| 704 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 705 | } |
| 706 | )*) |
| 707 | } |
| 708 | |
| 709 | macro_rules! unsuffixed_integers { |
| 710 | ($($name:ident => $kind:ident,)*) => ($( |
| 711 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 712 | if nightly_works() { |
| 713 | Literal::Nightly(proc_macro::Literal::$name(n)) |
| 714 | } else { |
| 715 | Literal::Stable(stable::Literal::$name(n)) |
| 716 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 717 | } |
| 718 | )*) |
| 719 | } |
| 720 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 721 | impl Literal { |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 722 | suffixed_numbers! { |
| 723 | u8_suffixed => u8, |
| 724 | u16_suffixed => u16, |
| 725 | u32_suffixed => u32, |
| 726 | u64_suffixed => u64, |
| 727 | usize_suffixed => usize, |
| 728 | i8_suffixed => i8, |
| 729 | i16_suffixed => i16, |
| 730 | i32_suffixed => i32, |
| 731 | i64_suffixed => i64, |
| 732 | isize_suffixed => isize, |
| 733 | |
| 734 | f32_suffixed => f32, |
| 735 | f64_suffixed => f64, |
| 736 | } |
| 737 | |
| 738 | unsuffixed_integers! { |
| 739 | u8_unsuffixed => u8, |
| 740 | u16_unsuffixed => u16, |
| 741 | u32_unsuffixed => u32, |
| 742 | u64_unsuffixed => u64, |
| 743 | usize_unsuffixed => usize, |
| 744 | i8_unsuffixed => i8, |
| 745 | i16_unsuffixed => i16, |
| 746 | i32_unsuffixed => i32, |
| 747 | i64_unsuffixed => i64, |
| 748 | isize_unsuffixed => isize, |
| 749 | } |
| 750 | |
| 751 | pub fn f32_unsuffixed(f: f32) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 752 | if nightly_works() { |
| 753 | Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f)) |
| 754 | } else { |
| 755 | Literal::Stable(stable::Literal::f32_unsuffixed(f)) |
| 756 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 760 | if nightly_works() { |
| 761 | Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f)) |
| 762 | } else { |
| 763 | Literal::Stable(stable::Literal::f64_unsuffixed(f)) |
| 764 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 767 | pub fn string(t: &str) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 768 | if nightly_works() { |
| 769 | Literal::Nightly(proc_macro::Literal::string(t)) |
| 770 | } else { |
| 771 | Literal::Stable(stable::Literal::string(t)) |
| 772 | } |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | pub fn character(t: char) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 776 | if nightly_works() { |
| 777 | Literal::Nightly(proc_macro::Literal::character(t)) |
| 778 | } else { |
| 779 | Literal::Stable(stable::Literal::character(t)) |
| 780 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 784 | if nightly_works() { |
| 785 | Literal::Nightly(proc_macro::Literal::byte_string(bytes)) |
| 786 | } else { |
| 787 | Literal::Stable(stable::Literal::byte_string(bytes)) |
| 788 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 791 | pub fn span(&self) -> Span { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 792 | match self { |
| 793 | Literal::Nightly(lit) => Span::Nightly(lit.span()), |
| 794 | Literal::Stable(lit) => Span::Stable(lit.span()), |
| 795 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 798 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 799 | match (self, span) { |
| 800 | (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s), |
| 801 | (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s), |
| 802 | _ => mismatch(), |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | fn unwrap_nightly(self) -> proc_macro::Literal { |
| 807 | match self { |
| 808 | Literal::Nightly(s) => s, |
| 809 | Literal::Stable(_) => mismatch(), |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | impl From<stable::Literal> for Literal { |
| 815 | fn from(s: stable::Literal) -> Literal { |
| 816 | Literal::Stable(s) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
| 820 | impl fmt::Display for Literal { |
| 821 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 822 | match self { |
| 823 | Literal::Nightly(t) => t.fmt(f), |
| 824 | Literal::Stable(t) => t.fmt(f), |
| 825 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | impl fmt::Debug for Literal { |
| 830 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 30a4e9e | 2018-04-27 17:02:19 -0700 | [diff] [blame] | 831 | match self { |
| 832 | Literal::Nightly(t) => t.fmt(f), |
| 833 | Literal::Stable(t) => t.fmt(f), |
| 834 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 835 | } |
| 836 | } |