@@ -371,30 +371,29 @@ impl<'i: 't, 't> Parser<'i, 't> {
371
371
}
372
372
_ => {
373
373
token = self . input . tokenizer . next ( ) . map_err ( |( ) | BasicParseError :: EndOfInput ) ?;
374
- match token {
375
- // Don’t cache whitespace or comment tokens.
376
- // A typical pattern is:
377
- //
378
- // ```
379
- // parser.try(|parser| {
380
- // match parser.next() { … }
381
- // }).or_else(|| {
382
- // match parser.next() { … }
383
- // })
384
- // ```
385
- //
386
- // If the curren position at the start of this code is at a whitespace token,
387
- // the "interesting" token (returned by `next`) comes later.
388
- // So in the second call to `next`, we don’t want "uninteresting" tokens
389
- // to overwrite the cache.
390
- Token :: WhiteSpace ( _) | Token :: Comment ( _) => { }
391
- _ => {
392
- self . input . cached_token = Some ( CachedToken {
393
- token : token. clone ( ) ,
394
- start_position : token_start_position,
395
- end_position : self . input . tokenizer . position ( ) ,
396
- } )
397
- }
374
+
375
+ // Only overwrite an existing cached token if the new one is further in the stream.
376
+ // A typical pattern is:
377
+ //
378
+ // ```
379
+ // parser.try(|parser| {
380
+ // match parser.next() { … }
381
+ // }).or_else(|| {
382
+ // match parser.next() { … }
383
+ // })
384
+ // ```
385
+ //
386
+ // If the current position at the start of this code is at a whitespace token,
387
+ // `next` is going to skip it and go to the next token.
388
+ // If `try` then rewinds, the second token is the one that will be in the cache.
389
+ // To be able to get any cache hit in this case,
390
+ // we don’t want to overwrite the cache with the intermediate whitespace token.
391
+ if self . input . cached_token . as_ref ( ) . map_or ( true , |c| c. start_position < token_start_position) {
392
+ self . input . cached_token = Some ( CachedToken {
393
+ token : token. clone ( ) ,
394
+ start_position : token_start_position,
395
+ end_position : self . input . tokenizer . position ( ) ,
396
+ } )
398
397
}
399
398
}
400
399
}
0 commit comments