blob: 6754a8583839969a677080ef46c1e161bb01c158 [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 {
Alex Crichton9cd80a62018-04-05 17:46:58 -070085 let streams = streams.into_iter().map(TokenStream::from)
86 .flat_map(|t| t.0);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070087 TokenStream(streams.collect::<proc_macro::TokenStream>())
88 }
89}
90
91impl fmt::Debug for TokenStream {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070093 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070094 }
95}
96
97impl fmt::Debug for LexError {
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070099 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700100 }
101}
102
Alex Crichton9cd80a62018-04-05 17:46:58 -0700103pub struct TokenTreeIter(proc_macro::token_stream::IntoIter);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700104
105impl IntoIterator for TokenStream {
106 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700107 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700108
Alex Crichton1a7f7622017-07-05 17:47:15 -0700109 fn into_iter(self) -> TokenTreeIter {
110 TokenTreeIter(self.0.into_iter())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700111 }
112}
113
Alex Crichton1a7f7622017-07-05 17:47:15 -0700114impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700115 type Item = TokenTree;
116
117 fn next(&mut self) -> Option<TokenTree> {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700118 let token = self.0.next()?;
Alex Crichton9cd80a62018-04-05 17:46:58 -0700119 Some(match token {
120 proc_macro::TokenTree::Group(tt) => {
121 let delim = match tt.delimiter() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700122 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
123 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
124 proc_macro::Delimiter::Brace => Delimiter::Brace,
125 proc_macro::Delimiter::None => Delimiter::None,
126 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700127 let stream = ::TokenStream::_new(TokenStream(tt.stream()));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700128 let mut g = Group::new(delim, stream);
Alex Crichton9cd80a62018-04-05 17:46:58 -0700129 g.set_span(::Span::_new(Span(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700130 g.into()
131 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700132 proc_macro::TokenTree::Op(tt) => {
133 let spacing = match tt.spacing() {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700134 proc_macro::Spacing::Joint => Spacing::Joint,
135 proc_macro::Spacing::Alone => Spacing::Alone,
136 };
Alex Crichton9cd80a62018-04-05 17:46:58 -0700137 let mut o = Op::new(tt.op(), spacing);
138 o.set_span(::Span::_new(Span(tt.span())));
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700139 o.into()
140 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700141 proc_macro::TokenTree::Term(s) => {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700142 ::Term::_new(Term {
143 term: s,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700144 }).into()
145 }
Alex Crichton9cd80a62018-04-05 17:46:58 -0700146 proc_macro::TokenTree::Literal(l) => {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700147 ::Literal::_new(Literal {
148 lit: l,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700149 }).into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700150 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700151 })
152 }
153
154 fn size_hint(&self) -> (usize, Option<usize>) {
155 self.0.size_hint()
156 }
157}
158
Alex Crichton1a7f7622017-07-05 17:47:15 -0700159impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700161 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700162 }
163}
164
Nika Layzellf8d5f212017-12-11 14:07:02 -0500165#[derive(Clone, PartialEq, Eq)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500166pub struct FileName(String);
167
168impl fmt::Display for FileName {
169 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
170 self.0.fmt(f)
171 }
172}
173
174// NOTE: We have to generate our own filename object here because we can't wrap
175// the one provided by proc_macro.
176#[derive(Clone, PartialEq, Eq)]
177pub struct SourceFile(proc_macro::SourceFile, FileName);
Nika Layzellf8d5f212017-12-11 14:07:02 -0500178
179impl SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500180 fn new(sf: proc_macro::SourceFile) -> Self {
181 let filename = FileName(sf.path().to_string());
182 SourceFile(sf, filename)
183 }
184
Nika Layzellf8d5f212017-12-11 14:07:02 -0500185 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500186 pub fn path(&self) -> &FileName {
187 &self.1
Nika Layzellf8d5f212017-12-11 14:07:02 -0500188 }
189
190 pub fn is_real(&self) -> bool {
191 self.0.is_real()
192 }
193}
194
Nika Layzellb35a9a32017-12-30 14:34:35 -0500195impl AsRef<FileName> for SourceFile {
196 fn as_ref(&self) -> &FileName {
197 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500198 }
199}
200
201impl fmt::Debug for SourceFile {
202 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
203 self.0.fmt(f)
204 }
205}
206
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500207pub struct LineColumn {
208 pub line: usize,
209 pub column: usize,
210}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500211
Alex Crichton9cd80a62018-04-05 17:46:58 -0700212#[derive(Copy, Clone)]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700213pub struct Span(proc_macro::Span);
214
Sergio Benitez13805082018-01-04 01:25:45 -0800215impl From<proc_macro::Span> for ::Span {
216 fn from(proc_span: proc_macro::Span) -> ::Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700217 ::Span::_new(Span(proc_span))
Sergio Benitez13805082018-01-04 01:25:45 -0800218 }
219}
220
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700221impl Span {
222 pub fn call_site() -> Span {
223 Span(proc_macro::Span::call_site())
224 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700225
Alex Crichtone6085b72017-11-21 07:24:25 -0800226 pub fn def_site() -> Span {
Alex Crichton998f6422017-11-19 08:06:27 -0800227 Span(proc_macro::Span::def_site())
228 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500229
David Tolnay4e8e3972018-01-05 18:10:22 -0800230 pub fn resolved_at(&self, other: Span) -> Span {
231 Span(self.0.resolved_at(other.0))
232 }
233
234 pub fn located_at(&self, other: Span) -> Span {
235 Span(self.0.located_at(other.0))
236 }
237
David Tolnay16a17202017-12-31 10:47:24 -0500238 pub fn unstable(self) -> proc_macro::Span {
239 self.0
240 }
241
Nika Layzellf8d5f212017-12-11 14:07:02 -0500242 pub fn source_file(&self) -> SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500243 SourceFile::new(self.0.source_file())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500244 }
245
246 pub fn start(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200247 let proc_macro::LineColumn { line, column } = self.0.start();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500248 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500249 }
250
251 pub fn end(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200252 let proc_macro::LineColumn { line, column } = self.0.end();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500253 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500254 }
255
256 pub fn join(&self, other: Span) -> Option<Span> {
257 self.0.join(other.0).map(Span)
258 }
Alex Crichton998f6422017-11-19 08:06:27 -0800259}
260
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700261impl fmt::Debug for Span {
262 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700263 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700264 }
265}
266
267#[derive(Copy, Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700268pub struct Term {
269 term: proc_macro::Term,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700270}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700271
David Tolnay10effeb2018-01-06 11:07:49 -0800272impl Term {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700273 pub fn new(string: &str, span: Span) -> Term {
274 Term {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700275 term: proc_macro::Term::new(string, span.0),
Alex Crichtonb2c94622018-04-04 07:36:41 -0700276 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700277 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700278
David Tolnay10effeb2018-01-06 11:07:49 -0800279 pub fn as_str(&self) -> &str {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700280 self.term.as_str()
281 }
282
283 pub fn span(&self) -> Span {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700284 Span(self.term.span())
Alex Crichtonb2c94622018-04-04 07:36:41 -0700285 }
286
287 pub fn set_span(&mut self, span: Span) {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700288 self.term.set_span(span.0);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700289 }
290}
291
Alex Crichton1a7f7622017-07-05 17:47:15 -0700292impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700293 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700294 self.term.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700295 }
296}
297
298#[derive(Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700299pub struct Literal {
300 lit: proc_macro::Literal,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700301}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700302
Alex Crichtona914a612018-04-04 07:48:44 -0700303macro_rules! suffixed_numbers {
304 ($($name:ident => $kind:ident,)*) => ($(
305 pub fn $name(n: $kind) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700306 Literal::_new(proc_macro::Literal::$name(n))
Alex Crichtona914a612018-04-04 07:48:44 -0700307 }
308 )*)
309}
310
311macro_rules! unsuffixed_integers {
312 ($($name:ident => $kind:ident,)*) => ($(
313 pub fn $name(n: $kind) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700314 Literal::_new(proc_macro::Literal::$name(n))
Alex Crichtona914a612018-04-04 07:48:44 -0700315 }
316 )*)
317}
318
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700319impl Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700320 fn _new(lit: proc_macro::Literal) -> Literal {
321 Literal {
322 lit,
Alex Crichtonb2c94622018-04-04 07:36:41 -0700323 }
324 }
325
Alex Crichtona914a612018-04-04 07:48:44 -0700326 suffixed_numbers! {
327 u8_suffixed => u8,
328 u16_suffixed => u16,
329 u32_suffixed => u32,
330 u64_suffixed => u64,
331 usize_suffixed => usize,
332 i8_suffixed => i8,
333 i16_suffixed => i16,
334 i32_suffixed => i32,
335 i64_suffixed => i64,
336 isize_suffixed => isize,
337
338 f32_suffixed => f32,
339 f64_suffixed => f64,
340 }
341
342 unsuffixed_integers! {
343 u8_unsuffixed => u8,
344 u16_unsuffixed => u16,
345 u32_unsuffixed => u32,
346 u64_unsuffixed => u64,
347 usize_unsuffixed => usize,
348 i8_unsuffixed => i8,
349 i16_unsuffixed => i16,
350 i32_unsuffixed => i32,
351 i64_unsuffixed => i64,
352 isize_unsuffixed => isize,
353 }
354
355 pub fn f32_unsuffixed(f: f32) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700356 Literal::_new(proc_macro::Literal::f32_unsuffixed(f))
Alex Crichtona914a612018-04-04 07:48:44 -0700357 }
358
359 pub fn f64_unsuffixed(f: f64) -> Literal {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700360 Literal::_new(proc_macro::Literal::f64_unsuffixed(f))
Alex Crichtona914a612018-04-04 07:48:44 -0700361 }
362
363
364 pub fn string(t: &str) -> Literal {
365 Literal::_new(proc_macro::Literal::string(t))
366 }
367
368 pub fn character(t: char) -> Literal {
369 Literal::_new(proc_macro::Literal::character(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700370 }
371
372 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700373 Literal::_new(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700374 }
375
Alex Crichtonb2c94622018-04-04 07:36:41 -0700376 pub fn span(&self) -> Span {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700377 Span(self.lit.span())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700378 }
379
Alex Crichtonb2c94622018-04-04 07:36:41 -0700380 pub fn set_span(&mut self, span: Span) {
Alex Crichton9cd80a62018-04-05 17:46:58 -0700381 self.lit.set_span(span.0);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700382 }
383}
384
385impl fmt::Display for Literal {
386 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700387 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700388 }
389}
390
391impl fmt::Debug for Literal {
392 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700393 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700394 }
395}