From a186eb8d970a3aadaba32b118543b3c178b2e431 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Tue, 20 Apr 2021 11:22:38 +0800 Subject: [PATCH] Fix hanging when parsing unterminated statements block. --- src/parser.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index a0d03b43..b2849364 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2417,7 +2417,23 @@ fn parse_block( #[cfg(not(feature = "no_module"))] let prev_mods_len = state.modules.len(); - while !match_token(input, Token::RightBrace).0 { + loop { + // Terminated? + match input.peek().unwrap() { + (Token::RightBrace, _) => { + eat_token(input, Token::RightBrace); + break; + } + (Token::EOF, pos) => { + return Err(PERR::MissingToken( + Token::RightBrace.into(), + "to terminate this block".into(), + ) + .into_err(*pos)); + } + _ => (), + } + // Parse statements inside the block settings.is_global = false; @@ -2443,11 +2459,13 @@ fn parse_block( eat_token(input, Token::SemiColon); } // { ... { stmt } ; - (Token::SemiColon, _) if !need_semicolon => (), + (Token::SemiColon, _) if !need_semicolon => { + eat_token(input, Token::SemiColon); + } // { ... { stmt } ??? (_, _) if !need_semicolon => (), // { ... stmt - (Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)), + (Token::LexError(err), err_pos) => return Err(err.clone().into_err(*err_pos)), // { ... stmt ??? (_, pos) => { // Semicolons are not optional between statements