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