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