blob: 55c72ba084baf30816da20bafa94c164462bf60c [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 {
54 let (span, kind) = match token {
55 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 Crichtonb2c94622018-04-04 07:36:41 -070062 let span = tt.span().inner;
Alex Crichtonaf5bad42018-03-27 14:45:10 -070063 let group = proc_macro::TokenNode::Group(delim, tt.stream.inner.0);
64 (span, group)
65 }
66 TokenTree::Op(tt) => {
67 let kind = match tt.spacing() {
68 Spacing::Joint => proc_macro::Spacing::Joint,
69 Spacing::Alone => proc_macro::Spacing::Alone,
70 };
Alex Crichtonb2c94622018-04-04 07:36:41 -070071 (tt.span().inner, proc_macro::TokenNode::Op(tt.op(), kind))
Alex Crichtonaf5bad42018-03-27 14:45:10 -070072 }
Alex Crichtonb2c94622018-04-04 07:36:41 -070073 TokenTree::Term(tt) => (tt.inner.span, proc_macro::TokenNode::Term(tt.inner.term)),
74 TokenTree::Literal(tt) => (tt.inner.span, proc_macro::TokenNode::Literal(tt.inner.lit)),
Alex Crichtonaf5bad42018-03-27 14:45:10 -070075 };
David Tolnayb28f38a2018-03-31 22:02:29 +020076 TokenStream(
77 proc_macro::TokenTree {
Alex Crichtonb2c94622018-04-04 07:36:41 -070078 span: span.0,
David Tolnayb28f38a2018-03-31 22:02:29 +020079 kind,
80 }.into(),
81 )
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070082 }
83}
84
Alex Crichtonaf5bad42018-03-27 14:45:10 -070085impl iter::FromIterator<TokenTree> for TokenStream {
David Tolnayb28f38a2018-03-31 22:02:29 +020086 fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070087 let streams = streams.into_iter().map(TokenStream::from);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070088 TokenStream(streams.collect::<proc_macro::TokenStream>())
89 }
90}
91
92impl fmt::Debug for TokenStream {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070094 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070095 }
96}
97
98impl fmt::Debug for LexError {
99 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700100 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700101 }
102}
103
Alex Crichton1a7f7622017-07-05 17:47:15 -0700104pub struct TokenTreeIter(proc_macro::TokenTreeIter);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700105
106impl IntoIterator for TokenStream {
107 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700108 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700109
Alex Crichton1a7f7622017-07-05 17:47:15 -0700110 fn into_iter(self) -> TokenTreeIter {
111 TokenTreeIter(self.0.into_iter())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700112 }
113}
114
Alex Crichton1a7f7622017-07-05 17:47:15 -0700115impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700116 type Item = TokenTree;
117
118 fn next(&mut self) -> Option<TokenTree> {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700119 let token = self.0.next()?;
120 let span = ::Span::_new(Span(token.span));
121 Some(match token.kind {
122 proc_macro::TokenNode::Group(delim, s) => {
123 let delim = match delim {
124 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
125 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
126 proc_macro::Delimiter::Brace => Delimiter::Brace,
127 proc_macro::Delimiter::None => Delimiter::None,
128 };
129 let stream = ::TokenStream::_new(TokenStream(s));
130 let mut g = Group::new(delim, stream);
131 g.set_span(span);
132 g.into()
133 }
134 proc_macro::TokenNode::Op(ch, kind) => {
135 let kind = match kind {
136 proc_macro::Spacing::Joint => Spacing::Joint,
137 proc_macro::Spacing::Alone => Spacing::Alone,
138 };
139 let mut o = Op::new(ch, kind);
140 o.span = span;
141 o.into()
142 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700143 proc_macro::TokenNode::Term(s) => {
144 ::Term::_new(Term {
145 term: s,
146 span: span.inner,
147 }).into()
148 }
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700149 proc_macro::TokenNode::Literal(l) => {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700150 ::Literal::_new(Literal {
151 lit: l,
152 span: span.inner,
153 }).into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700154 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700155 })
156 }
157
158 fn size_hint(&self) -> (usize, Option<usize>) {
159 self.0.size_hint()
160 }
161}
162
Alex Crichton1a7f7622017-07-05 17:47:15 -0700163impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700164 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700165 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700166 }
167}
168
Nika Layzellf8d5f212017-12-11 14:07:02 -0500169#[derive(Clone, PartialEq, Eq)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500170pub struct FileName(String);
171
172impl fmt::Display for FileName {
173 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174 self.0.fmt(f)
175 }
176}
177
178// NOTE: We have to generate our own filename object here because we can't wrap
179// the one provided by proc_macro.
180#[derive(Clone, PartialEq, Eq)]
181pub struct SourceFile(proc_macro::SourceFile, FileName);
Nika Layzellf8d5f212017-12-11 14:07:02 -0500182
183impl SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500184 fn new(sf: proc_macro::SourceFile) -> Self {
185 let filename = FileName(sf.path().to_string());
186 SourceFile(sf, filename)
187 }
188
Nika Layzellf8d5f212017-12-11 14:07:02 -0500189 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500190 pub fn path(&self) -> &FileName {
191 &self.1
Nika Layzellf8d5f212017-12-11 14:07:02 -0500192 }
193
194 pub fn is_real(&self) -> bool {
195 self.0.is_real()
196 }
197}
198
Nika Layzellb35a9a32017-12-30 14:34:35 -0500199impl AsRef<FileName> for SourceFile {
200 fn as_ref(&self) -> &FileName {
201 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500202 }
203}
204
205impl fmt::Debug for SourceFile {
206 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
207 self.0.fmt(f)
208 }
209}
210
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500211pub struct LineColumn {
212 pub line: usize,
213 pub column: usize,
214}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500215
Nika Layzell99737982018-03-11 18:51:27 -0400216#[derive(Copy, Clone, PartialEq, Eq)]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700217pub struct Span(proc_macro::Span);
218
Sergio Benitez13805082018-01-04 01:25:45 -0800219impl From<proc_macro::Span> for ::Span {
220 fn from(proc_span: proc_macro::Span) -> ::Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700221 ::Span::_new(Span(proc_span))
Sergio Benitez13805082018-01-04 01:25:45 -0800222 }
223}
224
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700225impl Span {
226 pub fn call_site() -> Span {
227 Span(proc_macro::Span::call_site())
228 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700229
Alex Crichtone6085b72017-11-21 07:24:25 -0800230 pub fn def_site() -> Span {
Alex Crichton998f6422017-11-19 08:06:27 -0800231 Span(proc_macro::Span::def_site())
232 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500233
David Tolnay4e8e3972018-01-05 18:10:22 -0800234 pub fn resolved_at(&self, other: Span) -> Span {
235 Span(self.0.resolved_at(other.0))
236 }
237
238 pub fn located_at(&self, other: Span) -> Span {
239 Span(self.0.located_at(other.0))
240 }
241
David Tolnay16a17202017-12-31 10:47:24 -0500242 pub fn unstable(self) -> proc_macro::Span {
243 self.0
244 }
245
Nika Layzellf8d5f212017-12-11 14:07:02 -0500246 pub fn source_file(&self) -> SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500247 SourceFile::new(self.0.source_file())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500248 }
249
250 pub fn start(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200251 let proc_macro::LineColumn { line, column } = self.0.start();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500252 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500253 }
254
255 pub fn end(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200256 let proc_macro::LineColumn { line, column } = self.0.end();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500257 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500258 }
259
260 pub fn join(&self, other: Span) -> Option<Span> {
261 self.0.join(other.0).map(Span)
262 }
Alex Crichton998f6422017-11-19 08:06:27 -0800263}
264
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700265impl fmt::Debug for Span {
266 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700267 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700268 }
269}
270
271#[derive(Copy, Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700272pub struct Term {
273 term: proc_macro::Term,
274 span: Span,
275}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700276
David Tolnay10effeb2018-01-06 11:07:49 -0800277impl Term {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700278 pub fn new(string: &str, span: Span) -> Term {
279 Term {
280 term: proc_macro::Term::intern(string),
281 span,
282 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700283 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700284
David Tolnay10effeb2018-01-06 11:07:49 -0800285 pub fn as_str(&self) -> &str {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700286 self.term.as_str()
287 }
288
289 pub fn span(&self) -> Span {
290 self.span
291 }
292
293 pub fn set_span(&mut self, span: Span) {
294 self.span = span;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700295 }
296}
297
Alex Crichton1a7f7622017-07-05 17:47:15 -0700298impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700299 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700300 self.term.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700301 }
302}
303
304#[derive(Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700305pub struct Literal {
306 lit: proc_macro::Literal,
307 span: Span,
308}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700309
Alex Crichtona914a612018-04-04 07:48:44 -0700310macro_rules! suffixed_numbers {
311 ($($name:ident => $kind:ident,)*) => ($(
312 pub fn $name(n: $kind) -> Literal {
313 Literal::_new(proc_macro::Literal::$kind(n))
314 }
315 )*)
316}
317
318macro_rules! unsuffixed_integers {
319 ($($name:ident => $kind:ident,)*) => ($(
320 pub fn $name(n: $kind) -> Literal {
321 Literal::_new(proc_macro::Literal::integer(n as i128))
322 }
323 )*)
324}
325
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700326impl Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700327 fn _new(lit: proc_macro::Literal) -> Literal {
328 Literal {
329 lit,
330 span: Span::call_site(),
331 }
332 }
333
Alex Crichtona914a612018-04-04 07:48:44 -0700334 suffixed_numbers! {
335 u8_suffixed => u8,
336 u16_suffixed => u16,
337 u32_suffixed => u32,
338 u64_suffixed => u64,
339 usize_suffixed => usize,
340 i8_suffixed => i8,
341 i16_suffixed => i16,
342 i32_suffixed => i32,
343 i64_suffixed => i64,
344 isize_suffixed => isize,
345
346 f32_suffixed => f32,
347 f64_suffixed => f64,
348 }
349
350 unsuffixed_integers! {
351 u8_unsuffixed => u8,
352 u16_unsuffixed => u16,
353 u32_unsuffixed => u32,
354 u64_unsuffixed => u64,
355 usize_unsuffixed => usize,
356 i8_unsuffixed => i8,
357 i16_unsuffixed => i16,
358 i32_unsuffixed => i32,
359 i64_unsuffixed => i64,
360 isize_unsuffixed => isize,
361 }
362
363 pub fn f32_unsuffixed(f: f32) -> Literal {
364 Literal::f64_unsuffixed(f.into())
365 }
366
367 pub fn f64_unsuffixed(f: f64) -> Literal {
368 Literal::_new(proc_macro::Literal::float(f))
369 }
370
371
372 pub fn string(t: &str) -> Literal {
373 Literal::_new(proc_macro::Literal::string(t))
374 }
375
376 pub fn character(t: char) -> Literal {
377 Literal::_new(proc_macro::Literal::character(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700378 }
379
380 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700381 Literal::_new(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700382 }
383
Alex Crichtonb2c94622018-04-04 07:36:41 -0700384 pub fn span(&self) -> Span {
385 self.span
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700386 }
387
Alex Crichtonb2c94622018-04-04 07:36:41 -0700388 pub fn set_span(&mut self, span: Span) {
389 self.span = span;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700390 }
391}
392
393impl fmt::Display for Literal {
394 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700395 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700396 }
397}
398
399impl fmt::Debug for Literal {
400 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700401 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700402 }
403}