@@ -320,9 +320,15 @@ impl TokenType {
320320 }
321321}
322322
323+ /// Used by [`Parser::expect_any_with_type`].
323324#[ derive( Copy , Clone , Debug ) ]
324325enum TokenExpectType {
326+ /// Unencountered tokens are inserted into [`Parser::expected_tokens`].
327+ /// See [`Parser::check`].
325328 Expect ,
329+
330+ /// Unencountered tokens are not inserted into [`Parser::expected_tokens`].
331+ /// See [`Parser::check_noexpect`].
326332 NoExpect ,
327333}
328334
@@ -504,18 +510,10 @@ impl<'a> Parser<'a> {
504510 }
505511
506512 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+ }
519517 }
520518
521519 /// Checks if the next token is `tok`, and returns `true` if so.
@@ -766,13 +764,17 @@ impl<'a> Parser<'a> {
766764 }
767765 }
768766
767+ /// Checks if the next token is contained within `kets`, and returns `true` if so.
769768 fn expect_any_with_type ( & mut self , kets : & [ & TokenKind ] , expect : TokenExpectType ) -> bool {
770769 kets. iter ( ) . any ( |k| match expect {
771770 TokenExpectType :: Expect => self . check ( k) ,
772- TokenExpectType :: NoExpect => self . token == * * k ,
771+ TokenExpectType :: NoExpect => self . check_noexpect ( k ) ,
773772 } )
774773 }
775774
775+ /// Parses a sequence until the specified delimiters. The function
776+ /// `f` must consume tokens until reaching the next separator or
777+ /// closing bracket.
776778 fn parse_seq_to_before_tokens < T > (
777779 & mut self ,
778780 kets : & [ & TokenKind ] ,
@@ -791,13 +793,15 @@ impl<'a> Parser<'a> {
791793 }
792794 if let Some ( t) = & sep. sep {
793795 if first {
796+ // no separator for the first element
794797 first = false ;
795798 } else {
799+ // check for separator
796800 match self . expect ( t) {
797- Ok ( false ) => {
801+ Ok ( false ) /* not recovered */ => {
798802 self . current_closure . take ( ) ;
799803 }
800- Ok ( true ) => {
804+ Ok ( true ) /* recovered */ => {
801805 self . current_closure . take ( ) ;
802806 recovered = true ;
803807 break ;
@@ -965,19 +969,19 @@ impl<'a> Parser<'a> {
965969 Ok ( ( ) )
966970 }
967971
968- /// Parses a sequence, not including the closing delimiter . The function
972+ /// Parses a sequence, not including the delimiters . The function
969973 /// `f` must consume tokens until reaching the next separator or
970974 /// closing bracket.
971975 fn parse_seq_to_before_end < T > (
972976 & mut self ,
973977 ket : & TokenKind ,
974978 sep : SeqSep ,
975979 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 */ ) > {
977981 self . parse_seq_to_before_tokens ( & [ ket] , sep, TokenExpectType :: Expect , f)
978982 }
979983
980- /// Parses a sequence, including the closing delimiter. The function
984+ /// Parses a sequence, including only the closing delimiter. The function
981985 /// `f` must consume tokens until reaching the next separator or
982986 /// closing bracket.
983987 fn parse_seq_to_end < T > (
@@ -993,7 +997,7 @@ impl<'a> Parser<'a> {
993997 Ok ( ( val, trailing) )
994998 }
995999
996- /// Parses a sequence, including the closing delimiter . The function
1000+ /// Parses a sequence, including both delimiters . The function
9971001 /// `f` must consume tokens until reaching the next separator or
9981002 /// closing bracket.
9991003 fn parse_unspanned_seq < T > (
@@ -1002,16 +1006,19 @@ impl<'a> Parser<'a> {
10021006 ket : & TokenKind ,
10031007 sep : SeqSep ,
10041008 f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1005- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1009+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
10061010 self . expect ( bra) ?;
10071011 self . parse_seq_to_end ( ket, sep, f)
10081012 }
10091013
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.
10101017 fn parse_delim_comma_seq < T > (
10111018 & mut self ,
10121019 delim : Delimiter ,
10131020 f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1014- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1021+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
10151022 self . parse_unspanned_seq (
10161023 & token:: OpenDelim ( delim) ,
10171024 & token:: CloseDelim ( delim) ,
@@ -1020,10 +1027,13 @@ impl<'a> Parser<'a> {
10201027 )
10211028 }
10221029
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.
10231033 fn parse_paren_comma_seq < T > (
10241034 & mut self ,
10251035 f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1026- ) -> PResult < ' a , ( ThinVec < T > , bool ) > {
1036+ ) -> PResult < ' a , ( ThinVec < T > , bool /* trailing */ ) > {
10271037 self . parse_delim_comma_seq ( Delimiter :: Parenthesis , f)
10281038 }
10291039
0 commit comments