From 16667f3e413c49295727f6557b2355a1d9a458fb Mon Sep 17 00:00:00 2001 From: reuben olinsky Date: Wed, 29 May 2024 20:44:18 -0700 Subject: [PATCH] Fix for regression with case clauses (#34) --- cli/tests/cases/compound_cmds/case.yaml | 7 +++++++ parser/src/parser.rs | 24 ++++++------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/cli/tests/cases/compound_cmds/case.yaml b/cli/tests/cases/compound_cmds/case.yaml index b8dfbbf0..008e35d7 100644 --- a/cli/tests/cases/compound_cmds/case.yaml +++ b/cli/tests/cases/compound_cmds/case.yaml @@ -47,3 +47,10 @@ cases: } myfunc + + - name: "Case with non-dsemi" + stdin: | + case "b" in + a) echo "a";; + b) echo "b" + esac diff --git a/parser/src/parser.rs b/parser/src/parser.rs index c814ed36..85ddeaf2 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -402,27 +402,15 @@ peg::parser! { // TODO: validate if this should call non_reserved_word() or word() pub(crate) rule case_clause() -> ast::CaseClauseCommand = - specific_word("case") w:non_reserved_word() linebreak() _in() linebreak() c:(case_list() / case_list_ns()) specific_word("esac") { - ast::CaseClauseCommand { value: ast::Word::from(w), cases: c } - } / - specific_word("case") w:non_reserved_word() linebreak() _in() linebreak() specific_word("esac") { - ast::CaseClauseCommand{ value: ast::Word::from(w), cases: vec![] } - } + specific_word("case") w:non_reserved_word() linebreak() _in() linebreak() first_items:case_item()* last_item:case_item_ns()? specific_word("esac") { + let mut cases = first_items; - rule case_list_ns() -> Vec = - first:case_list()? last:case_item_ns() { - let mut items = vec![]; - if let Some(mut first) = first { - for item in first.into_iter() { - items.push(item); - } + if let Some(last_item) = last_item { + cases.push(last_item); } - items.push(last); - items - } - rule case_list() -> Vec = - c:case_item()+ + ast::CaseClauseCommand { value: ast::Word::from(w), cases } + } pub(crate) rule case_item_ns() -> ast::CaseItem = specific_operator("(")? p:pattern() specific_operator(")") c:compound_list() {