David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 1 | //! Tokens representing Rust punctuation, keywords, and delimiters. |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 2 | //! |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 3 | //! The type names in this module can be difficult to keep straight, so we |
| 4 | //! prefer to use the [`Token!`] macro instead. This is a type-macro that |
| 5 | //! expands to the token type of the given token. |
| 6 | //! |
| 7 | //! [`Token!`]: ../macro.Token.html |
| 8 | //! |
| 9 | //! # Example |
| 10 | //! |
| 11 | //! The [`ItemStatic`] syntax tree node is defined like this. |
| 12 | //! |
| 13 | //! [`ItemStatic`]: ../struct.ItemStatic.html |
| 14 | //! |
David Tolnay | 95989db | 2019-01-01 15:05:57 -0500 | [diff] [blame] | 15 | //! ```edition2018 |
David Tolnay | fd5b117 | 2018-12-31 17:54:36 -0500 | [diff] [blame] | 16 | //! # use syn::{Attribute, Expr, Ident, Token, Type, Visibility}; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 17 | //! # |
| 18 | //! pub struct ItemStatic { |
| 19 | //! pub attrs: Vec<Attribute>, |
| 20 | //! pub vis: Visibility, |
| 21 | //! pub static_token: Token![static], |
| 22 | //! pub mutability: Option<Token![mut]>, |
| 23 | //! pub ident: Ident, |
| 24 | //! pub colon_token: Token![:], |
| 25 | //! pub ty: Box<Type>, |
| 26 | //! pub eq_token: Token![=], |
| 27 | //! pub expr: Box<Expr>, |
| 28 | //! pub semi_token: Token![;], |
| 29 | //! } |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 30 | //! ``` |
| 31 | //! |
| 32 | //! # Parsing |
| 33 | //! |
David Tolnay | d8cc055 | 2018-08-31 08:39:17 -0700 | [diff] [blame] | 34 | //! Keywords and punctuation can be parsed through the [`ParseStream::parse`] |
| 35 | //! method. Delimiter tokens are parsed using the [`parenthesized!`], |
| 36 | //! [`bracketed!`] and [`braced!`] macros. |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 37 | //! |
David Tolnay | d8cc055 | 2018-08-31 08:39:17 -0700 | [diff] [blame] | 38 | //! [`ParseStream::parse`]: ../parse/struct.ParseBuffer.html#method.parse |
| 39 | //! [`parenthesized!`]: ../macro.parenthesized.html |
| 40 | //! [`bracketed!`]: ../macro.bracketed.html |
| 41 | //! [`braced!`]: ../macro.braced.html |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 42 | //! |
David Tolnay | 95989db | 2019-01-01 15:05:57 -0500 | [diff] [blame] | 43 | //! ```edition2018 |
David Tolnay | 67fea04 | 2018-11-24 14:50:20 -0800 | [diff] [blame] | 44 | //! use syn::{Attribute, Result}; |
| 45 | //! use syn::parse::{Parse, ParseStream}; |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 46 | //! # |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 47 | //! # enum ItemStatic {} |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 48 | //! |
| 49 | //! // Parse the ItemStatic struct shown above. |
David Tolnay | d8cc055 | 2018-08-31 08:39:17 -0700 | [diff] [blame] | 50 | //! impl Parse for ItemStatic { |
| 51 | //! fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 9b00f65 | 2018-09-01 10:31:02 -0700 | [diff] [blame] | 52 | //! # use syn::ItemStatic; |
| 53 | //! # fn parse(input: ParseStream) -> Result<ItemStatic> { |
| 54 | //! Ok(ItemStatic { |
| 55 | //! attrs: input.call(Attribute::parse_outer)?, |
| 56 | //! vis: input.parse()?, |
| 57 | //! static_token: input.parse()?, |
| 58 | //! mutability: input.parse()?, |
| 59 | //! ident: input.parse()?, |
| 60 | //! colon_token: input.parse()?, |
| 61 | //! ty: input.parse()?, |
| 62 | //! eq_token: input.parse()?, |
| 63 | //! expr: input.parse()?, |
| 64 | //! semi_token: input.parse()?, |
| 65 | //! }) |
| 66 | //! # } |
| 67 | //! # unimplemented!() |
| 68 | //! } |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 69 | //! } |
David Tolnay | e79ae18 | 2018-01-06 19:23:37 -0800 | [diff] [blame] | 70 | //! ``` |
David Tolnay | 44d0d49 | 2019-02-02 08:33:54 -0800 | [diff] [blame^] | 71 | //! |
| 72 | //! # Other operations |
| 73 | //! |
| 74 | //! Every keyword and punctuation token supports the following operations. |
| 75 | //! |
| 76 | //! - [Peeking] — `input.peek(Token![...])` |
| 77 | //! |
| 78 | //! - [Parsing] — `input.parse::<Token![...]>()?` |
| 79 | //! |
| 80 | //! - [Printing] — `quote!( ... #the_token ... )` |
| 81 | //! |
| 82 | //! - Construction from a [`Span`] — `let the_token = Token` |
| 83 | //! |
| 84 | //! - Field access to its span — `let sp = the_token.span` |
| 85 | //! |
| 86 | //! [Peeking]: ../parse/struct.ParseBuffer.html#method.peek |
| 87 | //! [Parsing]: ../parse/struct.ParseBuffer.html#method.parse |
| 88 | //! [Printing]: https://docs.rs/quote/0.6/quote/trait.ToTokens.html |
| 89 | //! [`Span`]: ../struct.Span.html |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 90 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 91 | use std; |
| 92 | #[cfg(feature = "extra-traits")] |
| 93 | use std::cmp; |
| 94 | #[cfg(feature = "extra-traits")] |
| 95 | use std::fmt::{self, Debug}; |
| 96 | #[cfg(feature = "extra-traits")] |
| 97 | use std::hash::{Hash, Hasher}; |
| 98 | |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 99 | #[cfg(feature = "parsing")] |
| 100 | use proc_macro2::Delimiter; |
David Tolnay | af1df8c | 2018-09-22 13:22:24 -0700 | [diff] [blame] | 101 | #[cfg(any(feature = "parsing", feature = "printing"))] |
| 102 | use proc_macro2::Ident; |
| 103 | use proc_macro2::Span; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 104 | #[cfg(feature = "printing")] |
David Tolnay | abbf819 | 2018-09-22 13:24:46 -0700 | [diff] [blame] | 105 | use proc_macro2::TokenStream; |
| 106 | #[cfg(feature = "printing")] |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 107 | use quote::{ToTokens, TokenStreamExt}; |
| 108 | |
| 109 | #[cfg(feature = "parsing")] |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 110 | use buffer::Cursor; |
| 111 | #[cfg(feature = "parsing")] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 112 | use error::Result; |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 113 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 114 | #[cfg(feature = "parsing")] |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 115 | use lifetime::Lifetime; |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 116 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 117 | #[cfg(feature = "parsing")] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 118 | use lit::{Lit, LitBool, LitByte, LitByteStr, LitChar, LitFloat, LitInt, LitStr}; |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 119 | #[cfg(feature = "parsing")] |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 120 | use lookahead; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 121 | #[cfg(feature = "parsing")] |
David Tolnay | 7fb11e7 | 2018-09-06 01:02:27 -0700 | [diff] [blame] | 122 | use parse::{Parse, ParseStream}; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 123 | use span::IntoSpans; |
| 124 | |
| 125 | /// Marker trait for types that represent single tokens. |
| 126 | /// |
| 127 | /// This trait is sealed and cannot be implemented for types outside of Syn. |
| 128 | #[cfg(feature = "parsing")] |
| 129 | pub trait Token: private::Sealed { |
| 130 | // Not public API. |
| 131 | #[doc(hidden)] |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 132 | fn peek(cursor: Cursor) -> bool; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 133 | |
| 134 | // Not public API. |
| 135 | #[doc(hidden)] |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 136 | fn display() -> &'static str; |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | #[cfg(feature = "parsing")] |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 140 | mod private { |
| 141 | pub trait Sealed {} |
Sergio Benitez | d14d536 | 2018-04-28 15:38:25 -0700 | [diff] [blame] | 142 | } |
| 143 | |
David Tolnay | 65557f0 | 2018-09-01 11:08:27 -0700 | [diff] [blame] | 144 | #[cfg(feature = "parsing")] |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 145 | impl private::Sealed for Ident {} |
| 146 | |
David Tolnay | ff18ed9 | 2018-10-26 02:25:05 -0700 | [diff] [blame] | 147 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 719ad5a | 2018-09-02 18:01:15 -0700 | [diff] [blame] | 148 | #[cfg(feature = "parsing")] |
David Tolnay | 65557f0 | 2018-09-01 11:08:27 -0700 | [diff] [blame] | 149 | fn peek_impl(cursor: Cursor, peek: fn(ParseStream) -> bool) -> bool { |
David Tolnay | ff18ed9 | 2018-10-26 02:25:05 -0700 | [diff] [blame] | 150 | use std::cell::Cell; |
| 151 | use std::rc::Rc; |
| 152 | |
David Tolnay | 65557f0 | 2018-09-01 11:08:27 -0700 | [diff] [blame] | 153 | let scope = Span::call_site(); |
| 154 | let unexpected = Rc::new(Cell::new(None)); |
| 155 | let buffer = ::private::new_parse_buffer(scope, cursor, unexpected); |
| 156 | peek(&buffer) |
| 157 | } |
| 158 | |
David Tolnay | af1df8c | 2018-09-22 13:22:24 -0700 | [diff] [blame] | 159 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 160 | macro_rules! impl_token { |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 161 | ($name:ident $display:expr) => { |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 162 | #[cfg(feature = "parsing")] |
| 163 | impl Token for $name { |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 164 | fn peek(cursor: Cursor) -> bool { |
David Tolnay | 65557f0 | 2018-09-01 11:08:27 -0700 | [diff] [blame] | 165 | fn peek(input: ParseStream) -> bool { |
| 166 | <$name as Parse>::parse(input).is_ok() |
| 167 | } |
| 168 | peek_impl(cursor, peek) |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 169 | } |
| 170 | |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 171 | fn display() -> &'static str { |
| 172 | $display |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | #[cfg(feature = "parsing")] |
| 177 | impl private::Sealed for $name {} |
| 178 | }; |
| 179 | } |
| 180 | |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 181 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 182 | impl_token!(Lifetime "lifetime"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 183 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 184 | impl_token!(Lit "literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 185 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 186 | impl_token!(LitStr "string literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 187 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 188 | impl_token!(LitByteStr "byte string literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 189 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 190 | impl_token!(LitByte "byte literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 191 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 192 | impl_token!(LitChar "character literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 193 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 194 | impl_token!(LitInt "integer literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 195 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 196 | impl_token!(LitFloat "floating point literal"); |
David Tolnay | a465b2d | 2018-08-27 08:21:09 -0700 | [diff] [blame] | 197 | #[cfg(any(feature = "full", feature = "derive"))] |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 198 | impl_token!(LitBool "boolean literal"); |
David Tolnay | 4fb7123 | 2018-08-25 23:14:50 -0400 | [diff] [blame] | 199 | |
David Tolnay | 7fb11e7 | 2018-09-06 01:02:27 -0700 | [diff] [blame] | 200 | // Not public API. |
| 201 | #[cfg(feature = "parsing")] |
| 202 | #[doc(hidden)] |
| 203 | pub trait CustomKeyword { |
| 204 | fn ident() -> &'static str; |
| 205 | fn display() -> &'static str; |
| 206 | } |
| 207 | |
| 208 | #[cfg(feature = "parsing")] |
| 209 | impl<K: CustomKeyword> private::Sealed for K {} |
| 210 | |
| 211 | #[cfg(feature = "parsing")] |
| 212 | impl<K: CustomKeyword> Token for K { |
| 213 | fn peek(cursor: Cursor) -> bool { |
| 214 | parsing::peek_keyword(cursor, K::ident()) |
| 215 | } |
| 216 | |
| 217 | fn display() -> &'static str { |
| 218 | K::display() |
| 219 | } |
| 220 | } |
| 221 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 222 | macro_rules! define_keywords { |
| 223 | ($($token:tt pub struct $name:ident #[$doc:meta])*) => { |
| 224 | $( |
| 225 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 226 | #[$doc] |
| 227 | /// |
| 228 | /// Don't try to remember the name of this type -- use the [`Token!`] |
| 229 | /// macro instead. |
| 230 | /// |
| 231 | /// [`Token!`]: index.html |
| 232 | pub struct $name { |
| 233 | pub span: Span, |
| 234 | } |
| 235 | |
| 236 | #[doc(hidden)] |
| 237 | #[allow(non_snake_case)] |
| 238 | pub fn $name<S: IntoSpans<[Span; 1]>>(span: S) -> $name { |
| 239 | $name { |
| 240 | span: span.into_spans()[0], |
| 241 | } |
| 242 | } |
| 243 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 244 | impl std::default::Default for $name { |
| 245 | fn default() -> Self { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 246 | $name { |
| 247 | span: Span::call_site(), |
| 248 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | #[cfg(feature = "extra-traits")] |
| 253 | impl Debug for $name { |
| 254 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 255 | f.write_str(stringify!($name)) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | #[cfg(feature = "extra-traits")] |
| 260 | impl cmp::Eq for $name {} |
| 261 | |
| 262 | #[cfg(feature = "extra-traits")] |
| 263 | impl PartialEq for $name { |
| 264 | fn eq(&self, _other: &$name) -> bool { |
| 265 | true |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | #[cfg(feature = "extra-traits")] |
| 270 | impl Hash for $name { |
| 271 | fn hash<H: Hasher>(&self, _state: &mut H) {} |
| 272 | } |
| 273 | |
| 274 | #[cfg(feature = "printing")] |
| 275 | impl ToTokens for $name { |
| 276 | fn to_tokens(&self, tokens: &mut TokenStream) { |
David Tolnay | 7f7eb0e | 2018-11-21 01:03:47 -0800 | [diff] [blame] | 277 | printing::keyword($token, self.span, tokens); |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | #[cfg(feature = "parsing")] |
| 282 | impl Parse for $name { |
| 283 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 284 | Ok($name { |
| 285 | span: parsing::keyword(input, $token)?, |
| 286 | }) |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 287 | } |
| 288 | } |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 289 | |
| 290 | #[cfg(feature = "parsing")] |
| 291 | impl Token for $name { |
| 292 | fn peek(cursor: Cursor) -> bool { |
| 293 | parsing::peek_keyword(cursor, $token) |
| 294 | } |
| 295 | |
| 296 | fn display() -> &'static str { |
| 297 | concat!("`", $token, "`") |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | #[cfg(feature = "parsing")] |
| 302 | impl private::Sealed for $name {} |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 303 | )* |
| 304 | }; |
| 305 | } |
| 306 | |
| 307 | macro_rules! define_punctuation_structs { |
| 308 | ($($token:tt pub struct $name:ident/$len:tt #[$doc:meta])*) => { |
| 309 | $( |
| 310 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 311 | #[$doc] |
| 312 | /// |
| 313 | /// Don't try to remember the name of this type -- use the [`Token!`] |
| 314 | /// macro instead. |
| 315 | /// |
| 316 | /// [`Token!`]: index.html |
| 317 | pub struct $name { |
| 318 | pub spans: [Span; $len], |
| 319 | } |
| 320 | |
| 321 | #[doc(hidden)] |
| 322 | #[allow(non_snake_case)] |
| 323 | pub fn $name<S: IntoSpans<[Span; $len]>>(spans: S) -> $name { |
| 324 | $name { |
| 325 | spans: spans.into_spans(), |
| 326 | } |
| 327 | } |
| 328 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 329 | impl std::default::Default for $name { |
| 330 | fn default() -> Self { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 331 | $name { |
| 332 | spans: [Span::call_site(); $len], |
| 333 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | #[cfg(feature = "extra-traits")] |
| 338 | impl Debug for $name { |
| 339 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 340 | f.write_str(stringify!($name)) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | #[cfg(feature = "extra-traits")] |
| 345 | impl cmp::Eq for $name {} |
| 346 | |
| 347 | #[cfg(feature = "extra-traits")] |
| 348 | impl PartialEq for $name { |
| 349 | fn eq(&self, _other: &$name) -> bool { |
| 350 | true |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | #[cfg(feature = "extra-traits")] |
| 355 | impl Hash for $name { |
| 356 | fn hash<H: Hasher>(&self, _state: &mut H) {} |
| 357 | } |
| 358 | )* |
| 359 | }; |
| 360 | } |
| 361 | |
| 362 | macro_rules! define_punctuation { |
| 363 | ($($token:tt pub struct $name:ident/$len:tt #[$doc:meta])*) => { |
| 364 | $( |
| 365 | define_punctuation_structs! { |
| 366 | $token pub struct $name/$len #[$doc] |
| 367 | } |
| 368 | |
| 369 | #[cfg(feature = "printing")] |
| 370 | impl ToTokens for $name { |
| 371 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 372 | printing::punct($token, &self.spans, tokens); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | #[cfg(feature = "parsing")] |
| 377 | impl Parse for $name { |
| 378 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 379 | Ok($name { |
| 380 | spans: parsing::punct(input, $token)?, |
| 381 | }) |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 382 | } |
| 383 | } |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 384 | |
| 385 | #[cfg(feature = "parsing")] |
| 386 | impl Token for $name { |
| 387 | fn peek(cursor: Cursor) -> bool { |
| 388 | parsing::peek_punct(cursor, $token) |
| 389 | } |
| 390 | |
| 391 | fn display() -> &'static str { |
| 392 | concat!("`", $token, "`") |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | #[cfg(feature = "parsing")] |
| 397 | impl private::Sealed for $name {} |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 398 | )* |
| 399 | }; |
| 400 | } |
| 401 | |
| 402 | macro_rules! define_delimiters { |
| 403 | ($($token:tt pub struct $name:ident #[$doc:meta])*) => { |
| 404 | $( |
| 405 | #[cfg_attr(feature = "clone-impls", derive(Copy, Clone))] |
| 406 | #[$doc] |
| 407 | pub struct $name { |
| 408 | pub span: Span, |
| 409 | } |
| 410 | |
| 411 | #[doc(hidden)] |
| 412 | #[allow(non_snake_case)] |
| 413 | pub fn $name<S: IntoSpans<[Span; 1]>>(span: S) -> $name { |
| 414 | $name { |
| 415 | span: span.into_spans()[0], |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | impl std::default::Default for $name { |
| 420 | fn default() -> Self { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 421 | $name { |
| 422 | span: Span::call_site(), |
| 423 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | #[cfg(feature = "extra-traits")] |
| 428 | impl Debug for $name { |
| 429 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 430 | f.write_str(stringify!($name)) |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | #[cfg(feature = "extra-traits")] |
| 435 | impl cmp::Eq for $name {} |
| 436 | |
| 437 | #[cfg(feature = "extra-traits")] |
| 438 | impl PartialEq for $name { |
| 439 | fn eq(&self, _other: &$name) -> bool { |
| 440 | true |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | #[cfg(feature = "extra-traits")] |
| 445 | impl Hash for $name { |
| 446 | fn hash<H: Hasher>(&self, _state: &mut H) {} |
| 447 | } |
| 448 | |
| 449 | impl $name { |
| 450 | #[cfg(feature = "printing")] |
| 451 | pub fn surround<F>(&self, tokens: &mut TokenStream, f: F) |
| 452 | where |
| 453 | F: FnOnce(&mut TokenStream), |
| 454 | { |
David Tolnay | 7f7eb0e | 2018-11-21 01:03:47 -0800 | [diff] [blame] | 455 | printing::delim($token, self.span, tokens, f); |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 456 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 457 | } |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 458 | |
| 459 | #[cfg(feature = "parsing")] |
| 460 | impl private::Sealed for $name {} |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 461 | )* |
| 462 | }; |
| 463 | } |
| 464 | |
| 465 | define_punctuation_structs! { |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 466 | "_" pub struct Underscore/1 /// `_` |
Alex Crichton | 131308c | 2018-05-18 14:00:24 -0700 | [diff] [blame] | 467 | } |
| 468 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 469 | #[cfg(feature = "printing")] |
| 470 | impl ToTokens for Underscore { |
| 471 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 472 | tokens.append(Ident::new("_", self.spans[0])); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 473 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | #[cfg(feature = "parsing")] |
| 477 | impl Parse for Underscore { |
| 478 | fn parse(input: ParseStream) -> Result<Self> { |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 479 | input.step(|cursor| { |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 480 | if let Some((ident, rest)) = cursor.ident() { |
| 481 | if ident == "_" { |
| 482 | return Ok((Underscore(ident.span()), rest)); |
| 483 | } |
| 484 | } |
| 485 | if let Some((punct, rest)) = cursor.punct() { |
| 486 | if punct.as_char() == '_' { |
| 487 | return Ok((Underscore(punct.span()), rest)); |
| 488 | } |
| 489 | } |
| 490 | Err(cursor.error("expected `_`")) |
| 491 | }) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 492 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 493 | } |
| 494 | |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 495 | #[cfg(feature = "parsing")] |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 496 | impl Token for Underscore { |
| 497 | fn peek(cursor: Cursor) -> bool { |
| 498 | if let Some((ident, _rest)) = cursor.ident() { |
| 499 | return ident == "_"; |
| 500 | } |
| 501 | if let Some((punct, _rest)) = cursor.punct() { |
David Tolnay | 3db288c | 2018-09-09 22:16:51 -0700 | [diff] [blame] | 502 | return punct.as_char() == '_'; |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 503 | } |
| 504 | false |
| 505 | } |
| 506 | |
| 507 | fn display() -> &'static str { |
| 508 | "`_`" |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | #[cfg(feature = "parsing")] |
| 513 | impl private::Sealed for Underscore {} |
| 514 | |
| 515 | #[cfg(feature = "parsing")] |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 516 | impl Token for Paren { |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 517 | fn peek(cursor: Cursor) -> bool { |
| 518 | lookahead::is_delimiter(cursor, Delimiter::Parenthesis) |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 519 | } |
| 520 | |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 521 | fn display() -> &'static str { |
| 522 | "parentheses" |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | #[cfg(feature = "parsing")] |
| 527 | impl Token for Brace { |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 528 | fn peek(cursor: Cursor) -> bool { |
| 529 | lookahead::is_delimiter(cursor, Delimiter::Brace) |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 530 | } |
| 531 | |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 532 | fn display() -> &'static str { |
| 533 | "curly braces" |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | #[cfg(feature = "parsing")] |
| 538 | impl Token for Bracket { |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 539 | fn peek(cursor: Cursor) -> bool { |
| 540 | lookahead::is_delimiter(cursor, Delimiter::Bracket) |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 541 | } |
| 542 | |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 543 | fn display() -> &'static str { |
| 544 | "square brackets" |
David Tolnay | 2d84a08 | 2018-08-25 16:31:38 -0400 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 548 | #[cfg(feature = "parsing")] |
| 549 | impl Token for Group { |
David Tolnay | 00f81fd | 2018-09-01 10:50:12 -0700 | [diff] [blame] | 550 | fn peek(cursor: Cursor) -> bool { |
| 551 | lookahead::is_delimiter(cursor, Delimiter::None) |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 552 | } |
| 553 | |
David Tolnay | 2d03280 | 2018-09-01 10:51:59 -0700 | [diff] [blame] | 554 | fn display() -> &'static str { |
| 555 | "invisible group" |
David Tolnay | a7d69fc | 2018-08-26 13:30:24 -0400 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 559 | define_keywords! { |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 560 | "abstract" pub struct Abstract /// `abstract` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 561 | "as" pub struct As /// `as` |
| 562 | "async" pub struct Async /// `async` |
| 563 | "auto" pub struct Auto /// `auto` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 564 | "become" pub struct Become /// `become` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 565 | "box" pub struct Box /// `box` |
| 566 | "break" pub struct Break /// `break` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 567 | "const" pub struct Const /// `const` |
| 568 | "continue" pub struct Continue /// `continue` |
| 569 | "crate" pub struct Crate /// `crate` |
| 570 | "default" pub struct Default /// `default` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 571 | "do" pub struct Do /// `do` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 572 | "dyn" pub struct Dyn /// `dyn` |
| 573 | "else" pub struct Else /// `else` |
| 574 | "enum" pub struct Enum /// `enum` |
| 575 | "existential" pub struct Existential /// `existential` |
| 576 | "extern" pub struct Extern /// `extern` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 577 | "final" pub struct Final /// `final` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 578 | "fn" pub struct Fn /// `fn` |
| 579 | "for" pub struct For /// `for` |
| 580 | "if" pub struct If /// `if` |
| 581 | "impl" pub struct Impl /// `impl` |
| 582 | "in" pub struct In /// `in` |
| 583 | "let" pub struct Let /// `let` |
| 584 | "loop" pub struct Loop /// `loop` |
| 585 | "macro" pub struct Macro /// `macro` |
| 586 | "match" pub struct Match /// `match` |
| 587 | "mod" pub struct Mod /// `mod` |
| 588 | "move" pub struct Move /// `move` |
| 589 | "mut" pub struct Mut /// `mut` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 590 | "override" pub struct Override /// `override` |
| 591 | "priv" pub struct Priv /// `priv` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 592 | "pub" pub struct Pub /// `pub` |
| 593 | "ref" pub struct Ref /// `ref` |
| 594 | "return" pub struct Return /// `return` |
David Tolnay | c0e742d | 2018-10-30 02:17:17 -0700 | [diff] [blame] | 595 | "Self" pub struct SelfType /// `Self` |
csmoe | f04dcc0 | 2018-10-30 16:45:13 +0800 | [diff] [blame] | 596 | "self" pub struct SelfValue /// `self` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 597 | "static" pub struct Static /// `static` |
| 598 | "struct" pub struct Struct /// `struct` |
| 599 | "super" pub struct Super /// `super` |
| 600 | "trait" pub struct Trait /// `trait` |
| 601 | "try" pub struct Try /// `try` |
| 602 | "type" pub struct Type /// `type` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 603 | "typeof" pub struct Typeof /// `typeof` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 604 | "union" pub struct Union /// `union` |
| 605 | "unsafe" pub struct Unsafe /// `unsafe` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 606 | "unsized" pub struct Unsized /// `unsized` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 607 | "use" pub struct Use /// `use` |
David Tolnay | b1cbf7e | 2018-09-09 00:00:17 -0700 | [diff] [blame] | 608 | "virtual" pub struct Virtual /// `virtual` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 609 | "where" pub struct Where /// `where` |
| 610 | "while" pub struct While /// `while` |
| 611 | "yield" pub struct Yield /// `yield` |
| 612 | } |
| 613 | |
| 614 | define_punctuation! { |
| 615 | "+" pub struct Add/1 /// `+` |
| 616 | "+=" pub struct AddEq/2 /// `+=` |
| 617 | "&" pub struct And/1 /// `&` |
| 618 | "&&" pub struct AndAnd/2 /// `&&` |
| 619 | "&=" pub struct AndEq/2 /// `&=` |
| 620 | "@" pub struct At/1 /// `@` |
| 621 | "!" pub struct Bang/1 /// `!` |
| 622 | "^" pub struct Caret/1 /// `^` |
| 623 | "^=" pub struct CaretEq/2 /// `^=` |
| 624 | ":" pub struct Colon/1 /// `:` |
| 625 | "::" pub struct Colon2/2 /// `::` |
| 626 | "," pub struct Comma/1 /// `,` |
| 627 | "/" pub struct Div/1 /// `/` |
| 628 | "/=" pub struct DivEq/2 /// `/=` |
| 629 | "$" pub struct Dollar/1 /// `$` |
| 630 | "." pub struct Dot/1 /// `.` |
| 631 | ".." pub struct Dot2/2 /// `..` |
| 632 | "..." pub struct Dot3/3 /// `...` |
| 633 | "..=" pub struct DotDotEq/3 /// `..=` |
| 634 | "=" pub struct Eq/1 /// `=` |
| 635 | "==" pub struct EqEq/2 /// `==` |
| 636 | ">=" pub struct Ge/2 /// `>=` |
| 637 | ">" pub struct Gt/1 /// `>` |
| 638 | "<=" pub struct Le/2 /// `<=` |
| 639 | "<" pub struct Lt/1 /// `<` |
| 640 | "*=" pub struct MulEq/2 /// `*=` |
| 641 | "!=" pub struct Ne/2 /// `!=` |
| 642 | "|" pub struct Or/1 /// `|` |
| 643 | "|=" pub struct OrEq/2 /// `|=` |
| 644 | "||" pub struct OrOr/2 /// `||` |
| 645 | "#" pub struct Pound/1 /// `#` |
| 646 | "?" pub struct Question/1 /// `?` |
| 647 | "->" pub struct RArrow/2 /// `->` |
| 648 | "<-" pub struct LArrow/2 /// `<-` |
| 649 | "%" pub struct Rem/1 /// `%` |
| 650 | "%=" pub struct RemEq/2 /// `%=` |
| 651 | "=>" pub struct FatArrow/2 /// `=>` |
| 652 | ";" pub struct Semi/1 /// `;` |
| 653 | "<<" pub struct Shl/2 /// `<<` |
| 654 | "<<=" pub struct ShlEq/3 /// `<<=` |
| 655 | ">>" pub struct Shr/2 /// `>>` |
| 656 | ">>=" pub struct ShrEq/3 /// `>>=` |
| 657 | "*" pub struct Star/1 /// `*` |
| 658 | "-" pub struct Sub/1 /// `-` |
| 659 | "-=" pub struct SubEq/2 /// `-=` |
David Tolnay | f26be7d | 2018-09-23 01:32:08 -0700 | [diff] [blame] | 660 | "~" pub struct Tilde/1 /// `~` |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | define_delimiters! { |
| 664 | "{" pub struct Brace /// `{...}` |
| 665 | "[" pub struct Bracket /// `[...]` |
| 666 | "(" pub struct Paren /// `(...)` |
| 667 | " " pub struct Group /// None-delimited group |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 668 | } |
| 669 | |
David Tolnay | f005f96 | 2018-01-06 21:19:41 -0800 | [diff] [blame] | 670 | /// A type-macro that expands to the name of the Rust type representation of a |
| 671 | /// given token. |
| 672 | /// |
| 673 | /// See the [token module] documentation for details and examples. |
| 674 | /// |
| 675 | /// [token module]: token/index.html |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 676 | // Unfortunate duplication due to a rustdoc bug. |
| 677 | // https://github.com/rust-lang/rust/issues/45939 |
| 678 | #[macro_export] |
David Tolnay | a5ed2fe | 2018-04-29 12:23:34 -0700 | [diff] [blame] | 679 | #[cfg_attr(rustfmt, rustfmt_skip)] |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 680 | macro_rules! Token { |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 681 | (abstract) => { $crate::token::Abstract }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 682 | (as) => { $crate::token::As }; |
| 683 | (async) => { $crate::token::Async }; |
| 684 | (auto) => { $crate::token::Auto }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 685 | (become) => { $crate::token::Become }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 686 | (box) => { $crate::token::Box }; |
| 687 | (break) => { $crate::token::Break }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 688 | (const) => { $crate::token::Const }; |
| 689 | (continue) => { $crate::token::Continue }; |
| 690 | (crate) => { $crate::token::Crate }; |
| 691 | (default) => { $crate::token::Default }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 692 | (do) => { $crate::token::Do }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 693 | (dyn) => { $crate::token::Dyn }; |
| 694 | (else) => { $crate::token::Else }; |
| 695 | (enum) => { $crate::token::Enum }; |
| 696 | (existential) => { $crate::token::Existential }; |
| 697 | (extern) => { $crate::token::Extern }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 698 | (final) => { $crate::token::Final }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 699 | (fn) => { $crate::token::Fn }; |
| 700 | (for) => { $crate::token::For }; |
| 701 | (if) => { $crate::token::If }; |
| 702 | (impl) => { $crate::token::Impl }; |
| 703 | (in) => { $crate::token::In }; |
| 704 | (let) => { $crate::token::Let }; |
| 705 | (loop) => { $crate::token::Loop }; |
| 706 | (macro) => { $crate::token::Macro }; |
| 707 | (match) => { $crate::token::Match }; |
| 708 | (mod) => { $crate::token::Mod }; |
| 709 | (move) => { $crate::token::Move }; |
| 710 | (mut) => { $crate::token::Mut }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 711 | (override) => { $crate::token::Override }; |
| 712 | (priv) => { $crate::token::Priv }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 713 | (pub) => { $crate::token::Pub }; |
| 714 | (ref) => { $crate::token::Ref }; |
| 715 | (return) => { $crate::token::Return }; |
David Tolnay | c0e742d | 2018-10-30 02:17:17 -0700 | [diff] [blame] | 716 | (Self) => { $crate::token::SelfType }; |
csmoe | f04dcc0 | 2018-10-30 16:45:13 +0800 | [diff] [blame] | 717 | (self) => { $crate::token::SelfValue }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 718 | (static) => { $crate::token::Static }; |
| 719 | (struct) => { $crate::token::Struct }; |
| 720 | (super) => { $crate::token::Super }; |
| 721 | (trait) => { $crate::token::Trait }; |
| 722 | (try) => { $crate::token::Try }; |
| 723 | (type) => { $crate::token::Type }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 724 | (typeof) => { $crate::token::Typeof }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 725 | (union) => { $crate::token::Union }; |
| 726 | (unsafe) => { $crate::token::Unsafe }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 727 | (unsized) => { $crate::token::Unsized }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 728 | (use) => { $crate::token::Use }; |
David Tolnay | c327cd2 | 2018-09-09 00:37:28 -0700 | [diff] [blame] | 729 | (virtual) => { $crate::token::Virtual }; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 730 | (where) => { $crate::token::Where }; |
| 731 | (while) => { $crate::token::While }; |
| 732 | (yield) => { $crate::token::Yield }; |
David Tolnay | bb82ef0 | 2018-08-24 20:15:45 -0400 | [diff] [blame] | 733 | (+) => { $crate::token::Add }; |
| 734 | (+=) => { $crate::token::AddEq }; |
| 735 | (&) => { $crate::token::And }; |
| 736 | (&&) => { $crate::token::AndAnd }; |
| 737 | (&=) => { $crate::token::AndEq }; |
| 738 | (@) => { $crate::token::At }; |
| 739 | (!) => { $crate::token::Bang }; |
| 740 | (^) => { $crate::token::Caret }; |
| 741 | (^=) => { $crate::token::CaretEq }; |
| 742 | (:) => { $crate::token::Colon }; |
| 743 | (::) => { $crate::token::Colon2 }; |
| 744 | (,) => { $crate::token::Comma }; |
| 745 | (/) => { $crate::token::Div }; |
| 746 | (/=) => { $crate::token::DivEq }; |
| 747 | (.) => { $crate::token::Dot }; |
| 748 | (..) => { $crate::token::Dot2 }; |
| 749 | (...) => { $crate::token::Dot3 }; |
| 750 | (..=) => { $crate::token::DotDotEq }; |
| 751 | (=) => { $crate::token::Eq }; |
| 752 | (==) => { $crate::token::EqEq }; |
| 753 | (>=) => { $crate::token::Ge }; |
| 754 | (>) => { $crate::token::Gt }; |
| 755 | (<=) => { $crate::token::Le }; |
| 756 | (<) => { $crate::token::Lt }; |
| 757 | (*=) => { $crate::token::MulEq }; |
| 758 | (!=) => { $crate::token::Ne }; |
| 759 | (|) => { $crate::token::Or }; |
| 760 | (|=) => { $crate::token::OrEq }; |
| 761 | (||) => { $crate::token::OrOr }; |
| 762 | (#) => { $crate::token::Pound }; |
| 763 | (?) => { $crate::token::Question }; |
| 764 | (->) => { $crate::token::RArrow }; |
| 765 | (<-) => { $crate::token::LArrow }; |
| 766 | (%) => { $crate::token::Rem }; |
| 767 | (%=) => { $crate::token::RemEq }; |
| 768 | (=>) => { $crate::token::FatArrow }; |
| 769 | (;) => { $crate::token::Semi }; |
| 770 | (<<) => { $crate::token::Shl }; |
| 771 | (<<=) => { $crate::token::ShlEq }; |
| 772 | (>>) => { $crate::token::Shr }; |
| 773 | (>>=) => { $crate::token::ShrEq }; |
| 774 | (*) => { $crate::token::Star }; |
| 775 | (-) => { $crate::token::Sub }; |
| 776 | (-=) => { $crate::token::SubEq }; |
David Tolnay | f26be7d | 2018-09-23 01:32:08 -0700 | [diff] [blame] | 777 | (~) => { $crate::token::Tilde }; |
David Tolnay | bb82ef0 | 2018-08-24 20:15:45 -0400 | [diff] [blame] | 778 | (_) => { $crate::token::Underscore }; |
David Tolnay | f8db7ba | 2017-11-11 22:52:16 -0800 | [diff] [blame] | 779 | } |
| 780 | |
David Tolnay | ee61281 | 2018-10-30 02:16:03 -0700 | [diff] [blame] | 781 | // Old names. TODO: remove these re-exports in a breaking change. |
| 782 | // https://github.com/dtolnay/syn/issues/486 |
| 783 | #[doc(hidden)] |
| 784 | pub use self::SelfType as CapSelf; |
| 785 | #[doc(hidden)] |
| 786 | pub use self::SelfValue as Self_; |
| 787 | |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 788 | #[cfg(feature = "parsing")] |
| 789 | mod parsing { |
David Tolnay | a8205d9 | 2018-08-30 18:44:59 -0700 | [diff] [blame] | 790 | use proc_macro2::{Spacing, Span}; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 791 | |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 792 | use buffer::Cursor; |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 793 | use error::{Error, Result}; |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 794 | use parse::ParseStream; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 795 | use span::FromSpans; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 796 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 797 | pub fn keyword(input: ParseStream, token: &str) -> Result<Span> { |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 798 | input.step(|cursor| { |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 799 | if let Some((ident, rest)) = cursor.ident() { |
| 800 | if ident == token { |
| 801 | return Ok((ident.span(), rest)); |
Alex Crichton | 954046c | 2017-05-30 21:49:42 -0700 | [diff] [blame] | 802 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 803 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 804 | Err(cursor.error(format!("expected `{}`", token))) |
| 805 | }) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 806 | } |
| 807 | |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 808 | pub fn peek_keyword(cursor: Cursor, token: &str) -> bool { |
| 809 | if let Some((ident, _rest)) = cursor.ident() { |
| 810 | ident == token |
| 811 | } else { |
| 812 | false |
| 813 | } |
| 814 | } |
| 815 | |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 816 | pub fn punct<S: FromSpans>(input: ParseStream, token: &str) -> Result<S> { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 817 | let mut spans = [input.cursor().span(); 3]; |
| 818 | punct_helper(input, token, &mut spans)?; |
| 819 | Ok(S::from_spans(&spans)) |
| 820 | } |
| 821 | |
| 822 | fn punct_helper(input: ParseStream, token: &str, spans: &mut [Span; 3]) -> Result<()> { |
David Tolnay | b50c65a | 2018-08-30 21:14:57 -0700 | [diff] [blame] | 823 | input.step(|cursor| { |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 824 | let mut cursor = *cursor; |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 825 | assert!(token.len() <= spans.len()); |
| 826 | |
| 827 | for (i, ch) in token.chars().enumerate() { |
| 828 | match cursor.punct() { |
| 829 | Some((punct, rest)) => { |
| 830 | spans[i] = punct.span(); |
| 831 | if punct.as_char() != ch { |
| 832 | break; |
| 833 | } else if i == token.len() - 1 { |
David Tolnay | 4398c92 | 2018-09-01 11:23:10 -0700 | [diff] [blame] | 834 | return Ok(((), rest)); |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 835 | } else if punct.spacing() != Spacing::Joint { |
| 836 | break; |
| 837 | } |
| 838 | cursor = rest; |
| 839 | } |
| 840 | None => break, |
| 841 | } |
Michael Layzell | 0a1a663 | 2017-06-02 18:07:43 -0400 | [diff] [blame] | 842 | } |
David Tolnay | 776f8e0 | 2018-08-24 22:32:10 -0400 | [diff] [blame] | 843 | |
| 844 | Err(Error::new(spans[0], format!("expected `{}`", token))) |
| 845 | }) |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 846 | } |
David Tolnay | 68274de | 2018-09-02 17:15:01 -0700 | [diff] [blame] | 847 | |
| 848 | pub fn peek_punct(mut cursor: Cursor, token: &str) -> bool { |
| 849 | for (i, ch) in token.chars().enumerate() { |
| 850 | match cursor.punct() { |
| 851 | Some((punct, rest)) => { |
| 852 | if punct.as_char() != ch { |
| 853 | break; |
| 854 | } else if i == token.len() - 1 { |
| 855 | return true; |
| 856 | } else if punct.spacing() != Spacing::Joint { |
| 857 | break; |
| 858 | } |
| 859 | cursor = rest; |
| 860 | } |
| 861 | None => break, |
| 862 | } |
| 863 | } |
| 864 | false |
| 865 | } |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | #[cfg(feature = "printing")] |
| 869 | mod printing { |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 870 | use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream}; |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 871 | use quote::TokenStreamExt; |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 872 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 873 | pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) { |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 874 | assert_eq!(s.len(), spans.len()); |
| 875 | |
| 876 | let mut chars = s.chars(); |
| 877 | let mut spans = spans.iter(); |
| 878 | let ch = chars.next_back().unwrap(); |
| 879 | let span = spans.next_back().unwrap(); |
| 880 | for (ch, span) in chars.zip(spans) { |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 881 | let mut op = Punct::new(ch, Spacing::Joint); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 882 | op.set_span(*span); |
| 883 | tokens.append(op); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 886 | let mut op = Punct::new(ch, Spacing::Alone); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 887 | op.set_span(*span); |
| 888 | tokens.append(op); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 889 | } |
| 890 | |
David Tolnay | 7f7eb0e | 2018-11-21 01:03:47 -0800 | [diff] [blame] | 891 | pub fn keyword(s: &str, span: Span, tokens: &mut TokenStream) { |
| 892 | tokens.append(Ident::new(s, span)); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 893 | } |
| 894 | |
David Tolnay | 7f7eb0e | 2018-11-21 01:03:47 -0800 | [diff] [blame] | 895 | 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] | 896 | where |
Alex Crichton | a74a1c8 | 2018-05-16 10:20:44 -0700 | [diff] [blame] | 897 | F: FnOnce(&mut TokenStream), |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 898 | { |
David Tolnay | 00ab698 | 2017-12-31 18:15:06 -0500 | [diff] [blame] | 899 | let delim = match s { |
| 900 | "(" => Delimiter::Parenthesis, |
| 901 | "[" => Delimiter::Bracket, |
| 902 | "{" => Delimiter::Brace, |
| 903 | " " => Delimiter::None, |
| 904 | _ => panic!("unknown delimiter: {}", s), |
| 905 | }; |
hcpl | aa51179 | 2018-05-29 07:13:01 +0300 | [diff] [blame] | 906 | let mut inner = TokenStream::new(); |
David Tolnay | 00ab698 | 2017-12-31 18:15:06 -0500 | [diff] [blame] | 907 | f(&mut inner); |
David Tolnay | 106db5e | 2018-05-20 19:56:38 -0700 | [diff] [blame] | 908 | let mut g = Group::new(delim, inner); |
David Tolnay | 7f7eb0e | 2018-11-21 01:03:47 -0800 | [diff] [blame] | 909 | g.set_span(span); |
Alex Crichton | 9a4dca2 | 2018-03-28 06:32:19 -0700 | [diff] [blame] | 910 | tokens.append(g); |
Alex Crichton | 7b9e02f | 2017-05-30 15:54:33 -0700 | [diff] [blame] | 911 | } |
| 912 | } |