fix(parser): a case pattern's ) does not end a command substitution - #1255
Open
bresilla wants to merge 1 commit into
Open
fix(parser): a case pattern's ) does not end a command substitution#1255bresilla wants to merge 1 commit into
bresilla wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes #1052 by distinguishing case-pattern delimiters from command-substitution terminators.
Changes:
- Adds tokenizer state tracking for
caseconstructs. - Adds recursive
case ... esachandling in command substitutions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
brush-parser/src/word.rs |
Consumes case blocks within substitutions. |
brush-parser/src/tokenizer.rs |
Tracks case parsing state around parentheses. |
Suppressed comments (1)
brush-parser/src/tokenizer.rs:672
- Token spelling does not establish reserved-word context. In the valid
$(case x in case) echo Y;; esac)form, the pattern wordcasepushesAwaitingIn; its following)is then again mistaken for the substitution terminator. Only acasekeyword in command position should push a new state.
"case" => case_states.push(CaseState::AwaitingIn),
| // substitution, so `$(case a in a) echo Y;; esac)` terminated at `$(case a in a` and the | ||
| // rest became a syntax error. A pattern's `)` closes nothing that was opened, so it must | ||
| // not decrement the nesting count. | ||
| let mut case_states: Vec<CaseState> = vec![]; |
| *case_states.last_mut().unwrap() = CaseState::Pattern; | ||
| } | ||
| "esac" => { | ||
| case_states.pop(); |
| // piece above stops at the first one it sees — so `$(case a in a) echo Y;; esac)` was cut | ||
| // short at `case a in a`, leaving the rest as a syntax error. Nested `case`s recurse. | ||
| rule case_block() -> () = | ||
| "case" (!("case" / "esac") [_])* (case_block() (!("case" / "esac") [_])*)* "esac" {} |
| $(command_piece()*) | ||
|
|
||
| pub(crate) rule command_piece() -> () = | ||
| case_block() / |
Performance Benchmark Report
Code Coverage Report: Only Changed Files listed
Minimum allowed coverage is Test Summary: bash-completion test suite
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
$(case ...)doesn't parse:bash, dash and ksh all print [Y].
A case pattern ends in
), and two places read that as the)that closes the substitution.consume_nested_constructin tokenizer.rs counts parens, so it stops at$(case a in a. Thecommand()rule in word.rs stops at the first)for the same reason.The tokenizer now tracks where it is in a case (case / in / ;; / esac), so a pattern's
)closes the pattern. The( pattern )form's leading(doesn't open anything either. In the word grammar I added a rule that consumes a wholecase ... esacbefore the generic one sees it; it recurses, so nested cases work.Checked against bash: both pattern forms, several arms, nested case, nested$(), subshells, backticks, $ (( )), and a quoted
). All 225 brush-parser tests pass.Fixes #1052