blob: e01e1f09bd375921895f188df6cc02b22a29e417 [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
Alex Crichtonaf5bad42018-03-27 14:45:10 -070010use {TokenTree, Delimiter, Spacing, Group, Op};
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 };
63 let span = tt.span();
64 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 };
72 (tt.span(), proc_macro::TokenNode::Op(tt.op(), kind))
73 }
74 TokenTree::Term(tt) => {
75 (tt.span(), proc_macro::TokenNode::Term(tt.inner.0))
76 }
77 TokenTree::Literal(tt) => {
78 (tt.span(), proc_macro::TokenNode::Literal(tt.inner.0))
79 }
80 };
81 TokenStream(proc_macro::TokenTree { span: span.inner.0, kind }.into())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070082 }
83}
84
Alex Crichtonaf5bad42018-03-27 14:45:10 -070085impl iter::FromIterator<TokenTree> for TokenStream {
86 fn from_iter<I: IntoIterator<Item=TokenTree>>(streams: I) -> Self {
87 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 }
143 proc_macro::TokenNode::Term(s) => {
144 ::Term::_new(Term(s), span).into()
145 }
146 proc_macro::TokenNode::Literal(l) => {
147 let mut l = ::Literal::_new(Literal(l));
148 l.span = span;
149 l.into()
150 }
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
Nika Layzell99737982018-03-11 18:51:27 -0400212#[derive(Copy, Clone, PartialEq, Eq)]
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 {
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500247 let proc_macro::LineColumn{ line, column } = self.0.start();
248 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500249 }
250
251 pub fn end(&self) -> LineColumn {
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500252 let proc_macro::LineColumn{ line, column } = self.0.end();
253 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 Crichton1a7f7622017-07-05 17:47:15 -0700268pub struct Term(proc_macro::Term);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700269
David Tolnay10effeb2018-01-06 11:07:49 -0800270impl Term {
271 pub fn intern(string: &str) -> Term {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700272 Term(proc_macro::Term::intern(string))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700273 }
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700274
David Tolnay10effeb2018-01-06 11:07:49 -0800275 pub fn as_str(&self) -> &str {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700276 self.0.as_str()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700277 }
278}
279
Alex Crichton1a7f7622017-07-05 17:47:15 -0700280impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700281 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700282 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700283 }
284}
285
286#[derive(Clone)]
287pub struct Literal(proc_macro::Literal);
288
289impl Literal {
290 pub fn byte_char(byte: u8) -> Literal {
291 match byte {
292 0 => Literal(to_literal("b'\\0'")),
293 b'\"' => Literal(to_literal("b'\"'")),
294 n => {
295 let mut escaped = "b'".to_string();
296 escaped.extend(ascii::escape_default(n).map(|c| c as char));
297 escaped.push('\'');
298 Literal(to_literal(&escaped))
299 }
300 }
301 }
302
303 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700304 Literal(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700305 }
306
307 pub fn doccomment(s: &str) -> Literal {
308 Literal(to_literal(s))
309 }
310
Alex Crichton1a7f7622017-07-05 17:47:15 -0700311 pub fn float(s: f64) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700312 Literal(proc_macro::Literal::float(s))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700313 }
314
Alex Crichton1a7f7622017-07-05 17:47:15 -0700315 pub fn integer(s: i64) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700316 Literal(proc_macro::Literal::integer(s.into()))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700317 }
318
319 pub fn raw_string(s: &str, pounds: usize) -> Literal {
320 let mut ret = format!("r");
321 ret.extend((0..pounds).map(|_| "#"));
322 ret.push('"');
323 ret.push_str(s);
324 ret.push('"');
325 ret.extend((0..pounds).map(|_| "#"));
326 Literal(to_literal(&ret))
327 }
328
329 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
330 let mut ret = format!("br");
331 ret.extend((0..pounds).map(|_| "#"));
332 ret.push('"');
333 ret.push_str(s);
334 ret.push('"');
335 ret.extend((0..pounds).map(|_| "#"));
336 Literal(to_literal(&ret))
337 }
338}
339
340impl fmt::Display for Literal {
341 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
342 self.0.fmt(f)
343 }
344}
345
346impl fmt::Debug for Literal {
347 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700348 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700349 }
350}
351
352fn to_literal(s: &str) -> proc_macro::Literal {
353 let stream = s.parse::<proc_macro::TokenStream>().unwrap();
354 match stream.into_iter().next().unwrap().kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700355 proc_macro::TokenNode::Literal(l) => l,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700356 _ => unreachable!(),
357 }
358}
359
360macro_rules! ints {
Alex Crichton040be632017-07-05 17:56:48 -0700361 ($($t:ident,)*) => {$(
362 impl From<$t> for Literal {
363 fn from(t: $t) -> Literal {
364 Literal(proc_macro::Literal::$t(t))
365 }
366 }
367 )*}
368}
369
370ints! {
Alex Crichton6e81d762017-07-14 06:09:31 -0700371 u8, u16, u32, u64, usize,
372 i8, i16, i32, i64, isize,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700373}
374
375macro_rules! floats {
376 ($($t:ident,)*) => {$(
377 impl From<$t> for Literal {
378 fn from(t: $t) -> Literal {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700379 Literal(proc_macro::Literal::$t(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700380 }
381 }
382 )*}
383}
384
385floats! {
386 f32, f64,
387}
388
389impl<'a> From<&'a str> for Literal {
390 fn from(t: &'a str) -> Literal {
391 Literal(proc_macro::Literal::string(t))
392 }
393}
394
395impl From<char> for Literal {
396 fn from(t: char) -> Literal {
397 Literal(proc_macro::Literal::character(t))
398 }
399}