@@ -890,14 +890,13 @@ impl<'a> Parser<'a> {
890
890
/// Parses a sequence, including the closing delimiter. The function
891
891
/// `f` must consume tokens until reaching the next separator or
892
892
/// closing bracket.
893
- pub fn parse_seq_to_end < T , F > ( & mut self ,
894
- ket : & TokenKind ,
895
- sep : SeqSep ,
896
- f : F )
897
- -> PResult < ' a , Vec < T > > where
898
- F : FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
899
- {
900
- let ( val, recovered) = self . parse_seq_to_before_end ( ket, sep, f) ?;
893
+ pub fn parse_seq_to_end < T > (
894
+ & mut self ,
895
+ ket : & TokenKind ,
896
+ sep : SeqSep ,
897
+ f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
898
+ ) -> PResult < ' a , Vec < T > > {
899
+ let ( val, _, recovered) = self . parse_seq_to_before_end ( ket, sep, f) ?;
901
900
if !recovered {
902
901
self . bump ( ) ;
903
902
}
@@ -907,39 +906,39 @@ impl<'a> Parser<'a> {
907
906
/// Parses a sequence, not including the closing delimiter. The function
908
907
/// `f` must consume tokens until reaching the next separator or
909
908
/// closing bracket.
910
- pub fn parse_seq_to_before_end < T , F > (
909
+ pub fn parse_seq_to_before_end < T > (
911
910
& mut self ,
912
911
ket : & TokenKind ,
913
912
sep : SeqSep ,
914
- f : F ,
915
- ) -> PResult < ' a , ( Vec < T > , bool ) >
916
- where F : FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T >
917
- {
913
+ f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
914
+ ) -> PResult < ' a , ( Vec < T > , bool , bool ) > {
918
915
self . parse_seq_to_before_tokens ( & [ ket] , sep, TokenExpectType :: Expect , f)
919
916
}
920
917
921
- crate fn parse_seq_to_before_tokens < T , F > (
918
+ fn expect_any_with_type ( & mut self , kets : & [ & TokenKind ] , expect : TokenExpectType ) -> bool {
919
+ kets. iter ( ) . any ( |k| {
920
+ match expect {
921
+ TokenExpectType :: Expect => self . check ( k) ,
922
+ TokenExpectType :: NoExpect => self . token == * * k,
923
+ }
924
+ } )
925
+ }
926
+
927
+ crate fn parse_seq_to_before_tokens < T > (
922
928
& mut self ,
923
929
kets : & [ & TokenKind ] ,
924
930
sep : SeqSep ,
925
931
expect : TokenExpectType ,
926
- mut f : F ,
927
- ) -> PResult < ' a , ( Vec < T > , bool /* recovered */ ) >
928
- where F : FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T >
929
- {
932
+ mut f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
933
+ ) -> PResult < ' a , ( Vec < T > , bool /* trailing */ , bool /* recovered */ ) > {
930
934
let mut first = true ;
931
935
let mut recovered = false ;
936
+ let mut trailing = false ;
932
937
let mut v = vec ! [ ] ;
933
- while !kets. iter ( ) . any ( |k| {
934
- match expect {
935
- TokenExpectType :: Expect => self . check ( k) ,
936
- TokenExpectType :: NoExpect => self . token == * * k,
937
- }
938
- } ) {
939
- match self . token . kind {
940
- token:: CloseDelim ( ..) | token:: Eof => break ,
941
- _ => { }
942
- } ;
938
+ while !self . expect_any_with_type ( kets, expect) {
939
+ if let token:: CloseDelim ( ..) | token:: Eof = self . token . kind {
940
+ break
941
+ }
943
942
if let Some ( ref t) = sep. sep {
944
943
if first {
945
944
first = false ;
@@ -973,40 +972,34 @@ impl<'a> Parser<'a> {
973
972
}
974
973
}
975
974
}
976
- if sep. trailing_sep_allowed && kets. iter ( ) . any ( |k| {
977
- match expect {
978
- TokenExpectType :: Expect => self . check ( k) ,
979
- TokenExpectType :: NoExpect => self . token == * * k,
980
- }
981
- } ) {
975
+ if sep. trailing_sep_allowed && self . expect_any_with_type ( kets, expect) {
976
+ trailing = true ;
982
977
break ;
983
978
}
984
979
985
980
let t = f ( self ) ?;
986
981
v. push ( t) ;
987
982
}
988
983
989
- Ok ( ( v, recovered) )
984
+ Ok ( ( v, trailing , recovered) )
990
985
}
991
986
992
987
/// Parses a sequence, including the closing delimiter. The function
993
988
/// `f` must consume tokens until reaching the next separator or
994
989
/// closing bracket.
995
- fn parse_unspanned_seq < T , F > (
990
+ fn parse_unspanned_seq < T > (
996
991
& mut self ,
997
992
bra : & TokenKind ,
998
993
ket : & TokenKind ,
999
994
sep : SeqSep ,
1000
- f : F ,
1001
- ) -> PResult < ' a , Vec < T > > where
1002
- F : FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
1003
- {
995
+ f : impl FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
996
+ ) -> PResult < ' a , ( Vec < T > , bool ) > {
1004
997
self . expect ( bra) ?;
1005
- let ( result, recovered) = self . parse_seq_to_before_end ( ket, sep, f) ?;
998
+ let ( result, trailing , recovered) = self . parse_seq_to_before_end ( ket, sep, f) ?;
1006
999
if !recovered {
1007
1000
self . eat ( ket) ;
1008
1001
}
1009
- Ok ( result)
1002
+ Ok ( ( result, trailing ) )
1010
1003
}
1011
1004
1012
1005
/// Advance the parser by one token
0 commit comments