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