Skip to content

lexer: read / after a built-in variable as division - #143

Merged
sylvestre merged 2 commits into
uutils:mainfrom
jayhemnani9910:fix/division-after-special-variable
Oct 25, 2026
Merged

sylvestre merged 2 commits into
uutils:mainfrom
jayhemnani9910:fix/division-after-special-variable

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

Every built-in variable token (NR, NF, FNR, RSTART, ...) sets the lexer to expect an expression, so a / after one starts a regex. gawk 5.3.2 vs main:

$ printf 'a b\nc d\n' | awk '{ print NR / 2 }'
gawk: 0.5
      1
main: Error: Syntax error ... Unterminated regex

$ awk 'BEGIN { RSTART = 4; print RSTART / 2, RLENGTH / 1 }'
gawk: 2 0
main: 401

The second one parses as RSTART concatenated with the regex / 2, RLENGTH /, so it prints a wrong number with no error. x / 2 with a user variable already works.

The built-in variables are operands, so their tokens now use accept_operator, like identifiers. ARGIND had no callback and gets the same.

New test slash_after_special_variable_divides fails without the change. cargo test --workspace: 211 passed (210 on main plus the new one), clippy and fmt clean. 15 programs with / and /= after NR, NF, FNR, RSTART, RLENGTH and ARGC, and regexes after NR ~ and NR,, give the same result as gawk apart from two that differ for other reasons (ARGC's value, NR in an END-only program, which #140 fixes).

Copilot AI balanced review requested due to automatic review settings October 24, 2026 07:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

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.

Comment thread tests/by-util/test_awk.rs
fn slash_after_special_variable_divides() {
ucmd()
.arg("{ print NR / 2, NF/NR } END { RSTART = 8; print RSTART / 2 / 2 }")
.pipe_in("a b\nc d\n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also cover NR /= 2 here?
and maybe add NR/=1 to lexer_test_regex_ambiguity in lexer/src/tests.rs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both in f15885c: NR /= 2 in slash_after_special_variable_divides, and NR/=1 in lexer_test_regex_ambiguity (it fails on main, where /=1 starts a regex). The test sets NR first (NR = 6; NR /= 2), because on main NR in END after a file is still 0 (#140), and gawk keeps NR an integer, so NR /= 2 in a main rule truncates there.

@Alonely0

Copy link
Copy Markdown
Collaborator

sorry, needs to be rebased!

@Alonely0
Alonely0 force-pushed the fix/division-after-special-variable branch from 54cc70b to a2dd734 Compare October 24, 2026 16:45
Copilot AI balanced review requested due to automatic review settings October 24, 2026 16:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

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.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 18:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

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.

@sylvestre
sylvestre merged commit 1c24f87 into uutils:main Oct 25, 2026
15 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants