@@ -320,9 +320,15 @@ impl TokenType {
320
320
}
321
321
}
322
322
323
+ /// Used by [`Parser::expect_any_with_type`].
323
324
#[ derive( Copy , Clone , Debug ) ]
324
325
enum TokenExpectType {
326
+ /// Unencountered tokens are inserted into [`Parser::expected_tokens`].
327
+ /// See [`Parser::check`].
325
328
Expect ,
329
+
330
+ /// Unencountered tokens are not inserted into [`Parser::expected_tokens`].
331
+ /// See [`Parser::check_noexpect`].
326
332
NoExpect ,
327
333
}
328
334
@@ -504,18 +510,10 @@ impl<'a> Parser<'a> {
504
510
}
505
511
506
512
fn ident_or_err ( & mut self , recover : bool ) -> PResult < ' a , ( Ident , /* is_raw */ bool ) > {
507
- let result = self . token . ident ( ) . ok_or_else ( || self . expected_ident_found ( recover) ) ;
508
-
509
- let ( ident, is_raw) = match result {
510
- Ok ( ident) => ident,
511
- Err ( err) => match err {
512
- // we recovered!
513
- Ok ( ident) => ident,
514
- Err ( err) => return Err ( err) ,
515
- } ,
516
- } ;
517
-
518
- Ok ( ( ident, is_raw) )
513
+ match self . token . ident ( ) {
514
+ Some ( ident) => Ok ( ident) ,
515
+ None => self . expected_ident_found ( recover) ,
516
+ }
519
517
}
520
518
521
519
/// Checks if the next token is `tok`, and returns `true` if so.
@@ -766,13 +764,17 @@ impl<'a> Parser<'a> {
766
764
}
767
765
}
768
766
767
+ /// Checks if the next token is contained within `kets`, and returns `true` if so.
769
768
fn expect_any_with_type ( & mut self , kets : & [ & TokenKind ] , expect : TokenExpectType ) -> bool {
770
769
kets. iter ( ) . any ( |k| match expect {
771
770
TokenExpectType :: Expect => self . check ( k) ,
772
- TokenExpectType :: NoExpect => self . token == * * k ,
771
+ TokenExpectType :: NoExpect => self . check_noexpect ( k ) ,
773
772
} )
774
773
}
775
774
775
+ /// Parses a sequence until the specified delimiters. The function
776
+ /// `f` must consume tokens until reaching the next separator or
777
+ /// closing bracket.
776
778
fn parse_seq_to_before_tokens < T > (
777
779
& mut self ,
778
780
kets : & [ & TokenKind ] ,
@@ -791,13 +793,15 @@ impl<'a> Parser<'a> {
791
793
}
792
794
if let Some ( t) = & sep. sep {
793
795
if first {
796
+ // no separator for the first element
794
797
first = false ;
795
798
} else {
799
+ // check for separator
796
800
match self . expect ( t) {
797
- Ok ( false ) => {
801
+ Ok ( false ) /* not recovered */ => {
798
802
self . current_closure . take ( ) ;
799
803
}
800
- Ok ( true ) => {
804
+ Ok ( true ) /* recovered */ => {
801
805
self . current_closure . take ( ) ;
802
806
recovered = true ;
803
807
break ;
@@ -965,19 +969,19 @@ impl<'a> Parser<'a> {
965
969
Ok ( ( ) )
966
970
}
967
971
968
- /// Parses a sequence, not including the closing delimiter . The function
972
+ /// Parses a sequence, not including the delimiters . The function
969
973
/// `f` must consume tokens until reaching the next separator or
970
974
/// closing bracket.
971
975
fn parse_seq_to_before_end < T > (
972
976
& mut self ,
973
977
ket : & TokenKind ,
974
978
sep : SeqSep ,
975
979
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
976
- ) -> PResult < ' a , ( ThinVec < T > , bool , bool ) > {
980
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ , bool /* recovered */ ) > {
977
981
self . parse_seq_to_before_tokens ( & [ ket] , sep, TokenExpectType :: Expect , f)
978
982
}
979
983
980
- /// Parses a sequence, including the closing delimiter. The function
984
+ /// Parses a sequence, including only the closing delimiter. The function
981
985
/// `f` must consume tokens until reaching the next separator or
982
986
/// closing bracket.
983
987
fn parse_seq_to_end < T > (
@@ -993,7 +997,7 @@ impl<'a> Parser<'a> {
993
997
Ok ( ( val, trailing) )
994
998
}
995
999
996
- /// Parses a sequence, including the closing delimiter . The function
1000
+ /// Parses a sequence, including both delimiters . The function
997
1001
/// `f` must consume tokens until reaching the next separator or
998
1002
/// closing bracket.
999
1003
fn parse_unspanned_seq < T > (
@@ -1002,16 +1006,19 @@ impl<'a> Parser<'a> {
1002
1006
ket : & TokenKind ,
1003
1007
sep : SeqSep ,
1004
1008
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1005
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1009
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1006
1010
self . expect ( bra) ?;
1007
1011
self . parse_seq_to_end ( ket, sep, f)
1008
1012
}
1009
1013
1014
+ /// Parses a comma-separated sequence, including both delimiters.
1015
+ /// The function `f` must consume tokens until reaching the next separator or
1016
+ /// closing bracket.
1010
1017
fn parse_delim_comma_seq < T > (
1011
1018
& mut self ,
1012
1019
delim : Delimiter ,
1013
1020
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1014
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1021
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1015
1022
self . parse_unspanned_seq (
1016
1023
& token:: OpenDelim ( delim) ,
1017
1024
& token:: CloseDelim ( delim) ,
@@ -1020,10 +1027,13 @@ impl<'a> Parser<'a> {
1020
1027
)
1021
1028
}
1022
1029
1030
+ /// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
1031
+ /// The function `f` must consume tokens until reaching the next separator or
1032
+ /// closing bracket.
1023
1033
fn parse_paren_comma_seq < T > (
1024
1034
& mut self ,
1025
1035
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1026
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1036
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1027
1037
self . parse_delim_comma_seq ( Delimiter :: Parenthesis , f)
1028
1038
}
1029
1039
0 commit comments