11use std:: borrow:: Cow ;
22use std:: fmt;
33
4- pub use BinOpToken :: * ;
54pub use LitKind :: * ;
65pub use Nonterminal :: * ;
76pub use NtExprKind :: * ;
@@ -26,21 +25,6 @@ pub enum CommentKind {
2625 Block ,
2726}
2827
29- #[ derive( Clone , PartialEq , Encodable , Decodable , Hash , Debug , Copy ) ]
30- #[ derive( HashStable_Generic ) ]
31- pub enum BinOpToken {
32- Plus ,
33- Minus ,
34- Star ,
35- Slash ,
36- Percent ,
37- Caret ,
38- And ,
39- Or ,
40- Shl ,
41- Shr ,
42- }
43-
4428// This type must not implement `Hash` due to the unusual `PartialEq` impl below.
4529#[ derive( Copy , Clone , Debug , Encodable , Decodable , HashStable_Generic ) ]
4630pub enum InvisibleOrigin {
@@ -374,8 +358,46 @@ pub enum TokenKind {
374358 Not ,
375359 /// `~`
376360 Tilde ,
377- BinOp ( BinOpToken ) ,
378- BinOpEq ( BinOpToken ) ,
361+ // `+`
362+ Plus ,
363+ // `-`
364+ Minus ,
365+ // `*`
366+ Star ,
367+ // `/`
368+ Slash ,
369+ // `%`
370+ Percent ,
371+ // `^`
372+ Caret ,
373+ // `&`
374+ And ,
375+ // `|`
376+ Or ,
377+ // `<<`
378+ Shl ,
379+ // `>>`
380+ Shr ,
381+ // `+=`
382+ PlusEq ,
383+ // `-=`
384+ MinusEq ,
385+ // `*=`
386+ StarEq ,
387+ // `/=`
388+ SlashEq ,
389+ // `%=`
390+ PercentEq ,
391+ // `^=`
392+ CaretEq ,
393+ // `&=`
394+ AndEq ,
395+ // `|=`
396+ OrEq ,
397+ // `<<=`
398+ ShlEq ,
399+ // `>>=`
400+ ShrEq ,
379401
380402 /* Structural symbols */
381403 /// `@`
@@ -497,29 +519,29 @@ impl TokenKind {
497519 ( EqEq , 1 ) => ( Eq , Eq ) ,
498520 ( Ne , 1 ) => ( Not , Eq ) ,
499521 ( Ge , 1 ) => ( Gt , Eq ) ,
500- ( AndAnd , 1 ) => ( BinOp ( And ) , BinOp ( And ) ) ,
501- ( OrOr , 1 ) => ( BinOp ( Or ) , BinOp ( Or ) ) ,
502- ( BinOp ( Shl ) , 1 ) => ( Lt , Lt ) ,
503- ( BinOp ( Shr ) , 1 ) => ( Gt , Gt ) ,
504- ( BinOpEq ( Plus ) , 1 ) => ( BinOp ( Plus ) , Eq ) ,
505- ( BinOpEq ( Minus ) , 1 ) => ( BinOp ( Minus ) , Eq ) ,
506- ( BinOpEq ( Star ) , 1 ) => ( BinOp ( Star ) , Eq ) ,
507- ( BinOpEq ( Slash ) , 1 ) => ( BinOp ( Slash ) , Eq ) ,
508- ( BinOpEq ( Percent ) , 1 ) => ( BinOp ( Percent ) , Eq ) ,
509- ( BinOpEq ( Caret ) , 1 ) => ( BinOp ( Caret ) , Eq ) ,
510- ( BinOpEq ( And ) , 1 ) => ( BinOp ( And ) , Eq ) ,
511- ( BinOpEq ( Or ) , 1 ) => ( BinOp ( Or ) , Eq ) ,
512- ( BinOpEq ( Shl ) , 1 ) => ( Lt , Le ) , // `<` + `<=`
513- ( BinOpEq ( Shl ) , 2 ) => ( BinOp ( Shl ) , Eq ) , // `<<` + `=`
514- ( BinOpEq ( Shr ) , 1 ) => ( Gt , Ge ) , // `>` + `>=`
515- ( BinOpEq ( Shr ) , 2 ) => ( BinOp ( Shr ) , Eq ) , // `>>` + `=`
522+ ( AndAnd , 1 ) => ( And , And ) ,
523+ ( OrOr , 1 ) => ( Or , Or ) ,
524+ ( Shl , 1 ) => ( Lt , Lt ) ,
525+ ( Shr , 1 ) => ( Gt , Gt ) ,
526+ ( PlusEq , 1 ) => ( Plus , Eq ) ,
527+ ( MinusEq , 1 ) => ( Minus , Eq ) ,
528+ ( StarEq , 1 ) => ( Star , Eq ) ,
529+ ( SlashEq , 1 ) => ( Slash , Eq ) ,
530+ ( PercentEq , 1 ) => ( Percent , Eq ) ,
531+ ( CaretEq , 1 ) => ( Caret , Eq ) ,
532+ ( AndEq , 1 ) => ( And , Eq ) ,
533+ ( OrEq , 1 ) => ( Or , Eq ) ,
534+ ( ShlEq , 1 ) => ( Lt , Le ) , // `<` + `<=`
535+ ( ShlEq , 2 ) => ( Shl , Eq ) , // `<<` + `=`
536+ ( ShrEq , 1 ) => ( Gt , Ge ) , // `>` + `>=`
537+ ( ShrEq , 2 ) => ( Shr , Eq ) , // `>>` + `=`
516538 ( DotDot , 1 ) => ( Dot , Dot ) ,
517539 ( DotDotDot , 1 ) => ( Dot , DotDot ) , // `.` + `..`
518540 ( DotDotDot , 2 ) => ( DotDot , Dot ) , // `..` + `.`
519541 ( DotDotEq , 2 ) => ( DotDot , Eq ) ,
520542 ( PathSep , 1 ) => ( Colon , Colon ) ,
521- ( RArrow , 1 ) => ( BinOp ( Minus ) , Gt ) ,
522- ( LArrow , 1 ) => ( Lt , BinOp ( Minus ) ) ,
543+ ( RArrow , 1 ) => ( Minus , Gt ) ,
544+ ( LArrow , 1 ) => ( Lt , Minus ) ,
523545 ( FatArrow , 1 ) => ( Eq , Gt ) ,
524546 _ => return None ,
525547 } )
@@ -538,7 +560,7 @@ impl TokenKind {
538560 }
539561
540562 pub fn should_end_const_arg ( & self ) -> bool {
541- matches ! ( self , Gt | Ge | BinOp ( Shr ) | BinOpEq ( Shr ) )
563+ matches ! ( self , Gt | Ge | Shr | ShrEq )
542564 }
543565}
544566
@@ -577,19 +599,19 @@ impl Token {
577599
578600 pub fn is_punct ( & self ) -> bool {
579601 match self . kind {
580- Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp ( _ )
581- | BinOpEq ( _ ) | At | Dot | DotDot | DotDotDot | DotDotEq | Comma | Semi | Colon
582- | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question | SingleQuote => {
583- true
584- }
602+ Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | Plus | Minus
603+ | Star | Slash | Percent | Caret | And | Or | Shl | Shr | PlusEq | MinusEq | StarEq
604+ | SlashEq | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | Dot | DotDot
605+ | DotDotDot | DotDotEq | Comma | Semi | Colon | PathSep | RArrow | LArrow
606+ | FatArrow | Pound | Dollar | Question | SingleQuote => true ,
585607
586608 OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
587609 | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | Eof => false ,
588610 }
589611 }
590612
591613 pub fn is_like_plus ( & self ) -> bool {
592- matches ! ( self . kind, BinOp ( Plus ) | BinOpEq ( Plus ) )
614+ matches ! ( self . kind, Plus | PlusEq )
593615 }
594616
595617 /// Returns `true` if the token can appear at the start of an expression.
@@ -604,14 +626,14 @@ impl Token {
604626 OpenDelim ( Parenthesis | Brace | Bracket ) | // tuple, array or block
605627 Literal ( ..) | // literal
606628 Not | // operator not
607- BinOp ( Minus ) | // unary minus
608- BinOp ( Star ) | // dereference
609- BinOp ( Or ) | OrOr | // closure
610- BinOp ( And ) | // reference
629+ Minus | // unary minus
630+ Star | // dereference
631+ Or | OrOr | // closure
632+ And | // reference
611633 AndAnd | // double reference
612634 // DotDotDot is no longer supported, but we need some way to display the error
613635 DotDot | DotDotDot | DotDotEq | // range notation
614- Lt | BinOp ( Shl ) | // associated path
636+ Lt | Shl | // associated path
615637 PathSep | // global path
616638 Lifetime ( ..) | // labeled loop
617639 Pound => true , // expression attributes
@@ -641,17 +663,16 @@ impl Token {
641663 Ident ( ..) | NtIdent ( ..) |
642664 OpenDelim ( Delimiter :: Parenthesis ) | // tuple pattern
643665 OpenDelim ( Delimiter :: Bracket ) | // slice pattern
644- BinOp ( And ) | // reference
645- BinOp ( Minus ) | // negative literal
646- AndAnd | // double reference
647- Literal ( _) | // literal
648- DotDot | // range pattern (future compat)
649- DotDotDot | // range pattern (future compat)
650- PathSep | // path
651- Lt | // path (UFCS constant)
652- BinOp ( Shl ) => true , // path (double UFCS)
653- // leading vert `|` or-pattern
654- BinOp ( Or ) => matches ! ( pat_kind, PatWithOr ) ,
666+ And | // reference
667+ Minus | // negative literal
668+ AndAnd | // double reference
669+ Literal ( _) | // literal
670+ DotDot | // range pattern (future compat)
671+ DotDotDot | // range pattern (future compat)
672+ PathSep | // path
673+ Lt | // path (UFCS constant)
674+ Shl => true , // path (double UFCS)
675+ Or => matches ! ( pat_kind, PatWithOr ) , // leading vert `|` or-pattern
655676 Interpolated ( nt) =>
656677 matches ! ( & * * nt,
657678 | NtExpr ( ..)
@@ -680,14 +701,14 @@ impl Token {
680701 ident_can_begin_type ( name, self . span , is_raw) , // type name or keyword
681702 OpenDelim ( Delimiter :: Parenthesis ) | // tuple
682703 OpenDelim ( Delimiter :: Bracket ) | // array
683- Not | // never
684- BinOp ( Star ) | // raw pointer
685- BinOp ( And ) | // reference
686- AndAnd | // double reference
687- Question | // maybe bound in trait object
688- Lifetime ( ..) | // lifetime bound in trait object
689- Lt | BinOp ( Shl ) | // associated path
690- PathSep => true , // global path
704+ Not | // never
705+ Star | // raw pointer
706+ And | // reference
707+ AndAnd | // double reference
708+ Question | // maybe bound in trait object
709+ Lifetime ( ..) | // lifetime bound in trait object
710+ Lt | Shl | // associated path
711+ PathSep => true , // global path
691712 Interpolated ( ref nt) => matches ! ( & * * nt, NtTy ( ..) | NtPath ( ..) ) ,
692713 OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
693714 MetaVarKind :: Ty |
@@ -702,7 +723,7 @@ impl Token {
702723 /// Returns `true` if the token can appear at the start of a const param.
703724 pub fn can_begin_const_arg ( & self ) -> bool {
704725 match self . kind {
705- OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
726+ OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | Minus => true ,
706727 Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
707728 Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
708729 OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
@@ -751,7 +772,7 @@ impl Token {
751772 /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
752773 pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
753774 match self . uninterpolate ( ) . kind {
754- Literal ( ..) | BinOp ( Minus ) => true ,
775+ Literal ( ..) | Minus => true ,
755776 Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
756777 Interpolated ( ref nt) => match & * * nt {
757778 NtLiteral ( _) => true ,
@@ -887,7 +908,7 @@ impl Token {
887908 }
888909
889910 pub fn is_qpath_start ( & self ) -> bool {
890- self == & Lt || self == & BinOp ( Shl )
911+ self == & Lt || self == & Shl
891912 }
892913
893914 pub fn is_path_start ( & self ) -> bool {
@@ -969,59 +990,82 @@ impl Token {
969990 }
970991
971992 pub fn glue ( & self , joint : & Token ) -> Option < Token > {
972- let kind = match self . kind {
973- Eq => match joint. kind {
974- Eq => EqEq ,
975- Gt => FatArrow ,
976- _ => return None ,
977- } ,
978- Lt => match joint. kind {
979- Eq => Le ,
980- Lt => BinOp ( Shl ) ,
981- Le => BinOpEq ( Shl ) ,
982- BinOp ( Minus ) => LArrow ,
983- _ => return None ,
984- } ,
985- Gt => match joint. kind {
986- Eq => Ge ,
987- Gt => BinOp ( Shr ) ,
988- Ge => BinOpEq ( Shr ) ,
989- _ => return None ,
990- } ,
991- Not => match joint. kind {
992- Eq => Ne ,
993- _ => return None ,
994- } ,
995- BinOp ( op) => match joint. kind {
996- Eq => BinOpEq ( op) ,
997- BinOp ( And ) if op == And => AndAnd ,
998- BinOp ( Or ) if op == Or => OrOr ,
999- Gt if op == Minus => RArrow ,
1000- _ => return None ,
1001- } ,
1002- Dot => match joint. kind {
1003- Dot => DotDot ,
1004- DotDot => DotDotDot ,
1005- _ => return None ,
1006- } ,
1007- DotDot => match joint. kind {
1008- Dot => DotDotDot ,
1009- Eq => DotDotEq ,
1010- _ => return None ,
1011- } ,
1012- Colon => match joint. kind {
1013- Colon => PathSep ,
1014- _ => return None ,
1015- } ,
1016- SingleQuote => match joint. kind {
1017- Ident ( name, is_raw) => Lifetime ( Symbol :: intern ( & format ! ( "'{name}" ) ) , is_raw) ,
1018- _ => return None ,
1019- } ,
993+ let kind = match ( & self . kind , & joint. kind ) {
994+ ( Eq , Eq ) => EqEq ,
995+ ( Eq , Gt ) => FatArrow ,
996+ ( Eq , _) => return None ,
997+
998+ ( Lt , Eq ) => Le ,
999+ ( Lt , Lt ) => Shl ,
1000+ ( Lt , Le ) => ShlEq ,
1001+ ( Lt , Minus ) => LArrow ,
1002+ ( Lt , _) => return None ,
1003+
1004+ ( Gt , Eq ) => Ge ,
1005+ ( Gt , Gt ) => Shr ,
1006+ ( Gt , Ge ) => ShrEq ,
1007+ ( Gt , _) => return None ,
1008+
1009+ ( Not , Eq ) => Ne ,
1010+ ( Not , _) => return None ,
1011+
1012+ ( Plus , Eq ) => PlusEq ,
1013+ ( Plus , _) => return None ,
1014+
1015+ ( Minus , Eq ) => MinusEq ,
1016+ ( Minus , Gt ) => RArrow ,
1017+ ( Minus , _) => return None ,
10201018
1021- Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
1022- | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1023- | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1024- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => {
1019+ ( Star , Eq ) => StarEq ,
1020+ ( Star , _) => return None ,
1021+
1022+ ( Slash , Eq ) => SlashEq ,
1023+ ( Slash , _) => return None ,
1024+
1025+ ( Percent , Eq ) => PercentEq ,
1026+ ( Percent , _) => return None ,
1027+
1028+ ( Caret , Eq ) => CaretEq ,
1029+ ( Caret , _) => return None ,
1030+
1031+ ( And , Eq ) => AndEq ,
1032+ ( And , And ) => AndAnd ,
1033+ ( And , _) => return None ,
1034+
1035+ ( Or , Eq ) => OrEq ,
1036+ ( Or , Or ) => OrOr ,
1037+ ( Or , _) => return None ,
1038+
1039+ ( Shl , Eq ) => ShlEq ,
1040+ ( Shl , _) => return None ,
1041+
1042+ ( Shr , Eq ) => ShrEq ,
1043+ ( Shr , _) => return None ,
1044+
1045+ ( Dot , Dot ) => DotDot ,
1046+ ( Dot , DotDot ) => DotDotDot ,
1047+ ( Dot , _) => return None ,
1048+
1049+ ( DotDot , Dot ) => DotDotDot ,
1050+ ( DotDot , Eq ) => DotDotEq ,
1051+ ( DotDot , _) => return None ,
1052+
1053+ ( Colon , Colon ) => PathSep ,
1054+ ( Colon , _) => return None ,
1055+
1056+ ( SingleQuote , Ident ( name, is_raw) ) => {
1057+ Lifetime ( Symbol :: intern ( & format ! ( "'{name}" ) ) , * is_raw)
1058+ }
1059+ ( SingleQuote , _) => return None ,
1060+
1061+ (
1062+ Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | PlusEq | MinusEq | StarEq | SlashEq
1063+ | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq
1064+ | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question
1065+ | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1066+ | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof ,
1067+ _,
1068+ ) => {
10251069 return None ;
10261070 }
10271071 } ;
0 commit comments