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