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