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