blob: 85698aaeb17f16d0f58ad08d46c86e7bccaa4e11 [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 Crichtoncbec8ec2017-06-02 13:19:33 -07005use std::str::FromStr;
6
7use proc_macro;
8
David Tolnayb28f38a2018-03-31 22:02:29 +02009use {Delimiter, Group, Op, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070010
11#[derive(Clone)]
12pub struct TokenStream(proc_macro::TokenStream);
13
14pub struct LexError(proc_macro::LexError);
15
16impl TokenStream {
17 pub fn empty() -> TokenStream {
18 TokenStream(proc_macro::TokenStream::empty())
19 }
20
21 pub fn is_empty(&self) -> bool {
22 self.0.is_empty()
23 }
24}
25
26impl FromStr for TokenStream {
27 type Err = LexError;
28
29 fn from_str(src: &str) -> Result<TokenStream, LexError> {
30 Ok(TokenStream(src.parse().map_err(LexError)?))
31 }
32}
33
34impl fmt::Display for TokenStream {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 self.0.fmt(f)
37 }
38}
39
40impl From<proc_macro::TokenStream> for TokenStream {
41 fn from(inner: proc_macro::TokenStream) -> TokenStream {
42 TokenStream(inner)
43 }
44}
45
46impl From<TokenStream> for proc_macro::TokenStream {
47 fn from(inner: TokenStream) -> proc_macro::TokenStream {
48 inner.0
49 }
50}
51
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070052impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070053 fn from(token: TokenTree) -> TokenStream {
Alex Crichton9cd80a62018-04-05 17:46:58 -070054 let tt: proc_macro::TokenTree = match token {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070055 TokenTree::Group(tt) => {
56 let delim = match tt.delimiter() {
57 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
58 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
59 Delimiter::Brace => proc_macro::Delimiter::Brace,
60 Delimiter::None => proc_macro::Delimiter::None,
61 };
Alex Crichton9cd80a62018-04-05 17:46:58 -070062 let span = tt.span();
63 let mut group = proc_macro::Group::new(delim, tt.stream.inner.0);
64 group.set_span(span.inner.0);
65 group.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -070066 }
67 TokenTree::Op(tt) => {
Alex Crichton9cd80a62018-04-05 17:46:58 -070068 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070069 Spacing::Joint => proc_macro::Spacing::Joint,
70 Spacing::Alone => proc_macro::Spacing::Alone,
71 };
Alex Crichton9cd80a62018-04-05 17:46:58 -070072 let mut op = proc_macro::Op::new(tt.op(), spacing);
73 op.set_span(tt.span().inner.0);
74 op.into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -070075 }
Alex Crichton9cd80a62018-04-05 17:46:58 -070076 TokenTree::Term(tt) => tt.inner.term.into(),
77 TokenTree::Literal(tt) => tt.inner.lit.into(),
Alex Crichtonaf5bad42018-03-27 14:45:10 -070078 };
Alex Crichton9cd80a62018-04-05 17:46:58 -070079 TokenStream(tt.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070080 }
81}
82
Alex Crichtonaf5bad42018-03-27 14:45:10 -070083impl iter::FromIterator<TokenTree> for TokenStream {
David Tolnayb28f38a2018-03-31 22:02:29 +020084 fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
David Tolnay48ea5042018-04-23 19:17:35 -070085 let streams = streams.into_iter().map(TokenStream::from).flat_map(|t| t.0);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070086 TokenStream(streams.collect::<proc_macro::TokenStream>())
87 }
88}
89
90impl fmt::Debug for TokenStream {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070092 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070093 }
94}
95
96impl fmt::Debug for LexError {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070098 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070099 }
100}
101
Alex Crichton9cd80a62018-04-05 17:46:58 -0700102pub struct TokenTreeIter(proc_macro::token_stream::IntoIter);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700103
104impl IntoIterator for TokenStream {
105 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700106 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700107
Alex Crichton1a7f7622017-07-05 17:47:15 -0700108 fn into_iter(self) -> TokenTreeIter {
109 TokenTreeIter(self.0.into_iter())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700110 }
111}
112
Alex Crichton1a7f7622017-07-05 17:47:15 -0700113impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700114 type Item = TokenTree;
115
116 fn next(&mut self) -> Option<TokenTree> {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700117 let token = self.0.next()?;
Alex Crichton9cd80a62018-04-05 17:46:58 -0700118 Some(match token {
119 proc_macro::TokenTree::Group(tt) => {
120 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700121 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
122 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
123 proc_macro::Delimiter::Brace => Delimiter::Brace,
124 proc_macro::Delimiter::None => Delimiter::None,
125 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700126 let stream = ::TokenStream::_new(TokenStream(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700127 let mut g = Group::new(delim, stream);
Alex Crichton9cd80a62018-04-05 17:46:58 -0700128 g.set_span(::Span::_new(Span(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700129 g.into()
130 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700131 proc_macro::TokenTree::Op(tt) => {
132 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700133 proc_macro::Spacing::Joint => Spacing::Joint,
134 proc_macro::Spacing::Alone => Spacing::Alone,
135 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700136 let mut o = Op::new(tt.op(), spacing);
137 o.set_span(::Span::_new(Span(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700138 o.into()
139 }
David Tolnay48ea5042018-04-23 19:17:35 -0700140 proc_macro::TokenTree::Term(s) => ::Term::_new(Term { term: s }).into(),
141 proc_macro::TokenTree::Literal(l) => ::Literal::_new(Literal { lit: l }).into(),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700142 })
143 }
144
145 fn size_hint(&self) -> (usize, Option<usize>) {
146 self.0.size_hint()
147 }
148}
149
Alex Crichton1a7f7622017-07-05 17:47:15 -0700150impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700151 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700152 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700153 }
154}
155
Nika Layzellf8d5f212017-12-11 14:07:02 -0500156#[derive(Clone, PartialEq, Eq)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500157pub struct FileName(String);
158
159impl fmt::Display for FileName {
160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161 self.0.fmt(f)
162 }
163}
164
165// NOTE: We have to generate our own filename object here because we can't wrap
166// the one provided by proc_macro.
167#[derive(Clone, PartialEq, Eq)]
168pub struct SourceFile(proc_macro::SourceFile, FileName);
Nika Layzellf8d5f212017-12-11 14:07:02 -0500169
170impl SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500171 fn new(sf: proc_macro::SourceFile) -> Self {
172 let filename = FileName(sf.path().to_string());
173 SourceFile(sf, filename)
174 }
175
Nika Layzellf8d5f212017-12-11 14:07:02 -0500176 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500177 pub fn path(&self) -> &FileName {
178 &self.1
Nika Layzellf8d5f212017-12-11 14:07:02 -0500179 }
180
181 pub fn is_real(&self) -> bool {
182 self.0.is_real()
183 }
184}
185
Nika Layzellb35a9a32017-12-30 14:34:35 -0500186impl AsRef<FileName> for SourceFile {
187 fn as_ref(&self) -> &FileName {
188 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500189 }
190}
191
192impl fmt::Debug for SourceFile {
193 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
194 self.0.fmt(f)
195 }
196}
197
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500198pub struct LineColumn {
199 pub line: usize,
200 pub column: usize,
201}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500202
Alex Crichton9cd80a62018-04-05 17:46:58 -0700203#[derive(Copy, Clone)]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700204pub struct Span(proc_macro::Span);
205
Sergio Benitez13805082018-01-04 01:25:45 -0800206impl From<proc_macro::Span> for ::Span {
207 fn from(proc_span: proc_macro::Span) -> ::Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700208 ::Span::_new(Span(proc_span))
Sergio Benitez13805082018-01-04 01:25:45 -0800209 }
210}
211
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700212impl Span {
213 pub fn call_site() -> Span {
214 Span(proc_macro::Span::call_site())
215 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700216
Alex Crichtone6085b72017-11-21 07:24:25 -0800217 pub fn def_site() -> Span {
Alex Crichton998f6422017-11-19 08:06:27 -0800218 Span(proc_macro::Span::def_site())
219 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500220
David Tolnay4e8e3972018-01-05 18:10:22 -0800221 pub fn resolved_at(&self, other: Span) -> Span {
222 Span(self.0.resolved_at(other.0))
223 }
224
225 pub fn located_at(&self, other: Span) -> Span {
226 Span(self.0.located_at(other.0))
227 }
228
David Tolnay16a17202017-12-31 10:47:24 -0500229 pub fn unstable(self) -> proc_macro::Span {
230 self.0
231 }
232
Nika Layzellf8d5f212017-12-11 14:07:02 -0500233 pub fn source_file(&self) -> SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500234 SourceFile::new(self.0.source_file())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500235 }
236
237 pub fn start(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200238 let proc_macro::LineColumn { line, column } = self.0.start();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500239 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500240 }
241
242 pub fn end(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200243 let proc_macro::LineColumn { line, column } = self.0.end();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500244 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500245 }
246
247 pub fn join(&self, other: Span) -> Option<Span> {
248 self.0.join(other.0).map(Span)
249 }
Alex Crichtone24f7342018-04-05 17:58:11 -0700250
251 pub fn eq(&self, other: &Span) -> bool {
252 self.0.eq(&other.0)
253 }
Alex Crichton998f6422017-11-19 08:06:27 -0800254}
255
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700256impl fmt::Debug for Span {
257 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700258 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700259 }
260}
261
262#[derive(Copy, Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700263pub struct Term {
264 term: proc_macro::Term,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700265}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700266
David Tolnay10effeb2018-01-06 11:07:49 -0800267impl Term {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700268 pub fn new(string: &str, span: Span) -> Term {
269 Term {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700270 term: proc_macro::Term::new(string, span.0),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700271 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700272 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700273
David Tolnay10effeb2018-01-06 11:07:49 -0800274 pub fn as_str(&self) -> &str {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700275 self.term.as_str()
276 }
277
278 pub fn span(&self) -> Span {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700279 Span(self.term.span())
Alex Crichtonb2c94622018-04-04 07:36:41 -0700280 }
281
282 pub fn set_span(&mut self, span: Span) {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700283 self.term.set_span(span.0);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700284 }
285}
286
Alex Crichton1a7f7622017-07-05 17:47:15 -0700287impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700288 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700289 self.term.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700290 }
291}
292
293#[derive(Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700294pub struct Literal {
295 lit: proc_macro::Literal,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700296}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700297
Alex Crichtona914a612018-04-04 07:48:44 -0700298macro_rules! suffixed_numbers {
299 ($($name:ident => $kind:ident,)*) => ($(
300 pub fn $name(n: $kind) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700301 Literal::_new(proc_macro::Literal::$name(n))
Alex Crichtona914a612018-04-04 07:48:44 -0700302 }
303 )*)
304}
305
306macro_rules! unsuffixed_integers {
307 ($($name:ident => $kind:ident,)*) => ($(
308 pub fn $name(n: $kind) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700309 Literal::_new(proc_macro::Literal::$name(n))
Alex Crichtona914a612018-04-04 07:48:44 -0700310 }
311 )*)
312}
313
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700314impl Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700315 fn _new(lit: proc_macro::Literal) -> Literal {
David Tolnay48ea5042018-04-23 19:17:35 -0700316 Literal { lit }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700317 }
318
Alex Crichtona914a612018-04-04 07:48:44 -0700319 suffixed_numbers! {
320 u8_suffixed => u8,
321 u16_suffixed => u16,
322 u32_suffixed => u32,
323 u64_suffixed => u64,
324 usize_suffixed => usize,
325 i8_suffixed => i8,
326 i16_suffixed => i16,
327 i32_suffixed => i32,
328 i64_suffixed => i64,
329 isize_suffixed => isize,
330
331 f32_suffixed => f32,
332 f64_suffixed => f64,
333 }
334
335 unsuffixed_integers! {
336 u8_unsuffixed => u8,
337 u16_unsuffixed => u16,
338 u32_unsuffixed => u32,
339 u64_unsuffixed => u64,
340 usize_unsuffixed => usize,
341 i8_unsuffixed => i8,
342 i16_unsuffixed => i16,
343 i32_unsuffixed => i32,
344 i64_unsuffixed => i64,
345 isize_unsuffixed => isize,
346 }
347
348 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700349 Literal::_new(proc_macro::Literal::f32_unsuffixed(f))
Alex Crichtona914a612018-04-04 07:48:44 -0700350 }
351
352 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700353 Literal::_new(proc_macro::Literal::f64_unsuffixed(f))
Alex Crichtona914a612018-04-04 07:48:44 -0700354 }
355
Alex Crichtona914a612018-04-04 07:48:44 -0700356 pub fn string(t: &str) -> Literal {
357 Literal::_new(proc_macro::Literal::string(t))
358 }
359
360 pub fn character(t: char) -> Literal {
361 Literal::_new(proc_macro::Literal::character(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700362 }
363
364 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700365 Literal::_new(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700366 }
367
Alex Crichtonb2c94622018-04-04 07:36:41 -0700368 pub fn span(&self) -> Span {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700369 Span(self.lit.span())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700370 }
371
Alex Crichtonb2c94622018-04-04 07:36:41 -0700372 pub fn set_span(&mut self, span: Span) {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700373 self.lit.set_span(span.0);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700374 }
375}
376
377impl fmt::Display for Literal {
378 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700379 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700380 }
381}
382
383impl fmt::Debug for Literal {
384 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700385 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700386 }
387}