@@ -261,6 +261,7 @@ pub struct Parser<'a> {
261261 /// the previous token or None (only stashed sometimes).
262262 pub last_token : Option < Box < token:: Token > > ,
263263 last_token_interpolated : bool ,
264+ last_token_eof : bool ,
264265 pub buffer : [ TokenAndSpan ; 4 ] ,
265266 pub buffer_start : isize ,
266267 pub buffer_end : isize ,
@@ -369,6 +370,7 @@ impl<'a> Parser<'a> {
369370 last_span : span,
370371 last_token : None ,
371372 last_token_interpolated : false ,
373+ last_token_eof : false ,
372374 buffer : [
373375 placeholder. clone ( ) ,
374376 placeholder. clone ( ) ,
@@ -939,7 +941,9 @@ impl<'a> Parser<'a> {
939941 {
940942 try!( self . expect ( bra) ) ;
941943 let result = self . parse_seq_to_before_end ( ket, sep, f) ;
942- self . bump ( ) ;
944+ if self . token == * ket {
945+ self . bump ( ) ;
946+ }
943947 Ok ( result)
944948 }
945949
@@ -982,6 +986,15 @@ impl<'a> Parser<'a> {
982986
983987 /// Advance the parser by one token
984988 pub fn bump ( & mut self ) {
989+ if self . last_token_eof {
990+ // Bumping after EOF is a bad sign, usually an infinite loop.
991+ self . bug ( "attempted to bump the parser past EOF (may be stuck in a loop)" ) ;
992+ }
993+
994+ if self . token == token:: Eof {
995+ self . last_token_eof = true ;
996+ }
997+
985998 self . last_span = self . span ;
986999 // Stash token for error recovery (sometimes; clone is not necessarily cheap).
9871000 self . last_token = if self . token . is_ident ( ) ||
@@ -1265,15 +1278,21 @@ impl<'a> Parser<'a> {
12651278 Ok ( cua) => cua,
12661279 Err ( e) => {
12671280 loop {
1268- p. bump ( ) ;
1269- if p. token == token:: Semi {
1270- p. bump ( ) ;
1271- break ;
1272- }
1281+ match p. token {
1282+ token:: Eof => break ,
12731283
1274- if p. token == token:: OpenDelim ( token:: DelimToken :: Brace ) {
1275- try!( p. parse_token_tree ( ) ) ;
1276- break ;
1284+ token:: CloseDelim ( token:: Brace ) |
1285+ token:: Semi => {
1286+ p. bump ( ) ;
1287+ break ;
1288+ }
1289+
1290+ token:: OpenDelim ( token:: Brace ) => {
1291+ try!( p. parse_token_tree ( ) ) ;
1292+ break ;
1293+ }
1294+
1295+ _ => p. bump ( )
12771296 }
12781297 }
12791298
@@ -3790,7 +3809,9 @@ impl<'a> Parser<'a> {
37903809 fn recover_stmt_ ( & mut self , break_on_semi : SemiColonMode ) {
37913810 let mut brace_depth = 0 ;
37923811 let mut bracket_depth = 0 ;
3812+ debug ! ( "recover_stmt_ enter loop" ) ;
37933813 loop {
3814+ debug ! ( "recover_stmt_ loop {:?}" , self . token) ;
37943815 match self . token {
37953816 token:: OpenDelim ( token:: DelimToken :: Brace ) => {
37963817 brace_depth += 1 ;
@@ -3802,6 +3823,7 @@ impl<'a> Parser<'a> {
38023823 }
38033824 token:: CloseDelim ( token:: DelimToken :: Brace ) => {
38043825 if brace_depth == 0 {
3826+ debug ! ( "recover_stmt_ return - close delim {:?}" , self . token) ;
38053827 return ;
38063828 }
38073829 brace_depth -= 1 ;
@@ -3814,12 +3836,16 @@ impl<'a> Parser<'a> {
38143836 }
38153837 self . bump ( ) ;
38163838 }
3817- token:: Eof => return ,
3839+ token:: Eof => {
3840+ debug ! ( "recover_stmt_ return - Eof" ) ;
3841+ return ;
3842+ }
38183843 token:: Semi => {
38193844 self . bump ( ) ;
38203845 if break_on_semi == SemiColonMode :: Break &&
38213846 brace_depth == 0 &&
38223847 bracket_depth == 0 {
3848+ debug ! ( "recover_stmt_ return - Semi" ) ;
38233849 return ;
38243850 }
38253851 }
@@ -4008,6 +4034,8 @@ impl<'a> Parser<'a> {
40084034 while !self . eat ( & token:: CloseDelim ( token:: Brace ) ) {
40094035 let Spanned { node, span} = if let Some ( s) = self . parse_stmt_ ( ) {
40104036 s
4037+ } else if self . token == token:: Eof {
4038+ break ;
40114039 } else {
40124040 // Found only `;` or `}`.
40134041 continue ;
0 commit comments