@@ -454,10 +454,11 @@ impl TtParser {
454
454
token : & Token ,
455
455
approx_position : usize ,
456
456
track : & mut T ,
457
- ) -> Option < NamedParseResult < T :: Failure > > {
457
+ ) -> ( Option < NamedParseResult < T :: Failure > > , Option < Token > ) {
458
458
// Matcher positions that would be valid if the macro invocation was over now. Only
459
459
// modified if `token == Eof`.
460
460
let mut eof_mps = EofMatcherPositions :: None ;
461
+ let mut expected_token = None ;
461
462
462
463
while let Some ( mut mp) = self . cur_mps . pop ( ) {
463
464
let matcher_loc = & matcher[ mp. idx ] ;
@@ -541,6 +542,8 @@ impl TtParser {
541
542
// The separator matches the current token. Advance past it.
542
543
mp. idx += 1 ;
543
544
self . next_mps . push ( mp) ;
545
+ } else {
546
+ expected_token = Some ( separator. clone ( ) ) ;
544
547
}
545
548
}
546
549
& MatcherLoc :: SequenceKleeneOpAfterSep { idx_first } => {
@@ -560,7 +563,7 @@ impl TtParser {
560
563
} else {
561
564
// E.g. `$e` instead of `$e:expr`, reported as a hard error if actually used.
562
565
// Both this check and the one in `nameize` are necessary, surprisingly.
563
- return Some ( Error ( span, "missing fragment specifier" . to_string ( ) ) ) ;
566
+ return ( Some ( Error ( span, "missing fragment specifier" . to_string ( ) ) ) , None ) ;
564
567
}
565
568
}
566
569
MatcherLoc :: Eof => {
@@ -581,27 +584,35 @@ impl TtParser {
581
584
// If we reached the end of input, check that there is EXACTLY ONE possible matcher.
582
585
// Otherwise, either the parse is ambiguous (which is an error) or there is a syntax error.
583
586
if * token == token:: Eof {
584
- Some ( match eof_mps {
585
- EofMatcherPositions :: One ( mut eof_mp) => {
586
- // Need to take ownership of the matches from within the `Rc`.
587
- Rc :: make_mut ( & mut eof_mp. matches ) ;
588
- let matches = Rc :: try_unwrap ( eof_mp. matches ) . unwrap ( ) . into_iter ( ) ;
589
- self . nameize ( matcher, matches)
590
- }
591
- EofMatcherPositions :: Multiple => {
592
- Error ( token. span , "ambiguity: multiple successful parses" . to_string ( ) )
593
- }
594
- EofMatcherPositions :: None => Failure ( T :: build_failure (
595
- Token :: new (
596
- token:: Eof ,
597
- if token. span . is_dummy ( ) { token. span } else { token. span . shrink_to_hi ( ) } ,
598
- ) ,
599
- approx_position,
600
- "missing tokens in macro arguments" ,
601
- ) ) ,
602
- } )
587
+ (
588
+ Some ( match eof_mps {
589
+ EofMatcherPositions :: One ( mut eof_mp) => {
590
+ // Need to take ownership of the matches from within the `Rc`.
591
+ Rc :: make_mut ( & mut eof_mp. matches ) ;
592
+ let matches = Rc :: try_unwrap ( eof_mp. matches ) . unwrap ( ) . into_iter ( ) ;
593
+ self . nameize ( matcher, matches)
594
+ }
595
+ EofMatcherPositions :: Multiple => {
596
+ Error ( token. span , "ambiguity: multiple successful parses" . to_string ( ) )
597
+ }
598
+ EofMatcherPositions :: None => Failure ( T :: build_failure (
599
+ Token :: new (
600
+ token:: Eof ,
601
+ if token. span . is_dummy ( ) {
602
+ token. span
603
+ } else {
604
+ token. span . shrink_to_hi ( )
605
+ } ,
606
+ ) ,
607
+ None ,
608
+ approx_position,
609
+ "missing tokens in macro arguments" ,
610
+ ) ) ,
611
+ } ) ,
612
+ None ,
613
+ )
603
614
} else {
604
- None
615
+ ( None , expected_token )
605
616
}
606
617
}
607
618
@@ -626,12 +637,13 @@ impl TtParser {
626
637
627
638
// Process `cur_mps` until either we have finished the input or we need to get some
628
639
// parsing from the black-box parser done.
629
- let res = self . parse_tt_inner (
640
+ let ( res, expected_token ) = self . parse_tt_inner (
630
641
matcher,
631
642
& parser. token ,
632
643
parser. approx_token_stream_pos ( ) ,
633
644
track,
634
645
) ;
646
+
635
647
if let Some ( res) = res {
636
648
return res;
637
649
}
@@ -646,6 +658,7 @@ impl TtParser {
646
658
// parser: syntax error.
647
659
return Failure ( T :: build_failure (
648
660
parser. token . clone ( ) ,
661
+ expected_token,
649
662
parser. approx_token_stream_pos ( ) ,
650
663
"no rules expected this token in macro call" ,
651
664
) ) ;
0 commit comments