@@ -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
@@ -758,13 +764,17 @@ impl<'a> Parser<'a> {
758
764
}
759
765
}
760
766
767
+ /// Checks if the next token is contained within `kets`, and returns `true` if so.
761
768
fn expect_any_with_type ( & mut self , kets : & [ & TokenKind ] , expect : TokenExpectType ) -> bool {
762
769
kets. iter ( ) . any ( |k| match expect {
763
770
TokenExpectType :: Expect => self . check ( k) ,
764
- TokenExpectType :: NoExpect => self . token == * * k ,
771
+ TokenExpectType :: NoExpect => self . check_noexpect ( k ) ,
765
772
} )
766
773
}
767
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.
768
778
fn parse_seq_to_before_tokens < T > (
769
779
& mut self ,
770
780
kets : & [ & TokenKind ] ,
@@ -783,13 +793,15 @@ impl<'a> Parser<'a> {
783
793
}
784
794
if let Some ( t) = & sep. sep {
785
795
if first {
796
+ // no separator for the first element
786
797
first = false ;
787
798
} else {
799
+ // check for separator
788
800
match self . expect ( t) {
789
- Ok ( false ) => {
801
+ Ok ( false ) /* not recovered */ => {
790
802
self . current_closure . take ( ) ;
791
803
}
792
- Ok ( true ) => {
804
+ Ok ( true ) /* recovered */ => {
793
805
self . current_closure . take ( ) ;
794
806
recovered = true ;
795
807
break ;
@@ -957,19 +969,19 @@ impl<'a> Parser<'a> {
957
969
Ok ( ( ) )
958
970
}
959
971
960
- /// Parses a sequence, not including the closing delimiter . The function
972
+ /// Parses a sequence, not including the delimiters . The function
961
973
/// `f` must consume tokens until reaching the next separator or
962
974
/// closing bracket.
963
975
fn parse_seq_to_before_end < T > (
964
976
& mut self ,
965
977
ket : & TokenKind ,
966
978
sep : SeqSep ,
967
979
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
968
- ) -> PResult < ' a , ( ThinVec < T > , bool , bool ) > {
980
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ , bool /* recovered */ ) > {
969
981
self . parse_seq_to_before_tokens ( & [ ket] , sep, TokenExpectType :: Expect , f)
970
982
}
971
983
972
- /// Parses a sequence, including the closing delimiter. The function
984
+ /// Parses a sequence, including only the closing delimiter. The function
973
985
/// `f` must consume tokens until reaching the next separator or
974
986
/// closing bracket.
975
987
fn parse_seq_to_end < T > (
@@ -985,7 +997,7 @@ impl<'a> Parser<'a> {
985
997
Ok ( ( val, trailing) )
986
998
}
987
999
988
- /// Parses a sequence, including the closing delimiter . The function
1000
+ /// Parses a sequence, including both delimiters . The function
989
1001
/// `f` must consume tokens until reaching the next separator or
990
1002
/// closing bracket.
991
1003
fn parse_unspanned_seq < T > (
@@ -994,16 +1006,19 @@ impl<'a> Parser<'a> {
994
1006
ket : & TokenKind ,
995
1007
sep : SeqSep ,
996
1008
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
997
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1009
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
998
1010
self . expect ( bra) ?;
999
1011
self . parse_seq_to_end ( ket, sep, f)
1000
1012
}
1001
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.
1002
1017
fn parse_delim_comma_seq < T > (
1003
1018
& mut self ,
1004
1019
delim : Delimiter ,
1005
1020
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1006
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1021
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1007
1022
self . parse_unspanned_seq (
1008
1023
& token:: OpenDelim ( delim) ,
1009
1024
& token:: CloseDelim ( delim) ,
@@ -1012,10 +1027,13 @@ impl<'a> Parser<'a> {
1012
1027
)
1013
1028
}
1014
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.
1015
1033
fn parse_paren_comma_seq < T > (
1016
1034
& mut self ,
1017
1035
f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1018
- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1036
+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
1019
1037
self . parse_delim_comma_seq ( Delimiter :: Parenthesis , f)
1020
1038
}
1021
1039
0 commit comments