@@ -1052,33 +1052,48 @@ impl<'a> Parser<'a> {
10521052 }
10531053
10541054 /// 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.
10561057 pub fn look_ahead < R > ( & self , dist : usize , looker : impl FnOnce ( & Token ) -> R ) -> R {
10571058 if dist == 0 {
10581059 return looker ( & self . token ) ;
10591060 }
10601061
1061- let tree_cursor = & self . token_cursor . tree_cursor ;
10621062 if let Some ( & ( _, delim, span) ) = self . token_cursor . stack . last ( )
10631063 && delim != Delimiter :: Invisible
10641064 {
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 ;
10651069 let all_normal = ( 0 ..dist) . all ( |i| {
10661070 let token = tree_cursor. look_ahead ( i) ;
10671071 !matches ! ( token, Some ( TokenTree :: Delimited ( _, Delimiter :: Invisible , _) ) )
10681072 } ) ;
10691073 if all_normal {
1074+ // There were no skipped delimiters. Do lookahead by plain indexing.
10701075 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+ }
10751083 }
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+ }
10781091 } ;
10791092 }
10801093 }
10811094
1095+ // We are in a more complex case. Just clone the token cursor and use
1096+ // `next`, skipping delimiters as necessary. Slow but simple.
10821097 let mut cursor = self . token_cursor . clone ( ) ;
10831098 let mut i = 0 ;
10841099 let mut token = Token :: dummy ( ) ;
0 commit comments