blob: 660540a3fbcbb4800ee11b145d4c9f8e9b5831f3 [file] [log] [blame]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07001use std::ascii;
2use std::fmt;
3use std::iter;
4use std::ops;
5use std::str::FromStr;
6
7use proc_macro;
8
Alex Crichton1a7f7622017-07-05 17:47:15 -07009use {TokenTree, TokenNode, Delimiter, Spacing};
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 {
53 fn from(tree: TokenTree) -> TokenStream {
54 TokenStream(proc_macro::TokenTree {
55 span: (tree.span.0).0,
56 kind: match tree.kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -070057 TokenNode::Group(delim, s) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070058 let delim = match delim {
59 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
60 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
61 Delimiter::Brace => proc_macro::Delimiter::Brace,
62 Delimiter::None => proc_macro::Delimiter::None,
63 };
Alex Crichton1a7f7622017-07-05 17:47:15 -070064 proc_macro::TokenNode::Group(delim, (s.0).0)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070065 }
Alex Crichton1a7f7622017-07-05 17:47:15 -070066 TokenNode::Op(ch, kind) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070067 let kind = match kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -070068 Spacing::Joint => proc_macro::Spacing::Joint,
69 Spacing::Alone => proc_macro::Spacing::Alone,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070070 };
Alex Crichton1a7f7622017-07-05 17:47:15 -070071 proc_macro::TokenNode::Op(ch, kind)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070072 }
Alex Crichton1a7f7622017-07-05 17:47:15 -070073 TokenNode::Term(s) => {
74 proc_macro::TokenNode::Term((s.0).0)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070075 }
Alex Crichton1a7f7622017-07-05 17:47:15 -070076 TokenNode::Literal(l) => {
77 proc_macro::TokenNode::Literal((l.0).0)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070078 }
79 },
80 }.into())
81 }
82}
83
84impl iter::FromIterator<TokenStream> for TokenStream {
85 fn from_iter<I: IntoIterator<Item=TokenStream>>(streams: I) -> Self {
86 let streams = streams.into_iter().map(|s| s.0);
87 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 Crichton1a7f7622017-07-05 17:47:15 -0700103pub struct TokenTreeIter(proc_macro::TokenTreeIter);
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> {
118 let token = match self.0.next() {
119 Some(n) => n,
120 None => return None,
121 };
122 Some(TokenTree {
123 span: ::Span(Span(token.span)),
124 kind: match token.kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700125 proc_macro::TokenNode::Group(delim, s) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700126 let delim = match delim {
127 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
128 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
129 proc_macro::Delimiter::Brace => Delimiter::Brace,
130 proc_macro::Delimiter::None => Delimiter::None,
131 };
Alex Crichton1a7f7622017-07-05 17:47:15 -0700132 TokenNode::Group(delim, ::TokenStream(TokenStream(s)))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700133 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700134 proc_macro::TokenNode::Op(ch, kind) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700135 let kind = match kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700136 proc_macro::Spacing::Joint => Spacing::Joint,
137 proc_macro::Spacing::Alone => Spacing::Alone,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700138 };
Alex Crichton1a7f7622017-07-05 17:47:15 -0700139 TokenNode::Op(ch, kind)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700140 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700141 proc_macro::TokenNode::Term(s) => {
142 TokenNode::Term(::Term(Term(s)))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700143 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700144 proc_macro::TokenNode::Literal(l) => {
145 TokenNode::Literal(::Literal(Literal(l)))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700146 }
147 },
148 })
149 }
150
151 fn size_hint(&self) -> (usize, Option<usize>) {
152 self.0.size_hint()
153 }
154}
155
Alex Crichton1a7f7622017-07-05 17:47:15 -0700156impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700158 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700159 }
160}
161
David Tolnay1ebe3972018-01-02 20:14:20 -0800162#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500163#[derive(Clone, PartialEq, Eq)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500164pub struct FileName(String);
165
David Tolnay1ebe3972018-01-02 20:14:20 -0800166#[cfg(procmacro2_semver_exempt)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500167impl fmt::Display for FileName {
168 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169 self.0.fmt(f)
170 }
171}
172
173// NOTE: We have to generate our own filename object here because we can't wrap
174// the one provided by proc_macro.
David Tolnay1ebe3972018-01-02 20:14:20 -0800175#[cfg(procmacro2_semver_exempt)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500176#[derive(Clone, PartialEq, Eq)]
177pub struct SourceFile(proc_macro::SourceFile, FileName);
Nika Layzellf8d5f212017-12-11 14:07:02 -0500178
David Tolnay1ebe3972018-01-02 20:14:20 -0800179#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500180impl SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500181 fn new(sf: proc_macro::SourceFile) -> Self {
182 let filename = FileName(sf.path().to_string());
183 SourceFile(sf, filename)
184 }
185
Nika Layzellf8d5f212017-12-11 14:07:02 -0500186 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500187 pub fn path(&self) -> &FileName {
188 &self.1
Nika Layzellf8d5f212017-12-11 14:07:02 -0500189 }
190
191 pub fn is_real(&self) -> bool {
192 self.0.is_real()
193 }
194}
195
David Tolnay1ebe3972018-01-02 20:14:20 -0800196#[cfg(procmacro2_semver_exempt)]
Nika Layzellb35a9a32017-12-30 14:34:35 -0500197impl AsRef<FileName> for SourceFile {
198 fn as_ref(&self) -> &FileName {
199 self.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500200 }
201}
202
David Tolnay1ebe3972018-01-02 20:14:20 -0800203#[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500204impl fmt::Debug for SourceFile {
205 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
206 self.0.fmt(f)
207 }
208}
209
David Tolnay1ebe3972018-01-02 20:14:20 -0800210#[cfg(procmacro2_semver_exempt)]
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
Alex Crichton998f6422017-11-19 08:06:27 -0800216#[derive(Copy, Clone)]
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 {
221 ::Span(Span(proc_span))
222 }
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 Tolnay16a17202017-12-31 10:47:24 -0500234 pub fn unstable(self) -> proc_macro::Span {
235 self.0
236 }
237
David Tolnay1ebe3972018-01-02 20:14:20 -0800238 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500239 pub fn source_file(&self) -> SourceFile {
Nika Layzellb35a9a32017-12-30 14:34:35 -0500240 SourceFile::new(self.0.source_file())
Nika Layzellf8d5f212017-12-11 14:07:02 -0500241 }
242
David Tolnay1ebe3972018-01-02 20:14:20 -0800243 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500244 pub fn start(&self) -> LineColumn {
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500245 let proc_macro::LineColumn{ line, column } = self.0.start();
246 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500247 }
248
David Tolnay1ebe3972018-01-02 20:14:20 -0800249 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500250 pub fn end(&self) -> LineColumn {
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500251 let proc_macro::LineColumn{ line, column } = self.0.end();
252 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500253 }
254
David Tolnay1ebe3972018-01-02 20:14:20 -0800255 #[cfg(procmacro2_semver_exempt)]
Nika Layzellf8d5f212017-12-11 14:07:02 -0500256 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
Alex Crichton1a7f7622017-07-05 17:47:15 -0700270impl<'a> From<&'a str> for Term {
271 fn from(string: &'a str) -> Term {
272 Term(proc_macro::Term::intern(string))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700273 }
274}
275
Alex Crichton1a7f7622017-07-05 17:47:15 -0700276impl ops::Deref for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700277 type Target = str;
278
279 fn deref(&self) -> &str {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700280 self.0.as_str()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700281 }
282}
283
Alex Crichton1a7f7622017-07-05 17:47:15 -0700284impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700285 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700286 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700287 }
288}
289
290#[derive(Clone)]
291pub struct Literal(proc_macro::Literal);
292
293impl Literal {
294 pub fn byte_char(byte: u8) -> Literal {
295 match byte {
296 0 => Literal(to_literal("b'\\0'")),
297 b'\"' => Literal(to_literal("b'\"'")),
298 n => {
299 let mut escaped = "b'".to_string();
300 escaped.extend(ascii::escape_default(n).map(|c| c as char));
301 escaped.push('\'');
302 Literal(to_literal(&escaped))
303 }
304 }
305 }
306
307 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700308 Literal(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700309 }
310
311 pub fn doccomment(s: &str) -> Literal {
312 Literal(to_literal(s))
313 }
314
Alex Crichton1a7f7622017-07-05 17:47:15 -0700315 pub fn float(s: f64) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700316 Literal(proc_macro::Literal::float(s))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700317 }
318
Alex Crichton1a7f7622017-07-05 17:47:15 -0700319 pub fn integer(s: i64) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700320 Literal(proc_macro::Literal::integer(s.into()))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700321 }
322
323 pub fn raw_string(s: &str, pounds: usize) -> Literal {
324 let mut ret = format!("r");
325 ret.extend((0..pounds).map(|_| "#"));
326 ret.push('"');
327 ret.push_str(s);
328 ret.push('"');
329 ret.extend((0..pounds).map(|_| "#"));
330 Literal(to_literal(&ret))
331 }
332
333 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
334 let mut ret = format!("br");
335 ret.extend((0..pounds).map(|_| "#"));
336 ret.push('"');
337 ret.push_str(s);
338 ret.push('"');
339 ret.extend((0..pounds).map(|_| "#"));
340 Literal(to_literal(&ret))
341 }
342}
343
344impl fmt::Display for Literal {
345 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
346 self.0.fmt(f)
347 }
348}
349
350impl fmt::Debug for Literal {
351 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700352 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700353 }
354}
355
356fn to_literal(s: &str) -> proc_macro::Literal {
357 let stream = s.parse::<proc_macro::TokenStream>().unwrap();
358 match stream.into_iter().next().unwrap().kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700359 proc_macro::TokenNode::Literal(l) => l,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700360 _ => unreachable!(),
361 }
362}
363
364macro_rules! ints {
Alex Crichton040be632017-07-05 17:56:48 -0700365 ($($t:ident,)*) => {$(
366 impl From<$t> for Literal {
367 fn from(t: $t) -> Literal {
368 Literal(proc_macro::Literal::$t(t))
369 }
370 }
371 )*}
372}
373
374ints! {
Alex Crichton6e81d762017-07-14 06:09:31 -0700375 u8, u16, u32, u64, usize,
376 i8, i16, i32, i64, isize,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700377}
378
379macro_rules! floats {
380 ($($t:ident,)*) => {$(
381 impl From<$t> for Literal {
382 fn from(t: $t) -> Literal {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700383 Literal(proc_macro::Literal::$t(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700384 }
385 }
386 )*}
387}
388
389floats! {
390 f32, f64,
391}
392
393impl<'a> From<&'a str> for Literal {
394 fn from(t: &'a str) -> Literal {
395 Literal(proc_macro::Literal::string(t))
396 }
397}
398
399impl From<char> for Literal {
400 fn from(t: char) -> Literal {
401 Literal(proc_macro::Literal::character(t))
402 }
403}