From 0b64afa77deeae3bb2e95484f8e5bafca74bbc3d Mon Sep 17 00:00:00 2001 From: He1pa <56333845+He1pa@users.noreply.github.com> Date: Mon, 31 Jul 2023 20:04:36 +0800 Subject: [PATCH] bugfix: endless loop in if stmt error recovery (#630) --- kclvm/parser/src/parser/stmt.rs | 5 +++ kclvm/parser/src/tests/error_recovery.rs | 1 + ...s__error_recovery__if_stmt_recovery_7.snap | 39 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__if_stmt_recovery_7.snap diff --git a/kclvm/parser/src/parser/stmt.rs b/kclvm/parser/src/parser/stmt.rs index 2919ac108..823165c39 100644 --- a/kclvm/parser/src/parser/stmt.rs +++ b/kclvm/parser/src/parser/stmt.rs @@ -137,6 +137,11 @@ impl<'a> Parser<'a> { self.bump_token(open_tok); loop { + if self.token.kind == TokenKind::Eof { + self.bump(); + break; + } + if self.token.kind == close_tok { self.bump_token(close_tok); break; diff --git a/kclvm/parser/src/tests/error_recovery.rs b/kclvm/parser/src/tests/error_recovery.rs index 5c5632717..617fb2472 100644 --- a/kclvm/parser/src/tests/error_recovery.rs +++ b/kclvm/parser/src/tests/error_recovery.rs @@ -194,6 +194,7 @@ parse_module_snapshot! { if_stmt_recovery_3, r#"if True: a = 1 else b = 2"#} parse_module_snapshot! { if_stmt_recovery_4, r#"if True: else: b = 2"#} parse_module_snapshot! { if_stmt_recovery_5, r#"if"#} parse_module_snapshot! { if_stmt_recovery_6, r#"if else"#} +parse_module_snapshot! { if_stmt_recovery_7, r#"if True:"#} parse_module_snapshot! { schema_stmt_recovery_0, r#"schema"#} parse_module_snapshot! { schema_stmt_recovery_1, r#"schema A"#} parse_module_snapshot! { schema_stmt_recovery_2, r#"schema A["#} diff --git a/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__if_stmt_recovery_7.snap b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__if_stmt_recovery_7.snap new file mode 100644 index 000000000..5e2a57430 --- /dev/null +++ b/kclvm/parser/src/tests/snapshots/kclvm_parser__tests__error_recovery__if_stmt_recovery_7.snap @@ -0,0 +1,39 @@ +--- +source: parser/src/tests/error_recovery.rs +expression: "crate::tests::parsing_module_string(r#\"if True:\"#)" +--- +Module { + filename: "", + pkg: "", + doc: "", + name: "", + body: [ + Node { + node: If( + IfStmt { + body: [], + cond: Node { + node: NameConstantLit( + NameConstantLit { + value: True, + }, + ), + filename: "", + line: 1, + column: 3, + end_line: 1, + end_column: 7, + }, + orelse: [], + }, + ), + filename: "", + line: 1, + column: 0, + end_line: 1, + end_column: 8, + }, + ], + comments: [], +} +