blob: 1127e695e539f5821600f28a634d8271b890990b [file] [log] [blame]
Alex Crichtonbabc99e2017-07-05 18:00:29 -07001//! A "shim crate" intended to multiplex the `proc_macro` API on to stable Rust.
2//!
3//! Procedural macros in Rust operate over the upstream
4//! `proc_macro::TokenStream` type. This type currently is quite conservative
5//! and exposed no internal implementation details. Nightly compilers, however,
6//! contain a much richer interface. This richer interface allows fine-grained
7//! inspection of the token stream which avoids stringification/re-lexing and
8//! also preserves span information.
9//!
10//! The upcoming APIs added to `proc_macro` upstream are the foundation for
11//! productive procedural macros in the ecosystem. To help prepare the ecosystem
12//! for using them this crate serves to both compile on stable and nightly and
13//! mirrors the API-to-be. The intention is that procedural macros which switch
14//! to use this crate will be trivially able to switch to the upstream
15//! `proc_macro` crate once its API stabilizes.
16//!
17//! In the meantime this crate also has an `unstable` Cargo feature which
18//! enables it to reimplement itself with the unstable API of `proc_macro`.
19//! This'll allow immediate usage of the beneficial upstream API, particularly
20//! around preserving span information.
21
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070022#![cfg_attr(feature = "unstable", feature(proc_macro))]
23
Alex Crichton44bffbc2017-05-19 17:51:59 -070024extern crate proc_macro;
25
Alex Crichtone14e8fd2017-05-23 07:02:54 -070026#[cfg(not(feature = "unstable"))]
Nika Layzellf8d5f212017-12-11 14:07:02 -050027extern crate memchr;
28
29#[cfg(not(feature = "unstable"))]
David Tolnayb1032662017-05-31 15:52:28 -070030extern crate unicode_xid;
Alex Crichton44bffbc2017-05-19 17:51:59 -070031
32use std::fmt;
Alex Crichton44bffbc2017-05-19 17:51:59 -070033use std::str::FromStr;
34use std::iter::FromIterator;
35
David Tolnayb1032662017-05-31 15:52:28 -070036#[macro_use]
37#[cfg(not(feature = "unstable"))]
38mod strnom;
39
Alex Crichton44bffbc2017-05-19 17:51:59 -070040#[path = "stable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070041#[cfg(not(feature = "unstable"))]
Alex Crichtonb15c6352017-05-19 19:36:36 -070042mod imp;
43#[path = "unstable.rs"]
Alex Crichtone14e8fd2017-05-23 07:02:54 -070044#[cfg(feature = "unstable")]
Alex Crichton44bffbc2017-05-19 17:51:59 -070045mod imp;
46
David Tolnaycb1b85f2017-06-03 16:40:35 -070047#[macro_use]
48mod macros;
49
50#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -070051pub struct TokenStream(imp::TokenStream);
52
Alex Crichton44bffbc2017-05-19 17:51:59 -070053pub struct LexError(imp::LexError);
54
55impl FromStr for TokenStream {
56 type Err = LexError;
57
58 fn from_str(src: &str) -> Result<TokenStream, LexError> {
59 match src.parse() {
60 Ok(e) => Ok(TokenStream(e)),
61 Err(e) => Err(LexError(e)),
62 }
63 }
64}
65
Alex Crichton44bffbc2017-05-19 17:51:59 -070066impl From<proc_macro::TokenStream> for TokenStream {
67 fn from(inner: proc_macro::TokenStream) -> TokenStream {
68 TokenStream(inner.into())
69 }
70}
71
72impl From<TokenStream> for proc_macro::TokenStream {
73 fn from(inner: TokenStream) -> proc_macro::TokenStream {
74 inner.0.into()
75 }
76}
77
78impl From<TokenTree> for TokenStream {
79 fn from(tree: TokenTree) -> TokenStream {
80 TokenStream(tree.into())
81 }
82}
83
Alex Crichton44bffbc2017-05-19 17:51:59 -070084impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
85 fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
86 TokenStream(streams.into_iter().map(|t| t.into().0).collect())
87 }
88}
89
90impl IntoIterator for TokenStream {
91 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -070092 type IntoIter = TokenTreeIter;
Alex Crichton44bffbc2017-05-19 17:51:59 -070093
Alex Crichton1a7f7622017-07-05 17:47:15 -070094 fn into_iter(self) -> TokenTreeIter {
95 TokenTreeIter(self.0.into_iter())
Alex Crichton44bffbc2017-05-19 17:51:59 -070096 }
97}
98
99impl TokenStream {
100 pub fn empty() -> TokenStream {
101 TokenStream(imp::TokenStream::empty())
102 }
103
104 pub fn is_empty(&self) -> bool {
105 self.0.is_empty()
106 }
107}
108
Nika Layzellb35a9a32017-12-30 14:34:35 -0500109// Returned by reference, so we can't easily wrap it.
110pub use imp::FileName;
111
Nika Layzellf8d5f212017-12-11 14:07:02 -0500112#[derive(Clone, PartialEq, Eq)]
113pub struct SourceFile(imp::SourceFile);
114
115impl SourceFile {
116 /// Get the path to this source file as a string.
Nika Layzellb35a9a32017-12-30 14:34:35 -0500117 pub fn path(&self) -> &FileName {
118 self.0.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500119 }
120
121 pub fn is_real(&self) -> bool {
122 self.0.is_real()
123 }
124}
125
Nika Layzellb35a9a32017-12-30 14:34:35 -0500126impl AsRef<FileName> for SourceFile {
127 fn as_ref(&self) -> &FileName {
128 self.0.path()
Nika Layzellf8d5f212017-12-11 14:07:02 -0500129 }
130}
131
132impl fmt::Debug for SourceFile {
133 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
134 self.0.fmt(f)
135 }
136}
137
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500138pub struct LineColumn {
139 pub line: usize,
140 pub column: usize,
141}
Nika Layzellf8d5f212017-12-11 14:07:02 -0500142
David Tolnaycb1b85f2017-06-03 16:40:35 -0700143#[derive(Copy, Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700144pub struct Span(imp::Span);
145
Alex Crichtone6085b72017-11-21 07:24:25 -0800146#[doc(hidden)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700147impl Default for Span {
148 fn default() -> Span {
Alex Crichtone6085b72017-11-21 07:24:25 -0800149 Span(imp::Span::def_site())
Alex Crichton44bffbc2017-05-19 17:51:59 -0700150 }
151}
152
153impl Span {
154 pub fn call_site() -> Span {
155 Span(imp::Span::call_site())
156 }
Alex Crichtone6085b72017-11-21 07:24:25 -0800157
158 pub fn def_site() -> Span {
159 Span(imp::Span::def_site())
160 }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500161
162 pub fn source_file(&self) -> SourceFile {
163 SourceFile(self.0.source_file())
164 }
165
166 pub fn start(&self) -> LineColumn {
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500167 let imp::LineColumn{ line, column } = self.0.start();
168 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500169 }
170
171 pub fn end(&self) -> LineColumn {
Nika Layzell1ecb6ce2017-12-30 14:34:05 -0500172 let imp::LineColumn{ line, column } = self.0.end();
173 LineColumn { line, column }
Nika Layzellf8d5f212017-12-11 14:07:02 -0500174 }
175
176 pub fn join(&self, other: Span) -> Option<Span> {
177 self.0.join(other.0).map(Span)
178 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700179}
180
David Tolnay977f8282017-05-31 17:41:33 -0700181#[derive(Clone, Debug)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700182pub struct TokenTree {
183 pub span: Span,
Alex Crichton1a7f7622017-07-05 17:47:15 -0700184 pub kind: TokenNode,
185}
186
187impl From<TokenNode> for TokenTree {
188 fn from(kind: TokenNode) -> TokenTree {
189 TokenTree { span: Span::default(), kind: kind }
190 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700191}
192
Alex Crichton44bffbc2017-05-19 17:51:59 -0700193impl fmt::Display for TokenTree {
194 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195 TokenStream::from(self.clone()).fmt(f)
196 }
197}
198
David Tolnay977f8282017-05-31 17:41:33 -0700199#[derive(Clone, Debug)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700200pub enum TokenNode {
201 Group(Delimiter, TokenStream),
202 Term(Term),
203 Op(char, Spacing),
Alex Crichton44bffbc2017-05-19 17:51:59 -0700204 Literal(Literal),
205}
206
Michael Layzell5372f4b2017-06-02 10:29:31 -0400207#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700208pub enum Delimiter {
209 Parenthesis,
210 Brace,
211 Bracket,
212 None,
213}
214
David Tolnaycb1b85f2017-06-03 16:40:35 -0700215#[derive(Copy, Clone)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700216pub struct Term(imp::Term);
Alex Crichton44bffbc2017-05-19 17:51:59 -0700217
Alex Crichton1a7f7622017-07-05 17:47:15 -0700218impl Term {
219 pub fn intern(string: &str) -> Term {
220 Term(string.into())
Alex Crichton44bffbc2017-05-19 17:51:59 -0700221 }
Alex Crichton44bffbc2017-05-19 17:51:59 -0700222
Alex Crichton43881252017-05-26 08:51:06 -0700223 pub fn as_str(&self) -> &str {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700224 &self.0
225 }
226}
227
Lukas Kalbertodteb3f9302017-08-20 18:58:41 +0200228#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700229pub enum Spacing {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700230 Alone,
231 Joint,
232}
233
David Tolnaycb1b85f2017-06-03 16:40:35 -0700234#[derive(Clone)]
Alex Crichton44bffbc2017-05-19 17:51:59 -0700235pub struct Literal(imp::Literal);
236
Alex Crichton1a7f7622017-07-05 17:47:15 -0700237macro_rules! int_literals {
238 ($($kind:ident,)*) => ($(
239 pub fn $kind(n: $kind) -> Literal {
240 Literal(n.into())
241 }
242 )*)
243}
244
Alex Crichton852d53d2017-05-19 19:25:08 -0700245impl Literal {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700246 pub fn integer(s: i64) -> Literal {
247 Literal(imp::Literal::integer(s))
248 }
249
250 int_literals! {
Alex Crichton1a16c782017-07-05 18:06:36 -0700251 u8, u16, u32, u64, usize,
252 i8, i16, i32, i64, isize,
Alex Crichton1a7f7622017-07-05 17:47:15 -0700253 }
254
255 pub fn float(f: f64) -> Literal {
256 Literal(imp::Literal::float(f))
257 }
258
259 pub fn f64(f: f64) -> Literal {
260 Literal(f.into())
261 }
262
263 pub fn f32(f: f32) -> Literal {
264 Literal(f.into())
265 }
266
267 pub fn string(string: &str) -> Literal {
268 Literal(string.into())
269 }
270
271 pub fn character(ch: char) -> Literal {
272 Literal(ch.into())
Alex Crichton76a5cc82017-05-23 07:01:44 -0700273 }
274
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700275 pub fn byte_string(s: &[u8]) -> Literal {
276 Literal(imp::Literal::byte_string(s))
Alex Crichton852d53d2017-05-19 19:25:08 -0700277 }
Alex Crichton76a5cc82017-05-23 07:01:44 -0700278
Alex Crichton1a7f7622017-07-05 17:47:15 -0700279 // =======================================================================
280 // Not present upstream in proc_macro yet
281
282 pub fn byte_char(b: u8) -> Literal {
283 Literal(imp::Literal::byte_char(b))
284 }
285
Alex Crichton76a5cc82017-05-23 07:01:44 -0700286 pub fn doccomment(s: &str) -> Literal {
287 Literal(imp::Literal::doccomment(s))
288 }
Alex Crichton9c2fb0a2017-05-26 08:49:31 -0700289
Alex Crichton31316622017-05-26 12:54:47 -0700290 pub fn raw_string(s: &str, pounds: usize) -> Literal {
291 Literal(imp::Literal::raw_string(s, pounds))
292 }
293
294 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
295 Literal(imp::Literal::raw_byte_string(s, pounds))
296 }
Alex Crichton852d53d2017-05-19 19:25:08 -0700297}
298
Alex Crichton1a7f7622017-07-05 17:47:15 -0700299pub struct TokenTreeIter(imp::TokenTreeIter);
Alex Crichton44bffbc2017-05-19 17:51:59 -0700300
Alex Crichton1a7f7622017-07-05 17:47:15 -0700301impl Iterator for TokenTreeIter {
Alex Crichton44bffbc2017-05-19 17:51:59 -0700302 type Item = TokenTree;
303
304 fn next(&mut self) -> Option<TokenTree> {
305 self.0.next()
306 }
307}
David Tolnaycb1b85f2017-06-03 16:40:35 -0700308
309forward_fmt!(Debug for LexError);
310forward_fmt!(Debug for Literal);
311forward_fmt!(Debug for Span);
Alex Crichton1a7f7622017-07-05 17:47:15 -0700312forward_fmt!(Debug for Term);
313forward_fmt!(Debug for TokenTreeIter);
David Tolnaycb1b85f2017-06-03 16:40:35 -0700314forward_fmt!(Debug for TokenStream);
315forward_fmt!(Display for Literal);
316forward_fmt!(Display for TokenStream);