Repository navigation
feat(grep): add -P for Perl regexes, rejecting unsupported constructs - #219
Conversation
bbe2a79 to
76c66c7
Compare
76c66c7 to
2cbae54
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Verdict: two small fixes before merge. The design holds up. I ran ~50 differential probes against GNU grep 3.8 -P, and nearly every divergence is a loud exit-2 refusal, as the PR promises. I found one silent wrong answer.
- 🟡
\vreaches the crate unchanged, so it matches VT only.grep -cP 'a\vb'misses\f/\rlines (1 vs GNU's 3, exit 0). Fix is inline and I've tested it. - 🟡 The refusal message gives an agent no way forward.
\Kand lookbehind are the most common reasons agents reach for-P, and both hit this message. Suggested wording is inline. - Docs follow-up: harness-sdk
user-guide/shell/commands.mdx(General callouts) still says "grep -Pis unsupported". - Question (non-blocking): did you consider
fancy-regex? It's pure Rust, works on&[u8], and supports\K, lookaround and backrefs. It would shrink the translator, though class operators and the literal{would still need translating. The current surface would let us swap it in later, so this doesn't block. A line on why it was ruled out would help the next reviewer.
The full adversarial pass timed out twice. A focused 15-probe adversarial pass plus my own probes cover it, so treat that pass as partial.
✅ What I verified
- Head
2cbae54.cargo test --test shell_integration grep: 103 passed. - Applied the
\vsuggestion in a scratch copy.a\vb,a[\v]banda[^\v]bgive 3, 3 and 1, the same as GNU. The 103 tests still pass. - A 37-pattern differential harness gave 22 identical and 15 differing. Of the 15, 13 are loud refusals and 2 are listed below. A focused probe set (
\p{L},[a-],[-a],[^]a],[[:^alpha:]],a{2,3}?,-i é/É, unclosed(?,\x{41) found no panics and no new silent mismatches.
Appendix: non-blocking (8)
- These fail loudly where GNU accepts them, with a raw crate error rather than "-P does not support":
\Q…\E,\h,\Z,\N,\o{…},(?#…),a{,2}.\Q…\Eand\hwould be cheap to translate.\K, lookaround, backrefs, possessive quantifiers and(?x)are refused by design. a**anda*?+are accepted and matched; GNU exits 2 with "quantifier does not follow a repeatable item" (grep.rs:311-321). Only a typo produces these.(?-x)aand(?i-x)Aare refused as(?x)(grep.rs:299). The check should fire only whenxcomes before any-.\101(octalAin PCRE) is reported as "backreference" (grep.rs:210).- The help text (
grep.rs:19-20) leaves out\Kand(?x), which are both refused.\Kis the one agents will try. - With several
-epatterns,(?i)in one leaks into the next because of the bare|join (grep.rs:175):-P -e '(?i)a' -e Bmatchesb. GNU refuses multiple-ewith-P, so this only bites because the PR accepts them. Wrapping each pattern in(?:…)fixes it. Same behaviour on-E. - Pre-existing, also on main:
-wwraps in\b, while GNU uses non-word lookaround, soecho '@b x' | grep -owP '@b'prints nothing here.-oprints a blank line for each empty match (grep.rs:566-578;grep -oP 'b*'onabc).grep -E 'a('exits 1, where-Pexits 2. - Not a finding: piping host stdin into
strands-shell -c "grep …"givesbad fd 0. This is a documented CLI limitation, and plaingrepandwcfail the same way.
Reading order
grep.rs:190-353 (translator), then :355-372, then :649-661 (wiring/exit 2), then the tests in tests/shell_integration.rs.
|
I checked For the optional findings, I left the extra unsupported escapes, disabled- The |
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. Both 🟡 findings are fixed in 4a85886 and I didn't find any new problems.
- ✅
\v:a\vb,a[\v]b,a[^\v]b,a[x\v]b,a\v+band escapeda\\vball match GNU grep 3.8 (count and exit code). - ✅ The refusal message now points at
-o … | sed/cut.--helplists\Kand(?x). - Agreed on keeping
fancy-regexfor a separate change, since the execution limits and runtime errors it brings need their own design. Docs follow-up in harness-sdk is still open.
Verified / one nit
- Head
4a85886.cargo test --test shell_integration grep: 104 passed, including the newgrep_perl_vertical_whitespace. - ⚪
[\v-z]now matches-andz(the crate reads[[…]-z]as a nested class plus literals), while GNU exits 2 with "invalid range". It's a malformed pattern, so it falls in the same bucket you've already decided to leave alone. No action needed.
Description
Agents reach for
grep -Pall the time, but we rejected it outright:This adds
-P/--perl-regexp. It uses the Rust regex engine, so PCRE-only features like lookaround, backreferences, atomic groups, possessive quantifiers and(?x)fail loudly with exit 2 instead of mis-matching. A few PCRE literals the engine would otherwise read as syntax (\<,\>, and&&/~~inside a class) are kept literal, and-Pwith-Eor-Fis an error, like in GNU grep. It works with-i,-w,-x,-oand the rest.I compared it against GNU grep 3.8 for
\d,\s,\b,\t, lazy quantifiers,(?i), named groups and the flag combinations, and the output matches. There are two differences I know of: we accept several-epatterns with-P, where GNU 3.8 refuses them, and\dalso matches non-ASCII digits. Separately, BRE patterns still get ERE semantics. That's a different issue and I didn't touch it here.Related Issues
Part of #207
Documentation PR
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.