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