Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/syn-2.0.119/src/token.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / syn-2.0.119 / src / token.rs` 1104 lines · 36492 bytes Raw Blame History 1 //! Tokens representing Rust punctuation, keywords, and delimiters. 2 //! 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!`]: crate::Token 8 //! 9 //! # Example 10 //! 11 //! The [`ItemStatic`] syntax tree node is defined like this. 12 //! 13 //! [`ItemStatic`]: crate::ItemStatic 14 //! 15 //! ``` 16 //! # use syn::{Attribute, Expr, Ident, Token, Type, Visibility}; 17 //! # 18 //! pub struct ItemStatic { 19 //! pub attrs: Vec, 20 //! pub vis: Visibility, 21 //! pub static_token: Token![static], 22 //! pub mutability: Option, 23 //! pub ident: Ident, 24 //! pub colon_token: Token![:], 25 //! pub ty: Box, 26 //! pub eq_token: Token![=], 27 //! pub expr: Box, 28 //! pub semi_token: Token![;], 29 //! } 30 //! ``` 31 //! 32 //! # Parsing 33 //! 34 //! Keywords and punctuation can be parsed through the [`ParseStream::parse`] 35 //! method. Delimiter tokens are parsed using the [`parenthesized!`], 36 //! [`bracketed!`] and [`braced!`] macros. 37 //! 38 //! [`ParseStream::parse`]: crate::parse::ParseBuffer::parse() 39 //! [`parenthesized!`]: crate::parenthesized! 40 //! [`bracketed!`]: crate::bracketed! 41 //! [`braced!`]: crate::braced! 42 //! 43 //! ``` 44 //! use syn::{Attribute, Result}; 45 //! use syn::parse::{Parse, ParseStream}; 46 //! # 47 //! # enum ItemStatic {} 48 //! 49 //! // Parse the ItemStatic struct shown above. 50 //! impl Parse for ItemStatic { 51 //! fn parse(input: ParseStream) -> Result { 52 //! # use syn::ItemStatic; 53 //! # fn parse(input: ParseStream) -> Result { 54 //! Ok(ItemStatic { 55 //! attrs: input.call(Attribute::parse_outer)?, 56 //! vis: input.parse()?, 57 //! static_token: input.parse()?, 58 //! mutability: input.parse()?, 59 //! ident: input.parse()?, 60 //! colon_token: input.parse()?, 61 //! ty: input.parse()?, 62 //! eq_token: input.parse()?, 63 //! expr: input.parse()?, 64 //! semi_token: input.parse()?, 65 //! }) 66 //! # } 67 //! # unimplemented!() 68 //! } 69 //! } 70 //! ``` 71 //! 72 //! # Other operations 73 //! 74 //! Every keyword and punctuation token supports the following operations. 75 //! 76 //! - [Peeking] — `input.peek(Token![...])` 77 //! 78 //! - [Parsing] — `input.parse::()?` 79 //! 80 //! - [Printing] — `quote!( ... #the_token ... )` 81 //! 82 //! - Construction from a [`Span`] — `let the_token = Token![...](sp)` 83 //! 84 //! - Field access to its span — `let sp = the_token.span` 85 //! 86 //! [Peeking]: crate::parse::ParseBuffer::peek() 87 //! [Parsing]: crate::parse::ParseBuffer::parse() 88 //! [Printing]: https://docs.rs/quote/1.0/quote/trait.ToTokens.html 89 //! [`Span`]: https://docs.rs/proc-macro2/1.0/proc_macro2/struct.Span.html 90 91 #[cfg(feature = "parsing")] 92 pub(crate) use self::private::CustomToken; 93 use self::private::WithSpan; 94 #[cfg(feature = "parsing")] 95 use crate::buffer::Cursor; 96 #[cfg(feature = "parsing")] 97 use crate::error::Result; 98 #[cfg(feature = "parsing")] 99 use crate::lifetime::Lifetime; 100 #[cfg(feature = "parsing")] 101 use crate::parse::{Parse, ParseStream}; 102 use crate::span::IntoSpans; 103 #[cfg(feature = "extra-traits")] 104 use core::cmp; 105 #[cfg(feature = "extra-traits")] 106 use core::fmt::{self, Debug}; 107 #[cfg(feature = "extra-traits")] 108 use core::hash::{Hash, Hasher}; 109 use core::ops::{Deref, DerefMut}; 110 use proc_macro2::extra::DelimSpan; 111 use proc_macro2::Span; 112 #[cfg(feature = "printing")] 113 use proc_macro2::TokenStream; 114 #[cfg(any(feature = "parsing", feature = "printing"))] 115 use proc_macro2::{Delimiter, Ident}; 116 #[cfg(feature = "parsing")] 117 use proc_macro2::{Literal, Punct, TokenTree}; 118 #[cfg(feature = "printing")] 119 use quote::{ToTokens, TokenStreamExt as _}; 120 121 /// Marker trait for types that represent single tokens. 122 /// 123 /// This trait is sealed and cannot be implemented for types outside of Syn. 124 #[cfg(feature = "parsing")] 125 pub trait Token: private::Sealed { 126 // Not public API. 127 #[doc(hidden)] 128 fn peek(cursor: Cursor) -> bool; 129 130 // Not public API. 131 #[doc(hidden)] 132 fn display() -> &'static str; 133 } 134 135 pub(crate) mod private { 136 #[cfg(feature = "parsing")] 137 use crate::buffer::Cursor; 138 use proc_macro2::Span; 139 140 #[cfg(feature = "parsing")] 141 pub trait Sealed {} 142 143 /// Support writing `token.span` rather than `token.spans[0]` on tokens that 144 /// hold a single span. 145 #[repr(transparent)] 146 #[allow( 147 unknown_lints, 148 renamed_and_removed_lints, 149 // False positive: https://github.com/rust-lang/rust/issues/115922 150 repr_transparent_non_zst_fields, 151 )] 152 pub struct WithSpan { 153 pub span: Span, 154 } 155 156 // Not public API. 157 #[doc(hidden)] 158 #[cfg(feature = "parsing")] 159 pub trait CustomToken { 160 fn peek(cursor: Cursor) -> bool; 161 fn display() -> &'static str; 162 } 163 } 164 165 #[cfg(feature = "parsing")] 166 impl private::Sealed for Ident {} 167 168 macro_rules! impl_low_level_token { 169 ($display:literal $($path:ident)::+ $get:ident) => { 170 #[cfg(feature = "parsing")] 171 impl Token for $($path)::+ { 172 fn peek(cursor: Cursor) -> bool { 173 cursor.$get().is_some() 174 } 175 176 fn display() -> &'static str { 177 $display 178 } 179 } 180 181 #[cfg(feature = "parsing")] 182 impl private::Sealed for $($path)::+ {} 183 }; 184 } 185 186 impl_low_level_token!("punctuation token" Punct punct); 187 impl_low_level_token!("literal" Literal literal); 188 impl_low_level_token!("token" TokenTree token_tree); 189 impl_low_level_token!("group token" proc_macro2::Group any_group); 190 impl_low_level_token!("lifetime" Lifetime lifetime); 191 192 #[cfg(feature = "parsing")] 193 impl private::Sealed for T {} 194 195 #[cfg(feature = "parsing")] 196 impl Token for T { 197 fn peek(cursor: Cursor) -> bool { 198 ::peek(cursor) 199 } 200 201 fn display() -> &'static str { 202 ::display() 203 } 204 } 205 206 macro_rules! define_keywords { 207 ($($token:literal pub struct $name:ident)*) => { 208 $( 209 #[doc = concat!('`', $token, '`')] 210 /// 211 /// Don't try to remember the name of this type — use the 212 /// [`Token!`] macro instead. 213 /// 214 /// [`Token!`]: crate::token 215 pub struct $name { 216 pub span: Span, 217 } 218 219 #[doc(hidden)] 220 #[allow(non_snake_case)] 221 pub fn $name>(span: S) -> $name { 222 $name { 223 span: span.into_spans(), 224 } 225 } 226 227 impl core::default::Default for $name { 228 fn default() -> Self { 229 $name { 230 span: Span::call_site(), 231 } 232 } 233 } 234 235 #[cfg(feature = "clone-impls")] 236 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 237 impl Copy for $name {} 238 239 #[cfg(feature = "clone-impls")] 240 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 241 impl Clone for $name { 242 fn clone(&self) -> Self { 243 *self 244 } 245 } 246 247 #[cfg(feature = "extra-traits")] 248 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 249 impl Debug for $name { 250 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 251 f.write_str(stringify!($name)) 252 } 253 } 254 255 #[cfg(feature = "extra-traits")] 256 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 257 impl cmp::Eq for $name {} 258 259 #[cfg(feature = "extra-traits")] 260 #[cfg_attr(docsrs, doc(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 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 269 impl Hash for $name { 270 fn hash(&self, _state: &mut H) {} 271 } 272 273 #[cfg(feature = "printing")] 274 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] 275 impl ToTokens for $name { 276 fn to_tokens(&self, tokens: &mut TokenStream) { 277 printing::keyword($token, self.span, tokens); 278 } 279 } 280 281 #[cfg(feature = "parsing")] 282 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))] 283 impl Parse for $name { 284 fn parse(input: ParseStream) -> Result { 285 Ok($name { 286 span: parsing::keyword(input, $token)?, 287 }) 288 } 289 } 290 291 #[cfg(feature = "parsing")] 292 impl Token for $name { 293 fn peek(cursor: Cursor) -> bool { 294 parsing::peek_keyword(cursor, $token) 295 } 296 297 fn display() -> &'static str { 298 concat!("`", $token, "`") 299 } 300 } 301 302 #[cfg(feature = "parsing")] 303 impl private::Sealed for $name {} 304 )* 305 }; 306 } 307 308 macro_rules! impl_deref_if_len_is_1 { 309 ($name:ident/1) => { 310 impl Deref for $name { 311 type Target = WithSpan; 312 313 fn deref(&self) -> &Self::Target { 314 unsafe { &*(self as *const Self).cast::() } 315 } 316 } 317 318 impl DerefMut for $name { 319 fn deref_mut(&mut self) -> &mut Self::Target { 320 unsafe { &mut *(self as *mut Self).cast::() } 321 } 322 } 323 }; 324 325 ($name:ident/$len:literal) => {}; 326 } 327 328 macro_rules! define_punctuation_structs { 329 ($($token:literal pub struct $name:ident/$len:tt #[doc = $usage:literal])*) => { 330 $( 331 #[cfg_attr(not(doc), repr(transparent))] 332 #[allow( 333 unknown_lints, 334 renamed_and_removed_lints, 335 // False positive: https://github.com/rust-lang/rust/issues/115922 336 repr_transparent_non_zst_fields, 337 )] 338 #[doc = concat!('`', $token, '`')] 339 /// 340 /// Usage: 341 #[doc = concat!($usage, '.')] 342 /// 343 /// Don't try to remember the name of this type — use the 344 /// [`Token!`] macro instead. 345 /// 346 /// [`Token!`]: crate::token 347 pub struct $name { 348 pub spans: [Span; $len], 349 } 350 351 #[doc(hidden)] 352 #[allow(non_snake_case)] 353 pub fn $name>(spans: S) -> $name { 354 $name { 355 spans: spans.into_spans(), 356 } 357 } 358 359 impl core::default::Default for $name { 360 fn default() -> Self { 361 $name { 362 spans: [Span::call_site(); $len], 363 } 364 } 365 } 366 367 #[cfg(feature = "clone-impls")] 368 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 369 impl Copy for $name {} 370 371 #[cfg(feature = "clone-impls")] 372 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 373 impl Clone for $name { 374 fn clone(&self) -> Self { 375 *self 376 } 377 } 378 379 #[cfg(feature = "extra-traits")] 380 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 381 impl Debug for $name { 382 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 383 f.write_str(stringify!($name)) 384 } 385 } 386 387 #[cfg(feature = "extra-traits")] 388 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 389 impl cmp::Eq for $name {} 390 391 #[cfg(feature = "extra-traits")] 392 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 393 impl PartialEq for $name { 394 fn eq(&self, _other: &$name) -> bool { 395 true 396 } 397 } 398 399 #[cfg(feature = "extra-traits")] 400 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 401 impl Hash for $name { 402 fn hash(&self, _state: &mut H) {} 403 } 404 405 impl_deref_if_len_is_1!($name/$len); 406 )* 407 }; 408 } 409 410 macro_rules! define_punctuation { 411 ($($token:literal pub struct $name:ident/$len:tt #[doc = $usage:literal])*) => { 412 $( 413 define_punctuation_structs! { 414 $token pub struct $name/$len #[doc = $usage] 415 } 416 417 #[cfg(feature = "printing")] 418 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] 419 impl ToTokens for $name { 420 fn to_tokens(&self, tokens: &mut TokenStream) { 421 printing::punct($token, &self.spans, tokens); 422 } 423 } 424 425 #[cfg(feature = "parsing")] 426 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))] 427 impl Parse for $name { 428 fn parse(input: ParseStream) -> Result { 429 Ok($name { 430 spans: parsing::punct(input, $token)?, 431 }) 432 } 433 } 434 435 #[cfg(feature = "parsing")] 436 impl Token for $name { 437 fn peek(cursor: Cursor) -> bool { 438 parsing::peek_punct(cursor, $token) 439 } 440 441 fn display() -> &'static str { 442 concat!("`", $token, "`") 443 } 444 } 445 446 #[cfg(feature = "parsing")] 447 impl private::Sealed for $name {} 448 )* 449 }; 450 } 451 452 macro_rules! define_delimiters { 453 ($($delim:ident pub struct $name:ident #[$doc:meta])*) => { 454 $( 455 #[$doc] 456 pub struct $name { 457 pub span: DelimSpan, 458 } 459 460 #[doc(hidden)] 461 #[allow(non_snake_case)] 462 pub fn $name>(span: S) -> $name { 463 $name { 464 span: span.into_spans(), 465 } 466 } 467 468 impl core::default::Default for $name { 469 fn default() -> Self { 470 $name(Span::call_site()) 471 } 472 } 473 474 #[cfg(feature = "clone-impls")] 475 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 476 impl Copy for $name {} 477 478 #[cfg(feature = "clone-impls")] 479 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 480 impl Clone for $name { 481 fn clone(&self) -> Self { 482 *self 483 } 484 } 485 486 #[cfg(feature = "extra-traits")] 487 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 488 impl Debug for $name { 489 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 490 f.write_str(stringify!($name)) 491 } 492 } 493 494 #[cfg(feature = "extra-traits")] 495 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 496 impl cmp::Eq for $name {} 497 498 #[cfg(feature = "extra-traits")] 499 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 500 impl PartialEq for $name { 501 fn eq(&self, _other: &$name) -> bool { 502 true 503 } 504 } 505 506 #[cfg(feature = "extra-traits")] 507 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 508 impl Hash for $name { 509 fn hash(&self, _state: &mut H) {} 510 } 511 512 impl $name { 513 #[cfg(feature = "printing")] 514 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] 515 pub fn surround(&self, tokens: &mut TokenStream, f: F) 516 where 517 F: FnOnce(&mut TokenStream), 518 { 519 let mut inner = TokenStream::new(); 520 f(&mut inner); 521 printing::delim(Delimiter::$delim, self.span.join(), tokens, inner); 522 } 523 } 524 525 #[cfg(feature = "parsing")] 526 impl private::Sealed for $name {} 527 )* 528 }; 529 } 530 531 define_punctuation_structs! { 532 "_" pub struct Underscore/1 /// wildcard patterns, inferred types, unnamed items in constants, extern crates, use declarations, and destructuring assignment 533 } 534 535 #[cfg(feature = "printing")] 536 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] 537 impl ToTokens for Underscore { 538 fn to_tokens(&self, tokens: &mut TokenStream) { 539 tokens.append(Ident::new("_", self.span)); 540 } 541 } 542 543 #[cfg(feature = "parsing")] 544 #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))] 545 impl Parse for Underscore { 546 fn parse(input: ParseStream) -> Result { 547 input.step(|cursor| { 548 if let Some((ident, rest)) = cursor.ident() { 549 if ident == "_" { 550 return Ok((Underscore(ident.span()), rest)); 551 } 552 } 553 if let Some((punct, rest)) = cursor.punct() { 554 if punct.as_char() == '_' { 555 return Ok((Underscore(punct.span()), rest)); 556 } 557 } 558 Err(cursor.error("expected `_`")) 559 }) 560 } 561 } 562 563 #[cfg(feature = "parsing")] 564 impl Token for Underscore { 565 fn peek(cursor: Cursor) -> bool { 566 if let Some((ident, _rest)) = cursor.ident() { 567 return ident == "_"; 568 } 569 if let Some((punct, _rest)) = cursor.punct() { 570 return punct.as_char() == '_'; 571 } 572 false 573 } 574 575 fn display() -> &'static str { 576 "`_`" 577 } 578 } 579 580 #[cfg(feature = "parsing")] 581 impl private::Sealed for Underscore {} 582 583 /// None-delimited group 584 pub struct Group { 585 pub span: Span, 586 } 587 588 #[doc(hidden)] 589 #[allow(non_snake_case)] 590 pub fn Group>(span: S) -> Group { 591 Group { 592 span: span.into_spans(), 593 } 594 } 595 596 impl core::default::Default for Group { 597 fn default() -> Self { 598 Group { 599 span: Span::call_site(), 600 } 601 } 602 } 603 604 #[cfg(feature = "clone-impls")] 605 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 606 impl Copy for Group {} 607 608 #[cfg(feature = "clone-impls")] 609 #[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))] 610 impl Clone for Group { 611 fn clone(&self) -> Self { 612 *self 613 } 614 } 615 616 #[cfg(feature = "extra-traits")] 617 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 618 impl Debug for Group { 619 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 620 f.write_str("Group") 621 } 622 } 623 624 #[cfg(feature = "extra-traits")] 625 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 626 impl cmp::Eq for Group {} 627 628 #[cfg(feature = "extra-traits")] 629 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 630 impl PartialEq for Group { 631 fn eq(&self, _other: &Group) -> bool { 632 true 633 } 634 } 635 636 #[cfg(feature = "extra-traits")] 637 #[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))] 638 impl Hash for Group { 639 fn hash(&self, _state: &mut H) {} 640 } 641 642 impl Group { 643 #[cfg(feature = "printing")] 644 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] 645 pub fn surround(&self, tokens: &mut TokenStream, f: F) 646 where 647 F: FnOnce(&mut TokenStream), 648 { 649 let mut inner = TokenStream::new(); 650 f(&mut inner); 651 printing::delim(Delimiter::None, self.span, tokens, inner); 652 } 653 } 654 655 #[cfg(feature = "parsing")] 656 impl private::Sealed for Group {} 657 658 #[cfg(feature = "parsing")] 659 impl Token for Paren { 660 fn peek(cursor: Cursor) -> bool { 661 cursor.group(Delimiter::Parenthesis).is_some() 662 } 663 664 fn display() -> &'static str { 665 "parentheses" 666 } 667 } 668 669 #[cfg(feature = "parsing")] 670 impl Token for Brace { 671 fn peek(cursor: Cursor) -> bool { 672 cursor.group(Delimiter::Brace).is_some() 673 } 674 675 fn display() -> &'static str { 676 "curly braces" 677 } 678 } 679 680 #[cfg(feature = "parsing")] 681 impl Token for Bracket { 682 fn peek(cursor: Cursor) -> bool { 683 cursor.group(Delimiter::Bracket).is_some() 684 } 685 686 fn display() -> &'static str { 687 "square brackets" 688 } 689 } 690 691 #[cfg(feature = "parsing")] 692 impl Token for Group { 693 fn peek(cursor: Cursor) -> bool { 694 cursor.group(Delimiter::None).is_some() 695 } 696 697 fn display() -> &'static str { 698 "invisible group" 699 } 700 } 701 702 define_keywords! { 703 "abstract" pub struct Abstract 704 "as" pub struct As 705 "async" pub struct Async 706 "auto" pub struct Auto 707 "await" pub struct Await 708 "become" pub struct Become 709 "box" pub struct Box 710 "break" pub struct Break 711 "const" pub struct Const 712 "continue" pub struct Continue 713 "crate" pub struct Crate 714 "default" pub struct Default 715 "do" pub struct Do 716 "dyn" pub struct Dyn 717 "else" pub struct Else 718 "enum" pub struct Enum 719 "extern" pub struct Extern 720 "final" pub struct Final 721 "fn" pub struct Fn 722 "for" pub struct For 723 "if" pub struct If 724 "impl" pub struct Impl 725 "in" pub struct In 726 "let" pub struct Let 727 "loop" pub struct Loop 728 "macro" pub struct Macro 729 "match" pub struct Match 730 "mod" pub struct Mod 731 "move" pub struct Move 732 "mut" pub struct Mut 733 "override" pub struct Override 734 "priv" pub struct Priv 735 "pub" pub struct Pub 736 "raw" pub struct Raw 737 "ref" pub struct Ref 738 "return" pub struct Return 739 "Self" pub struct SelfType 740 "self" pub struct SelfValue 741 "static" pub struct Static 742 "struct" pub struct Struct 743 "super" pub struct Super 744 "trait" pub struct Trait 745 "try" pub struct Try 746 "type" pub struct Type 747 "typeof" pub struct Typeof 748 "union" pub struct Union 749 "unsafe" pub struct Unsafe 750 "unsized" pub struct Unsized 751 "use" pub struct Use 752 "virtual" pub struct Virtual 753 "where" pub struct Where 754 "while" pub struct While 755 "yield" pub struct Yield 756 } 757 758 define_punctuation! { 759 "&" pub struct And/1 /// bitwise and logical AND, borrow, references, reference patterns 760 "&&" pub struct AndAnd/2 /// lazy AND, borrow, references, reference patterns 761 "&=" pub struct AndEq/2 /// bitwise AND assignment 762 "@" pub struct At/1 /// subpattern binding 763 "^" pub struct Caret/1 /// bitwise and logical XOR 764 "^=" pub struct CaretEq/2 /// bitwise XOR assignment 765 ":" pub struct Colon/1 /// various separators 766 "," pub struct Comma/1 /// various separators 767 "$" pub struct Dollar/1 /// macros 768 "." pub struct Dot/1 /// field access, tuple index 769 ".." pub struct DotDot/2 /// range, struct expressions, patterns, range patterns 770 "..." pub struct DotDotDot/3 /// variadic functions, range patterns 771 "..=" pub struct DotDotEq/3 /// inclusive range, range patterns 772 "=" pub struct Eq/1 /// assignment, attributes, various type definitions 773 "==" pub struct EqEq/2 /// equal 774 "=>" pub struct FatArrow/2 /// match arms, macros 775 ">=" pub struct Ge/2 /// greater than or equal to, generics 776 ">" pub struct Gt/1 /// greater than, generics, paths 777 "<-" pub struct LArrow/2 /// unused 778 "<=" pub struct Le/2 /// less than or equal to 779 "<" pub struct Lt/1 /// less than, generics, paths 780 "-" pub struct Minus/1 /// subtraction, negation 781 "-=" pub struct MinusEq/2 /// subtraction assignment 782 "!=" pub struct Ne/2 /// not equal 783 "!" pub struct Not/1 /// bitwise and logical NOT, macro calls, inner attributes, never type, negative impls 784 "|" pub struct Or/1 /// bitwise and logical OR, closures, patterns in match, if let, and while let 785 "|=" pub struct OrEq/2 /// bitwise OR assignment 786 "||" pub struct OrOr/2 /// lazy OR, closures 787 "::" pub struct PathSep/2 /// path separator 788 "%" pub struct Percent/1 /// remainder 789 "%=" pub struct PercentEq/2 /// remainder assignment 790 "+" pub struct Plus/1 /// addition, trait bounds, macro Kleene matcher 791 "+=" pub struct PlusEq/2 /// addition assignment 792 "#" pub struct Pound/1 /// attributes 793 "?" pub struct Question/1 /// question mark operator, questionably sized, macro Kleene matcher 794 "->" pub struct RArrow/2 /// function return type, closure return type, function pointer type 795 ";" pub struct Semi/1 /// terminator for various items and statements, array types 796 "<<" pub struct Shl/2 /// shift left, nested generics 797 "<<=" pub struct ShlEq/3 /// shift left assignment 798 ">>" pub struct Shr/2 /// shift right, nested generics 799 ">>=" pub struct ShrEq/3 /// shift right assignment, nested generics 800 "/" pub struct Slash/1 /// division 801 "/=" pub struct SlashEq/2 /// division assignment 802 "*" pub struct Star/1 /// multiplication, dereference, raw pointers, macro Kleene matcher, use wildcards 803 "*=" pub struct StarEq/2 /// multiplication assignment 804 "~" pub struct Tilde/1 /// unused since before Rust 1.0 805 } 806 807 define_delimiters! { 808 Brace pub struct Brace /// `{`…`}` 809 Bracket pub struct Bracket /// `[`…`]` 810 Parenthesis pub struct Paren /// `(`…`)` 811 } 812 813 /// A type-macro that expands to the name of the Rust type representation of a 814 /// given token. 815 /// 816 /// As a type, `Token!` is commonly used in the type of struct fields, the type 817 /// of a `let` statement, or in turbofish for a `parse` function. 818 /// 819 /// ``` 820 /// use syn::{Ident, Token}; 821 /// use syn::parse::{Parse, ParseStream, Result}; 822 /// 823 /// // `struct Foo;` 824 /// pub struct UnitStruct { 825 /// struct_token: Token![struct], 826 /// ident: Ident, 827 /// semi_token: Token![;], 828 /// } 829 /// 830 /// impl Parse for UnitStruct { 831 /// fn parse(input: ParseStream) -> Result { 832 /// let struct_token: Token![struct] = input.parse()?; 833 /// let ident: Ident = input.parse()?; 834 /// let semi_token = input.parse::()?; 835 /// Ok(UnitStruct { struct_token, ident, semi_token }) 836 /// } 837 /// } 838 /// ``` 839 /// 840 /// As an expression, `Token!` is used for peeking tokens or instantiating 841 /// tokens from a span. 842 /// 843 /// ``` 844 /// # use syn::{Ident, Token}; 845 /// # use syn::parse::{Parse, ParseStream, Result}; 846 /// # 847 /// # struct UnitStruct { 848 /// # struct_token: Token![struct], 849 /// # ident: Ident, 850 /// # semi_token: Token![;], 851 /// # } 852 /// # 853 /// # impl Parse for UnitStruct { 854 /// # fn parse(input: ParseStream) -> Result { 855 /// # unimplemented!() 856 /// # } 857 /// # } 858 /// # 859 /// fn make_unit_struct(name: Ident) -> UnitStruct { 860 /// let span = name.span(); 861 /// UnitStruct { 862 /// struct_token: Token![struct](span), 863 /// ident: name, 864 /// semi_token: Token![;](span), 865 /// } 866 /// } 867 /// 868 /// # fn parse(input: ParseStream) -> Result<()> { 869 /// if input.peek(Token![struct]) { 870 /// let unit_struct: UnitStruct = input.parse()?; 871 /// /* ... */ 872 /// } 873 /// # Ok(()) 874 /// # } 875 /// ``` 876 /// 877 /// See the [token module] documentation for details and examples. 878 /// 879 /// [token module]: crate::token 880 #[macro_export] 881 macro_rules! Token { 882 [abstract] => { $crate::token::Abstract }; 883 [as] => { $crate::token::As }; 884 [async] => { $crate::token::Async }; 885 [auto] => { $crate::token::Auto }; 886 [await] => { $crate::token::Await }; 887 [become] => { $crate::token::Become }; 888 [box] => { $crate::token::Box }; 889 [break] => { $crate::token::Break }; 890 [const] => { $crate::token::Const }; 891 [continue] => { $crate::token::Continue }; 892 [crate] => { $crate::token::Crate }; 893 [default] => { $crate::token::Default }; 894 [do] => { $crate::token::Do }; 895 [dyn] => { $crate::token::Dyn }; 896 [else] => { $crate::token::Else }; 897 [enum] => { $crate::token::Enum }; 898 [extern] => { $crate::token::Extern }; 899 [final] => { $crate::token::Final }; 900 [fn] => { $crate::token::Fn }; 901 [for] => { $crate::token::For }; 902 [if] => { $crate::token::If }; 903 [impl] => { $crate::token::Impl }; 904 [in] => { $crate::token::In }; 905 [let] => { $crate::token::Let }; 906 [loop] => { $crate::token::Loop }; 907 [macro] => { $crate::token::Macro }; 908 [match] => { $crate::token::Match }; 909 [mod] => { $crate::token::Mod }; 910 [move] => { $crate::token::Move }; 911 [mut] => { $crate::token::Mut }; 912 [override] => { $crate::token::Override }; 913 [priv] => { $crate::token::Priv }; 914 [pub] => { $crate::token::Pub }; 915 [raw] => { $crate::token::Raw }; 916 [ref] => { $crate::token::Ref }; 917 [return] => { $crate::token::Return }; 918 [Self] => { $crate::token::SelfType }; 919 [self] => { $crate::token::SelfValue }; 920 [static] => { $crate::token::Static }; 921 [struct] => { $crate::token::Struct }; 922 [super] => { $crate::token::Super }; 923 [trait] => { $crate::token::Trait }; 924 [try] => { $crate::token::Try }; 925 [type] => { $crate::token::Type }; 926 [typeof] => { $crate::token::Typeof }; 927 [union] => { $crate::token::Union }; 928 [unsafe] => { $crate::token::Unsafe }; 929 [unsized] => { $crate::token::Unsized }; 930 [use] => { $crate::token::Use }; 931 [virtual] => { $crate::token::Virtual }; 932 [where] => { $crate::token::Where }; 933 [while] => { $crate::token::While }; 934 [yield] => { $crate::token::Yield }; 935 [&] => { $crate::token::And }; 936 [&&] => { $crate::token::AndAnd }; 937 [&=] => { $crate::token::AndEq }; 938 [@] => { $crate::token::At }; 939 [^] => { $crate::token::Caret }; 940 [^=] => { $crate::token::CaretEq }; 941 [:] => { $crate::token::Colon }; 942 [,] => { $crate::token::Comma }; 943 [$] => { $crate::token::Dollar }; 944 [.] => { $crate::token::Dot }; 945 [..] => { $crate::token::DotDot }; 946 [...] => { $crate::token::DotDotDot }; 947 [..=] => { $crate::token::DotDotEq }; 948 [=] => { $crate::token::Eq }; 949 [==] => { $crate::token::EqEq }; 950 [=>] => { $crate::token::FatArrow }; 951 [>=] => { $crate::token::Ge }; 952 [>] => { $crate::token::Gt }; 953 [<-] => { $crate::token::LArrow }; 954 [<=] => { $crate::token::Le }; 955 [<] => { $crate::token::Lt }; 956 [-] => { $crate::token::Minus }; 957 [-=] => { $crate::token::MinusEq }; 958 [!=] => { $crate::token::Ne }; 959 [!] => { $crate::token::Not }; 960 [|] => { $crate::token::Or }; 961 [|=] => { $crate::token::OrEq }; 962 [||] => { $crate::token::OrOr }; 963 [::] => { $crate::token::PathSep }; 964 [%] => { $crate::token::Percent }; 965 [%=] => { $crate::token::PercentEq }; 966 [+] => { $crate::token::Plus }; 967 [+=] => { $crate::token::PlusEq }; 968 [#] => { $crate::token::Pound }; 969 [?] => { $crate::token::Question }; 970 [->] => { $crate::token::RArrow }; 971 [;] => { $crate::token::Semi }; 972 [<<] => { $crate::token::Shl }; 973 [<<=] => { $crate::token::ShlEq }; 974 [>>] => { $crate::token::Shr }; 975 [>>=] => { $crate::token::ShrEq }; 976 [/] => { $crate::token::Slash }; 977 [/=] => { $crate::token::SlashEq }; 978 [*] => { $crate::token::Star }; 979 [*=] => { $crate::token::StarEq }; 980 [~] => { $crate::token::Tilde }; 981 [_] => { $crate::token::Underscore }; 982 } 983 984 // Not public API. 985 #[doc(hidden)] 986 #[cfg(feature = "parsing")] 987 pub(crate) mod parsing { 988 use crate::buffer::Cursor; 989 use crate::error::{Error, Result}; 990 use crate::parse::ParseStream; 991 use alloc::format; 992 use proc_macro2::{Spacing, Span}; 993 994 pub(crate) fn keyword(input: ParseStream, token: &str) -> Result { 995 input.step(|cursor| { 996 if let Some((ident, rest)) = cursor.ident() { 997 if ident == token { 998 return Ok((ident.span(), rest)); 999 } 1000 } 1001 Err(cursor.error(format!("expected `{}`", token))) 1002 }) 1003 } 1004 1005 pub(crate) fn peek_keyword(cursor: Cursor, token: &str) -> bool { 1006 if let Some((ident, _rest)) = cursor.ident() { 1007 ident == token 1008 } else { 1009 false 1010 } 1011 } 1012 1013 #[doc(hidden)] 1014 pub fn punct(input: ParseStream, token: &str) -> Result<[Span; N]> { 1015 let mut spans = [input.span(); N]; 1016 punct_helper(input, token, &mut spans)?; 1017 Ok(spans) 1018 } 1019 1020 fn punct_helper(input: ParseStream, token: &str, spans: &mut [Span]) -> Result<()> { 1021 input.step(|cursor| { 1022 let mut cursor = *cursor; 1023 assert_eq!(token.len(), spans.len()); 1024 1025 for (i, ch) in token.chars().enumerate() { 1026 match cursor.punct() { 1027 Some((punct, rest)) => { 1028 spans[i] = punct.span(); 1029 if punct.as_char() != ch { 1030 break; 1031 } else if i == token.len() - 1 { 1032 return Ok(((), rest)); 1033 } else if punct.spacing() != Spacing::Joint { 1034 break; 1035 } 1036 cursor = rest; 1037 } 1038 None => break, 1039 } 1040 } 1041 1042 Err(Error::new(spans[0], format!("expected `{}`", token))) 1043 }) 1044 } 1045 1046 #[doc(hidden)] 1047 pub fn peek_punct(mut cursor: Cursor, token: &str) -> bool { 1048 for (i, ch) in token.chars().enumerate() { 1049 match cursor.punct() { 1050 Some((punct, rest)) => { 1051 if punct.as_char() != ch { 1052 break; 1053 } else if i == token.len() - 1 { 1054 return true; 1055 } else if punct.spacing() != Spacing::Joint { 1056 break; 1057 } 1058 cursor = rest; 1059 } 1060 None => break, 1061 } 1062 } 1063 false 1064 } 1065 } 1066 1067 // Not public API. 1068 #[doc(hidden)] 1069 #[cfg(feature = "printing")] 1070 pub(crate) mod printing { 1071 use crate::ext::PunctExt as _; 1072 use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream}; 1073 use quote::TokenStreamExt as _; 1074 1075 #[doc(hidden)] 1076 pub fn punct(s: &str, spans: &[Span], tokens: &mut TokenStream) { 1077 assert_eq!(s.len(), spans.len()); 1078 1079 let mut chars = s.chars(); 1080 let mut spans = spans.iter(); 1081 let ch = chars.next_back().unwrap(); 1082 let span = spans.next_back().unwrap(); 1083 for (ch, span) in chars.zip(spans) { 1084 tokens.append(Punct::new_spanned(ch, Spacing::Joint, *span)); 1085 } 1086 1087 tokens.append(Punct::new_spanned(ch, Spacing::Alone, *span)); 1088 } 1089 1090 pub(crate) fn keyword(s: &str, span: Span, tokens: &mut TokenStream) { 1091 tokens.append(Ident::new(s, span)); 1092 } 1093 1094 pub(crate) fn delim( 1095 delim: Delimiter, 1096 span: Span, 1097 tokens: &mut TokenStream, 1098 inner: TokenStream, 1099 ) { 1100 let mut g = Group::new(delim, inner); 1101 g.set_span(span); 1102 tokens.append(g); 1103 } 1104 }