David Tolnay | 5553501 | 2018-01-05 16:39:23 -0800 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 9 | //! Tokens representing Rust punctuation, keywords, and delimiters. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 10 | //! |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 11 | //! The type names in this module can be difficult to keep straight, so we |
| 12 | //! prefer to use the [`Token!`] macro instead. This is a type-macro that |
| 13 | //! expands to the token type of the given token. |
| 14 | //! |
| 15 | //! [`Token!`]: ../macro.Token.html |
| 16 | //! |
| 17 | //! # Example |
| 18 | //! |
| 19 | //! The [`ItemStatic`] syntax tree node is defined like this. |
| 20 | //! |
| 21 | //! [`ItemStatic`]: ../struct.ItemStatic.html |
| 22 | //! |
| 23 | //! ``` |
| 24 | //! # #[macro_use] |
| 25 | //! # extern crate syn; |
Alex Crichton | f0fea9a | 2018-05-17 11:19:34 -0700 | [diff] [blame] | 26 | //! # extern crate proc_macro2; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 27 | //! # |
Alex Crichton | f0fea9a | 2018-05-17 11:19:34 -0700 | [diff] [blame] | 28 | //! # use proc_macro2::Ident; |
| 29 | //! # use syn::{Attribute, Visibility, Type, Expr}; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 30 | //! # |
| 31 | //! pub struct ItemStatic { |
| 32 | //! pub attrs: Vec<Attribute>, |
| 33 | //! pub vis: Visibility, |
| 34 | //! pub static_token: Token![static], |
| 35 | //! pub mutability: Option<Token![mut]>, |
| 36 | //! pub ident: Ident, |
| 37 | //! pub colon_token: Token![:], |
| 38 | //! pub ty: Box<Type>, |
| 39 | //! pub eq_token: Token![=], |
| 40 | //! pub expr: Box<Expr>, |
| 41 | //! pub semi_token: Token![;], |
| 42 | //! } |
| 43 | //! # |
| 44 | //! # fn main() {} |
| 45 | //! ``` |
| 46 | //! |
| 47 | //! # Parsing |
| 48 | //! |
| 49 | //! These tokens can be parsed using the [`Synom`] trait and the parser |
| 50 | //! combinator macros [`punct!`], [`keyword!`], [`parens!`], [`braces!`], and |
| 51 | //! [`brackets!`]. |
| 52 | //! |
| 53 | //! [`Synom`]: ../synom/trait.Synom.html |
| 54 | //! [`punct!`]: ../macro.punct.html |
| 55 | //! [`keyword!`]: ../macro.keyword.html |
| 56 | //! [`parens!`]: ../macro.parens.html |
| 57 | //! [`braces!`]: ../macro.braces.html |
| 58 | //! [`brackets!`]: ../macro.brackets.html |
| 59 | //! |
| 60 | //! ``` |
| 61 | //! #[macro_use] |
| 62 | //! extern crate syn; |
Alex Crichton | f0fea9a | 2018-05-17 11:19:34 -0700 | [diff] [blame] | 63 | //! extern crate proc_macro2; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 64 | //! |
Alex Crichton | f0fea9a | 2018-05-17 11:19:34 -0700 | [diff] [blame] | 65 | //! use proc_macro2::Ident; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 66 | //! use syn::synom::Synom; |
Alex Crichton | f0fea9a | 2018-05-17 11:19:34 -0700 | [diff] [blame] | 67 | //! use syn::{Attribute, Visibility, Type, Expr}; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 68 | //! # |
| 69 | //! # struct ItemStatic; |
| 70 | //! # use syn::ItemStatic as SynItemStatic; |
| 71 | //! |
| 72 | //! // Parse the ItemStatic struct shown above. |
| 73 | //! impl Synom for ItemStatic { |
| 74 | //! named!(parse -> Self, do_parse!( |
| 75 | //! # (ItemStatic) |
| 76 | //! # )); |
| 77 | //! # } |
| 78 | //! # |
| 79 | //! # mod example { |
| 80 | //! # use super::*; |
| 81 | //! # use super::SynItemStatic as ItemStatic; |
| 82 | //! # |
| 83 | //! # named!(parse -> ItemStatic, do_parse!( |
| 84 | //! attrs: many0!(Attribute::parse_outer) >> |
| 85 | //! vis: syn!(Visibility) >> |
| 86 | //! static_token: keyword!(static) >> |
| 87 | //! mutability: option!(keyword!(mut)) >> |
| 88 | //! ident: syn!(Ident) >> |
| 89 | //! colon_token: punct!(:) >> |
| 90 | //! ty: syn!(Type) >> |
| 91 | //! eq_token: punct!(=) >> |
| 92 | //! expr: syn!(Expr) >> |
| 93 | //! semi_token: punct!(;) >> |
| 94 | //! (ItemStatic { |
| 95 | //! attrs, vis, static_token, mutability, ident, colon_token, |
| 96 | //! ty: Box::new(ty), eq_token, expr: Box::new(expr), semi_token, |
| 97 | //! }) |
| 98 | //! )); |
| 99 | //! } |
| 100 | //! # |
| 101 | //! # fn main() {} |
| 102 | //! ``` |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 103 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 104 | use proc_macro2::{Span, Ident}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 105 | |
| 106 | macro_rules! tokens { |
| 107 | ( |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 108 | punct: { |
| 109 | $($punct:tt pub struct $punct_name:ident/$len:tt #[$punct_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 110 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 111 | delimiter: { |
| 112 | $($delimiter:tt pub struct $delimiter_name:ident #[$delimiter_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 113 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 114 | keyword: { |
| 115 | $($keyword:tt pub struct $keyword_name:ident #[$keyword_doc:meta])* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 116 | } |
| 117 | ) => ( |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 118 | $(token_punct_def! { #[$punct_doc] $punct pub struct $punct_name/$len })* |
| 119 | $(token_punct_parser! { $punct pub struct $punct_name })* |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 120 | $(token_delimiter! { #[$delimiter_doc] $delimiter pub struct $delimiter_name })* |
| 121 | $(token_keyword! { #[$keyword_doc] $keyword pub struct $keyword_name })* |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 122 | ) |
| 123 | } |
| 124 | |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 125 | macro_rules! token_punct_def { |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 126 | (#[$doc:meta] $s:tt pub struct $name:ident / $len:tt) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 127 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 128 | #[$doc] |
David Tolnay | 1bb760c | 2018-01-07 11:18:15 -0800 | [diff] [blame] | 129 | /// |
| 130 | /// Don't try to remember the name of this type -- use the [`Token!`] |
| 131 | /// macro instead. |
| 132 | /// |
| 133 | /// [`Token!`]: index.html |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 134 | pub struct $name(pub [Span; $len]); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 135 | |
David Tolnay | 0bdb055 | 2017-12-27 21:31:51 -0500 | [diff] [blame] | 136 | impl $name { |
| 137 | pub fn new(span: Span) -> Self { |
| 138 | $name([span; $len]) |
| 139 | } |
| 140 | } |
| 141 | |
David Tolnay | 66bb8d5 | 2018-01-08 08:22:31 -0800 | [diff] [blame] | 142 | impl ::std::default::Default for $name { |
| 143 | fn default() -> Self { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 144 | $name([Span::call_site(); $len]) |
David Tolnay | 66bb8d5 | 2018-01-08 08:22:31 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 148 | #[cfg(feature = "extra-traits")] |
| 149 | impl ::std::fmt::Debug for $name { |
| 150 | fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 151 | f.write_str(stringify!($name)) |
Nika Layzell | d73a365 | 2017-10-24 08:57:05 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 155 | #[cfg(feature = "extra-traits")] |
| 156 | impl ::std::cmp::Eq for $name {} |
| 157 | |
| 158 | #[cfg(feature = "extra-traits")] |
| 159 | impl ::std::cmp::PartialEq for $name { |
| 160 | fn eq(&self, _other: &$name) -> bool { |
| 161 | true |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | #[cfg(feature = "extra-traits")] |
| 166 | impl ::std::hash::Hash for $name { |
| 167 | fn hash<H>(&self, _state: &mut H) |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 168 | where |
| 169 | H: ::std::hash::Hasher, |
| 170 | { |
| 171 | } |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 174 | impl From<Span> for $name { |
| 175 | fn from(span: Span) -> Self { |
| 176 | $name([span; $len]) |
| 177 | } |
| 178 | } |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 179 | }; |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | macro_rules! token_punct_parser { |
| 183 | ($s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 184 | #[cfg(feature = "printing")] |
| 185 | impl ::quote::ToTokens for $name { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 186 | fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 187 | printing::punct($s, &self.0, tokens); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | #[cfg(feature = "parsing")] |
| 192 | impl ::Synom for $name { |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 193 | fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 194 | parsing::punct($s, tokens, $name) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 195 | } |
Sergio Benitez | 5680d6a | 2017-12-29 11:20:29 -0800 | [diff] [blame] | 196 | |
| 197 | fn description() -> Option<&'static str> { |
| 198 | Some(concat!("`", $s, "`")) |
| 199 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 200 | } |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 201 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 202 | } |
| 203 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 204 | macro_rules! token_keyword { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 205 | (#[$doc:meta] $s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 206 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 207 | #[$doc] |
David Tolnay | 1bb760c | 2018-01-07 11:18:15 -0800 | [diff] [blame] | 208 | /// |
| 209 | /// Don't try to remember the name of this type -- use the [`Token!`] |
| 210 | /// macro instead. |
| 211 | /// |
| 212 | /// [`Token!`]: index.html |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 213 | pub struct $name(pub Span); |
| 214 | |
David Tolnay | 66bb8d5 | 2018-01-08 08:22:31 -0800 | [diff] [blame] | 215 | impl ::std::default::Default for $name { |
| 216 | fn default() -> Self { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 217 | $name(Span::call_site()) |
David Tolnay | 66bb8d5 | 2018-01-08 08:22:31 -0800 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 221 | #[cfg(feature = "extra-traits")] |
| 222 | impl ::std::fmt::Debug for $name { |
| 223 | fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 224 | f.write_str(stringify!($name)) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #[cfg(feature = "extra-traits")] |
| 229 | impl ::std::cmp::Eq for $name {} |
| 230 | |
| 231 | #[cfg(feature = "extra-traits")] |
| 232 | impl ::std::cmp::PartialEq for $name { |
| 233 | fn eq(&self, _other: &$name) -> bool { |
| 234 | true |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #[cfg(feature = "extra-traits")] |
| 239 | impl ::std::hash::Hash for $name { |
| 240 | fn hash<H>(&self, _state: &mut H) |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 241 | where |
| 242 | H: ::std::hash::Hasher, |
| 243 | { |
| 244 | } |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 247 | #[cfg(feature = "printing")] |
| 248 | impl ::quote::ToTokens for $name { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 249 | fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 250 | printing::keyword($s, &self.0, tokens); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | #[cfg(feature = "parsing")] |
| 255 | impl ::Synom for $name { |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 256 | fn parse(tokens: $crate::buffer::Cursor) -> $crate::synom::PResult<$name> { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 257 | parsing::keyword($s, tokens, $name) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 258 | } |
David Tolnay | 7977733 | 2018-01-07 10:04:42 -0800 | [diff] [blame] | 259 | |
| 260 | fn description() -> Option<&'static str> { |
| 261 | Some(concat!("`", $s, "`")) |
| 262 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 263 | } |
강동윤 | d5229da | 2018-01-12 13:11:33 +0900 | [diff] [blame] | 264 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 265 | impl From<$name> for Ident { |
| 266 | fn from(me: $name) -> Ident { |
| 267 | Ident::new($s, me.0) |
| 268 | } |
| 269 | } |
| 270 | |
강동윤 | d5229da | 2018-01-12 13:11:33 +0900 | [diff] [blame] | 271 | impl From<Span> for $name { |
| 272 | fn from(span: Span) -> Self { |
| 273 | $name(span) |
| 274 | } |
| 275 | } |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 276 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 277 | } |
| 278 | |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 279 | macro_rules! token_delimiter { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 280 | (#[$doc:meta] $s:tt pub struct $name:ident) => { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 281 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 282 | #[$doc] |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 283 | pub struct $name(pub Span); |
| 284 | |
David Tolnay | 66bb8d5 | 2018-01-08 08:22:31 -0800 | [diff] [blame] | 285 | impl ::std::default::Default for $name { |
| 286 | fn default() -> Self { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 287 | $name(Span::call_site()) |
David Tolnay | 66bb8d5 | 2018-01-08 08:22:31 -0800 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 291 | #[cfg(feature = "extra-traits")] |
| 292 | impl ::std::fmt::Debug for $name { |
| 293 | fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 294 | f.write_str(stringify!($name)) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | #[cfg(feature = "extra-traits")] |
| 299 | impl ::std::cmp::Eq for $name {} |
| 300 | |
| 301 | #[cfg(feature = "extra-traits")] |
| 302 | impl ::std::cmp::PartialEq for $name { |
| 303 | fn eq(&self, _other: &$name) -> bool { |
| 304 | true |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #[cfg(feature = "extra-traits")] |
| 309 | impl ::std::hash::Hash for $name { |
| 310 | fn hash<H>(&self, _state: &mut H) |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 311 | where |
| 312 | H: ::std::hash::Hasher, |
| 313 | { |
| 314 | } |
David Tolnay | 9894256 | 2017-12-26 21:24:35 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 317 | impl $name { |
| 318 | #[cfg(feature = "printing")] |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 319 | pub fn surround<F>(&self, tokens: &mut ::proc_macro2::TokenStream, f: F) |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 320 | where |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 321 | F: FnOnce(&mut ::proc_macro2::TokenStream), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 322 | { |
| 323 | printing::delim($s, &self.0, tokens, f); |
| 324 | } |
| 325 | |
| 326 | #[cfg(feature = "parsing")] |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 327 | pub fn parse<F, R>( |
| 328 | tokens: $crate::buffer::Cursor, |
| 329 | f: F, |
| 330 | ) -> $crate::synom::PResult<($name, R)> |
| 331 | where |
| 332 | F: FnOnce($crate::buffer::Cursor) -> $crate::synom::PResult<R>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 333 | { |
| 334 | parsing::delim($s, tokens, $name, f) |
| 335 | } |
| 336 | } |
강동윤 | d5229da | 2018-01-12 13:11:33 +0900 | [diff] [blame] | 337 | |
| 338 | impl From<Span> for $name { |
| 339 | fn from(span: Span) -> Self { |
| 340 | $name(span) |
| 341 | } |
| 342 | } |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 343 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 346 | token_punct_def! { |
| 347 | /// `_` |
| 348 | "_" pub struct Underscore/1 |
| 349 | } |
| 350 | |
| 351 | #[cfg(feature = "printing")] |
| 352 | impl ::quote::ToTokens for Underscore { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 353 | fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { |
| 354 | use quote::TokenStreamExt; |
| 355 | tokens.append(::proc_macro2::Ident::new("_", self.0[0])); |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | #[cfg(feature = "parsing")] |
| 360 | impl ::Synom for Underscore { |
| 361 | fn parse(input: ::buffer::Cursor) -> ::synom::PResult<Underscore> { |
| 362 | match input.term() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 363 | Some((term, rest)) => { |
| 364 | if term.to_string() == "_" { |
| 365 | Ok((Underscore([term.span()]), rest)) |
| 366 | } else { |
| 367 | ::parse_error() |
| 368 | } |
| 369 | } |
David Tolnay | 94d2b79 | 2018-04-29 12:26:10 -0700 | [diff] [blame] | 370 | None => parsing::punct("_", input, Underscore), |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | fn description() -> Option<&'static str> { |
| 375 | Some("`_`") |
| 376 | } |
| 377 | } |
| 378 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 379 | tokens! { |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 380 | punct: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 381 | "+" pub struct Add/1 /// `+` |
| 382 | "+=" pub struct AddEq/2 /// `+=` |
| 383 | "&" pub struct And/1 /// `&` |
| 384 | "&&" pub struct AndAnd/2 /// `&&` |
| 385 | "&=" pub struct AndEq/2 /// `&=` |
| 386 | "@" pub struct At/1 /// `@` |
| 387 | "!" pub struct Bang/1 /// `!` |
| 388 | "^" pub struct Caret/1 /// `^` |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 389 | "^=" pub struct CaretEq/2 /// `^=` |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 390 | ":" pub struct Colon/1 /// `:` |
| 391 | "::" pub struct Colon2/2 /// `::` |
| 392 | "," pub struct Comma/1 /// `,` |
| 393 | "/" pub struct Div/1 /// `/` |
| 394 | "/=" pub struct DivEq/2 /// `/=` |
MSleepyPanda | f5137d7 | 2018-05-05 18:57:10 +0200 | [diff] [blame] | 395 | "$" pub struct Dollar/1 /// `$` |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 396 | "." pub struct Dot/1 /// `.` |
| 397 | ".." pub struct Dot2/2 /// `..` |
| 398 | "..." pub struct Dot3/3 /// `...` |
| 399 | "..=" pub struct DotDotEq/3 /// `..=` |
| 400 | "=" pub struct Eq/1 /// `=` |
| 401 | "==" pub struct EqEq/2 /// `==` |
| 402 | ">=" pub struct Ge/2 /// `>=` |
| 403 | ">" pub struct Gt/1 /// `>` |
| 404 | "<=" pub struct Le/2 /// `<=` |
| 405 | "<" pub struct Lt/1 /// `<` |
| 406 | "*=" pub struct MulEq/2 /// `*=` |
| 407 | "!=" pub struct Ne/2 /// `!=` |
| 408 | "|" pub struct Or/1 /// `|` |
| 409 | "|=" pub struct OrEq/2 /// `|=` |
| 410 | "||" pub struct OrOr/2 /// `||` |
| 411 | "#" pub struct Pound/1 /// `#` |
| 412 | "?" pub struct Question/1 /// `?` |
| 413 | "->" pub struct RArrow/2 /// `->` |
| 414 | "<-" pub struct LArrow/2 /// `<-` |
| 415 | "%" pub struct Rem/1 /// `%` |
| 416 | "%=" pub struct RemEq/2 /// `%=` |
David Tolnay | 1762415 | 2018-03-31 18:11:40 +0200 | [diff] [blame] | 417 | "=>" pub struct FatArrow/2 /// `=>` |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 418 | ";" pub struct Semi/1 /// `;` |
| 419 | "<<" pub struct Shl/2 /// `<<` |
| 420 | "<<=" pub struct ShlEq/3 /// `<<=` |
| 421 | ">>" pub struct Shr/2 /// `>>` |
| 422 | ">>=" pub struct ShrEq/3 /// `>>=` |
| 423 | "*" pub struct Star/1 /// `*` |
| 424 | "-" pub struct Sub/1 /// `-` |
| 425 | "-=" pub struct SubEq/2 /// `-=` |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 426 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 427 | delimiter: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 428 | "{" pub struct Brace /// `{...}` |
| 429 | "[" pub struct Bracket /// `[...]` |
| 430 | "(" pub struct Paren /// `(...)` |
| 431 | " " pub struct Group /// None-delimited group |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 432 | } |
David Tolnay | 73c98de | 2017-12-31 15:56:56 -0500 | [diff] [blame] | 433 | keyword: { |
David Tolnay | 5a20f63 | 2017-12-26 22:11:28 -0500 | [diff] [blame] | 434 | "as" pub struct As /// `as` |
| 435 | "auto" pub struct Auto /// `auto` |
| 436 | "box" pub struct Box /// `box` |
| 437 | "break" pub struct Break /// `break` |
| 438 | "Self" pub struct CapSelf /// `Self` |
| 439 | "catch" pub struct Catch /// `catch` |
| 440 | "const" pub struct Const /// `const` |
| 441 | "continue" pub struct Continue /// `continue` |
| 442 | "crate" pub struct Crate /// `crate` |
| 443 | "default" pub struct Default /// `default` |
| 444 | "do" pub struct Do /// `do` |
| 445 | "dyn" pub struct Dyn /// `dyn` |
| 446 | "else" pub struct Else /// `else` |
| 447 | "enum" pub struct Enum /// `enum` |
| 448 | "extern" pub struct Extern /// `extern` |
| 449 | "fn" pub struct Fn /// `fn` |
| 450 | "for" pub struct For /// `for` |
| 451 | "if" pub struct If /// `if` |
| 452 | "impl" pub struct Impl /// `impl` |
| 453 | "in" pub struct In /// `in` |
| 454 | "let" pub struct Let /// `let` |
| 455 | "loop" pub struct Loop /// `loop` |
| 456 | "macro" pub struct Macro /// `macro` |
| 457 | "match" pub struct Match /// `match` |
| 458 | "mod" pub struct Mod /// `mod` |
| 459 | "move" pub struct Move /// `move` |
| 460 | "mut" pub struct Mut /// `mut` |
| 461 | "pub" pub struct Pub /// `pub` |
| 462 | "ref" pub struct Ref /// `ref` |
| 463 | "return" pub struct Return /// `return` |
| 464 | "self" pub struct Self_ /// `self` |
| 465 | "static" pub struct Static /// `static` |
| 466 | "struct" pub struct Struct /// `struct` |
| 467 | "super" pub struct Super /// `super` |
| 468 | "trait" pub struct Trait /// `trait` |
| 469 | "type" pub struct Type /// `type` |
| 470 | "union" pub struct Union /// `union` |
| 471 | "unsafe" pub struct Unsafe /// `unsafe` |
| 472 | "use" pub struct Use /// `use` |
| 473 | "where" pub struct Where /// `where` |
| 474 | "while" pub struct While /// `while` |
| 475 | "yield" pub struct Yield /// `yield` |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
David Tolnay | f005f96 | 2018-01-06 21:19:41 -0800 | [diff] [blame] | 479 | /// A type-macro that expands to the name of the Rust type representation of a |
| 480 | /// given token. |
| 481 | /// |
| 482 | /// See the [token module] documentation for details and examples. |
| 483 | /// |
| 484 | /// [token module]: token/index.html |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 485 | // Unfortunate duplication due to a rustdoc bug. |
| 486 | // https://github.com/rust-lang/rust/issues/45939 |
| 487 | #[macro_export] |
David Tolnay | a5ed2fe | 2018-04-29 12:23:34 -0700 | [diff] [blame] | 488 | #[cfg_attr(rustfmt, rustfmt_skip)] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 489 | macro_rules! Token { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 490 | (+) => { $crate::token::Add }; |
| 491 | (+=) => { $crate::token::AddEq }; |
| 492 | (&) => { $crate::token::And }; |
| 493 | (&&) => { $crate::token::AndAnd }; |
| 494 | (&=) => { $crate::token::AndEq }; |
| 495 | (@) => { $crate::token::At }; |
| 496 | (!) => { $crate::token::Bang }; |
| 497 | (^) => { $crate::token::Caret }; |
| 498 | (^=) => { $crate::token::CaretEq }; |
| 499 | (:) => { $crate::token::Colon }; |
| 500 | (::) => { $crate::token::Colon2 }; |
| 501 | (,) => { $crate::token::Comma }; |
| 502 | (/) => { $crate::token::Div }; |
| 503 | (/=) => { $crate::token::DivEq }; |
| 504 | (.) => { $crate::token::Dot }; |
| 505 | (..) => { $crate::token::Dot2 }; |
| 506 | (...) => { $crate::token::Dot3 }; |
| 507 | (..=) => { $crate::token::DotDotEq }; |
| 508 | (=) => { $crate::token::Eq }; |
| 509 | (==) => { $crate::token::EqEq }; |
| 510 | (>=) => { $crate::token::Ge }; |
| 511 | (>) => { $crate::token::Gt }; |
| 512 | (<=) => { $crate::token::Le }; |
| 513 | (<) => { $crate::token::Lt }; |
| 514 | (*=) => { $crate::token::MulEq }; |
| 515 | (!=) => { $crate::token::Ne }; |
| 516 | (|) => { $crate::token::Or }; |
| 517 | (|=) => { $crate::token::OrEq }; |
| 518 | (||) => { $crate::token::OrOr }; |
| 519 | (#) => { $crate::token::Pound }; |
| 520 | (?) => { $crate::token::Question }; |
| 521 | (->) => { $crate::token::RArrow }; |
| 522 | (<-) => { $crate::token::LArrow }; |
| 523 | (%) => { $crate::token::Rem }; |
| 524 | (%=) => { $crate::token::RemEq }; |
David Tolnay | 1762415 | 2018-03-31 18:11:40 +0200 | [diff] [blame] | 525 | (=>) => { $crate::token::FatArrow }; |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 526 | (;) => { $crate::token::Semi }; |
| 527 | (<<) => { $crate::token::Shl }; |
| 528 | (<<=) => { $crate::token::ShlEq }; |
| 529 | (>>) => { $crate::token::Shr }; |
| 530 | (>>=) => { $crate::token::ShrEq }; |
| 531 | (*) => { $crate::token::Star }; |
| 532 | (-) => { $crate::token::Sub }; |
| 533 | (-=) => { $crate::token::SubEq }; |
| 534 | (_) => { $crate::token::Underscore }; |
| 535 | (as) => { $crate::token::As }; |
| 536 | (auto) => { $crate::token::Auto }; |
| 537 | (box) => { $crate::token::Box }; |
| 538 | (break) => { $crate::token::Break }; |
| 539 | (Self) => { $crate::token::CapSelf }; |
| 540 | (catch) => { $crate::token::Catch }; |
| 541 | (const) => { $crate::token::Const }; |
| 542 | (continue) => { $crate::token::Continue }; |
| 543 | (crate) => { $crate::token::Crate }; |
| 544 | (default) => { $crate::token::Default }; |
| 545 | (do) => { $crate::token::Do }; |
| 546 | (dyn) => { $crate::token::Dyn }; |
| 547 | (else) => { $crate::token::Else }; |
| 548 | (enum) => { $crate::token::Enum }; |
| 549 | (extern) => { $crate::token::Extern }; |
| 550 | (fn) => { $crate::token::Fn }; |
| 551 | (for) => { $crate::token::For }; |
| 552 | (if) => { $crate::token::If }; |
| 553 | (impl) => { $crate::token::Impl }; |
| 554 | (in) => { $crate::token::In }; |
| 555 | (let) => { $crate::token::Let }; |
| 556 | (loop) => { $crate::token::Loop }; |
| 557 | (macro) => { $crate::token::Macro }; |
| 558 | (match) => { $crate::token::Match }; |
| 559 | (mod) => { $crate::token::Mod }; |
| 560 | (move) => { $crate::token::Move }; |
| 561 | (mut) => { $crate::token::Mut }; |
| 562 | (pub) => { $crate::token::Pub }; |
| 563 | (ref) => { $crate::token::Ref }; |
| 564 | (return) => { $crate::token::Return }; |
| 565 | (self) => { $crate::token::Self_ }; |
| 566 | (static) => { $crate::token::Static }; |
| 567 | (struct) => { $crate::token::Struct }; |
| 568 | (super) => { $crate::token::Super }; |
| 569 | (trait) => { $crate::token::Trait }; |
| 570 | (type) => { $crate::token::Type }; |
| 571 | (union) => { $crate::token::Union }; |
| 572 | (unsafe) => { $crate::token::Unsafe }; |
| 573 | (use) => { $crate::token::Use }; |
| 574 | (where) => { $crate::token::Where }; |
| 575 | (while) => { $crate::token::While }; |
| 576 | (yield) => { $crate::token::Yield }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 577 | } |
| 578 | |
David Tolnay | f005f96 | 2018-01-06 21:19:41 -0800 | [diff] [blame] | 579 | /// Parse a single Rust punctuation token. |
| 580 | /// |
| 581 | /// See the [token module] documentation for details and examples. |
| 582 | /// |
| 583 | /// [token module]: token/index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 584 | /// |
| 585 | /// *This macro is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | 0fbe328 | 2017-12-26 21:46:16 -0500 | [diff] [blame] | 586 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 587 | #[macro_export] |
David Tolnay | a5ed2fe | 2018-04-29 12:23:34 -0700 | [diff] [blame] | 588 | #[cfg_attr(rustfmt, rustfmt_skip)] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 589 | macro_rules! punct { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 590 | ($i:expr, +) => { call!($i, <$crate::token::Add as $crate::synom::Synom>::parse) }; |
| 591 | ($i:expr, +=) => { call!($i, <$crate::token::AddEq as $crate::synom::Synom>::parse) }; |
| 592 | ($i:expr, &) => { call!($i, <$crate::token::And as $crate::synom::Synom>::parse) }; |
| 593 | ($i:expr, &&) => { call!($i, <$crate::token::AndAnd as $crate::synom::Synom>::parse) }; |
| 594 | ($i:expr, &=) => { call!($i, <$crate::token::AndEq as $crate::synom::Synom>::parse) }; |
| 595 | ($i:expr, @) => { call!($i, <$crate::token::At as $crate::synom::Synom>::parse) }; |
| 596 | ($i:expr, !) => { call!($i, <$crate::token::Bang as $crate::synom::Synom>::parse) }; |
| 597 | ($i:expr, ^) => { call!($i, <$crate::token::Caret as $crate::synom::Synom>::parse) }; |
| 598 | ($i:expr, ^=) => { call!($i, <$crate::token::CaretEq as $crate::synom::Synom>::parse) }; |
| 599 | ($i:expr, :) => { call!($i, <$crate::token::Colon as $crate::synom::Synom>::parse) }; |
| 600 | ($i:expr, ::) => { call!($i, <$crate::token::Colon2 as $crate::synom::Synom>::parse) }; |
| 601 | ($i:expr, ,) => { call!($i, <$crate::token::Comma as $crate::synom::Synom>::parse) }; |
| 602 | ($i:expr, /) => { call!($i, <$crate::token::Div as $crate::synom::Synom>::parse) }; |
| 603 | ($i:expr, /=) => { call!($i, <$crate::token::DivEq as $crate::synom::Synom>::parse) }; |
| 604 | ($i:expr, .) => { call!($i, <$crate::token::Dot as $crate::synom::Synom>::parse) }; |
| 605 | ($i:expr, ..) => { call!($i, <$crate::token::Dot2 as $crate::synom::Synom>::parse) }; |
| 606 | ($i:expr, ...) => { call!($i, <$crate::token::Dot3 as $crate::synom::Synom>::parse) }; |
| 607 | ($i:expr, ..=) => { call!($i, <$crate::token::DotDotEq as $crate::synom::Synom>::parse) }; |
| 608 | ($i:expr, =) => { call!($i, <$crate::token::Eq as $crate::synom::Synom>::parse) }; |
| 609 | ($i:expr, ==) => { call!($i, <$crate::token::EqEq as $crate::synom::Synom>::parse) }; |
| 610 | ($i:expr, >=) => { call!($i, <$crate::token::Ge as $crate::synom::Synom>::parse) }; |
| 611 | ($i:expr, >) => { call!($i, <$crate::token::Gt as $crate::synom::Synom>::parse) }; |
| 612 | ($i:expr, <=) => { call!($i, <$crate::token::Le as $crate::synom::Synom>::parse) }; |
| 613 | ($i:expr, <) => { call!($i, <$crate::token::Lt as $crate::synom::Synom>::parse) }; |
| 614 | ($i:expr, *=) => { call!($i, <$crate::token::MulEq as $crate::synom::Synom>::parse) }; |
| 615 | ($i:expr, !=) => { call!($i, <$crate::token::Ne as $crate::synom::Synom>::parse) }; |
| 616 | ($i:expr, |) => { call!($i, <$crate::token::Or as $crate::synom::Synom>::parse) }; |
| 617 | ($i:expr, |=) => { call!($i, <$crate::token::OrEq as $crate::synom::Synom>::parse) }; |
| 618 | ($i:expr, ||) => { call!($i, <$crate::token::OrOr as $crate::synom::Synom>::parse) }; |
| 619 | ($i:expr, #) => { call!($i, <$crate::token::Pound as $crate::synom::Synom>::parse) }; |
| 620 | ($i:expr, ?) => { call!($i, <$crate::token::Question as $crate::synom::Synom>::parse) }; |
| 621 | ($i:expr, ->) => { call!($i, <$crate::token::RArrow as $crate::synom::Synom>::parse) }; |
| 622 | ($i:expr, <-) => { call!($i, <$crate::token::LArrow as $crate::synom::Synom>::parse) }; |
| 623 | ($i:expr, %) => { call!($i, <$crate::token::Rem as $crate::synom::Synom>::parse) }; |
| 624 | ($i:expr, %=) => { call!($i, <$crate::token::RemEq as $crate::synom::Synom>::parse) }; |
David Tolnay | 1762415 | 2018-03-31 18:11:40 +0200 | [diff] [blame] | 625 | ($i:expr, =>) => { call!($i, <$crate::token::FatArrow as $crate::synom::Synom>::parse) }; |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 626 | ($i:expr, ;) => { call!($i, <$crate::token::Semi as $crate::synom::Synom>::parse) }; |
| 627 | ($i:expr, <<) => { call!($i, <$crate::token::Shl as $crate::synom::Synom>::parse) }; |
| 628 | ($i:expr, <<=) => { call!($i, <$crate::token::ShlEq as $crate::synom::Synom>::parse) }; |
| 629 | ($i:expr, >>) => { call!($i, <$crate::token::Shr as $crate::synom::Synom>::parse) }; |
| 630 | ($i:expr, >>=) => { call!($i, <$crate::token::ShrEq as $crate::synom::Synom>::parse) }; |
| 631 | ($i:expr, *) => { call!($i, <$crate::token::Star as $crate::synom::Synom>::parse) }; |
| 632 | ($i:expr, -) => { call!($i, <$crate::token::Sub as $crate::synom::Synom>::parse) }; |
| 633 | ($i:expr, -=) => { call!($i, <$crate::token::SubEq as $crate::synom::Synom>::parse) }; |
| 634 | ($i:expr, _) => { call!($i, <$crate::token::Underscore as $crate::synom::Synom>::parse) }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 635 | } |
| 636 | |
David Tolnay | f005f96 | 2018-01-06 21:19:41 -0800 | [diff] [blame] | 637 | /// Parse a single Rust keyword token. |
| 638 | /// |
| 639 | /// See the [token module] documentation for details and examples. |
| 640 | /// |
| 641 | /// [token module]: token/index.html |
David Tolnay | 461d98e | 2018-01-07 11:07:19 -0800 | [diff] [blame] | 642 | /// |
| 643 | /// *This macro is available if Syn is built with the `"parsing"` feature.* |
David Tolnay | 0fbe328 | 2017-12-26 21:46:16 -0500 | [diff] [blame] | 644 | #[cfg(feature = "parsing")] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 645 | #[macro_export] |
David Tolnay | a5ed2fe | 2018-04-29 12:23:34 -0700 | [diff] [blame] | 646 | #[cfg_attr(rustfmt, rustfmt_skip)] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 647 | macro_rules! keyword { |
David Tolnay | 32954ef | 2017-12-26 22:43:16 -0500 | [diff] [blame] | 648 | ($i:expr, as) => { call!($i, <$crate::token::As as $crate::synom::Synom>::parse) }; |
| 649 | ($i:expr, auto) => { call!($i, <$crate::token::Auto as $crate::synom::Synom>::parse) }; |
| 650 | ($i:expr, box) => { call!($i, <$crate::token::Box as $crate::synom::Synom>::parse) }; |
| 651 | ($i:expr, break) => { call!($i, <$crate::token::Break as $crate::synom::Synom>::parse) }; |
| 652 | ($i:expr, Self) => { call!($i, <$crate::token::CapSelf as $crate::synom::Synom>::parse) }; |
| 653 | ($i:expr, catch) => { call!($i, <$crate::token::Catch as $crate::synom::Synom>::parse) }; |
| 654 | ($i:expr, const) => { call!($i, <$crate::token::Const as $crate::synom::Synom>::parse) }; |
| 655 | ($i:expr, continue) => { call!($i, <$crate::token::Continue as $crate::synom::Synom>::parse) }; |
| 656 | ($i:expr, crate) => { call!($i, <$crate::token::Crate as $crate::synom::Synom>::parse) }; |
| 657 | ($i:expr, default) => { call!($i, <$crate::token::Default as $crate::synom::Synom>::parse) }; |
| 658 | ($i:expr, do) => { call!($i, <$crate::token::Do as $crate::synom::Synom>::parse) }; |
| 659 | ($i:expr, dyn) => { call!($i, <$crate::token::Dyn as $crate::synom::Synom>::parse) }; |
| 660 | ($i:expr, else) => { call!($i, <$crate::token::Else as $crate::synom::Synom>::parse) }; |
| 661 | ($i:expr, enum) => { call!($i, <$crate::token::Enum as $crate::synom::Synom>::parse) }; |
| 662 | ($i:expr, extern) => { call!($i, <$crate::token::Extern as $crate::synom::Synom>::parse) }; |
| 663 | ($i:expr, fn) => { call!($i, <$crate::token::Fn as $crate::synom::Synom>::parse) }; |
| 664 | ($i:expr, for) => { call!($i, <$crate::token::For as $crate::synom::Synom>::parse) }; |
| 665 | ($i:expr, if) => { call!($i, <$crate::token::If as $crate::synom::Synom>::parse) }; |
| 666 | ($i:expr, impl) => { call!($i, <$crate::token::Impl as $crate::synom::Synom>::parse) }; |
| 667 | ($i:expr, in) => { call!($i, <$crate::token::In as $crate::synom::Synom>::parse) }; |
| 668 | ($i:expr, let) => { call!($i, <$crate::token::Let as $crate::synom::Synom>::parse) }; |
| 669 | ($i:expr, loop) => { call!($i, <$crate::token::Loop as $crate::synom::Synom>::parse) }; |
| 670 | ($i:expr, macro) => { call!($i, <$crate::token::Macro as $crate::synom::Synom>::parse) }; |
| 671 | ($i:expr, match) => { call!($i, <$crate::token::Match as $crate::synom::Synom>::parse) }; |
| 672 | ($i:expr, mod) => { call!($i, <$crate::token::Mod as $crate::synom::Synom>::parse) }; |
| 673 | ($i:expr, move) => { call!($i, <$crate::token::Move as $crate::synom::Synom>::parse) }; |
| 674 | ($i:expr, mut) => { call!($i, <$crate::token::Mut as $crate::synom::Synom>::parse) }; |
| 675 | ($i:expr, pub) => { call!($i, <$crate::token::Pub as $crate::synom::Synom>::parse) }; |
| 676 | ($i:expr, ref) => { call!($i, <$crate::token::Ref as $crate::synom::Synom>::parse) }; |
| 677 | ($i:expr, return) => { call!($i, <$crate::token::Return as $crate::synom::Synom>::parse) }; |
| 678 | ($i:expr, self) => { call!($i, <$crate::token::Self_ as $crate::synom::Synom>::parse) }; |
| 679 | ($i:expr, static) => { call!($i, <$crate::token::Static as $crate::synom::Synom>::parse) }; |
| 680 | ($i:expr, struct) => { call!($i, <$crate::token::Struct as $crate::synom::Synom>::parse) }; |
| 681 | ($i:expr, super) => { call!($i, <$crate::token::Super as $crate::synom::Synom>::parse) }; |
| 682 | ($i:expr, trait) => { call!($i, <$crate::token::Trait as $crate::synom::Synom>::parse) }; |
| 683 | ($i:expr, type) => { call!($i, <$crate::token::Type as $crate::synom::Synom>::parse) }; |
| 684 | ($i:expr, union) => { call!($i, <$crate::token::Union as $crate::synom::Synom>::parse) }; |
| 685 | ($i:expr, unsafe) => { call!($i, <$crate::token::Unsafe as $crate::synom::Synom>::parse) }; |
| 686 | ($i:expr, use) => { call!($i, <$crate::token::Use as $crate::synom::Synom>::parse) }; |
| 687 | ($i:expr, where) => { call!($i, <$crate::token::Where as $crate::synom::Synom>::parse) }; |
| 688 | ($i:expr, while) => { call!($i, <$crate::token::While as $crate::synom::Synom>::parse) }; |
| 689 | ($i:expr, yield) => { call!($i, <$crate::token::Yield as $crate::synom::Synom>::parse) }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 692 | #[cfg(feature = "parsing")] |
| 693 | mod parsing { |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 694 | use proc_macro2::{Delimiter, Spacing, Span}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 695 | |
David Tolnay | dfc886b | 2018-01-06 08:03:09 -0800 | [diff] [blame] | 696 | use buffer::Cursor; |
David Tolnay | 203557a | 2017-12-27 23:59:33 -0500 | [diff] [blame] | 697 | use parse_error; |
| 698 | use synom::PResult; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 699 | |
| 700 | pub trait FromSpans: Sized { |
| 701 | fn from_spans(spans: &[Span]) -> Self; |
| 702 | } |
| 703 | |
| 704 | impl FromSpans for [Span; 1] { |
| 705 | fn from_spans(spans: &[Span]) -> Self { |
| 706 | [spans[0]] |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | impl FromSpans for [Span; 2] { |
| 711 | fn from_spans(spans: &[Span]) -> Self { |
| 712 | [spans[0], spans[1]] |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | impl FromSpans for [Span; 3] { |
| 717 | fn from_spans(spans: &[Span]) -> Self { |
| 718 | [spans[0], spans[1], spans[2]] |
| 719 | } |
| 720 | } |
| 721 | |
David Tolnay | 2b06954 | 2018-05-09 12:59:36 -0700 | [diff] [blame] | 722 | pub fn punct<'a, T, R>(s: &str, mut tokens: Cursor<'a>, new: fn(T) -> R) -> PResult<'a, R> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 723 | where |
| 724 | T: FromSpans, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 725 | { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 726 | let mut spans = [Span::call_site(); 3]; |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 727 | assert!(s.len() <= spans.len()); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 728 | let chars = s.chars(); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 729 | |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 730 | for (i, (ch, slot)) in chars.zip(&mut spans).enumerate() { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 731 | match tokens.op() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 732 | Some((op, rest)) => { |
| 733 | if op.as_char() == ch { |
| 734 | if i != s.len() - 1 { |
| 735 | match op.spacing() { |
| 736 | Spacing::Joint => {} |
| 737 | _ => return parse_error(), |
| 738 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 739 | } |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 740 | *slot = op.span(); |
| 741 | tokens = rest; |
| 742 | } else { |
| 743 | return parse_error() |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 744 | } |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 745 | } |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 746 | _ => return parse_error(), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 747 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 748 | } |
David Tolnay | 2b06954 | 2018-05-09 12:59:36 -0700 | [diff] [blame] | 749 | Ok((new(T::from_spans(&spans)), tokens)) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 750 | } |
| 751 | |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 752 | pub fn keyword<'a, T>( |
| 753 | keyword: &str, |
| 754 | tokens: Cursor<'a>, |
David Tolnay | 2b06954 | 2018-05-09 12:59:36 -0700 | [diff] [blame] | 755 | new: fn(Span) -> T, |
David Tolnay | b57c849 | 2018-05-05 00:32:04 -0700 | [diff] [blame] | 756 | ) -> PResult<'a, T> { |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 757 | if let Some((term, rest)) = tokens.term() { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 758 | if term.to_string() == keyword { |
David Tolnay | 2b06954 | 2018-05-09 12:59:36 -0700 | [diff] [blame] | 759 | return Ok((new(term.span()), rest)); |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 760 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 761 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 762 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 763 | } |
| 764 | |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 765 | pub fn delim<'a, F, R, T>( |
| 766 | delim: &str, |
| 767 | tokens: Cursor<'a>, |
David Tolnay | 2b06954 | 2018-05-09 12:59:36 -0700 | [diff] [blame] | 768 | new: fn(Span) -> T, |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 769 | f: F, |
David Tolnay | 8875fca | 2017-12-31 13:52:37 -0500 | [diff] [blame] | 770 | ) -> PResult<'a, (T, R)> |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 771 | where |
| 772 | F: FnOnce(Cursor) -> PResult<R>, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 773 | { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 774 | // NOTE: We should support none-delimited sequences here. |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 775 | let delim = match delim { |
| 776 | "(" => Delimiter::Parenthesis, |
| 777 | "{" => Delimiter::Brace, |
| 778 | "[" => Delimiter::Bracket, |
Michael Layzell | 93c3628 | 2017-06-04 20:43:14 -0400 | [diff] [blame] | 779 | " " => Delimiter::None, |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 780 | _ => panic!("unknown delimiter: {}", delim), |
| 781 | }; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 782 | |
David Tolnay | 6572948 | 2017-12-31 16:14:50 -0500 | [diff] [blame] | 783 | if let Some((inside, span, rest)) = tokens.group(delim) { |
| 784 | match f(inside) { |
David Tolnay | f4aa6b4 | 2017-12-31 16:40:33 -0500 | [diff] [blame] | 785 | Ok((ret, remaining)) => { |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 786 | if remaining.eof() { |
David Tolnay | 2b06954 | 2018-05-09 12:59:36 -0700 | [diff] [blame] | 787 | return Ok(((new(span), ret), rest)); |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 788 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 789 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 790 | Err(err) => return Err(err), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 791 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 792 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 793 | parse_error() |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
| 797 | #[cfg(feature = "printing")] |
| 798 | mod printing { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 799 | use proc_macro2::{Delimiter, Group, Punct, Spacing, Span, Ident, TokenStream}; |
| 800 | use quote::TokenStreamExt; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 801 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 802 | pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 803 | assert_eq!(s.len(), spans.len()); |
| 804 | |
| 805 | let mut chars = s.chars(); |
| 806 | let mut spans = spans.iter(); |
| 807 | let ch = chars.next_back().unwrap(); |
| 808 | let span = spans.next_back().unwrap(); |
| 809 | for (ch, span) in chars.zip(spans) { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 810 | let mut op = Punct::new(ch, Spacing::Joint); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 811 | op.set_span(*span); |
| 812 | tokens.append(op); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 815 | let mut op = Punct::new(ch, Spacing::Alone); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 816 | op.set_span(*span); |
| 817 | tokens.append(op); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 820 | pub fn keyword(s: &str, span: &Span, tokens: &mut TokenStream) { |
| 821 | tokens.append(Ident::new(s, *span)); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 824 | pub fn delim<F>(s: &str, span: &Span, tokens: &mut TokenStream, f: F) |
David Tolnay | 5138205 | 2017-12-27 13:46:21 -0500 | [diff] [blame] | 825 | where |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 826 | F: FnOnce(&mut TokenStream), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 827 | { |
David Tolnay | 00ab698 | 2017-12-31 18:15:06 -0500 | [diff] [blame] | 828 | let delim = match s { |
| 829 | "(" => Delimiter::Parenthesis, |
| 830 | "[" => Delimiter::Bracket, |
| 831 | "{" => Delimiter::Brace, |
| 832 | " " => Delimiter::None, |
| 833 | _ => panic!("unknown delimiter: {}", s), |
| 834 | }; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 835 | let mut inner = TokenStream::empty(); |
David Tolnay | 00ab698 | 2017-12-31 18:15:06 -0500 | [diff] [blame] | 836 | f(&mut inner); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 837 | let mut g = Group::new(delim, inner.into()); |
| 838 | g.set_span(*span); |
| 839 | tokens.append(g); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 840 | } |
| 841 | } |