1#[cfg(feature = "parsing")]
92pub(crate) use self::private::CustomToken;
93use self::private::WithSpan;
94#[cfg(feature = "parsing")]
95use crate::buffer::Cursor;
96#[cfg(feature = "parsing")]
97use crate::error::Result;
98#[cfg(feature = "parsing")]
99use crate::lifetime::Lifetime;
100#[cfg(feature = "parsing")]
101use crate::parse::{Parse, ParseStream};
102use crate::span::IntoSpans;
103#[cfg(feature = "extra-traits")]
104use core::cmp;
105#[cfg(feature = "extra-traits")]
106use core::fmt::{self, Debug};
107#[cfg(feature = "extra-traits")]
108use core::hash::{Hash, Hasher};
109use core::ops::{Deref, DerefMut};
110use proc_macro2::extra::DelimSpan;
111use proc_macro2::Span;
112#[cfg(feature = "printing")]
113use proc_macro2::TokenStream;
114#[cfg(any(feature = "parsing", feature = "printing"))]
115use proc_macro2::{Delimiter, Ident};
116#[cfg(feature = "parsing")]
117use proc_macro2::{Literal, Punct, TokenTree};
118#[cfg(feature = "printing")]
119use quote::{ToTokens, TokenStreamExt as _};
120
121#[cfg(feature = "parsing")]
125pub trait Token: private::Sealed {
126 #[doc(hidden)]
128 fn peek(cursor: Cursor) -> bool;
129
130 #[doc(hidden)]
132 fn display() -> &'static str;
133}
134
135pub(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 #[repr(transparent)]
146 #[allow(
147 unknown_lints,
148 renamed_and_removed_lints,
149 repr_transparent_non_zst_fields,
151 )]
152 pub struct WithSpan {
153 pub span: Span,
154 }
155
156 #[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")]
166impl private::Sealed for Ident {}
167
168macro_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
186impl Token for Punct {
fn peek(cursor: Cursor) -> bool { cursor.punct().is_some() }
fn display() -> &'static str { "punctuation token" }
}
impl private::Sealed for Punct { }impl_low_level_token!("punctuation token" Punct punct);
187impl Token for Literal {
fn peek(cursor: Cursor) -> bool { cursor.literal().is_some() }
fn display() -> &'static str { "literal" }
}
impl private::Sealed for Literal { }impl_low_level_token!("literal" Literal literal);
188impl Token for TokenTree {
fn peek(cursor: Cursor) -> bool { cursor.token_tree().is_some() }
fn display() -> &'static str { "token" }
}
impl private::Sealed for TokenTree { }impl_low_level_token!("token" TokenTree token_tree);
189impl Token for proc_macro2::Group {
fn peek(cursor: Cursor) -> bool { cursor.any_group().is_some() }
fn display() -> &'static str { "group token" }
}
impl private::Sealed for proc_macro2::Group { }impl_low_level_token!("group token" proc_macro2::Group any_group);
190impl Token for Lifetime {
fn peek(cursor: Cursor) -> bool { cursor.lifetime().is_some() }
fn display() -> &'static str { "lifetime" }
}
impl private::Sealed for Lifetime { }impl_low_level_token!("lifetime" Lifetime lifetime);
191
192#[cfg(feature = "parsing")]
193impl<T: CustomToken> private::Sealed for T {}
194
195#[cfg(feature = "parsing")]
196impl<T: CustomToken> Token for T {
197 fn peek(cursor: Cursor) -> bool {
198 <Self as CustomToken>::peek(cursor)
199 }
200
201 fn display() -> &'static str {
202 <Self as CustomToken>::display()
203 }
204}
205
206macro_rules! define_keywords {
207 ($($token:literal pub struct $name:ident)*) => {
208 $(
209 #[doc = concat!('`', $token, '`')]
210 pub struct $name {
216 pub span: Span,
217 }
218
219 #[doc(hidden)]
220 #[allow(non_snake_case)]
221 pub fn $name<S: IntoSpans<Span>>(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<H: Hasher>(&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<Self> {
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
308macro_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::<WithSpan>() }
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::<WithSpan>() }
321 }
322 }
323 };
324
325 ($name:ident/$len:literal) => {};
326}
327
328macro_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 repr_transparent_non_zst_fields,
337 )]
338 #[doc = concat!('`', $token, '`')]
339 #[doc = concat!($usage, '.')]
342 pub struct $name {
348 pub spans: [Span; $len],
349 }
350
351 #[doc(hidden)]
352 #[allow(non_snake_case)]
353 pub fn $name<S: IntoSpans<[Span; $len]>>(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<H: Hasher>(&self, _state: &mut H) {}
403 }
404
405 impl_deref_if_len_is_1!($name/$len);
406 )*
407 };
408}
409
410macro_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<Self> {
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
452macro_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<S: IntoSpans<DelimSpan>>(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<H: Hasher>(&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<F>(&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#[allow(unknown_lints, renamed_and_removed_lints,
repr_transparent_non_zst_fields,)]
#[doc = "`_`"]
#[doc =
" wildcard patterns, inferred types, unnamed items in constants, extern crates, use declarations, and destructuring assignment."]
pub struct Underscore {
pub spans: [Span; 1],
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn Underscore<S: IntoSpans<[Span; 1]>>(spans: S) -> Underscore {
Underscore { spans: spans.into_spans() }
}
impl core::default::Default for Underscore {
fn default() -> Self { Underscore { spans: [Span::call_site(); 1] } }
}
#[doc(cfg(feature = "clone-impls"))]
impl Copy for Underscore { }
#[doc(cfg(feature = "clone-impls"))]
impl Clone for Underscore {
fn clone(&self) -> Self { *self }
}
#[doc(cfg(feature = "extra-traits"))]
impl Debug for Underscore {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Underscore")
}
}
#[doc(cfg(feature = "extra-traits"))]
impl cmp::Eq for Underscore { }
#[doc(cfg(feature = "extra-traits"))]
impl PartialEq for Underscore {
fn eq(&self, _other: &Underscore) -> bool { true }
}
#[doc(cfg(feature = "extra-traits"))]
impl Hash for Underscore {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
impl Deref for Underscore {
type Target = WithSpan;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self).cast::<WithSpan>() }
}
}
impl DerefMut for Underscore {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self).cast::<WithSpan>() }
}
}define_punctuation_structs! {
532 "_" pub struct Underscore/1 }
534
535#[cfg(feature = "printing")]
536#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
537impl 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")))]
545impl Parse for Underscore {
546 fn parse(input: ParseStream) -> Result<Self> {
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")]
564impl 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")]
581impl private::Sealed for Underscore {}
582
583pub struct Group {
585 pub span: Span,
586}
587
588#[doc(hidden)]
589#[allow(non_snake_case)]
590pub fn Group<S: IntoSpans<Span>>(span: S) -> Group {
591 Group {
592 span: span.into_spans(),
593 }
594}
595
596impl 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")))]
606impl Copy for Group {}
607
608#[cfg(feature = "clone-impls")]
609#[cfg_attr(docsrs, doc(cfg(feature = "clone-impls")))]
610impl 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")))]
618impl 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")))]
626impl cmp::Eq for Group {}
627
628#[cfg(feature = "extra-traits")]
629#[cfg_attr(docsrs, doc(cfg(feature = "extra-traits")))]
630impl 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")))]
638impl Hash for Group {
639 fn hash<H: Hasher>(&self, _state: &mut H) {}
640}
641
642impl Group {
643 #[cfg(feature = "printing")]
644 #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
645 pub fn surround<F>(&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")]
656impl private::Sealed for Group {}
657
658#[cfg(feature = "parsing")]
659impl 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")]
670impl 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")]
681impl 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")]
692impl 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#[doc = "`yield`"]
pub struct Yield {
pub span: Span,
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn Yield<S: IntoSpans<Span>>(span: S) -> Yield {
Yield { span: span.into_spans() }
}
impl core::default::Default for Yield {
fn default() -> Self { Yield { span: Span::call_site() } }
}
#[doc(cfg(feature = "clone-impls"))]
impl Copy for Yield { }
#[doc(cfg(feature = "clone-impls"))]
impl Clone for Yield {
fn clone(&self) -> Self { *self }
}
#[doc(cfg(feature = "extra-traits"))]
impl Debug for Yield {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Yield")
}
}
#[doc(cfg(feature = "extra-traits"))]
impl cmp::Eq for Yield { }
#[doc(cfg(feature = "extra-traits"))]
impl PartialEq for Yield {
fn eq(&self, _other: &Yield) -> bool { true }
}
#[doc(cfg(feature = "extra-traits"))]
impl Hash for Yield {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
#[doc(cfg(feature = "printing"))]
impl ToTokens for Yield {
fn to_tokens(&self, tokens: &mut TokenStream) {
printing::keyword("yield", self.span, tokens);
}
}
#[doc(cfg(feature = "parsing"))]
impl Parse for Yield {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Yield { span: parsing::keyword(input, "yield")? })
}
}
impl Token for Yield {
fn peek(cursor: Cursor) -> bool { parsing::peek_keyword(cursor, "yield") }
fn display() -> &'static str { "`yield`" }
}
impl private::Sealed for Yield { }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
758impl Deref for Tilde {
type Target = WithSpan;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self).cast::<WithSpan>() }
}
}
impl DerefMut for Tilde {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self).cast::<WithSpan>() }
}
}
#[doc(cfg(feature = "printing"))]
impl ToTokens for Tilde {
fn to_tokens(&self, tokens: &mut TokenStream) {
printing::punct("~", &self.spans, tokens);
}
}
#[doc(cfg(feature = "parsing"))]
impl Parse for Tilde {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Tilde { spans: parsing::punct(input, "~")? })
}
}
impl Token for Tilde {
fn peek(cursor: Cursor) -> bool { parsing::peek_punct(cursor, "~") }
fn display() -> &'static str { "`~`" }
}
impl private::Sealed for Tilde { }define_punctuation! {
759 "&" pub struct And/1 "&&" pub struct AndAnd/2 "&=" pub struct AndEq/2 "@" pub struct At/1 "^" pub struct Caret/1 "^=" pub struct CaretEq/2 ":" pub struct Colon/1 "," pub struct Comma/1 "$" pub struct Dollar/1 "." pub struct Dot/1 ".." pub struct DotDot/2 "..." pub struct DotDotDot/3 "..=" pub struct DotDotEq/3 "=" pub struct Eq/1 "==" pub struct EqEq/2 "=>" pub struct FatArrow/2 ">=" pub struct Ge/2 ">" pub struct Gt/1 "<-" pub struct LArrow/2 "<=" pub struct Le/2 "<" pub struct Lt/1 "-" pub struct Minus/1 "-=" pub struct MinusEq/2 "!=" pub struct Ne/2 "!" pub struct Not/1 "|" pub struct Or/1 "|=" pub struct OrEq/2 "||" pub struct OrOr/2 "::" pub struct PathSep/2 "%" pub struct Percent/1 "%=" pub struct PercentEq/2 "+" pub struct Plus/1 "+=" pub struct PlusEq/2 "#" pub struct Pound/1 "?" pub struct Question/1 "->" pub struct RArrow/2 ";" pub struct Semi/1 "<<" pub struct Shl/2 "<<=" pub struct ShlEq/3 ">>" pub struct Shr/2 ">>=" pub struct ShrEq/3 "/" pub struct Slash/1 "/=" pub struct SlashEq/2 "*" pub struct Star/1 "*=" pub struct StarEq/2 "~" pub struct Tilde/1 }
806
807#[doc = r" `(`…`)`"]
pub struct Paren {
pub span: DelimSpan,
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn Paren<S: IntoSpans<DelimSpan>>(span: S) -> Paren {
Paren { span: span.into_spans() }
}
impl core::default::Default for Paren {
fn default() -> Self { Paren(Span::call_site()) }
}
#[doc(cfg(feature = "clone-impls"))]
impl Copy for Paren { }
#[doc(cfg(feature = "clone-impls"))]
impl Clone for Paren {
fn clone(&self) -> Self { *self }
}
#[doc(cfg(feature = "extra-traits"))]
impl Debug for Paren {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Paren")
}
}
#[doc(cfg(feature = "extra-traits"))]
impl cmp::Eq for Paren { }
#[doc(cfg(feature = "extra-traits"))]
impl PartialEq for Paren {
fn eq(&self, _other: &Paren) -> bool { true }
}
#[doc(cfg(feature = "extra-traits"))]
impl Hash for Paren {
fn hash<H: Hasher>(&self, _state: &mut H) {}
}
impl Paren {
#[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::Parenthesis, self.span.join(), tokens,
inner);
}
}
impl private::Sealed for Paren { }define_delimiters! {
808 Brace pub struct Brace Bracket pub struct Bracket Parenthesis pub struct Paren }
812
813#[macro_export]
881macro_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#[doc(hidden)]
986#[cfg(feature = "parsing")]
987pub(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<Span> {
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(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}`", token))
})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<const N: usize>(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 match (&token.len(), &spans.len()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};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], ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}`", token))
})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#[doc(hidden)]
1069#[cfg(feature = "printing")]
1070pub(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 match (&s.len(), &spans.len()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};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}