Skip to content

Commit

Permalink
fix: stop incorrectly parsing assignment as function def (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Nov 26, 2024
1 parent 191889a commit 7ca1f25
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 0 additions & 1 deletion brush-core/src/builtins/brushinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) struct BrushInfoCommand {
enum CommandGroup {
#[clap(subcommand)]
Process(ProcessCommand),

#[clap(subcommand)]
Complete(CompleteCommand),
}
Expand Down
5 changes: 4 additions & 1 deletion brush-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ peg::parser! {
c:compound_command() r:redirect_list()? { ast::FunctionBody(c, r) }

rule fname() -> &'input str =
name()
// Special-case: don't allow it to end with an equals sign, to avoid the challenge of
// misinterpreting certain declaration assignments as function definitions.
// TODO: Find a way to make this still work without requiring this targeted exception.
w:[Token::Word(word, _) if !word.ends_with('=')] { w.to_str() }

rule brace_group() -> ast::BraceGroupCommand =
specific_word("{") c:compound_list() specific_word("}") { ast::BraceGroupCommand(c) }
Expand Down
13 changes: 13 additions & 0 deletions brush-shell/tests/cases/arrays.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,16 @@ cases:
ourarray+=("${otherarray[@]/a/x}")
declare -p otherarray
declare -p ourarray
- name: "Array declaration in a function"
stdin: |
outer() {
inner=()
if test true; then
echo true
else
echo false
fi
}
outer
33 changes: 33 additions & 0 deletions brush-shell/tests/cases/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,36 @@ cases:
myfunc
myvar="overridden" myfunc
myfunc
- name: "Function definition without braces"
stdin: |
myfunc()
if true; then
echo true
else
echo false
fi
myfunc
- name: "Nested function definition"
stdin: |
outer() {
echo "Entered outer"
inner() {
echo "In inner"
}
echo "Invoking inner"
inner
echo "Returning from outer"
}
echo "Calling outer from toplevel"
outer
echo "Calling inner from toplevel"
inner

0 comments on commit 7ca1f25

Please sign in to comment.