@@ -2,7 +2,6 @@ use std::borrow::Cow;
2
2
use std:: fmt;
3
3
use std:: sync:: Arc ;
4
4
5
- pub use BinOpToken :: * ;
6
5
pub use LitKind :: * ;
7
6
pub use Nonterminal :: * ;
8
7
pub use NtExprKind :: * ;
@@ -26,21 +25,6 @@ pub enum CommentKind {
26
25
Block ,
27
26
}
28
27
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
-
44
28
// This type must not implement `Hash` due to the unusual `PartialEq` impl below.
45
29
#[ derive( Copy , Clone , Debug , Encodable , Decodable , HashStable_Generic ) ]
46
30
pub enum InvisibleOrigin {
@@ -379,8 +363,46 @@ pub enum TokenKind {
379
363
Not ,
380
364
/// `~`
381
365
Tilde ,
382
- BinOp ( BinOpToken ) ,
383
- BinOpEq ( BinOpToken ) ,
366
+ // `+`
367
+ Plus ,
368
+ // `-`
369
+ Minus ,
370
+ // `*`
371
+ Star ,
372
+ // `/`
373
+ Slash ,
374
+ // `%`
375
+ Percent ,
376
+ // `^`
377
+ Caret ,
378
+ // `&`
379
+ And ,
380
+ // `|`
381
+ Or ,
382
+ // `<<`
383
+ Shl ,
384
+ // `>>`
385
+ Shr ,
386
+ // `+=`
387
+ PlusEq ,
388
+ // `-=`
389
+ MinusEq ,
390
+ // `*=`
391
+ StarEq ,
392
+ // `/=`
393
+ SlashEq ,
394
+ // `%=`
395
+ PercentEq ,
396
+ // `^=`
397
+ CaretEq ,
398
+ // `&=`
399
+ AndEq ,
400
+ // `|=`
401
+ OrEq ,
402
+ // `<<=`
403
+ ShlEq ,
404
+ // `>>=`
405
+ ShrEq ,
384
406
385
407
/* Structural symbols */
386
408
/// `@`
@@ -502,29 +524,29 @@ impl TokenKind {
502
524
( EqEq , 1 ) => ( Eq , Eq ) ,
503
525
( Ne , 1 ) => ( Not , Eq ) ,
504
526
( Ge , 1 ) => ( Gt , Eq ) ,
505
- ( AndAnd , 1 ) => ( BinOp ( And ) , BinOp ( And ) ) ,
506
- ( OrOr , 1 ) => ( BinOp ( Or ) , BinOp ( Or ) ) ,
507
- ( BinOp ( Shl ) , 1 ) => ( Lt , Lt ) ,
508
- ( BinOp ( Shr ) , 1 ) => ( Gt , Gt ) ,
509
- ( BinOpEq ( Plus ) , 1 ) => ( BinOp ( Plus ) , Eq ) ,
510
- ( BinOpEq ( Minus ) , 1 ) => ( BinOp ( Minus ) , Eq ) ,
511
- ( BinOpEq ( Star ) , 1 ) => ( BinOp ( Star ) , Eq ) ,
512
- ( BinOpEq ( Slash ) , 1 ) => ( BinOp ( Slash ) , Eq ) ,
513
- ( BinOpEq ( Percent ) , 1 ) => ( BinOp ( Percent ) , Eq ) ,
514
- ( BinOpEq ( Caret ) , 1 ) => ( BinOp ( Caret ) , Eq ) ,
515
- ( BinOpEq ( And ) , 1 ) => ( BinOp ( And ) , Eq ) ,
516
- ( BinOpEq ( Or ) , 1 ) => ( BinOp ( Or ) , Eq ) ,
517
- ( BinOpEq ( Shl ) , 1 ) => ( Lt , Le ) , // `<` + `<=`
518
- ( BinOpEq ( Shl ) , 2 ) => ( BinOp ( Shl ) , Eq ) , // `<<` + `=`
519
- ( BinOpEq ( Shr ) , 1 ) => ( Gt , Ge ) , // `>` + `>=`
520
- ( BinOpEq ( Shr ) , 2 ) => ( BinOp ( Shr ) , Eq ) , // `>>` + `=`
527
+ ( AndAnd , 1 ) => ( And , And ) ,
528
+ ( OrOr , 1 ) => ( Or , Or ) ,
529
+ ( Shl , 1 ) => ( Lt , Lt ) ,
530
+ ( Shr , 1 ) => ( Gt , Gt ) ,
531
+ ( PlusEq , 1 ) => ( Plus , Eq ) ,
532
+ ( MinusEq , 1 ) => ( Minus , Eq ) ,
533
+ ( StarEq , 1 ) => ( Star , Eq ) ,
534
+ ( SlashEq , 1 ) => ( Slash , Eq ) ,
535
+ ( PercentEq , 1 ) => ( Percent , Eq ) ,
536
+ ( CaretEq , 1 ) => ( Caret , Eq ) ,
537
+ ( AndEq , 1 ) => ( And , Eq ) ,
538
+ ( OrEq , 1 ) => ( Or , Eq ) ,
539
+ ( ShlEq , 1 ) => ( Lt , Le ) , // `<` + `<=`
540
+ ( ShlEq , 2 ) => ( Shl , Eq ) , // `<<` + `=`
541
+ ( ShrEq , 1 ) => ( Gt , Ge ) , // `>` + `>=`
542
+ ( ShrEq , 2 ) => ( Shr , Eq ) , // `>>` + `=`
521
543
( DotDot , 1 ) => ( Dot , Dot ) ,
522
544
( DotDotDot , 1 ) => ( Dot , DotDot ) , // `.` + `..`
523
545
( DotDotDot , 2 ) => ( DotDot , Dot ) , // `..` + `.`
524
546
( DotDotEq , 2 ) => ( DotDot , Eq ) ,
525
547
( PathSep , 1 ) => ( Colon , Colon ) ,
526
- ( RArrow , 1 ) => ( BinOp ( Minus ) , Gt ) ,
527
- ( LArrow , 1 ) => ( Lt , BinOp ( Minus ) ) ,
548
+ ( RArrow , 1 ) => ( Minus , Gt ) ,
549
+ ( LArrow , 1 ) => ( Lt , Minus ) ,
528
550
( FatArrow , 1 ) => ( Eq , Gt ) ,
529
551
_ => return None ,
530
552
} )
@@ -543,7 +565,7 @@ impl TokenKind {
543
565
}
544
566
545
567
pub fn should_end_const_arg ( & self ) -> bool {
546
- matches ! ( self , Gt | Ge | BinOp ( Shr ) | BinOpEq ( Shr ) )
568
+ matches ! ( self , Gt | Ge | Shr | ShrEq )
547
569
}
548
570
}
549
571
@@ -582,19 +604,19 @@ impl Token {
582
604
583
605
pub fn is_punct ( & self ) -> bool {
584
606
match self . kind {
585
- Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp ( _ )
586
- | BinOpEq ( _ ) | At | Dot | DotDot | DotDotDot | DotDotEq | Comma | Semi | Colon
587
- | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question | SingleQuote => {
588
- true
589
- }
607
+ Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | Plus | Minus
608
+ | Star | Slash | Percent | Caret | And | Or | Shl | Shr | PlusEq | MinusEq | StarEq
609
+ | SlashEq | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | Dot | DotDot
610
+ | DotDotDot | DotDotEq | Comma | Semi | Colon | PathSep | RArrow | LArrow
611
+ | FatArrow | Pound | Dollar | Question | SingleQuote => true ,
590
612
591
613
OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
592
614
| NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | Eof => false ,
593
615
}
594
616
}
595
617
596
618
pub fn is_like_plus ( & self ) -> bool {
597
- matches ! ( self . kind, BinOp ( Plus ) | BinOpEq ( Plus ) )
619
+ matches ! ( self . kind, Plus | PlusEq )
598
620
}
599
621
600
622
/// Returns `true` if the token can appear at the start of an expression.
@@ -609,14 +631,14 @@ impl Token {
609
631
OpenDelim ( Parenthesis | Brace | Bracket ) | // tuple, array or block
610
632
Literal ( ..) | // literal
611
633
Not | // operator not
612
- BinOp ( Minus ) | // unary minus
613
- BinOp ( Star ) | // dereference
614
- BinOp ( Or ) | OrOr | // closure
615
- BinOp ( And ) | // reference
634
+ Minus | // unary minus
635
+ Star | // dereference
636
+ Or | OrOr | // closure
637
+ And | // reference
616
638
AndAnd | // double reference
617
639
// DotDotDot is no longer supported, but we need some way to display the error
618
640
DotDot | DotDotDot | DotDotEq | // range notation
619
- Lt | BinOp ( Shl ) | // associated path
641
+ Lt | Shl | // associated path
620
642
PathSep | // global path
621
643
Lifetime ( ..) | // labeled loop
622
644
Pound => true , // expression attributes
@@ -645,17 +667,16 @@ impl Token {
645
667
Ident ( ..) | NtIdent ( ..) |
646
668
OpenDelim ( Delimiter :: Parenthesis ) | // tuple pattern
647
669
OpenDelim ( Delimiter :: Bracket ) | // slice pattern
648
- BinOp ( And ) | // reference
649
- BinOp ( Minus ) | // negative literal
650
- AndAnd | // double reference
651
- Literal ( _) | // literal
652
- DotDot | // range pattern (future compat)
653
- DotDotDot | // range pattern (future compat)
654
- PathSep | // path
655
- Lt | // path (UFCS constant)
656
- BinOp ( Shl ) => true , // path (double UFCS)
657
- // leading vert `|` or-pattern
658
- BinOp ( Or ) => matches ! ( pat_kind, PatWithOr ) ,
670
+ And | // reference
671
+ Minus | // negative literal
672
+ AndAnd | // double reference
673
+ Literal ( _) | // literal
674
+ DotDot | // range pattern (future compat)
675
+ DotDotDot | // range pattern (future compat)
676
+ PathSep | // path
677
+ Lt | // path (UFCS constant)
678
+ Shl => true , // path (double UFCS)
679
+ Or => matches ! ( pat_kind, PatWithOr ) , // leading vert `|` or-pattern
659
680
Interpolated ( nt) =>
660
681
matches ! ( & * * nt,
661
682
| NtExpr ( ..)
@@ -676,18 +697,18 @@ impl Token {
676
697
/// Returns `true` if the token can appear at the start of a type.
677
698
pub fn can_begin_type ( & self ) -> bool {
678
699
match self . uninterpolate ( ) . kind {
679
- Ident ( name, is_raw) =>
700
+ Ident ( name, is_raw) =>
680
701
ident_can_begin_type ( name, self . span , is_raw) , // type name or keyword
681
702
OpenDelim ( Delimiter :: Parenthesis ) | // tuple
682
703
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
691
712
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
692
713
MetaVarKind :: Ty { .. } |
693
714
MetaVarKind :: Path
@@ -701,7 +722,7 @@ impl Token {
701
722
/// Returns `true` if the token can appear at the start of a const param.
702
723
pub fn can_begin_const_arg ( & self ) -> bool {
703
724
match self . kind {
704
- OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
725
+ OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | Minus => true ,
705
726
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
706
727
Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
707
728
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
@@ -750,7 +771,7 @@ impl Token {
750
771
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
751
772
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
752
773
match self . uninterpolate ( ) . kind {
753
- Literal ( ..) | BinOp ( Minus ) => true ,
774
+ Literal ( ..) | Minus => true ,
754
775
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
755
776
Interpolated ( ref nt) => match & * * nt {
756
777
NtLiteral ( _) => true ,
@@ -875,7 +896,7 @@ impl Token {
875
896
}
876
897
877
898
pub fn is_qpath_start ( & self ) -> bool {
878
- self == & Lt || self == & BinOp ( Shl )
899
+ self == & Lt || self == & Shl
879
900
}
880
901
881
902
pub fn is_path_start ( & self ) -> bool {
@@ -967,59 +988,82 @@ impl Token {
967
988
}
968
989
969
990
pub fn glue ( & self , joint : & Token ) -> Option < Token > {
970
- let kind = match self . kind {
971
- Eq => match joint. kind {
972
- Eq => EqEq ,
973
- Gt => FatArrow ,
974
- _ => return None ,
975
- } ,
976
- Lt => match joint. kind {
977
- Eq => Le ,
978
- Lt => BinOp ( Shl ) ,
979
- Le => BinOpEq ( Shl ) ,
980
- BinOp ( Minus ) => LArrow ,
981
- _ => return None ,
982
- } ,
983
- Gt => match joint. kind {
984
- Eq => Ge ,
985
- Gt => BinOp ( Shr ) ,
986
- Ge => BinOpEq ( Shr ) ,
987
- _ => return None ,
988
- } ,
989
- Not => match joint. kind {
990
- Eq => Ne ,
991
- _ => return None ,
992
- } ,
993
- BinOp ( op) => match joint. kind {
994
- Eq => BinOpEq ( op) ,
995
- BinOp ( And ) if op == And => AndAnd ,
996
- BinOp ( Or ) if op == Or => OrOr ,
997
- Gt if op == Minus => RArrow ,
998
- _ => return None ,
999
- } ,
1000
- Dot => match joint. kind {
1001
- Dot => DotDot ,
1002
- DotDot => DotDotDot ,
1003
- _ => return None ,
1004
- } ,
1005
- DotDot => match joint. kind {
1006
- Dot => DotDotDot ,
1007
- Eq => DotDotEq ,
1008
- _ => return None ,
1009
- } ,
1010
- Colon => match joint. kind {
1011
- Colon => PathSep ,
1012
- _ => return None ,
1013
- } ,
1014
- SingleQuote => match joint. kind {
1015
- Ident ( name, is_raw) => Lifetime ( Symbol :: intern ( & format ! ( "'{name}" ) ) , is_raw) ,
1016
- _ => return None ,
1017
- } ,
991
+ let kind = match ( & self . kind , & joint. kind ) {
992
+ ( Eq , Eq ) => EqEq ,
993
+ ( Eq , Gt ) => FatArrow ,
994
+ ( Eq , _) => return None ,
995
+
996
+ ( Lt , Eq ) => Le ,
997
+ ( Lt , Lt ) => Shl ,
998
+ ( Lt , Le ) => ShlEq ,
999
+ ( Lt , Minus ) => LArrow ,
1000
+ ( Lt , _) => return None ,
1001
+
1002
+ ( Gt , Eq ) => Ge ,
1003
+ ( Gt , Gt ) => Shr ,
1004
+ ( Gt , Ge ) => ShrEq ,
1005
+ ( Gt , _) => return None ,
1006
+
1007
+ ( Not , Eq ) => Ne ,
1008
+ ( Not , _) => return None ,
1009
+
1010
+ ( Plus , Eq ) => PlusEq ,
1011
+ ( Plus , _) => return None ,
1012
+
1013
+ ( Minus , Eq ) => MinusEq ,
1014
+ ( Minus , Gt ) => RArrow ,
1015
+ ( Minus , _) => return None ,
1018
1016
1019
- Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
1020
- | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1021
- | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1022
- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => {
1017
+ ( Star , Eq ) => StarEq ,
1018
+ ( Star , _) => return None ,
1019
+
1020
+ ( Slash , Eq ) => SlashEq ,
1021
+ ( Slash , _) => return None ,
1022
+
1023
+ ( Percent , Eq ) => PercentEq ,
1024
+ ( Percent , _) => return None ,
1025
+
1026
+ ( Caret , Eq ) => CaretEq ,
1027
+ ( Caret , _) => return None ,
1028
+
1029
+ ( And , Eq ) => AndEq ,
1030
+ ( And , And ) => AndAnd ,
1031
+ ( And , _) => return None ,
1032
+
1033
+ ( Or , Eq ) => OrEq ,
1034
+ ( Or , Or ) => OrOr ,
1035
+ ( Or , _) => return None ,
1036
+
1037
+ ( Shl , Eq ) => ShlEq ,
1038
+ ( Shl , _) => return None ,
1039
+
1040
+ ( Shr , Eq ) => ShrEq ,
1041
+ ( Shr , _) => return None ,
1042
+
1043
+ ( Dot , Dot ) => DotDot ,
1044
+ ( Dot , DotDot ) => DotDotDot ,
1045
+ ( Dot , _) => return None ,
1046
+
1047
+ ( DotDot , Dot ) => DotDotDot ,
1048
+ ( DotDot , Eq ) => DotDotEq ,
1049
+ ( DotDot , _) => return None ,
1050
+
1051
+ ( Colon , Colon ) => PathSep ,
1052
+ ( Colon , _) => return None ,
1053
+
1054
+ ( SingleQuote , Ident ( name, is_raw) ) => {
1055
+ Lifetime ( Symbol :: intern ( & format ! ( "'{name}" ) ) , * is_raw)
1056
+ }
1057
+ ( SingleQuote , _) => return None ,
1058
+
1059
+ (
1060
+ Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | PlusEq | MinusEq | StarEq | SlashEq
1061
+ | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq
1062
+ | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question
1063
+ | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1064
+ | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof ,
1065
+ _,
1066
+ ) => {
1023
1067
return None ;
1024
1068
}
1025
1069
} ;
0 commit comments