blob: a178d1875d2f9926538c837007ee478d2dd18062 [file] [log] [blame]
Alex Crichtonaf5bad42018-03-27 14:45:10 -07001#![allow(dead_code)]
2
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07003use std::ascii;
4use std::fmt;
5use std::iter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07006use std::str::FromStr;
7
8use proc_macro;
9
David Tolnayb28f38a2018-03-31 22:02:29 +020010use {Delimiter, Group, Op, Spacing, TokenTree};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070011
12#[derive(Clone)]
13pub struct TokenStream(proc_macro::TokenStream);
14
15pub struct LexError(proc_macro::LexError);
16
17impl TokenStream {
18 pub fn empty() -> TokenStream {
19 TokenStream(proc_macro::TokenStream::empty())
20 }
21
22 pub fn is_empty(&self) -> bool {
23 self.0.is_empty()
24 }
25}
26
27impl FromStr for TokenStream {
28 type Err = LexError;
29
30 fn from_str(src: &str) -> Result<TokenStream, LexError> {
31 Ok(TokenStream(src.parse().map_err(LexError)?))
32 }
33}
34
35impl fmt::Display for TokenStream {
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 self.0.fmt(f)
38 }
39}
40
41impl From<proc_macro::TokenStream> for TokenStream {
42 fn from(inner: proc_macro::TokenStream) -> TokenStream {
43 TokenStream(inner)
44 }
45}
46
47impl From<TokenStream> for proc_macro::TokenStream {
48 fn from(inner: TokenStream) -> proc_macro::TokenStream {
49 inner.0
50 }
51}
52
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070053impl From<TokenTree> for TokenStream {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070054 fn from(token: TokenTree) -> TokenStream {
55 let (span, kind) = match token {
56 TokenTree::Group(tt) => {
57 let delim = match tt.delimiter() {
58 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
59 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
60 Delimiter::Brace => proc_macro::Delimiter::Brace,
61 Delimiter::None => proc_macro::Delimiter::None,
62 };
Alex Crichtonb2c94622018-04-04 07:36:41 -070063 let span = tt.span().inner;
Alex Crichtonaf5bad42018-03-27 14:45:10 -070064 let group = proc_macro::TokenNode::Group(delim, tt.stream.inner.0);
65 (span, group)
66 }
67 TokenTree::Op(tt) => {
68 let kind = match tt.spacing() {
69 Spacing::Joint => proc_macro::Spacing::Joint,
70 Spacing::Alone => proc_macro::Spacing::Alone,
71 };
Alex Crichtonb2c94622018-04-04 07:36:41 -070072 (tt.span().inner, proc_macro::TokenNode::Op(tt.op(), kind))
Alex Crichtonaf5bad42018-03-27 14:45:10 -070073 }
Alex Crichtonb2c94622018-04-04 07:36:41 -070074 TokenTree::Term(tt) => (tt.inner.span, proc_macro::TokenNode::Term(tt.inner.term)),
75 TokenTree::Literal(tt) => (tt.inner.span, proc_macro::TokenNode::Literal(tt.inner.lit)),
Alex Crichtonaf5bad42018-03-27 14:45:10 -070076 };
David Tolnayb28f38a2018-03-31 22:02:29 +020077 TokenStream(
78 proc_macro::TokenTree {
Alex Crichtonb2c94622018-04-04 07:36:41 -070079 span: span.0,
David Tolnayb28f38a2018-03-31 22:02:29 +020080 kind,
81 }.into(),
82 )
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070083 }
84}
85
Alex Crichtonaf5bad42018-03-27 14:45:10 -070086impl iter::FromIterator<TokenTree> for TokenStream {
David Tolnayb28f38a2018-03-31 22:02:29 +020087 fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
Alex Crichtonaf5bad42018-03-27 14:45:10 -070088 let streams = streams.into_iter().map(TokenStream::from);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070089 TokenStream(streams.collect::<proc_macro::TokenStream>())
90 }
91}
92
93impl fmt::Debug for TokenStream {
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070095 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070096 }
97}
98
99impl fmt::Debug for LexError {
100 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700101 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700102 }
103}
104
Alex Crichton1a7f7622017-07-05 17:47:15 -0700105pub struct TokenTreeIter(proc_macro::TokenTreeIter);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700106
107impl IntoIterator for TokenStream {
108 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700109 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700110
Alex Crichton1a7f7622017-07-05 17:47:15 -0700111 fn into_iter(self) -> TokenTreeIter {
112 TokenTreeIter(self.0.into_iter())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700113 }
114}
115
Alex Crichton1a7f7622017-07-05 17:47:15 -0700116impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700117 type Item = TokenTree;
118
119 fn next(&mut self) -> Option<TokenTree> {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700120 let token = self.0.next()?;
121 let span = ::Span::_new(Span(token.span));
122 Some(match token.kind {
123 proc_macro::TokenNode::Group(delim, s) => {
124 let delim = match delim {
125 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
126 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
127 proc_macro::Delimiter::Brace => Delimiter::Brace,
128 proc_macro::Delimiter::None => Delimiter::None,
129 };
130 let stream = ::TokenStream::_new(TokenStream(s));
131 let mut g = Group::new(delim, stream);
132 g.set_span(span);
133 g.into()
134 }
135 proc_macro::TokenNode::Op(ch, kind) => {
136 let kind = match kind {
137 proc_macro::Spacing::Joint => Spacing::Joint,
138 proc_macro::Spacing::Alone => Spacing::Alone,
139 };
140 let mut o = Op::new(ch, kind);
141 o.span = span;
142 o.into()
143 }
Alex Crichtonb2c94622018-04-04 07:36:41 -0700144 proc_macro::TokenNode::Term(s) => {
145 ::Term::_new(Term {
146 term: s,
147 span: span.inner,
148 }).into()
149 }
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700150 proc_macro::TokenNode::Literal(l) => {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700151 ::Literal::_new(Literal {
152 lit: l,
153 span: span.inner,
154 }).into()
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700155 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700156 })
157 }
158
159 fn size_hint(&self) -> (usize, Option<usize>) {
160 self.0.size_hint()
161 }
162}
163
Alex Crichton1a7f7622017-07-05 17:47:15 -0700164impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700165 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700166 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700167 }
168}
169
Nika Layzellf8d5f212017-12-11 14:07:02 -0500170#[derive(Clone, PartialEq, Eq)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500171pub struct FileName(String);
172
173impl fmt::Display for FileName {
174 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175 self.0.fmt(f)
176 }
177}
178
179// NOTE: We have to generate our own filename object here because we can't wrap
180// the one provided by proc_macro.
181#[derive(Clone, PartialEq, Eq)]
182pub struct SourceFile(proc_macro::SourceFile, FileName);
Nika Layzellf8d5f212017-12-11 14:07:02 -0500183
184impl SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500185 fn new(sf: proc_macro::SourceFile) -> Self {
186 let filename = FileName(sf.path().to_string());
187 SourceFile(sf, filename)
188 }
189
Nika Layzellf8d5f212017-12-11 14:07:02 -0500190 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500191 pub fn path(&self) -> &FileName {
192 &self.1
Nika Layzellf8d5f212017-12-11 14:07:02 -0500193 }
194
195 pub fn is_real(&self) -> bool {
196 self.0.is_real()
197 }
198}
199
Nika Layzellb35a9a32017-12-30 14:34:35 -0500200impl AsRef<FileName> for SourceFile {
201 fn as_ref(&self) -> &FileName {
202 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500203 }
204}
205
206impl fmt::Debug for SourceFile {
207 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
208 self.0.fmt(f)
209 }
210}
211
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500212pub struct LineColumn {
213 pub line: usize,
214 pub column: usize,
215}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500216
Nika Layzell99737982018-03-11 18:51:27 -0400217#[derive(Copy, Clone, PartialEq, Eq)]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700218pub struct Span(proc_macro::Span);
219
Sergio Benitez13805082018-01-04 01:25:45 -0800220impl From<proc_macro::Span> for ::Span {
221 fn from(proc_span: proc_macro::Span) -> ::Span {
Alex Crichtonaf5bad42018-03-27 14:45:10 -0700222 ::Span::_new(Span(proc_span))
Sergio Benitez13805082018-01-04 01:25:45 -0800223 }
224}
225
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700226impl Span {
227 pub fn call_site() -> Span {
228 Span(proc_macro::Span::call_site())
229 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700230
Alex Crichtone6085b72017-11-21 07:24:25 -0800231 pub fn def_site() -> Span {
Alex Crichton998f6422017-11-19 08:06:27 -0800232 Span(proc_macro::Span::def_site())
233 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500234
David Tolnay4e8e3972018-01-05 18:10:22 -0800235 pub fn resolved_at(&self, other: Span) -> Span {
236 Span(self.0.resolved_at(other.0))
237 }
238
239 pub fn located_at(&self, other: Span) -> Span {
240 Span(self.0.located_at(other.0))
241 }
242
David Tolnay16a17202017-12-31 10:47:24 -0500243 pub fn unstable(self) -> proc_macro::Span {
244 self.0
245 }
246
Nika Layzellf8d5f212017-12-11 14:07:02 -0500247 pub fn source_file(&self) -> SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500248 SourceFile::new(self.0.source_file())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500249 }
250
251 pub fn start(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200252 let proc_macro::LineColumn { line, column } = self.0.start();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500253 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500254 }
255
256 pub fn end(&self) -> LineColumn {
David Tolnayb28f38a2018-03-31 22:02:29 +0200257 let proc_macro::LineColumn { line, column } = self.0.end();
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500258 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500259 }
260
261 pub fn join(&self, other: Span) -> Option<Span> {
262 self.0.join(other.0).map(Span)
263 }
Alex Crichton998f6422017-11-19 08:06:27 -0800264}
265
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700266impl fmt::Debug for Span {
267 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700268 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700269 }
270}
271
272#[derive(Copy, Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700273pub struct Term {
274 term: proc_macro::Term,
275 span: Span,
276}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700277
David Tolnay10effeb2018-01-06 11:07:49 -0800278impl Term {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700279 pub fn new(string: &str, span: Span) -> Term {
280 Term {
281 term: proc_macro::Term::intern(string),
282 span,
283 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700284 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700285
David Tolnay10effeb2018-01-06 11:07:49 -0800286 pub fn as_str(&self) -> &str {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700287 self.term.as_str()
288 }
289
290 pub fn span(&self) -> Span {
291 self.span
292 }
293
294 pub fn set_span(&mut self, span: Span) {
295 self.span = span;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700296 }
297}
298
Alex Crichton1a7f7622017-07-05 17:47:15 -0700299impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700300 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700301 self.term.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700302 }
303}
304
305#[derive(Clone)]
Alex Crichtonb2c94622018-04-04 07:36:41 -0700306pub struct Literal {
307 lit: proc_macro::Literal,
308 span: Span,
309}
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700310
311impl Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700312 fn _new(lit: proc_macro::Literal) -> Literal {
313 Literal {
314 lit,
315 span: Span::call_site(),
316 }
317 }
318
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700319 pub fn byte_char(byte: u8) -> Literal {
320 match byte {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700321 0 => Literal::_new(to_literal("b'\\0'")),
322 b'\"' => Literal::_new(to_literal("b'\"'")),
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700323 n => {
324 let mut escaped = "b'".to_string();
325 escaped.extend(ascii::escape_default(n).map(|c| c as char));
326 escaped.push('\'');
Alex Crichtonb2c94622018-04-04 07:36:41 -0700327 Literal::_new(to_literal(&escaped))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700328 }
329 }
330 }
331
332 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700333 Literal::_new(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700334 }
335
Alex Crichton1a7f7622017-07-05 17:47:15 -0700336 pub fn float(s: f64) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700337 Literal::_new(proc_macro::Literal::float(s))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700338 }
339
Alex Crichton1a7f7622017-07-05 17:47:15 -0700340 pub fn integer(s: i64) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700341 Literal::_new(proc_macro::Literal::integer(s.into()))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700342 }
343
Alex Crichtonb2c94622018-04-04 07:36:41 -0700344 pub fn span(&self) -> Span {
345 self.span
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700346 }
347
Alex Crichtonb2c94622018-04-04 07:36:41 -0700348 pub fn set_span(&mut self, span: Span) {
349 self.span = span;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700350 }
351}
352
353impl fmt::Display for Literal {
354 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700355 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700356 }
357}
358
359impl fmt::Debug for Literal {
360 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700361 self.lit.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700362 }
363}
364
365fn to_literal(s: &str) -> proc_macro::Literal {
366 let stream = s.parse::<proc_macro::TokenStream>().unwrap();
367 match stream.into_iter().next().unwrap().kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700368 proc_macro::TokenNode::Literal(l) => l,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700369 _ => unreachable!(),
370 }
371}
372
373macro_rules! ints {
Alex Crichton040be632017-07-05 17:56:48 -0700374 ($($t:ident,)*) => {$(
375 impl From<$t> for Literal {
376 fn from(t: $t) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700377 Literal::_new(proc_macro::Literal::$t(t))
Alex Crichton040be632017-07-05 17:56:48 -0700378 }
379 }
380 )*}
381}
382
383ints! {
Alex Crichton6e81d762017-07-14 06:09:31 -0700384 u8, u16, u32, u64, usize,
385 i8, i16, i32, i64, isize,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700386}
387
388macro_rules! floats {
389 ($($t:ident,)*) => {$(
390 impl From<$t> for Literal {
391 fn from(t: $t) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700392 Literal::_new(proc_macro::Literal::$t(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700393 }
394 }
395 )*}
396}
397
398floats! {
399 f32, f64,
400}
401
402impl<'a> From<&'a str> for Literal {
403 fn from(t: &'a str) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700404 Literal::_new(proc_macro::Literal::string(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700405 }
406}
407
408impl From<char> for Literal {
409 fn from(t: char) -> Literal {
Alex Crichtonb2c94622018-04-04 07:36:41 -0700410 Literal::_new(proc_macro::Literal::character(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700411 }
412}