1
1
use std:: borrow:: Cow ;
2
2
use std:: fmt;
3
3
4
- pub use BinOpToken :: * ;
5
4
pub use LitKind :: * ;
6
5
pub use Nonterminal :: * ;
7
6
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 {
@@ -374,8 +358,46 @@ pub enum TokenKind {
374
358
Not ,
375
359
/// `~`
376
360
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 ,
379
401
380
402
/* Structural symbols */
381
403
/// `@`
@@ -497,29 +519,29 @@ impl TokenKind {
497
519
( EqEq , 1 ) => ( Eq , Eq ) ,
498
520
( Ne , 1 ) => ( Not , Eq ) ,
499
521
( 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 ) , // `>>` + `=`
516
538
( DotDot , 1 ) => ( Dot , Dot ) ,
517
539
( DotDotDot , 1 ) => ( Dot , DotDot ) , // `.` + `..`
518
540
( DotDotDot , 2 ) => ( DotDot , Dot ) , // `..` + `.`
519
541
( DotDotEq , 2 ) => ( DotDot , Eq ) ,
520
542
( 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 ) ,
523
545
( FatArrow , 1 ) => ( Eq , Gt ) ,
524
546
_ => return None ,
525
547
} )
@@ -538,7 +560,7 @@ impl TokenKind {
538
560
}
539
561
540
562
pub fn should_end_const_arg ( & self ) -> bool {
541
- matches ! ( self , Gt | Ge | BinOp ( Shr ) | BinOpEq ( Shr ) )
563
+ matches ! ( self , Gt | Ge | Shr | ShrEq )
542
564
}
543
565
}
544
566
@@ -577,19 +599,19 @@ impl Token {
577
599
578
600
pub fn is_punct ( & self ) -> bool {
579
601
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 ,
585
607
586
608
OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
587
609
| NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | Eof => false ,
588
610
}
589
611
}
590
612
591
613
pub fn is_like_plus ( & self ) -> bool {
592
- matches ! ( self . kind, BinOp ( Plus ) | BinOpEq ( Plus ) )
614
+ matches ! ( self . kind, Plus | PlusEq )
593
615
}
594
616
595
617
/// Returns `true` if the token can appear at the start of an expression.
@@ -604,14 +626,14 @@ impl Token {
604
626
OpenDelim ( Parenthesis | Brace | Bracket ) | // tuple, array or block
605
627
Literal ( ..) | // literal
606
628
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
611
633
AndAnd | // double reference
612
634
// DotDotDot is no longer supported, but we need some way to display the error
613
635
DotDot | DotDotDot | DotDotEq | // range notation
614
- Lt | BinOp ( Shl ) | // associated path
636
+ Lt | Shl | // associated path
615
637
PathSep | // global path
616
638
Lifetime ( ..) | // labeled loop
617
639
Pound => true , // expression attributes
@@ -641,17 +663,16 @@ impl Token {
641
663
Ident ( ..) | NtIdent ( ..) |
642
664
OpenDelim ( Delimiter :: Parenthesis ) | // tuple pattern
643
665
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
655
676
Interpolated ( nt) =>
656
677
matches ! ( & * * nt,
657
678
| NtExpr ( ..)
@@ -680,14 +701,14 @@ impl Token {
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
Interpolated ( ref nt) => matches ! ( & * * nt, NtTy ( ..) | NtPath ( ..) ) ,
692
713
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
693
714
MetaVarKind :: Ty |
@@ -702,7 +723,7 @@ impl Token {
702
723
/// Returns `true` if the token can appear at the start of a const param.
703
724
pub fn can_begin_const_arg ( & self ) -> bool {
704
725
match self . kind {
705
- OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
726
+ OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | Minus => true ,
706
727
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
707
728
Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
708
729
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
@@ -751,7 +772,7 @@ impl Token {
751
772
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
752
773
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
753
774
match self . uninterpolate ( ) . kind {
754
- Literal ( ..) | BinOp ( Minus ) => true ,
775
+ Literal ( ..) | Minus => true ,
755
776
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
756
777
Interpolated ( ref nt) => match & * * nt {
757
778
NtLiteral ( _) => true ,
@@ -887,7 +908,7 @@ impl Token {
887
908
}
888
909
889
910
pub fn is_qpath_start ( & self ) -> bool {
890
- self == & Lt || self == & BinOp ( Shl )
911
+ self == & Lt || self == & Shl
891
912
}
892
913
893
914
pub fn is_path_start ( & self ) -> bool {
@@ -970,59 +991,82 @@ impl Token {
970
991
}
971
992
972
993
pub fn glue ( & self , joint : & Token ) -> Option < Token > {
973
- let kind = match self . kind {
974
- Eq => match joint. kind {
975
- Eq => EqEq ,
976
- Gt => FatArrow ,
977
- _ => return None ,
978
- } ,
979
- Lt => match joint. kind {
980
- Eq => Le ,
981
- Lt => BinOp ( Shl ) ,
982
- Le => BinOpEq ( Shl ) ,
983
- BinOp ( Minus ) => LArrow ,
984
- _ => return None ,
985
- } ,
986
- Gt => match joint. kind {
987
- Eq => Ge ,
988
- Gt => BinOp ( Shr ) ,
989
- Ge => BinOpEq ( Shr ) ,
990
- _ => return None ,
991
- } ,
992
- Not => match joint. kind {
993
- Eq => Ne ,
994
- _ => return None ,
995
- } ,
996
- BinOp ( op) => match joint. kind {
997
- Eq => BinOpEq ( op) ,
998
- BinOp ( And ) if op == And => AndAnd ,
999
- BinOp ( Or ) if op == Or => OrOr ,
1000
- Gt if op == Minus => RArrow ,
1001
- _ => return None ,
1002
- } ,
1003
- Dot => match joint. kind {
1004
- Dot => DotDot ,
1005
- DotDot => DotDotDot ,
1006
- _ => return None ,
1007
- } ,
1008
- DotDot => match joint. kind {
1009
- Dot => DotDotDot ,
1010
- Eq => DotDotEq ,
1011
- _ => return None ,
1012
- } ,
1013
- Colon => match joint. kind {
1014
- Colon => PathSep ,
1015
- _ => return None ,
1016
- } ,
1017
- SingleQuote => match joint. kind {
1018
- Ident ( name, is_raw) => Lifetime ( Symbol :: intern ( & format ! ( "'{name}" ) ) , is_raw) ,
1019
- _ => return None ,
1020
- } ,
994
+ let kind = match ( & self . kind , & joint. kind ) {
995
+ ( Eq , Eq ) => EqEq ,
996
+ ( Eq , Gt ) => FatArrow ,
997
+ ( Eq , _) => return None ,
998
+
999
+ ( Lt , Eq ) => Le ,
1000
+ ( Lt , Lt ) => Shl ,
1001
+ ( Lt , Le ) => ShlEq ,
1002
+ ( Lt , Minus ) => LArrow ,
1003
+ ( Lt , _) => return None ,
1004
+
1005
+ ( Gt , Eq ) => Ge ,
1006
+ ( Gt , Gt ) => Shr ,
1007
+ ( Gt , Ge ) => ShrEq ,
1008
+ ( Gt , _) => return None ,
1009
+
1010
+ ( Not , Eq ) => Ne ,
1011
+ ( Not , _) => return None ,
1012
+
1013
+ ( Plus , Eq ) => PlusEq ,
1014
+ ( Plus , _) => return None ,
1015
+
1016
+ ( Minus , Eq ) => MinusEq ,
1017
+ ( Minus , Gt ) => RArrow ,
1018
+ ( Minus , _) => return None ,
1021
1019
1022
- Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
1023
- | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1024
- | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1025
- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => {
1020
+ ( Star , Eq ) => StarEq ,
1021
+ ( Star , _) => return None ,
1022
+
1023
+ ( Slash , Eq ) => SlashEq ,
1024
+ ( Slash , _) => return None ,
1025
+
1026
+ ( Percent , Eq ) => PercentEq ,
1027
+ ( Percent , _) => return None ,
1028
+
1029
+ ( Caret , Eq ) => CaretEq ,
1030
+ ( Caret , _) => return None ,
1031
+
1032
+ ( And , Eq ) => AndEq ,
1033
+ ( And , And ) => AndAnd ,
1034
+ ( And , _) => return None ,
1035
+
1036
+ ( Or , Eq ) => OrEq ,
1037
+ ( Or , Or ) => OrOr ,
1038
+ ( Or , _) => return None ,
1039
+
1040
+ ( Shl , Eq ) => ShlEq ,
1041
+ ( Shl , _) => return None ,
1042
+
1043
+ ( Shr , Eq ) => ShrEq ,
1044
+ ( Shr , _) => return None ,
1045
+
1046
+ ( Dot , Dot ) => DotDot ,
1047
+ ( Dot , DotDot ) => DotDotDot ,
1048
+ ( Dot , _) => return None ,
1049
+
1050
+ ( DotDot , Dot ) => DotDotDot ,
1051
+ ( DotDot , Eq ) => DotDotEq ,
1052
+ ( DotDot , _) => return None ,
1053
+
1054
+ ( Colon , Colon ) => PathSep ,
1055
+ ( Colon , _) => return None ,
1056
+
1057
+ ( SingleQuote , Ident ( name, is_raw) ) => {
1058
+ Lifetime ( Symbol :: intern ( & format ! ( "'{name}" ) ) , * is_raw)
1059
+ }
1060
+ ( SingleQuote , _) => return None ,
1061
+
1062
+ (
1063
+ Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | PlusEq | MinusEq | StarEq | SlashEq
1064
+ | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq
1065
+ | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question
1066
+ | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1067
+ | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof ,
1068
+ _,
1069
+ ) => {
1026
1070
return None ;
1027
1071
}
1028
1072
} ;
0 commit comments