Skip to content

parser: accept a single statement body and newlines before while in do-while - #214

Open
Harp404 wants to merge 3 commits into
uutils:mainfrom
Harp404:fix-do-while
Open

Harp404 wants to merge 3 commits into
uutils:mainfrom
Harp404:fix-do-while

Conversation

@Harp404

@Harp404 Harp404 commented Dec 1, 2026

Copy link
Copy Markdown

do only accepted a braced body that was directly followed by while, so two forms that POSIX allows (and gawk and mawk run) were syntax errors:

$ awk 'BEGIN { i = 0; do i++; while (i < 3); print i }'
Error: Syntax error
 1 │ BEGIN { i = 0; do i++; while (i < 3); print i }
   │                   ╰── Expected opening brace `{`.

$ awk 'BEGIN {
    do {
        i++
    }
    while (i < 3)
    print i
}'
Error: Syntax error
 3 │     }
   │      ╰── Expected a while statement after a do block.

In the POSIX grammar, the body of do is a statement like any other loop body (Do newline_opt terminated_statement / a braced action), and newline_opt is allowed before While.

The change, in parser/src/lib.rs:

  • The do body is parsed with parse_statement_body, as while and for already do, so a single terminated statement (do i++;, do ;, do print "x" on its own line) works.
  • Newlines after the body are skipped before expecting while. The body is always terminated at that point (}, ; or a newline), so this can't join two statements.

do i++ while (...) (no terminator) is still a syntax error, the same as in gawk.

I checked 10 do shapes (single statement, braced, newlines around the body, empty body, break/continue inside) against gawk: the output matches for all of them. The 4 new tests in tests/by-util/test_awk.rs fail on main and pass with this change. cargo test --workspace passes, and cargo fmt and cargo clippy --workspace --all-targets -- -D warnings are clean.

I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:42

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. 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.

@Alonely0 Alonely0 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am pretty sure this would be subsumed into your other PR where I proposed to bail on these tokens eating the newlines.

@Harp404

Harp404 commented Dec 1, 2026

Copy link
Copy Markdown
Author

@Alonely0 I checked this against the reworked #209 (d000c63), which now drops the newlines in the parser as you proposed, and all four tests here still fail:

  • do i++; while (i < 3) and do with the body on its own line fail with "Expected opening brace": do only takes a braced body (parse_body).
  • do { ... } followed by while on the next line fails with "Expected a while statement after a do block". The newline comes after }, and that's not one of the tokens whose newlines are dropped.

So this PR is still needed for those: parse_statement_body for the body, then skipping the newlines before while. Once #209 is merged, I can make the newline skip use whatever #209 ends up with.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 17:33

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. 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.

Comment thread tests/by-util/test_awk.rs
.arg("BEGIN { do print \"once\"; while (0) }")
.succeeds()
.stdout_only("once\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 add a test that do i++ while (i < 3) still fails? (gawk rejects it too)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added in 5c6db9c: do_while_unterminated_body_fails checks that BEGIN { do i++ while (i < 3); print i } fails with no output. gawk 5.3 gives a syntax error at while for it too. The other do_while tests still pass.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 02:28

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. 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.

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