@@ -242,12 +242,17 @@ struct TokenCursorFrame {
242
242
delim : token:: DelimToken ,
243
243
span : DelimSpan ,
244
244
tree_cursor : tokenstream:: Cursor ,
245
- close_delim : bool ,
245
+ need_to_produce_close_delim : bool ,
246
246
}
247
247
248
248
impl TokenCursorFrame {
249
- fn new ( span : DelimSpan , delim : DelimToken , tts : TokenStream , close_delim : bool ) -> Self {
250
- TokenCursorFrame { delim, span, tree_cursor : tts. into_trees ( ) , close_delim }
249
+ fn new ( span : DelimSpan , delim : DelimToken , tts : TokenStream ) -> Self {
250
+ TokenCursorFrame {
251
+ delim,
252
+ span,
253
+ tree_cursor : tts. into_trees ( ) ,
254
+ need_to_produce_close_delim : delim != DelimToken :: NoDelim ,
255
+ }
251
256
}
252
257
}
253
258
@@ -261,28 +266,32 @@ impl TokenCursor {
261
266
fn inlined_next ( & mut self , desugar_doc_comments : bool ) -> ( Token , Spacing ) {
262
267
loop {
263
268
if let Some ( ( tree, spacing) ) = self . frame . tree_cursor . next_with_spacing ( ) {
264
- return match tree {
269
+ match tree {
265
270
TokenTree :: Token ( token) => match ( desugar_doc_comments, & token) {
266
271
( true , & Token { kind : token:: DocComment ( _, attr_style, data) , span } ) => {
267
- self . desugar ( attr_style, data, span)
272
+ return self . desugar ( attr_style, data, span) ;
268
273
}
269
- _ => ( token, spacing) ,
274
+ _ => return ( token, spacing) ,
270
275
} ,
271
276
TokenTree :: Delimited ( sp, delim, tts) => {
272
277
// Set `open_delim` to true here because we deal with it immediately.
273
- let frame = TokenCursorFrame :: new ( sp, delim, tts, false ) ;
278
+ let frame = TokenCursorFrame :: new ( sp, delim, tts) ;
274
279
self . stack . push ( mem:: replace ( & mut self . frame , frame) ) ;
275
- ( Token :: new ( token:: OpenDelim ( delim) , sp. open ) , Spacing :: Alone )
280
+ if delim != DelimToken :: NoDelim {
281
+ return ( Token :: new ( token:: OpenDelim ( delim) , sp. open ) , Spacing :: Alone ) ;
282
+ }
283
+ // No open delimeter to return; continue on to the next iteration.
276
284
}
277
285
} ;
278
- } else if ! self . frame . close_delim {
279
- self . frame . close_delim = true ;
286
+ } else if self . frame . need_to_produce_close_delim {
287
+ self . frame . need_to_produce_close_delim = false ;
280
288
return (
281
289
Token :: new ( token:: CloseDelim ( self . frame . delim ) , self . frame . span . close ) ,
282
290
Spacing :: Alone ,
283
291
) ;
284
292
} else if let Some ( frame) = self . stack . pop ( ) {
285
293
self . frame = frame;
294
+ // Back to the parent frame; continue on to the next iteration.
286
295
} else {
287
296
return ( Token :: new ( token:: Eof , DUMMY_SP ) , Spacing :: Alone ) ;
288
297
}
@@ -333,7 +342,6 @@ impl TokenCursor {
333
342
. cloned ( )
334
343
. collect :: < TokenStream > ( )
335
344
} ,
336
- true ,
337
345
) ,
338
346
) ) ;
339
347
@@ -422,7 +430,7 @@ impl<'a> Parser<'a> {
422
430
desugar_doc_comments : bool ,
423
431
subparser_name : Option < & ' static str > ,
424
432
) -> Self {
425
- let start_frame = TokenCursorFrame :: new ( DelimSpan :: dummy ( ) , token:: NoDelim , tokens, true ) ;
433
+ let start_frame = TokenCursorFrame :: new ( DelimSpan :: dummy ( ) , token:: NoDelim , tokens) ;
426
434
427
435
let mut parser = Parser {
428
436
sess,
@@ -993,24 +1001,21 @@ impl<'a> Parser<'a> {
993
1001
/// Advance the parser by one token.
994
1002
pub fn bump ( & mut self ) {
995
1003
let fallback_span = self . token . span ;
996
- loop {
997
- let ( mut next, spacing) = self . token_cursor . inlined_next ( self . desugar_doc_comments ) ;
998
- self . token_cursor . num_next_calls += 1 ;
999
- // We've retrieved an token from the underlying
1000
- // cursor, so we no longer need to worry about
1001
- // an unglued token. See `break_and_eat` for more details
1002
- self . token_cursor . break_last_token = false ;
1003
- if next. span . is_dummy ( ) {
1004
- // Tweak the location for better diagnostics, but keep syntactic context intact.
1005
- next. span = fallback_span. with_ctxt ( next. span . ctxt ( ) ) ;
1006
- }
1007
- if !matches ! (
1008
- next. kind,
1009
- token:: OpenDelim ( token:: NoDelim ) | token:: CloseDelim ( token:: NoDelim )
1010
- ) {
1011
- return self . inlined_bump_with ( ( next, spacing) ) ;
1012
- }
1004
+ let ( mut next, spacing) = self . token_cursor . inlined_next ( self . desugar_doc_comments ) ;
1005
+ self . token_cursor . num_next_calls += 1 ;
1006
+ // We've retrieved an token from the underlying
1007
+ // cursor, so we no longer need to worry about
1008
+ // an unglued token. See `break_and_eat` for more details
1009
+ self . token_cursor . break_last_token = false ;
1010
+ if next. span . is_dummy ( ) {
1011
+ // Tweak the location for better diagnostics, but keep syntactic context intact.
1012
+ next. span = fallback_span. with_ctxt ( next. span . ctxt ( ) ) ;
1013
1013
}
1014
+ debug_assert ! ( !matches!(
1015
+ next. kind,
1016
+ token:: OpenDelim ( token:: NoDelim ) | token:: CloseDelim ( token:: NoDelim )
1017
+ ) ) ;
1018
+ self . inlined_bump_with ( ( next, spacing) )
1014
1019
}
1015
1020
1016
1021
/// Look-ahead `dist` tokens of `self.token` and get access to that token there.
0 commit comments