Alex Crichton | babc99e | 2017-07-05 18:00:29 -0700 | [diff] [blame^] | 1 | //! 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 Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 22 | #![cfg_attr(feature = "unstable", feature(proc_macro))] |
| 23 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 24 | extern crate proc_macro; |
| 25 | |
Alex Crichton | e14e8fd | 2017-05-23 07:02:54 -0700 | [diff] [blame] | 26 | #[cfg(not(feature = "unstable"))] |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 27 | extern crate unicode_xid; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 28 | |
| 29 | use std::fmt; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 30 | use std::str::FromStr; |
| 31 | use std::iter::FromIterator; |
| 32 | |
David Tolnay | b103266 | 2017-05-31 15:52:28 -0700 | [diff] [blame] | 33 | #[macro_use] |
| 34 | #[cfg(not(feature = "unstable"))] |
| 35 | mod strnom; |
| 36 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 37 | #[path = "stable.rs"] |
Alex Crichton | e14e8fd | 2017-05-23 07:02:54 -0700 | [diff] [blame] | 38 | #[cfg(not(feature = "unstable"))] |
Alex Crichton | b15c635 | 2017-05-19 19:36:36 -0700 | [diff] [blame] | 39 | mod imp; |
| 40 | #[path = "unstable.rs"] |
Alex Crichton | e14e8fd | 2017-05-23 07:02:54 -0700 | [diff] [blame] | 41 | #[cfg(feature = "unstable")] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 42 | mod imp; |
| 43 | |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 44 | #[macro_use] |
| 45 | mod macros; |
| 46 | |
| 47 | #[derive(Clone)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 48 | pub struct TokenStream(imp::TokenStream); |
| 49 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 50 | pub struct LexError(imp::LexError); |
| 51 | |
| 52 | impl FromStr for TokenStream { |
| 53 | type Err = LexError; |
| 54 | |
| 55 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
| 56 | match src.parse() { |
| 57 | Ok(e) => Ok(TokenStream(e)), |
| 58 | Err(e) => Err(LexError(e)), |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 63 | impl From<proc_macro::TokenStream> for TokenStream { |
| 64 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
| 65 | TokenStream(inner.into()) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl From<TokenStream> for proc_macro::TokenStream { |
| 70 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
| 71 | inner.0.into() |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl From<TokenTree> for TokenStream { |
| 76 | fn from(tree: TokenTree) -> TokenStream { |
| 77 | TokenStream(tree.into()) |
| 78 | } |
| 79 | } |
| 80 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 81 | impl<T: Into<TokenStream>> FromIterator<T> for TokenStream { |
| 82 | fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self { |
| 83 | TokenStream(streams.into_iter().map(|t| t.into().0).collect()) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl IntoIterator for TokenStream { |
| 88 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 89 | type IntoIter = TokenTreeIter; |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 90 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 91 | fn into_iter(self) -> TokenTreeIter { |
| 92 | TokenTreeIter(self.0.into_iter()) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | impl TokenStream { |
| 97 | pub fn empty() -> TokenStream { |
| 98 | TokenStream(imp::TokenStream::empty()) |
| 99 | } |
| 100 | |
| 101 | pub fn is_empty(&self) -> bool { |
| 102 | self.0.is_empty() |
| 103 | } |
| 104 | } |
| 105 | |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 106 | #[derive(Copy, Clone)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 107 | pub struct Span(imp::Span); |
| 108 | |
| 109 | impl Default for Span { |
| 110 | fn default() -> Span { |
| 111 | Span(imp::Span::default()) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl Span { |
| 116 | pub fn call_site() -> Span { |
| 117 | Span(imp::Span::call_site()) |
| 118 | } |
| 119 | } |
| 120 | |
David Tolnay | 977f828 | 2017-05-31 17:41:33 -0700 | [diff] [blame] | 121 | #[derive(Clone, Debug)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 122 | pub struct TokenTree { |
| 123 | pub span: Span, |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 124 | pub kind: TokenNode, |
| 125 | } |
| 126 | |
| 127 | impl From<TokenNode> for TokenTree { |
| 128 | fn from(kind: TokenNode) -> TokenTree { |
| 129 | TokenTree { span: Span::default(), kind: kind } |
| 130 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 133 | impl fmt::Display for TokenTree { |
| 134 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 135 | TokenStream::from(self.clone()).fmt(f) |
| 136 | } |
| 137 | } |
| 138 | |
David Tolnay | 977f828 | 2017-05-31 17:41:33 -0700 | [diff] [blame] | 139 | #[derive(Clone, Debug)] |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 140 | pub enum TokenNode { |
| 141 | Group(Delimiter, TokenStream), |
| 142 | Term(Term), |
| 143 | Op(char, Spacing), |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 144 | Literal(Literal), |
| 145 | } |
| 146 | |
Michael Layzell | 5372f4b | 2017-06-02 10:29:31 -0400 | [diff] [blame] | 147 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 148 | pub enum Delimiter { |
| 149 | Parenthesis, |
| 150 | Brace, |
| 151 | Bracket, |
| 152 | None, |
| 153 | } |
| 154 | |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 155 | #[derive(Copy, Clone)] |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 156 | pub struct Term(imp::Term); |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 157 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 158 | impl Term { |
| 159 | pub fn intern(string: &str) -> Term { |
| 160 | Term(string.into()) |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 161 | } |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 162 | |
Alex Crichton | 4388125 | 2017-05-26 08:51:06 -0700 | [diff] [blame] | 163 | pub fn as_str(&self) -> &str { |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 164 | &self.0 |
| 165 | } |
| 166 | } |
| 167 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 168 | #[derive(Copy, Clone, Debug)] |
| 169 | pub enum Spacing { |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 170 | Alone, |
| 171 | Joint, |
| 172 | } |
| 173 | |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 174 | #[derive(Clone)] |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 175 | pub struct Literal(imp::Literal); |
| 176 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 177 | macro_rules! int_literals { |
| 178 | ($($kind:ident,)*) => ($( |
| 179 | pub fn $kind(n: $kind) -> Literal { |
| 180 | Literal(n.into()) |
| 181 | } |
| 182 | )*) |
| 183 | } |
| 184 | |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 185 | impl Literal { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 186 | pub fn integer(s: i64) -> Literal { |
| 187 | Literal(imp::Literal::integer(s)) |
| 188 | } |
| 189 | |
| 190 | int_literals! { |
| 191 | u8, u16, u32, u64, /*usize*/ |
| 192 | i8, i16, i32, i64, /*isize,*/ |
| 193 | } |
| 194 | |
| 195 | pub fn float(f: f64) -> Literal { |
| 196 | Literal(imp::Literal::float(f)) |
| 197 | } |
| 198 | |
| 199 | pub fn f64(f: f64) -> Literal { |
| 200 | Literal(f.into()) |
| 201 | } |
| 202 | |
| 203 | pub fn f32(f: f32) -> Literal { |
| 204 | Literal(f.into()) |
| 205 | } |
| 206 | |
| 207 | pub fn string(string: &str) -> Literal { |
| 208 | Literal(string.into()) |
| 209 | } |
| 210 | |
| 211 | pub fn character(ch: char) -> Literal { |
| 212 | Literal(ch.into()) |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Alex Crichton | 9c2fb0a | 2017-05-26 08:49:31 -0700 | [diff] [blame] | 215 | pub fn byte_string(s: &[u8]) -> Literal { |
| 216 | Literal(imp::Literal::byte_string(s)) |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 217 | } |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 218 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 219 | // ======================================================================= |
| 220 | // Not present upstream in proc_macro yet |
| 221 | |
| 222 | pub fn byte_char(b: u8) -> Literal { |
| 223 | Literal(imp::Literal::byte_char(b)) |
| 224 | } |
| 225 | |
Alex Crichton | 76a5cc8 | 2017-05-23 07:01:44 -0700 | [diff] [blame] | 226 | pub fn doccomment(s: &str) -> Literal { |
| 227 | Literal(imp::Literal::doccomment(s)) |
| 228 | } |
Alex Crichton | 9c2fb0a | 2017-05-26 08:49:31 -0700 | [diff] [blame] | 229 | |
Alex Crichton | 3131662 | 2017-05-26 12:54:47 -0700 | [diff] [blame] | 230 | pub fn raw_string(s: &str, pounds: usize) -> Literal { |
| 231 | Literal(imp::Literal::raw_string(s, pounds)) |
| 232 | } |
| 233 | |
| 234 | pub fn raw_byte_string(s: &str, pounds: usize) -> Literal { |
| 235 | Literal(imp::Literal::raw_byte_string(s, pounds)) |
| 236 | } |
Alex Crichton | 852d53d | 2017-05-19 19:25:08 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 239 | pub struct TokenTreeIter(imp::TokenTreeIter); |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 240 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 241 | impl Iterator for TokenTreeIter { |
Alex Crichton | 44bffbc | 2017-05-19 17:51:59 -0700 | [diff] [blame] | 242 | type Item = TokenTree; |
| 243 | |
| 244 | fn next(&mut self) -> Option<TokenTree> { |
| 245 | self.0.next() |
| 246 | } |
| 247 | } |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 248 | |
| 249 | forward_fmt!(Debug for LexError); |
| 250 | forward_fmt!(Debug for Literal); |
| 251 | forward_fmt!(Debug for Span); |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 252 | forward_fmt!(Debug for Term); |
| 253 | forward_fmt!(Debug for TokenTreeIter); |
David Tolnay | cb1b85f | 2017-06-03 16:40:35 -0700 | [diff] [blame] | 254 | forward_fmt!(Debug for TokenStream); |
| 255 | forward_fmt!(Display for Literal); |
| 256 | forward_fmt!(Display for TokenStream); |