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