Repository navigation
fix(reader): end grouped gather at a stray close inside an open group (niubash#482) - #955
Conversation
… (niubash#482)
GNU's reader reports `syntax error near unexpected token `)'' the moment
a `)` that closes no `(` and is not a case-pattern terminator is read
inside an open compound — yyerror + jump_to_top_level abandons the whole
group and the interactive shell returns to PS1 (parse.y yyerror /
print_offending_line 6813-6824; brace_group grammar parse.y:1196).
The completeness readers had no such terminal condition: a stray `)`
inside an unclosed `{`/function body kept the group open forever, so
run_interactive_stdin / run_stdin_script / read_next_source_group wedged
in PS2 with no diagnostic, and the niubash#431 stray-close guard only
armed on input already judged complete.
- Lexer stamps `Token::case_pattern_close` on the `)` that leaves
PST_CASEPAT (scanner.rs record_token, parse.y:3787-3788): it is the one
legal `)` that pops no paren.
- stdin_source_needs_more_posix and GroupScanFeeder::fold_token now end
the group on an unclosable `)` and on `;;`/`;&`/`;;;&` outside
case/arithmetic — the "complete" verdict hands the offending text to
the armed parse, which owns the diagnostic.
- unclosed_brace_eof_node/unclosed_paren_eof_node run the existing
first_mismatched_closer scan before the EOF node, so the abandoned
group reports `unexpected token `)'' at the stray line (GNU) instead
of `unexpected end of file from '{''.
- first_mismatched_closer honors the new flag so `)` inside a case BODY
is recognized as stray while pattern closers stay legal.
Verified vs Git Bash GNU 5.2.37 script-file probes (WSL forbidden by
task): `{ echo c\n)` / `f() {\n)` / `case x in\na) e\n)` all print
`line 2/3: syntax error near unexpected token `)'' + verbatim echo,
rc 2, session recovers and runs the next line — byte-matching output.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Review report (pre-merge verification, all green)Reviewed as the engine half of the paired niubash#482 fix. Merged as 95700a5 ( GNU conformance (oracle: Git Bash GNU bash 5.2.37, script-file / Red/green discriminative check (this machine):
No regressions: targeted suites on the branch and on a local merge of Pairing: niubash#484 (host half) verified in combination locally — its 8/8 One environment note for the record: the winuxcmd-bundled GNU bash 5.3.0 (msys) swallows a stray |
…#484) * fix(repl): terminate the gather on a stray close inside an open group (niubash#482) `{ echo c` opens a brace group; the next line `)` closes nothing — GNU reports `syntax error near unexpected token `)'' at that line, abandons the whole group, rc 2, and the session returns to PS1. Niubash's input completeness scanner had no such terminal condition: the open `{` kept the buffer "incomplete" forever, so the REPL (reedline, --noediting and piped `-i` alike) wedged in PS2 with no diagnostic. The niubash#431 stray-close guard only audits COMPLETE groups, which this input never became. - scan_repl_input now emits `;;`/`;&`/`;;;&` as single operators (they are case-clause terminators, not two `;`). - is_repl_input_complete tracks an open `case` by phase (operand / in / pattern / body): `)` and `;;`-family that cannot close anything are judged complete so the armed engine parse reports GNU's diagnostic instead of gathering forever; `a)`/`(a|b)`/`;;` inside case stay legal so real multi-line cases still gather. - Foreign keyword closers (`fi`/`done`/`esac`/`}`) mismatched under a keyword opener end the gather the same way — GNU's read-time verdict; under `(`/case frames they remain legal words (arithmetic, patterns). - Engine side (unixwin/rubash#955): the group gather stops at the stray token and the unclosed-`{`/`(` EOF paths report the mismatched closer at its line instead of `unexpected end of file`. GNU 5.2.37 script-file probes (WSL forbidden by task): `niu -i`, stdin, `-s` and `-c` transcripts all byte-match GNU for the ticket family. Tests: tests/issue482_stray_close_gather.rs — 8 cases covering the ticket repro, PS1 recovery + rc=2, mid-command/case-body strays and a normal-group battery; `cargo test -p niubash-runtime --lib repl::` 68/68. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * chore(deps): add sha2 to Cargo.lock (rubash master dep shift) Same one-line fix as 9f4621a on master: rubash master gained a sha2 dependency (#939 provenance work); this branch's lock was refreshed against a pre-sha2 checkout. Unblocks the 8 --locked CI jobs. --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Summary
Fixes unixwin/niubash#482 (engine half): a stray
)inside an OPEN multi-line grouped command made every gather loop unbounded — the reader waited forever for the unclosed opener, soniu -iand the REPL sat in PS2 with no diagnostic. GNU abandons the group at the token:syntax error near unexpected token ')', rc 2, PS1 recovery.The niubash#431 stray-close guard only audited input already judged complete; an unresolved opener meant the parser never saw the
). Root cause: the completeness readers had no terminal condition for an unclosable close.Token::case_pattern_close: lexer stamps the)that ends a case pattern list (PST_CASEPAT,parse.y:3787-3788) — the one legal)that pops no paren.stdin_source_needs_more_posix+GroupScanFeeder::fold_token: an unclosable)(and;;-family outside case/arith, and foreign keyword closers under keyword openers) ends the group — the armed parse then owns the diagnostic.unclosed_brace_eof_node/unclosed_paren_eof_noderunfirst_mismatched_closerbefore the EOF node, so the abandoned group reportsunexpected token ')'at the stray line (GNUprint_offending_line/yyerror, parse.y:6813-6824) instead ofunexpected end of file from '{'.first_mismatched_closerhonors the flag:)inside a case BODY is stray; pattern closers stay legal.Refs unixwin/niubash#482, unixwin/niubash#431.
Test plan
{ echo c\n)stdin/-s/-c/run_interactive_stdin:line 2: syntax error near unexpected token ')'+ verbatim echo, rc 2 — byte-matches GNU 5.2.37 script-file probes (WSL forbidden by task)f() {\n),{ echo c\necho a),case x in\na) e\n)→ stray-token diagnostic at the right line, session recovers{ },( ),caseincl.(a|b),f() {},for ((;;)),a=(1 2\n),$()— all still gather and runtests/issue482_stray_close_gather.rs(8 tests) green on the patched build