blob: e62c86a85744130cf086f2f3d79c844f071538ff [file] [log] [blame]
Alex Crichtonce0904d2018-08-27 17:29:49 -07001#![cfg_attr(not(super_unstable), 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 Crichtonce0904d2018-08-27 17:29:49 -0700295#[cfg(super_unstable)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700296pub enum SourceFile {
297 Nightly(proc_macro::SourceFile, FileName),
298 Stable(stable::SourceFile),
299}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500300
Alex Crichtonce0904d2018-08-27 17:29:49 -0700301#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500302impl SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700303 fn nightly(sf: proc_macro::SourceFile) -> Self {
Alex Crichton9f0a28a2018-07-21 18:53:35 -0700304 let filename = stable::file_name(sf.path().display().to_string());
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700305 SourceFile::Nightly(sf, filename)
Nika Layzellb35a9a32017-12-30 14:34:35 -0500306 }
307
Nika Layzellf8d5f212017-12-11 14:07:02 -0500308 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500309 pub fn path(&self) -> &FileName {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700310 match self {
311 SourceFile::Nightly(_, f) => f,
312 SourceFile::Stable(a) => a.path(),
313 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500314 }
315
316 pub fn is_real(&self) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700317 match self {
318 SourceFile::Nightly(a, _) => a.is_real(),
319 SourceFile::Stable(a) => a.is_real(),
320 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500321 }
322}
323
Alex Crichtonce0904d2018-08-27 17:29:49 -0700324#[cfg(super_unstable)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500325impl AsRef<FileName> for SourceFile {
326 fn as_ref(&self) -> &FileName {
327 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500328 }
329}
330
Alex Crichtonce0904d2018-08-27 17:29:49 -0700331#[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500332impl fmt::Debug for SourceFile {
333 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700334 match self {
335 SourceFile::Nightly(a, _) => a.fmt(f),
336 SourceFile::Stable(a) => a.fmt(f),
337 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500338 }
339}
340
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500341pub struct LineColumn {
342 pub line: usize,
343 pub column: usize,
344}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500345
Alex Crichton9cd80a62018-04-05 17:46:58 -0700346#[derive(Copy, Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700347pub enum Span {
348 Nightly(proc_macro::Span),
349 Stable(stable::Span),
Sergio Benitez13805082018-01-04 01:25:45 -0800350}
351
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700352impl Span {
353 pub fn call_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700354 if nightly_works() {
355 Span::Nightly(proc_macro::Span::call_site())
356 } else {
357 Span::Stable(stable::Span::call_site())
358 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700359 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700360
Alex Crichtonce0904d2018-08-27 17:29:49 -0700361 #[cfg(super_unstable)]
Alex Crichtone6085b72017-11-21 07:24:25 -0800362 pub fn def_site() -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700363 if nightly_works() {
364 Span::Nightly(proc_macro::Span::def_site())
365 } else {
366 Span::Stable(stable::Span::def_site())
367 }
Alex Crichton998f6422017-11-19 08:06:27 -0800368 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500369
Alex Crichtonce0904d2018-08-27 17:29:49 -0700370 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800371 pub fn resolved_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700372 match (self, other) {
373 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
374 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.resolved_at(b)),
375 _ => mismatch(),
376 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800377 }
378
Alex Crichtonce0904d2018-08-27 17:29:49 -0700379 #[cfg(super_unstable)]
David Tolnay4e8e3972018-01-05 18:10:22 -0800380 pub fn located_at(&self, other: Span) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700381 match (self, other) {
382 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
383 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.located_at(b)),
384 _ => mismatch(),
385 }
David Tolnay4e8e3972018-01-05 18:10:22 -0800386 }
387
David Tolnay16a17202017-12-31 10:47:24 -0500388 pub fn unstable(self) -> proc_macro::Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700389 match self {
390 Span::Nightly(s) => s,
391 Span::Stable(_) => mismatch(),
392 }
David Tolnay16a17202017-12-31 10:47:24 -0500393 }
394
Alex Crichtonce0904d2018-08-27 17:29:49 -0700395 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500396 pub fn source_file(&self) -> SourceFile {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700397 match self {
398 Span::Nightly(s) => SourceFile::nightly(s.source_file()),
399 Span::Stable(s) => SourceFile::Stable(s.source_file()),
400 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500401 }
402
Alex Crichtonce0904d2018-08-27 17:29:49 -0700403 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500404 pub fn start(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700405 match self {
406 Span::Nightly(s) => {
407 let proc_macro::LineColumn { line, column } = s.start();
408 LineColumn { line, column }
409 }
410 Span::Stable(s) => {
411 let stable::LineColumn { line, column } = s.start();
412 LineColumn { line, column }
413 }
414 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500415 }
416
Alex Crichtonce0904d2018-08-27 17:29:49 -0700417 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500418 pub fn end(&self) -> LineColumn {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700419 match self {
420 Span::Nightly(s) => {
421 let proc_macro::LineColumn { line, column } = s.end();
422 LineColumn { line, column }
423 }
424 Span::Stable(s) => {
425 let stable::LineColumn { line, column } = s.end();
426 LineColumn { line, column }
427 }
428 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500429 }
430
Alex Crichtonce0904d2018-08-27 17:29:49 -0700431 #[cfg(super_unstable)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500432 pub fn join(&self, other: Span) -> Option<Span> {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700433 let ret = match (self, other) {
434 (Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
435 (Span::Stable(a), Span::Stable(b)) => Span::Stable(a.join(b)?),
436 _ => return None,
437 };
438 Some(ret)
Nika Layzellf8d5f212017-12-11 14:07:02 -0500439 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700440
Alex Crichtonce0904d2018-08-27 17:29:49 -0700441 #[cfg(super_unstable)]
Alex Crichtone24f7342018-04-05 17:58:11 -0700442 pub fn eq(&self, other: &Span) -> bool {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700443 match (self, other) {
444 (Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
445 (Span::Stable(a), Span::Stable(b)) => a.eq(b),
446 _ => false,
447 }
448 }
449
450 fn unwrap_nightly(self) -> proc_macro::Span {
451 match self {
452 Span::Nightly(s) => s,
453 Span::Stable(_) => mismatch(),
454 }
455 }
456}
457
458impl From<proc_macro::Span> for ::Span {
459 fn from(proc_span: proc_macro::Span) -> ::Span {
460 ::Span::_new(Span::Nightly(proc_span))
461 }
462}
463
464impl From<stable::Span> for Span {
465 fn from(inner: stable::Span) -> Span {
466 Span::Stable(inner)
Alex Crichtone24f7342018-04-05 17:58:11 -0700467 }
Alex Crichton998f6422017-11-19 08:06:27 -0800468}
469
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700470impl fmt::Debug for Span {
471 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700472 match self {
473 Span::Nightly(s) => s.fmt(f),
474 Span::Stable(s) => s.fmt(f),
475 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700476 }
477}
478
Alex Crichtonf3888432018-05-16 09:11:05 -0700479#[derive(Clone)]
480pub enum Ident {
481 Nightly(proc_macro::Ident),
482 Stable(stable::Ident),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700483}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700484
Alex Crichtonf3888432018-05-16 09:11:05 -0700485impl Ident {
486 pub fn new(string: &str, span: Span) -> Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700487 match span {
Alex Crichtonf3888432018-05-16 09:11:05 -0700488 Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new(string, s)),
489 Span::Stable(s) => Ident::Stable(stable::Ident::new(string, s)),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700490 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700491 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700492
Alex Crichtonf3888432018-05-16 09:11:05 -0700493 pub fn new_raw(string: &str, span: Span) -> Ident {
494 match span {
Alex Crichtonce0904d2018-08-27 17:29:49 -0700495 Span::Nightly(s) => {
496 let p: proc_macro::TokenStream = string.parse().unwrap();
497 let ident = match p.into_iter().next() {
498 Some(proc_macro::TokenTree::Ident(mut i)) => {
499 i.set_span(s);
500 i
501 }
502 _ => panic!(),
503 };
504 Ident::Nightly(ident)
505 }
Alex Crichtonf3888432018-05-16 09:11:05 -0700506 Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700507 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700508 }
509
510 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700511 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700512 Ident::Nightly(t) => Span::Nightly(t.span()),
513 Ident::Stable(t) => Span::Stable(t.span()),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700514 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700515 }
516
517 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700518 match (self, span) {
Alex Crichtonf3888432018-05-16 09:11:05 -0700519 (Ident::Nightly(t), Span::Nightly(s)) => t.set_span(s),
520 (Ident::Stable(t), Span::Stable(s)) => t.set_span(s),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700521 _ => mismatch(),
522 }
523 }
524
Alex Crichtonf3888432018-05-16 09:11:05 -0700525 fn unwrap_nightly(self) -> proc_macro::Ident {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700526 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700527 Ident::Nightly(s) => s,
528 Ident::Stable(_) => mismatch(),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700529 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700530 }
531}
532
Alex Crichtonf3888432018-05-16 09:11:05 -0700533impl fmt::Display for Ident {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700534 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700535 match self {
Alex Crichtonf3888432018-05-16 09:11:05 -0700536 Ident::Nightly(t) => t.fmt(f),
537 Ident::Stable(t) => t.fmt(f),
538 }
539 }
540}
541
542impl fmt::Debug for Ident {
543 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
544 match self {
545 Ident::Nightly(t) => t.fmt(f),
546 Ident::Stable(t) => t.fmt(f),
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700547 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700548 }
549}
550
551#[derive(Clone)]
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700552pub enum Literal {
553 Nightly(proc_macro::Literal),
554 Stable(stable::Literal),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700555}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700556
Alex Crichtona914a612018-04-04 07:48:44 -0700557macro_rules! suffixed_numbers {
558 ($($name:ident => $kind:ident,)*) => ($(
559 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700560 if nightly_works() {
561 Literal::Nightly(proc_macro::Literal::$name(n))
562 } else {
563 Literal::Stable(stable::Literal::$name(n))
564 }
Alex Crichtona914a612018-04-04 07:48:44 -0700565 }
566 )*)
567}
568
569macro_rules! unsuffixed_integers {
570 ($($name:ident => $kind:ident,)*) => ($(
571 pub fn $name(n: $kind) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700572 if nightly_works() {
573 Literal::Nightly(proc_macro::Literal::$name(n))
574 } else {
575 Literal::Stable(stable::Literal::$name(n))
576 }
Alex Crichtona914a612018-04-04 07:48:44 -0700577 }
578 )*)
579}
580
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700581impl Literal {
Alex Crichtona914a612018-04-04 07:48:44 -0700582 suffixed_numbers! {
583 u8_suffixed => u8,
584 u16_suffixed => u16,
585 u32_suffixed => u32,
586 u64_suffixed => u64,
587 usize_suffixed => usize,
588 i8_suffixed => i8,
589 i16_suffixed => i16,
590 i32_suffixed => i32,
591 i64_suffixed => i64,
592 isize_suffixed => isize,
593
594 f32_suffixed => f32,
595 f64_suffixed => f64,
596 }
597
598 unsuffixed_integers! {
599 u8_unsuffixed => u8,
600 u16_unsuffixed => u16,
601 u32_unsuffixed => u32,
602 u64_unsuffixed => u64,
603 usize_unsuffixed => usize,
604 i8_unsuffixed => i8,
605 i16_unsuffixed => i16,
606 i32_unsuffixed => i32,
607 i64_unsuffixed => i64,
608 isize_unsuffixed => isize,
609 }
610
611 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700612 if nightly_works() {
613 Literal::Nightly(proc_macro::Literal::f32_unsuffixed(f))
614 } else {
615 Literal::Stable(stable::Literal::f32_unsuffixed(f))
616 }
Alex Crichtona914a612018-04-04 07:48:44 -0700617 }
618
619 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700620 if nightly_works() {
621 Literal::Nightly(proc_macro::Literal::f64_unsuffixed(f))
622 } else {
623 Literal::Stable(stable::Literal::f64_unsuffixed(f))
624 }
Alex Crichtona914a612018-04-04 07:48:44 -0700625 }
626
Alex Crichtona914a612018-04-04 07:48:44 -0700627 pub fn string(t: &str) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700628 if nightly_works() {
629 Literal::Nightly(proc_macro::Literal::string(t))
630 } else {
631 Literal::Stable(stable::Literal::string(t))
632 }
Alex Crichtona914a612018-04-04 07:48:44 -0700633 }
634
635 pub fn character(t: char) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700636 if nightly_works() {
637 Literal::Nightly(proc_macro::Literal::character(t))
638 } else {
639 Literal::Stable(stable::Literal::character(t))
640 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700641 }
642
643 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700644 if nightly_works() {
645 Literal::Nightly(proc_macro::Literal::byte_string(bytes))
646 } else {
647 Literal::Stable(stable::Literal::byte_string(bytes))
648 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700649 }
650
Alex Crichtonb2c94622018-04-04 07:36:41 -0700651 pub fn span(&self) -> Span {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700652 match self {
653 Literal::Nightly(lit) => Span::Nightly(lit.span()),
654 Literal::Stable(lit) => Span::Stable(lit.span()),
655 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700656 }
657
Alex Crichtonb2c94622018-04-04 07:36:41 -0700658 pub fn set_span(&mut self, span: Span) {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700659 match (self, span) {
660 (Literal::Nightly(lit), Span::Nightly(s)) => lit.set_span(s),
661 (Literal::Stable(lit), Span::Stable(s)) => lit.set_span(s),
662 _ => mismatch(),
663 }
664 }
665
666 fn unwrap_nightly(self) -> proc_macro::Literal {
667 match self {
668 Literal::Nightly(s) => s,
669 Literal::Stable(_) => mismatch(),
670 }
671 }
672}
673
674impl From<stable::Literal> for Literal {
675 fn from(s: stable::Literal) -> Literal {
676 Literal::Stable(s)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700677 }
678}
679
680impl fmt::Display for Literal {
681 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700682 match self {
683 Literal::Nightly(t) => t.fmt(f),
684 Literal::Stable(t) => t.fmt(f),
685 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700686 }
687}
688
689impl fmt::Debug for Literal {
690 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton30a4e9e2018-04-27 17:02:19 -0700691 match self {
692 Literal::Nightly(t) => t.fmt(f),
693 Literal::Stable(t) => t.fmt(f),
694 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700695 }
696}