blob: 66158ec8fe5f39aefd354db62b1d5601dc8bac0a [file] [log] [blame]
Alex Crichtona914a612018-04-04 07:48:44 -07001#![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))]
Alex Crichtonaf5bad42018-03-27 14:45:10 -07002
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07003use std::fmt;
4use std::iter;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07005use std::panic;
David Tolnay3d9d6ad2018-05-18 10:51:55 -07006use std::str::FromStr;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07007
8use proc_macro;
Alex Crichton30a4e9e2018-04-27 17:02:19 -07009use stable;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070010
Alex Crichtonf3888432018-05-16 09:11:05 -070011use {Delimiter, Group, Punct, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070012
13#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -070014pub enum TokenStream {
15 Nightly(proc_macro::TokenStream),
16 Stable(stable::TokenStream),
17}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070018
Alex Crichton30a4e9e2018-04-27 17:02:19 -070019pub enum LexError {
20 Nightly(proc_macro::LexError),
21 Stable(stable::LexError),
22}
23
24fn 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
38fn mismatch() -> ! {
39 panic!("stable/nightly mismatch")
40}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070041
42impl TokenStream {
David Tolnayc3bb4592018-05-28 20:09:44 -070043 pub fn new() -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070044 if nightly_works() {
David Tolnayc3bb4592018-05-28 20:09:44 -070045 TokenStream::Nightly(proc_macro::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070046 } else {
David Tolnayc3bb4592018-05-28 20:09:44 -070047 TokenStream::Stable(stable::TokenStream::new())
Alex Crichton30a4e9e2018-04-27 17:02:19 -070048 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070049 }
50
51 pub fn is_empty(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070052 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 Crichtoncbec8ec2017-06-02 13:19:33 -070063 }
David Tolnay5c58c532018-08-13 11:33:51 -070064
65 fn unwrap_stable(self) -> stable::TokenStream {
66 match self {
67 TokenStream::Nightly(_) => mismatch(),
68 TokenStream::Stable(s) => s,
69 }
70 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070071}
72
73impl FromStr for TokenStream {
74 type Err = LexError;
75
76 fn from_str(src: &str) -> Result<TokenStream, LexError> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070077 if nightly_works() {
78 Ok(TokenStream::Nightly(src.parse()?))
79 } else {
80 Ok(TokenStream::Stable(src.parse()?))
81 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070082 }
83}
84
85impl fmt::Display for TokenStream {
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070087 match self {
88 TokenStream::Nightly(tts) => tts.fmt(f),
89 TokenStream::Stable(tts) => tts.fmt(f),
90 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070091 }
92}
93
94impl From<proc_macro::TokenStream> for TokenStream {
95 fn from(inner: proc_macro::TokenStream) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -070096 TokenStream::Nightly(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070097 }
98}
99
100impl From<TokenStream> for proc_macro::TokenStream {
101 fn from(inner: TokenStream) -> proc_macro::TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700102 match inner {
103 TokenStream::Nightly(inner) => inner,
104 TokenStream::Stable(inner) => inner.to_string().parse().unwrap(),
105 }
106 }
107}
108
109impl From<stable::TokenStream> for TokenStream {
110 fn from(inner: stable::TokenStream) -> TokenStream {
111 TokenStream::Stable(inner)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700112 }
113}
114
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700115impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700116 fn from(token: TokenTree) -> TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700117 if !nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700118 return TokenStream::Stable(token.into());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700119 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700120 let tt: proc_macro::TokenTree = match token {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700121 TokenTree::Group(tt) => {
122 let delim = match tt.delimiter() {
123 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
124 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
125 Delimiter::Brace => proc_macro::Delimiter::Brace,
126 Delimiter::None => proc_macro::Delimiter::None,
127 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700128 let span = tt.span();
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700129 let mut group = proc_macro::Group::new(delim, tt.stream.inner.unwrap_nightly());
130 group.set_span(span.inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700131 group.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700132 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700133 TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700134 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700135 Spacing::Joint => proc_macro::Spacing::Joint,
136 Spacing::Alone => proc_macro::Spacing::Alone,
137 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700138 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700139 op.set_span(tt.span().inner.unwrap_nightly());
Alex Crichton9cd80a62018-04-05 17:46:58 -0700140 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700141 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700142 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700143 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700144 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700145 TokenStream::Nightly(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700146 }
147}
148
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700149impl iter::FromIterator<TokenTree> for TokenStream {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700150 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
151 if nightly_works() {
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700152 let trees = trees
153 .into_iter()
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700154 .map(TokenStream::from)
David Tolnay3d9d6ad2018-05-18 10:51:55 -0700155 .flat_map(|t| match t {
156 TokenStream::Nightly(s) => s,
157 TokenStream::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700158 });
159 TokenStream::Nightly(trees.collect())
160 } else {
161 TokenStream::Stable(trees.into_iter().collect())
162 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700163 }
164}
165
Alex Crichtonf3888432018-05-16 09:11:05 -0700166impl Extend<TokenTree> for TokenStream {
167 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
168 match self {
169 TokenStream::Nightly(tts) => {
David Tolnay40d4ffd2018-08-12 13:49:09 -0700170 tts.extend(
171 streams
172 .into_iter()
173 .map(|t| TokenStream::from(t).unwrap_nightly()),
174 );
Alex Crichtonf3888432018-05-16 09:11:05 -0700175 }
176 TokenStream::Stable(tts) => tts.extend(streams),
177 }
178 }
179}
180
David Tolnay5c58c532018-08-13 11:33:51 -0700181impl Extend<TokenStream> for TokenStream {
182 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
183 match self {
184 TokenStream::Nightly(tts) => {
185 tts.extend(streams.into_iter().map(|stream| stream.unwrap_nightly()))
186 }
187 TokenStream::Stable(tts) => {
188 tts.extend(streams.into_iter().map(|stream| stream.unwrap_stable()))
189 }
190 }
191 }
192}
193
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700194impl fmt::Debug for TokenStream {
195 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700196 match self {
197 TokenStream::Nightly(tts) => tts.fmt(f),
198 TokenStream::Stable(tts) => tts.fmt(f),
199 }
200 }
201}
202
203impl From<proc_macro::LexError> for LexError {
204 fn from(e: proc_macro::LexError) -> LexError {
205 LexError::Nightly(e)
206 }
207}
208
209impl From<stable::LexError> for LexError {
210 fn from(e: stable::LexError) -> LexError {
211 LexError::Stable(e)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700212 }
213}
214
215impl fmt::Debug for LexError {
216 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700217 match self {
218 LexError::Nightly(e) => e.fmt(f),
219 LexError::Stable(e) => e.fmt(f),
220 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700221 }
222}
223
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700224pub enum TokenTreeIter {
225 Nightly(proc_macro::token_stream::IntoIter),
226 Stable(stable::TokenTreeIter),
227}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700228
229impl IntoIterator for TokenStream {
230 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700231 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700232
Alex Crichton1a7f7622017-07-05 17:47:15 -0700233 fn into_iter(self) -> TokenTreeIter {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700234 match self {
235 TokenStream::Nightly(tts) => TokenTreeIter::Nightly(tts.into_iter()),
236 TokenStream::Stable(tts) => TokenTreeIter::Stable(tts.into_iter()),
237 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700238 }
239}
240
Alex Crichton1a7f7622017-07-05 17:47:15 -0700241impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700242 type Item = TokenTree;
243
244 fn next(&mut self) -> Option<TokenTree> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700245 let token = match self {
246 TokenTreeIter::Nightly(iter) => iter.next()?,
247 TokenTreeIter::Stable(iter) => return iter.next(),
248 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700249 Some(match token {
250 proc_macro::TokenTree::Group(tt) => {
251 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700252 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
253 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
254 proc_macro::Delimiter::Brace => Delimiter::Brace,
255 proc_macro::Delimiter::None => Delimiter::None,
256 };
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700257 let stream = ::TokenStream::_new(TokenStream::Nightly(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700258 let mut g = Group::new(delim, stream);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700259 g.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700260 g.into()
261 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700262 proc_macro::TokenTree::Punct(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700263 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700264 proc_macro::Spacing::Joint => Spacing::Joint,
265 proc_macro::Spacing::Alone => Spacing::Alone,
266 };
Alex Crichtonf3888432018-05-16 09:11:05 -0700267 let mut o = Punct::new(tt.as_char(), spacing);
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700268 o.set_span(::Span::_new(Span::Nightly(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700269 o.into()
270 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700271 proc_macro::TokenTree::Ident(s) => ::Ident::_new(Ident::Nightly(s)).into(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700272 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal::Nightly(l)).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700273 })
274 }
275
276 fn size_hint(&self) -> (usize, Option<usize>) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700277 match self {
278 TokenTreeIter::Nightly(tts) => tts.size_hint(),
279 TokenTreeIter::Stable(tts) => tts.size_hint(),
280 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700281 }
282}
283
Alex Crichton1a7f7622017-07-05 17:47:15 -0700284impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700285 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700286 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700287 }
288}
289
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700290pub use stable::FileName;
Nika Layzellb35a9a32017-12-30 14:34:35 -0500291
292// NOTE: We have to generate our own filename object here because we can't wrap
293// the one provided by proc_macro.
294#[derive(Clone, PartialEq, Eq)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700295pub enum SourceFile {
296 Nightly(proc_macro::SourceFile, FileName),
297 Stable(stable::SourceFile),
298}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500299
300impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700301 fn nightly(sf: proc_macro::SourceFile) -> Self {
Alex Crichton9f0a28a2018-07-21 18:53:35 -0700302 let filename = stable::file_name(sf.path().display().to_string());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700303 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500304 }
305
Nika Layzellf8d5f212017-12-11 14:07:02 -0500306 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500307 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700308 match self {
309 SourceFile::Nightly(_, f) => f,
310 SourceFile::Stable(a) => a.path(),
311 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500312 }
313
314 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700315 match self {
316 SourceFile::Nightly(a, _) => a.is_real(),
317 SourceFile::Stable(a) => a.is_real(),
318 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500319 }
320}
321
Nika Layzellb35a9a32017-12-30 14:34:35 -0500322impl AsRef<FileName> for SourceFile {
323 fn as_ref(&self) -> &FileName {
324 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500325 }
326}
327
328impl fmt::Debug for SourceFile {
329 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700330 match self {
331 SourceFile::Nightly(a, _) => a.fmt(f),
332 SourceFile::Stable(a) => a.fmt(f),
333 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500334 }
335}
336
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500337pub struct LineColumn {
338 pub line: usize,
339 pub column: usize,
340}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500341
Alex Crichton9cd80a62018-04-05 17:46:58 -0700342#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700343pub enum Span {
344 Nightly(proc_macro::Span),
345 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800346}
347
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700348impl Span {
349 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700350 if nightly_works() {
351 Span::Nightly(proc_macro::Span::call_site())
352 } else {
353 Span::Stable(stable::Span::call_site())
354 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700355 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700356
Alex Crichtone6085b72017-11-21 07:24:25 -0800357 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700358 if nightly_works() {
359 Span::Nightly(proc_macro::Span::def_site())
360 } else {
361 Span::Stable(stable::Span::def_site())
362 }
Alex Crichton998f6422017-11-19 08:06:27 -0800363 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500364
David Tolnay4e8e3972018-01-05 18:10:22 -0800365 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700366 match (self, other) {
367 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
368 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
369 _ => mismatch(),
370 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800371 }
372
373 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700374 match (self, other) {
375 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
376 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
377 _ => mismatch(),
378 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800379 }
380
David Tolnay16a17202017-12-31 10:47:24 -0500381 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700382 match self {
383 Span::Nightly(s) => s,
384 Span::Stable(_) => mismatch(),
385 }
David Tolnay16a17202017-12-31 10:47:24 -0500386 }
387
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700388 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500389 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700390 match self {
391 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
392 Span::Stable(s) => SourceFile::Stable(s.source_file()),
393 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500394 }
395
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700396 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500397 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700398 match self {
399 Span::Nightly(s) => {
400 let proc_macro::LineColumn { line, column } = s.start();
401 LineColumn { line, column }
402 }
403 Span::Stable(s) => {
404 let stable::LineColumn { line, column } = s.start();
405 LineColumn { line, column }
406 }
407 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500408 }
409
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700410 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500411 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700412 match self {
413 Span::Nightly(s) => {
414 let proc_macro::LineColumn { line, column } = s.end();
415 LineColumn { line, column }
416 }
417 Span::Stable(s) => {
418 let stable::LineColumn { line, column } = s.end();
419 LineColumn { line, column }
420 }
421 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500422 }
423
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700424 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500425 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700426 let ret = match (self, other) {
427 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
428 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
429 _ => return None,
430 };
431 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500432 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700433
434 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700435 match (self, other) {
436 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
437 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
438 _ => false,
439 }
440 }
441
442 fn unwrap_nightly(self) -> proc_macro::Span {
443 match self {
444 Span::Nightly(s) => s,
445 Span::Stable(_) => mismatch(),
446 }
447 }
448}
449
450impl From<proc_macro::Span> for ::Span {
451 fn from(proc_span: proc_macro::Span) -> ::Span {
452 ::Span::_new(Span::Nightly(proc_span))
453 }
454}
455
456impl From<stable::Span> for Span {
457 fn from(inner: stable::Span) -> Span {
458 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700459 }
Alex Crichton998f6422017-11-19 08:06:27 -0800460}
461
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700462impl fmt::Debug for Span {
463 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700464 match self {
465 Span::Nightly(s) => s.fmt(f),
466 Span::Stable(s) => s.fmt(f),
467 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700468 }
469}
470
Alex Crichtonf3888432018-05-16 09:11:05 -0700471#[derive(Clone)]
472pub enum Ident {
473 Nightly(proc_macro::Ident),
474 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700475}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700476
Alex Crichtonf3888432018-05-16 09:11:05 -0700477impl Ident {
478 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700479 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700480 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
481 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700482 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700483 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700484
Alex Crichtonf3888432018-05-16 09:11:05 -0700485 pub fn new_raw(string: &str, span: Span) -> Ident {
486 match span {
487 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)),
488 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700489 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700490 }
491
492 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700493 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700494 Ident::Nightly(t) => Span::Nightly(t.span()),
495 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700496 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700497 }
498
499 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700500 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700501 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
502 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700503 _ => mismatch(),
504 }
505 }
506
Alex Crichtonf3888432018-05-16 09:11:05 -0700507 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700508 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700509 Ident::Nightly(s) => s,
510 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700511 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700512 }
513}
514
Alex Crichtonf3888432018-05-16 09:11:05 -0700515impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700516 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700517 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700518 Ident::Nightly(t) => t.fmt(f),
519 Ident::Stable(t) => t.fmt(f),
520 }
521 }
522}
523
524impl fmt::Debug for Ident {
525 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
526 match self {
527 Ident::Nightly(t) => t.fmt(f),
528 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700529 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700530 }
531}
532
533#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700534pub enum Literal {
535 Nightly(proc_macro::Literal),
536 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700537}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700538
Alex Crichtona914a612018-04-04 07:48:44 -0700539macro_rules! suffixed_numbers {
540 ($($name:ident => $kind:ident,)*) => ($(
541 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700542 if nightly_works() {
543 Literal::Nightly(proc_macro::Literal::$name(n))
544 } else {
545 Literal::Stable(stable::Literal::$name(n))
546 }
Alex Crichtona914a612018-04-04 07:48:44 -0700547 }
548 )*)
549}
550
551macro_rules! unsuffixed_integers {
552 ($($name:ident => $kind:ident,)*) => ($(
553 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700554 if nightly_works() {
555 Literal::Nightly(proc_macro::Literal::$name(n))
556 } else {
557 Literal::Stable(stable::Literal::$name(n))
558 }
Alex Crichtona914a612018-04-04 07:48:44 -0700559 }
560 )*)
561}
562
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700563impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700564 suffixed_numbers! {
565 u8_suffixed => u8,
566 u16_suffixed => u16,
567 u32_suffixed => u32,
568 u64_suffixed => u64,
569 usize_suffixed => usize,
570 i8_suffixed => i8,
571 i16_suffixed => i16,
572 i32_suffixed => i32,
573 i64_suffixed => i64,
574 isize_suffixed => isize,
575
576 f32_suffixed => f32,
577 f64_suffixed => f64,
578 }
579
580 unsuffixed_integers! {
581 u8_unsuffixed => u8,
582 u16_unsuffixed => u16,
583 u32_unsuffixed => u32,
584 u64_unsuffixed => u64,
585 usize_unsuffixed => usize,
586 i8_unsuffixed => i8,
587 i16_unsuffixed => i16,
588 i32_unsuffixed => i32,
589 i64_unsuffixed => i64,
590 isize_unsuffixed => isize,
591 }
592
593 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700594 if nightly_works() {
595 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
596 } else {
597 Literal::Stable(stable::Literal::f32_unsuffixed(f))
598 }
Alex Crichtona914a612018-04-04 07:48:44 -0700599 }
600
601 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700602 if nightly_works() {
603 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
604 } else {
605 Literal::Stable(stable::Literal::f64_unsuffixed(f))
606 }
Alex Crichtona914a612018-04-04 07:48:44 -0700607 }
608
Alex Crichtona914a612018-04-04 07:48:44 -0700609 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700610 if nightly_works() {
611 Literal::Nightly(proc_macro::Literal::string(t))
612 } else {
613 Literal::Stable(stable::Literal::string(t))
614 }
Alex Crichtona914a612018-04-04 07:48:44 -0700615 }
616
617 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700618 if nightly_works() {
619 Literal::Nightly(proc_macro::Literal::character(t))
620 } else {
621 Literal::Stable(stable::Literal::character(t))
622 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700623 }
624
625 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700626 if nightly_works() {
627 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
628 } else {
629 Literal::Stable(stable::Literal::byte_string(bytes))
630 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700631 }
632
Alex Crichtonb2c94622018-04-04 07:36:41 -0700633 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700634 match self {
635 Literal::Nightly(lit) => Span::Nightly(lit.span()),
636 Literal::Stable(lit) => Span::Stable(lit.span()),
637 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700638 }
639
Alex Crichtonb2c94622018-04-04 07:36:41 -0700640 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700641 match (self, span) {
642 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
643 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
644 _ => mismatch(),
645 }
646 }
647
648 fn unwrap_nightly(self) -> proc_macro::Literal {
649 match self {
650 Literal::Nightly(s) => s,
651 Literal::Stable(_) => mismatch(),
652 }
653 }
654}
655
656impl From<stable::Literal> for Literal {
657 fn from(s: stable::Literal) -> Literal {
658 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700659 }
660}
661
662impl fmt::Display for Literal {
663 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700664 match self {
665 Literal::Nightly(t) => t.fmt(f),
666 Literal::Stable(t) => t.fmt(f),
667 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700668 }
669}
670
671impl fmt::Debug for Literal {
672 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700673 match self {
674 Literal::Nightly(t) => t.fmt(f),
675 Literal::Stable(t) => t.fmt(f),
676 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700677 }
678}