Repository navigation
grep: name the -f file and line of a pattern that fails to compile - #189
sarankumar1325 wants to merge 3 commits into
Conversation
GNU grep reports a syntax error in a pattern read with -f FILE as
"FILE:LINE: message" ("-:LINE:" for -f -), so the user can tell which
line of the pattern file to fix. Patterns from -e or the command line and
errors from the PCRE backend keep the plain message.
Fixes uutils#179
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
| for raw in config.patterns { | ||
| patterns.push(CompiledPattern::compile(raw, config)?); | ||
| for (index, raw) in config.patterns.iter().enumerate() { | ||
| let origin = config.pattern_origins.get(index).copied().flatten(); |
There was a problem hiding this comment.
.get(index)...flatten() hides a length mismatch between the two vecs
could we zip them, or keep (pattern, origin) together in one vec?
| // Like GNU grep, a regex syntax error in a pattern read with `-f FILE` | ||
| // names the file and line (`FILE:LINE: message`). GNU's PCRE backend | ||
| // reports its errors without this location. | ||
| let location = match origin { |
There was a problem hiding this comment.
this formats a string for every pattern, even the ones that compile
could you please pass origin to compile_with and format only in the error path?
| // GNU grep prefixes a syntax error in a pattern read with `-f FILE` with | ||
| // `FILE:LINE: `, so the user knows which line of the file to fix. | ||
| let (scene, mut c) = ucmd(); | ||
| scene.fixtures.write("pats", "foo\n[z-a]\n"); |
There was a problem hiding this comment.
please add an empty line and a duplicate to the fixture, so we check they are counted too
…on error Store patterns as (text, origin) pairs so the two can't drift apart, pass the origin to compile_with and build the FILE:LINE prefix only when a pattern fails to compile. Add an empty line and a duplicate to the test fixture so they are counted as lines, and ignore the stdin write error in cases where grep exits before reading its input.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Errors returned through uucore are prefixed with the binary name as invoked, which is grep.exe on Windows, so the exact stderr checks in pattern_file_compile_error_names_file_and_line failed there. Build the expected message with the platform's name.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Fixes #179.
When a pattern read with
-f FILEfails to compile, GNU grep names the file and line it came from (grep: pats:2: Invalid range end, and-:2:for-f -).uu_grepprinted only the message, so with a long pattern file you couldn't tell which line was wrong.Change
uumainrecords where each pattern came from (-ffile and 1-based line, or nothing for-eand positional patterns) in a newConfig::pattern_originslist, kept in step withpatterns.CompiledPattern::compileputsFILE:LINE:in front of the regex compile error when the pattern came from a file.-eor positional patterns, for the[:space:]check (GNU's dfa reports that one without a location), or for-Perrors (GNU's PCRE backend doesn't add one either).Compared with GNU grep 3.11 (
LC_ALL=C):The wording of other Oniguruma errors is still different from GNU's (that's #185), but they now carry the same location prefix.
Tests
New test
pattern_file_compile_error_names_file_and_linecovers-f FILE,-f -, line numbers counted per file with-E, and no prefix for-eor-P. It fails onmainand passes with this change.cargo test,cargo fmt --checkandcargo clippy --all-targets -- -D warningspass.I wrote this change with help from an AI assistant. I reviewed and tested it myself.