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