Skip to content

refactor(cmdline): declare flag grammars and bash reserved words once, as data - #4401

Open
KuSh wants to merge 1 commit into
lex/13-span-editsfrom
lex/14-grammar-tables
Open

KuSh wants to merge 1 commit into
lex/13-span-editsfrom
lex/14-grammar-tables

Conversation

@KuSh

@KuSh KuSh commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

One commit. The facts that several readers hand-coded about bash's reserved words move into one declared table.

  • core/cmdline/bash_grammar.rs lists bash 5.3's 22 reserved words with their role in a compound command and what may follow each. A test derives the roles from bash's parse.y.
  • The segmenter, read_grammar, the multi-line classifier and classify_command read it.

What the rewrite does with them:

  • rtk is never put in front of a reserved word, inside [[ … ]] or (( … )), in a case pattern, or inside word text (extglob groups, array literals, ${ }). until false; do git status; done keeps its until, and [[ -f a || ls ]] is left as written.
  • A ( glued to a word is text, as in a[(b)], but for name() and the in( that opens a case pattern, and the { of $${ opens nothing, since $$ is a variable.
  • time after | is the program time.
  • rtk discover splits a line where the rewrite does.

Test plan

  • cargo fmt --all --check && cargo clippy --all-targets && cargo test --all: 6151 passing, clippy clean
  • wincheck.sh (cfg(windows) type-check): to re-run on this head
  • Hook verdicts compared with the parent commit, and every allowed rewrite that differs run in bash next to the original line: to re-run on this head

Part of stack #4139. Based on #4400. Each commit gates green on its own, so the stack is bisectable.

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

♻️ Type refactor
🟡 Risk medium

Summary

Refactors flag parsing and bash reserved-word handling to declare them once as data tables instead of scattering logic across call sites. One commit converts git/golangci-lint/ast-grep/grep/rg/dotnet flag classification into declared Grammar tables checked by table tests; the other centralizes bash's 22 reserved words into a single table used by the segmenter, classifier, and multi-line logic.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh
KuSh force-pushed the lex/14-grammar-tables branch 2 times, most recently from 02ccced to acbff25 Compare October 5, 2026 22:36
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from acbff25 to cea1ffe Compare October 7, 2026 07:16
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch 2 times, most recently from 997f6b7 to b8a98a2 Compare October 9, 2026 16:27
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from b8a98a2 to 1923346 Compare October 9, 2026 20:29
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch 2 times, most recently from 6bb9f3e to e580491 Compare October 15, 2026 12:03
bharvey42 added a commit to bharvey42/rtk that referenced this pull request Oct 18, 2026
ChildCommand quotes every argument holding a glob character, so an MSYS
child no longer globifies it. That is right for a pattern and wrong for a
file operand: from PowerShell or cmd, `rtk grep x *.rs` and
`rtk find ./s* -name '*.rs'` reached the child as literal names and failed.

grep/rg and find now expand their own file operands, where their parsers
already know which arguments name files: grep/rg's path positionals and
find's starting points. Patterns, flags and flag values still reach the
child as written. core/operand_glob follows globify's rules as measured
against Git for Windows' ls, except that `\` separates components and an
operand naming an existing path is never expanded. On Unix operands pass
through, since the shell already expanded.

ls, wc and tree need a declared flag grammar before their operands can be
bounded, so they follow separately: ls on top of rtk-ai#4401, wc and tree with
grammars of their own.
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from e580491 to 4193e21 Compare October 19, 2026 16:38
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch 2 times, most recently from c21ba12 to 4f191ef Compare November 3, 2026 06:27
@KuSh
KuSh marked this pull request as draft November 4, 2026 07:41
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch 2 times, most recently from 07151b9 to 65e79e5 Compare November 5, 2026 12:12
@KuSh
KuSh marked this pull request as ready for review November 5, 2026 12:22
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from 65e79e5 to 5bca62d Compare November 17, 2026 01:42
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch 2 times, most recently from 9468a55 to f9105b6 Compare November 19, 2026 01:12
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from f9105b6 to 98a1e3e Compare November 21, 2026 09:57
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from 98a1e3e to 25837e6 Compare November 23, 2026 00:14
bharvey42 added a commit to bharvey42/rtk that referenced this pull request Nov 24, 2026
ChildCommand quotes every argument holding a glob character, so an MSYS
child no longer globifies it. That is right for a pattern and wrong for a
file operand: from PowerShell or cmd, `rtk grep x *.rs` and
`rtk find ./s* -name '*.rs'` reached the child as literal names and failed.

grep/rg and find now expand their own file operands, where their parsers
already know which arguments name files: grep/rg's path positionals and
find's starting points. Patterns, flags and flag values still reach the
child as written. core/operand_glob follows globify's rules as measured
against Git for Windows' ls, except that `\` separates components and an
operand naming an existing path is never expanded. On Unix operands pass
through, since the shell already expanded.

ls, wc and tree need a declared flag grammar before their operands can be
bounded, so they follow separately: ls on top of rtk-ai#4401, wc and tree with
grammars of their own.
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from 25837e6 to 8da6718 Compare November 25, 2026 02:13
bharvey42 added a commit to bharvey42/rtk that referenced this pull request Nov 25, 2026
ChildCommand quotes every argument holding a glob character, so an MSYS
child no longer globifies it. That is right for a pattern and wrong for a
file operand: from PowerShell or cmd, `rtk grep x *.rs` and
`rtk find ./s* -name '*.rs'` reached the child as literal names and failed.

grep/rg and find now expand their own file operands, where their parsers
already know which arguments name files: grep/rg's path positionals and
find's starting points. Patterns, flags and flag values still reach the
child as written. core/operand_glob follows globify's rules as measured
against Git for Windows' ls, except that `\` separates components and an
operand naming an existing path is never expanded. On Unix operands pass
through, since the shell already expanded.

ls, wc and tree need a declared flag grammar before their operands can be
bounded, so they follow separately: ls on top of rtk-ai#4401, wc and tree with
grammars of their own.
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from 8da6718 to edf1afe Compare November 27, 2026 22:44
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from edf1afe to 092eb08 Compare November 28, 2026 01:23
`core/cmdline/bash_grammar.rs` holds bash's reserved words as one table:
the 22 words bash 5.3 lists (`compgen -k`), each with where the word
after it stands (where a command starts, where only a reserved word
starts, as after `fi`, `done`, `esac`, `}` and `]]`, or a plain word, as
after `case` or `for`), whether that word may be a name after which only
a reserved word starts (`for`, `select`, `function`, `coproc`), the
options bash reads right after it (`time -p --`), and its role in the
compound command it belongs to: it opens one, continues it into its next
part, closes it, or prefixes a pipeline and belongs to none (`time`). A
test derives every role from the productions of bash's `parse.y`.
`reserved_word` matches a word by its exact spelling, so a quoted or
escaped spelling is an ordinary word, as bash reads it.

Every reader of a command line that needs one of these facts reads the
table:
- the segmenter's private `CaseTracker`, which follows `case`, `in` and
  `esac`;
- `read_grammar`, the reading of a line that the rewrite and
  classification share, which finds where a command or a reserved word
  starts, `[[ … ]]` expressions and `case` patterns from the positions,
  the roles and the options;
- the multi-line classifier in `discover/registry.rs`, which passes a
  block through when a line starts with `(`, `)` or a word that delimits a
  command list of a compound command: every word of `if`, `case`, the
  loops, `function` and `coproc` apart from `in`;
- `classify_command`, which ignores a segment that starts with grammar as
  `read_grammar` reads it: a reserved word, whole (`[[(-f a)]]` starts
  with `[[`), or the `((` of an arithmetic command. `((ls) )` is two
  subshells, not arithmetic. A segment after `|` is read where a
  pipeline's stage starts, so its `time` is the program `time`.

The rewrite follows these rules of bash's grammar:
- A segment that starts with a reserved word is grammar around commands,
  never a command, so the rewrite never puts `rtk` in front of one. Under
  a filter matching every command, `until false; do git status; done`,
  `if …; elif true; then …; fi`, `case x in x) ls;; esac`, `coproc ls`
  and `[[ -f x ]]` keep their reserved words as written: `rtk until …`
  and `rtk esac` do not parse.
- After a compound command's last word (`fi`, `done`, `esac`, `}`, `]]`,
  a subshell's `)`, an arithmetic command's `))`), after the name that
  follows `for`, `select`, `function` or `coproc`, and after `name ( )`,
  bash reads a reserved word and no command. So `if (true) then`,
  `for x do` and `while (( 0 )) do` open their lists, the `[[ … ]]` or
  `(( … ))` behind them is an expression, and
  `for x do [[ -f a || ls ]]; done` is left as written, where
  `… || rtk ls ]]` does not parse. `case x in a) (ls) esac` closes the
  `case`. A `(` after a word that opens no word text (below) is a
  function's `name ( )`, not a subshell.
- `time` is reserved only where a pipeline starts. After `|`, `|&` or
  `coproc` it is the program `time`, as bash's `time_command_acceptable`
  decides, so `ls | time [[ -f a || ls ]]` runs `time` with `[[ -f a`,
  then `ls ]]`, and becomes `ls | time [[ -f a || rtk ls ]]`.
  `ReservedWord::pipeline_follows` names the words after which a pipeline
  may start.
- After `time`, bash reads `-p` and then `--`, each optional, as part of
  the reserved word (`time -p`, `time --`, `time -p --`), and the word
  after them is in command position. Any other word there is a command's
  name, `-p` after `--` or after another `-p` included. So
  `time -p [[ -f a || -d b ]]` is one expression, left as written, where
  `time -p [[ -f a || rtk -d b ]]` does not parse. Bash compares these
  options as written, so the reader matches them literally: rule 6 of
  `.claude/rules/rust-patterns.md` names reserved-word grammar as the one
  exception to parsing through `arg_tokenizer`.
- Inside `[[ … ]]` nothing is a command: `&&`/`||` there are expression
  operators, and so are its brackets and a regex's `|`. The expression
  runs from a `[[` where a reserved word starts to the first `]]` outside
  its brackets and its word text, or to the end of the line.
  `[[ -f a || ls ]]` is left as written, since `[[ -f a || rtk ls ]]` does
  not parse, and under a filter matching every command
  `[[ -f a && -d b ]]` keeps its `-d b`.
- An arithmetic command runs no command. A `((` where a reserved word
  starts, or right after `for`, is arithmetic when the `)` that closes its
  second `(` is followed at once by another `)`, as bash's `parse_dparen`
  decides, and everything up to that `))` is its expression: `&&`, `||`,
  `;`, `<` and brackets there end nothing. Under a filter matching every
  command, `(( a || b ))` is left as written, where
  `(( rtk a || rtk b ))` is an arithmetic syntax error. Otherwise the
  two brackets are subshells, one inside the other (`((ls) )`), whose
  commands are rewritten. With no `)` that closes the second `(`, bash
  reports a syntax error, and the rest of the line is read as the
  expression, so nothing in it is rewritten. `$((` inside a word is an
  arithmetic expansion and opens nothing. After `for (( … ))`, `do` is a
  reserved word.
- A `case` pattern is never a command: after `in`, and after each `;;`,
  `;&` or `;;&`, the words up to the pattern's `)` outside brackets and
  word text are patterns (an optional leading `(`, alternatives joined by
  `|`), and the arm's commands start after that `)`. `esac` closes the
  `case` only where a pattern or a reserved word would start. In
  `case $x in a) echo hi ;; ls) ls ;; esac` only the arm's `ls` becomes
  `rtk ls`, and the argument of `echo esac` leaves the `case` open, so a
  `(ls)` pattern after it is not read as a subshell.
- Word text is never a command. An extglob group (a `(` right after a
  bare `!`, `@`, `*`, `?` or `+` in a word, glued to it or starting it),
  an array literal (`NAME=(…)` or `NAME+=(…)`, as in
  `declare -a a=(…)`) and a `${ }` are text of one word, from the bracket
  that opens them to the one that closes them; a `${ }` ends at its first
  `}`, as bash's `parse_matched_pair` reads it. A `(` glued to any other word
  is text the same way (`a[(b)]`), but for `name()` and for the `in(` that opens
  a `case` pattern, and the `{` of `$${` opens nothing, since `$$` is a variable.
  This holds in a command,
  in a `case` pattern and in a `[[ … ]]` expression alike: nothing inside
  one is rewritten or ends the command, the pattern or the expression, and
  in a command `words` splits no word on a blank inside one. So
  `x=(git status)`, `git !(ls)` and `ls ?((ls))` keep their text, under a
  filter matching every command `arr=(a)` keeps its element,
  `case $x in ${y:-a)b}) ls;; esac` becomes
  `case $x in ${y:-a)b}) rtk ls;; esac`, and `[[ ${x- ]] } == a || ls ]]`
  is left as written. Bash reads an extglob group as one word with
  `extglob` on; with it off the line is a syntax error, and the text stays
  as written. Word text's `|` and brackets still count for the rule that
  leaves a line piping and grouping commands as written, as the permission
  gate's segmenter reads them, so `ls @(a|b) && git status` stays as
  written.
- `$( )`, `<( )` and `>( )` hold a command list of their own, which
  `read_grammar` reads to the `)` that ends it, past a `)` that a `case`
  pattern or a subshell inside it takes, so `echo $(case y in b) :;; esac)`
  leaves the reader in step. Each token of a substitution takes the
  reading around it.
- A line that pipes and groups commands is left as written, and only
  command text counts: a `case` pattern's `|` and brackets and a `[[ ]]`
  regex's do not, so `[[ $y =~ ^(a|b)$ ]] && git status` and
  `case $y in a|b) git status;; esac` are rewritten.

The permission gate keeps its own reading: `segment()` splits inside
`[[ … ]]` and `(( … ))` and after a pattern's `)`, so a segment it
checks may run nothing, and every command that runs reaches its rules.
It is `segment()`'s one policy.

`rtk discover` splits a line where the rewrite does: `split_for_classify`
reads `read_grammar`'s readings and splits only at an operator, a pipe,
`&` or a subshell's bracket read as command text. A `[[ … ]]` expression
or an arithmetic command is one segment, and a `case` pattern is in
none, so `[[ -f a || ls ]]`, `(( a || ls ))` and
`case $x in ls) echo 1;; esac` report no `ls` that the rewrite leaves
alone, while `ls && git status` is still two commands. A segment that
starts with a reserved word counts as ignored, not as an unsupported
command named after its first words (`until false`, `time cargo`).
After `|`, `time cargo test` runs the program `time`, and stays an
unsupported command.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KuSh
KuSh force-pushed the lex/14-grammar-tables branch from 092eb08 to b46205f Compare November 29, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant