@@ -315,9 +315,15 @@ impl TokenType {
315
315
}
316
316
}
317
317
318
+ /// Used by [`Parser::expect_any_with_type`].
318
319
#[ derive( Copy , Clone , Debug ) ]
319
320
enum TokenExpectType {
321
+ /// Unencountered tokens are inserted into [`Parser::expected_tokens`].
322
+ /// See [`Parser::check`].
320
323
Expect ,
324
+
325
+ /// Unencountered tokens are not inserted into [`Parser::expected_tokens`].
326
+ /// See [`Parser::check_noexpect`].
321
327
NoExpect ,
322
328
}
323
329
@@ -761,13 +767,17 @@ impl<'a> Parser<'a> {
761
767
}
762
768
}
763
769
770
+ /// Checks if the next token is contained within `kets`, and returns `true` if so.
764
771
fn expect_any_with_type ( & mut self , kets : & [ & TokenKind ] , expect : TokenExpectType ) -> bool {
765
772
kets. iter ( ) . any ( |k| match expect {
766
773
TokenExpectType :: Expect => self . check ( k) ,
767
- TokenExpectType :: NoExpect => self . token == * * k ,
774
+ TokenExpectType :: NoExpect => self . check_noexpect ( k ) ,
768
775
} )
769
776
}
770
777
778
+ /// Parses a sequence until the specified delimiters. The function
779
+ /// `f` must consume tokens until reaching the next separator or
780
+ /// closing bracket.
771
781
fn parse_seq_to_before_tokens < T > (
772
782
& mut self ,
773
783
kets : & [ & TokenKind ] ,
@@ -786,13 +796,15 @@ impl<'a> Parser<'a> {
786
796
}
787
797
if let Some ( t) = & sep. sep {
788
798
if first {
799
+ // no separator for the first element
789
800
first = false ;
790
801
} else {
802
+ // check for separator
791
803
match self . expect ( t) {
792
- Ok ( false ) => {
804
+ Ok ( false ) /* not recovered */ => {
793
805
self . current_closure . take ( ) ;
794
806
}
795
- Ok ( true ) => {
807
+ Ok ( true ) /* recovered */ => {
796
808
self . current_closure . take ( ) ;
797
809
recovered = true ;
798
810
break ;
@@ -960,19 +972,19 @@ impl<'a> Parser<'a> {
960
972
Ok ( ( ) )
961
973
}
962
974
963
- /// Parses a sequence, not including the closing delimiter . The function
975
+ /// Parses a sequence, not including the delimiters . The function
964
976
/// `f` must consume tokens until reaching the next separator or
965
977
/// closing bracket.
966
978
fn parse_seq_to_before_end < T > (
967
979
& mut self ,
968
980
ket : & TokenKind ,
969
981
sep : SeqSep ,
970
982
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
971
- ) -> PResult < ' a , ( ThinVec < T > , bool , bool ) > {
983
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ , bool /* recovered */ ) > {
972
984
self . parse_seq_to_before_tokens ( & [ ket] , sep, TokenExpectType :: Expect , f)
973
985
}
974
986
975
- /// Parses a sequence, including the closing delimiter. The function
987
+ /// Parses a sequence, including only the closing delimiter. The function
976
988
/// `f` must consume tokens until reaching the next separator or
977
989
/// closing bracket.
978
990
fn parse_seq_to_end < T > (
@@ -988,7 +1000,7 @@ impl<'a> Parser<'a> {
988
1000
Ok ( ( val, trailing) )
989
1001
}
990
1002
991
- /// Parses a sequence, including the closing delimiter . The function
1003
+ /// Parses a sequence, including both delimiters . The function
992
1004
/// `f` must consume tokens until reaching the next separator or
993
1005
/// closing bracket.
994
1006
fn parse_unspanned_seq < T > (
@@ -997,16 +1009,19 @@ impl<'a> Parser<'a> {
997
1009
ket : & TokenKind ,
998
1010
sep : SeqSep ,
999
1011
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1000
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1012
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1001
1013
self . expect ( bra) ?;
1002
1014
self . parse_seq_to_end ( ket, sep, f)
1003
1015
}
1004
1016
1017
+ /// Parses a comma-separated sequence, including both delimiters.
1018
+ /// The function `f` must consume tokens until reaching the next separator or
1019
+ /// closing bracket.
1005
1020
fn parse_delim_comma_seq < T > (
1006
1021
& mut self ,
1007
1022
delim : Delimiter ,
1008
1023
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1009
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1024
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1010
1025
self . parse_unspanned_seq (
1011
1026
& token:: OpenDelim ( delim) ,
1012
1027
& token:: CloseDelim ( delim) ,
@@ -1015,10 +1030,13 @@ impl<'a> Parser<'a> {
1015
1030
)
1016
1031
}
1017
1032
1033
+ /// Parses a comma-separated sequence delimited by parentheses (e.g. `(x, y)`).
1034
+ /// The function `f` must consume tokens until reaching the next separator or
1035
+ /// closing bracket.
1018
1036
fn parse_paren_comma_seq < T > (
1019
1037
& mut self ,
1020
1038
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1021
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1039
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1022
1040
self . parse_delim_comma_seq ( Delimiter :: Parenthesis , f)
1023
1041
}
1024
1042
0 commit comments