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