@@ -547,40 +547,38 @@ impl<'a> Parser<'a> {
547
547
}
548
548
}
549
549
550
- crate fn check_ident ( & mut self ) -> bool {
551
- if self . token . is_ident ( ) {
550
+ fn check_or_expected ( & mut self , ok : bool , mk_type : impl FnOnce ( ) -> TokenType ) -> bool {
551
+ if ok {
552
552
true
553
553
} else {
554
- self . expected_tokens . push ( TokenType :: Ident ) ;
554
+ self . expected_tokens . push ( mk_type ( ) ) ;
555
555
false
556
556
}
557
557
}
558
558
559
+ crate fn check_ident ( & mut self ) -> bool {
560
+ self . check_or_expected ( self . token . is_ident ( ) , || TokenType :: Ident )
561
+ }
562
+
559
563
fn check_path ( & mut self ) -> bool {
560
- if self . token . is_path_start ( ) {
561
- true
562
- } else {
563
- self . expected_tokens . push ( TokenType :: Path ) ;
564
- false
565
- }
564
+ self . check_or_expected ( self . token . is_path_start ( ) , || TokenType :: Path )
566
565
}
567
566
568
567
fn check_type ( & mut self ) -> bool {
569
- if self . token . can_begin_type ( ) {
570
- true
571
- } else {
572
- self . expected_tokens . push ( TokenType :: Type ) ;
573
- false
574
- }
568
+ self . check_or_expected ( self . token . can_begin_type ( ) , || TokenType :: Type )
575
569
}
576
570
577
571
fn check_const_arg ( & mut self ) -> bool {
578
- if self . token . can_begin_const_arg ( ) {
579
- true
580
- } else {
581
- self . expected_tokens . push ( TokenType :: Const ) ;
582
- false
583
- }
572
+ self . check_or_expected ( self . token . can_begin_const_arg ( ) , || TokenType :: Const )
573
+ }
574
+
575
+ /// Checks to see if the next token is either `+` or `+=`.
576
+ /// Otherwise returns `false`.
577
+ fn check_plus ( & mut self ) -> bool {
578
+ self . check_or_expected (
579
+ self . token . is_like_plus ( ) ,
580
+ || TokenType :: Token ( token:: BinOp ( token:: Plus ) ) ,
581
+ )
584
582
}
585
583
586
584
/// Expects and consumes a `+`. if `+=` is seen, replaces it with a `=`
@@ -604,18 +602,6 @@ impl<'a> Parser<'a> {
604
602
}
605
603
}
606
604
607
- /// Checks to see if the next token is either `+` or `+=`.
608
- /// Otherwise returns `false`.
609
- fn check_plus ( & mut self ) -> bool {
610
- if self . token . is_like_plus ( ) {
611
- true
612
- }
613
- else {
614
- self . expected_tokens . push ( TokenType :: Token ( token:: BinOp ( token:: Plus ) ) ) ;
615
- false
616
- }
617
- }
618
-
619
605
/// Expects and consumes an `&`. If `&&` is seen, replaces it with a single
620
606
/// `&` and continues. If an `&` is not seen, signals an error.
621
607
fn expect_and ( & mut self ) -> PResult < ' a , ( ) > {
@@ -910,15 +896,13 @@ impl<'a> Parser<'a> {
910
896
self . expected_tokens . clear ( ) ;
911
897
}
912
898
913
- pub fn look_ahead < R , F > ( & self , dist : usize , f : F ) -> R where
914
- F : FnOnce ( & Token ) -> R ,
915
- {
899
+ pub fn look_ahead < R > ( & self , dist : usize , looker : impl FnOnce ( & Token ) -> R ) -> R {
916
900
if dist == 0 {
917
- return f ( & self . token ) ;
901
+ return looker ( & self . token ) ;
918
902
}
919
903
920
904
let frame = & self . token_cursor . frame ;
921
- f ( & match frame. tree_cursor . look_ahead ( dist - 1 ) {
905
+ looker ( & match frame. tree_cursor . look_ahead ( dist - 1 ) {
922
906
Some ( tree) => match tree {
923
907
TokenTree :: Token ( token) => token,
924
908
TokenTree :: Delimited ( dspan, delim, _) =>
@@ -1008,9 +992,10 @@ impl<'a> Parser<'a> {
1008
992
Ok ( ( delim, tts. into ( ) ) )
1009
993
}
1010
994
1011
- fn parse_or_use_outer_attributes ( & mut self ,
1012
- already_parsed_attrs : Option < ThinVec < Attribute > > )
1013
- -> PResult < ' a , ThinVec < Attribute > > {
995
+ fn parse_or_use_outer_attributes (
996
+ & mut self ,
997
+ already_parsed_attrs : Option < ThinVec < Attribute > > ,
998
+ ) -> PResult < ' a , ThinVec < Attribute > > {
1014
999
if let Some ( attrs) = already_parsed_attrs {
1015
1000
Ok ( attrs)
1016
1001
} else {
@@ -1539,9 +1524,10 @@ impl<'a> Parser<'a> {
1539
1524
}
1540
1525
}
1541
1526
1542
- fn collect_tokens < F , R > ( & mut self , f : F ) -> PResult < ' a , ( R , TokenStream ) >
1543
- where F : FnOnce ( & mut Self ) -> PResult < ' a , R >
1544
- {
1527
+ fn collect_tokens < R > (
1528
+ & mut self ,
1529
+ f : impl FnOnce ( & mut Self ) -> PResult < ' a , R > ,
1530
+ ) -> PResult < ' a , ( R , TokenStream ) > {
1545
1531
// Record all tokens we parse when parsing this item.
1546
1532
let mut tokens = Vec :: new ( ) ;
1547
1533
let prev_collecting = match self . token_cursor . frame . last_token {
0 commit comments