@@ -1052,33 +1052,48 @@ impl<'a> Parser<'a> {
1052
1052
}
1053
1053
1054
1054
/// Look-ahead `dist` tokens of `self.token` and get access to that token there.
1055
- /// When `dist == 0` then the current token is looked at.
1055
+ /// When `dist == 0` then the current token is looked at. `Eof` will be
1056
+ /// returned if the look-ahead is any distance past the end of the tokens.
1056
1057
pub fn look_ahead < R > ( & self , dist : usize , looker : impl FnOnce ( & Token ) -> R ) -> R {
1057
1058
if dist == 0 {
1058
1059
return looker ( & self . token ) ;
1059
1060
}
1060
1061
1061
- let tree_cursor = & self . token_cursor . tree_cursor ;
1062
1062
if let Some ( & ( _, delim, span) ) = self . token_cursor . stack . last ( )
1063
1063
&& delim != Delimiter :: Invisible
1064
1064
{
1065
+ // We are not in the outermost token stream, and the token stream
1066
+ // we are in has non-skipped delimiters. Look for skipped
1067
+ // delimiters in the lookahead range.
1068
+ let tree_cursor = & self . token_cursor . tree_cursor ;
1065
1069
let all_normal = ( 0 ..dist) . all ( |i| {
1066
1070
let token = tree_cursor. look_ahead ( i) ;
1067
1071
!matches ! ( token, Some ( TokenTree :: Delimited ( _, Delimiter :: Invisible , _) ) )
1068
1072
} ) ;
1069
1073
if all_normal {
1074
+ // There were no skipped delimiters. Do lookahead by plain indexing.
1070
1075
return match tree_cursor. look_ahead ( dist - 1 ) {
1071
- Some ( tree) => match tree {
1072
- TokenTree :: Token ( token, _) => looker ( token) ,
1073
- TokenTree :: Delimited ( dspan, delim, _) => {
1074
- looker ( & Token :: new ( token:: OpenDelim ( * delim) , dspan. open ) )
1076
+ Some ( tree) => {
1077
+ // Indexing stayed within the current token stream.
1078
+ match tree {
1079
+ TokenTree :: Token ( token, _) => looker ( token) ,
1080
+ TokenTree :: Delimited ( dspan, delim, _) => {
1081
+ looker ( & Token :: new ( token:: OpenDelim ( * delim) , dspan. open ) )
1082
+ }
1075
1083
}
1076
- } ,
1077
- None => looker ( & Token :: new ( token:: CloseDelim ( delim) , span. close ) ) ,
1084
+ }
1085
+ None => {
1086
+ // Indexing went past the end of the current token
1087
+ // stream. Use the close delimiter, no matter how far
1088
+ // ahead `dist` went.
1089
+ looker ( & Token :: new ( token:: CloseDelim ( delim) , span. close ) )
1090
+ }
1078
1091
} ;
1079
1092
}
1080
1093
}
1081
1094
1095
+ // We are in a more complex case. Just clone the token cursor and use
1096
+ // `next`, skipping delimiters as necessary. Slow but simple.
1082
1097
let mut cursor = self . token_cursor . clone ( ) ;
1083
1098
let mut i = 0 ;
1084
1099
let mut token = Token :: dummy ( ) ;
0 commit comments