Repository navigation
Conversation
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.
|
GNU awk testsuite comparison: |
1 similar comment
|
GNU awk testsuite comparison: |
| Record, | ||
| #[regex(r"\n+", accept_expression)] | ||
| // Blank and comment-only lines after a newline belong to the same token. | ||
| #[regex(r"\n(?:(?&ignore)|\n|(?&comment))*", accept_expression)] |
There was a problem hiding this comment.
the upstream comparison shows getnr2tm as a new failure
could you please check whether this change explains it?
There was a problem hiding this comment.
Yes, this change caused it, thanks. Line 52 of getnr2tm.awk is a comment holding a byte that is not UTF-8 (0xC8). The old skip_line callback skipped raw bytes, but the new #[^\n]* subpattern only matches UTF-8 characters. So the newline token that now takes in the following comment lines stopped in the middle of line 52, and the 0xC8 byte became an unexpected token.
Fixed in 9cdcaa1: the subpattern is now #(?-u:[^\n])*, which matches bytes. getnr2tm passes again locally against the gawk 5.3.2 testsuite, and fldterm, leaddig and sortglos still pass. I also added lexer_test_comments_may_hold_non_utf8_bytes, which fails on the previous commit.
| #[logos(subpattern identifier = r"[a-zA-Z_][a-zA-Z0-9_]*")] | ||
| #[logos(subpattern ignore = r"(?:[ \t\r]|(\\\r?\n))+")] | ||
| #[logos(subpattern ignore_with_nl = r"(?:(?&ignore)|\n)*")] | ||
| #[logos(subpattern comment = r"#[^\n]*")] |
There was a problem hiding this comment.
# comments are now described in two places, this subpattern and skip("#", skip_line) above
could they share one?
There was a problem hiding this comment.
Done in 9cdcaa1. skip_line is gone and the skip is now #[logos(skip(r"(?&comment)", allow_greedy = true))], so the subpattern is the only definition of a comment. logos needs allow_greedy for the [^\n]*, but that only reads to the end of the line, which is the same as what skip_line did with memchr.
| } | ||
|
|
||
| #[test] | ||
| fn comment_lines_between_statements() { |
There was a problem hiding this comment.
please split this into separate tests (like the ones in the issue), with a loop a failure doesn't say which program broke
There was a problem hiding this comment.
Done in 9cdcaa1. There is now one test per program, for example comment_line_after_if_condition, comment_line_between_arguments and hash_in_string_is_not_a_comment.
|
thanks for working on this! |
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.
|
GNU awk testsuite comparison: |
|
nice: |
| #[logos(skip(r"(?&ignore)"))] | ||
| #[logos(skip("#", skip_line))] | ||
| // `[^\n]*` stops at the end of the line, not of the input. | ||
| #[logos(skip(r"(?&comment)", allow_greedy = true))] |
There was a problem hiding this comment.
allow_greedy hurts perf considerably, since the lexer must go over the whole input to get to an accepting state for each token. The way to do this usually is a callback that performs the lookbehind or greedy checks (like we do with memchr to quickly skip to a delimiter) and bumps the input, so the DFA doesn't scan the (considerably long) strings/comments/etc. This essentially keeps the DFA as a fast path.
What I propose instead is to change skip_line in the original # skip rule to something like this funtion, and then wire through the parser the newline ignoring for all tokens that currently hacked this through ignore_with_nl at the end; i.e., else, do, ||, &&, {, ,, ?, and :. This function only preserves the newlines on lone comments, where they are significant, and we would rely on the parser to ignore newlines instead of those tokens consuming them.
fn skip_comment(lex: &mut Lexer) -> Skip {
let prev = &lex.source()[..lex.span().start];
let is_line_comment = prev
.iter()
.rfind(|b| !matches!(b, b' ' | b'\t' | b'\r'))
.is_none_or(|&b| b == b'\n');
lex.bump(memchr(b'\n', lex.remainder()).unwrap_or(lex.remainder().len()));
if is_line_comment && let [b'\n', ..] = lex.remainder() {
lex.bump(1);
}
logos::skip(lex)
}A comment line between two statements, after if (...), or after a , or
{ left an extra Newline token the parser did not expect. Let a Newline
token take the blank and comment-only lines that follow it, and let the
tokens that skip newlines skip comment lines too. Fixes uutils#164.
…es in them Split the comment tests into one test per program.
9cdcaa1 to
17404b0
Compare
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.
|
GNU awk testsuite comparison: |
Comments are skipped by a memchr callback again instead of a greedy
pattern. A comment alone on its line also takes its newline, so comment
lines add no newline token.
else, do, &&, ||, {, ',', ? and : no longer take the newlines after
them; the parser's token stream drops the newlines after them and after
another newline. POSIX mode still refuses a newline after ? and :.
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.
|
@Alonely0 thanks, done in d000c63. I built it on top of @sylvestre's rebase.
To check for regressions without the GNU harness (gawk doesn't configure here), I ran all 608
On #214: this doesn't cover the |
|
GNU awk testsuite comparison: |
Alonely0
left a comment
There was a problem hiding this comment.
Looks good! Tho notice that both ternary operators ? and : mustn't accept newlines after them in POSIX mode; this is a GNU extension (the lexer had different rules for them depending on mode). Other than that, I don't see any issues, tho will give it a closer look later today.
|
@Alonely0 thanks! That one should still hold. The lexer refuses it before the parser sees the newline:
One caveat, the same as on main: |
Alonely0
left a comment
There was a problem hiding this comment.
Nice! I mentioned a small clean-up opportunity, but this looks great. Since it came up here, I will be adding some documentation to the lexer to explain the intricacies and footguns in its implementation, so new contributors have an easier time making changes.
| Token::Newline | ||
| | Token::Else | ||
| | Token::Do | ||
| | Token::BooleanAnd | ||
| | Token::BooleanOr | ||
| | Token::OpenBrace | ||
| | Token::Comma | ||
| | Token::QuestionMark | ||
| | Token::Colon, |
There was a problem hiding this comment.
this reads a bit ugly to me. Do you think it'd be cleaner to move it to a method in TokenExt? Like the existing is_*
Oh, you're totally right, I hadn't seen the helper method for it, sorry hahah (by the way, this reads a bit LLMy. Just FYI, but it is our policy that replies to maintainers should be written by humans, mainly for two reasons: contributors should be able to explain the code without their assistance, and LLM prose is just tiring to read. It is okay if your English is not perfect, mine isn't either :) ) |
Fixes #164.
A
#comment on a line of its own broke the statement around it when it sat between two statements, afterif (...), after{, or inside an argument list:The lexer skipped the comment text, but the newlines on either side of it came out as separate
Newlinetokens. The tokens that skip newlines ({,,,&&,||,else,do, ...) only skipped the first one. The parser then found a secondNewlinewhere it expected a statement and reported "invalid statement start".The change is in the lexer only:
commentsubpattern (#[^\n]*) is part ofignore_with_nl, so the tokens that skip newlines also skip comment lines.Newlinetoken takes the blank and comment-only lines that follow it (\n(?:(?&ignore)|\n|(?&comment))*instead of\n+). Runs of newlines separated by spaces now also come out as one token rather than several, which the parser already treats the same way. I updated the expected token lists inlexer_test_newlines_non_posixandlexer_test_collapsible_delimitersfor that, and addedlexer_test_comment_lines_join_newlines.#inside strings and regexes is unaffected, since those are lexed as their own tokens.I compared 24 programs with gawk and they all match. They cover comment lines between statements, at the top of a block, after
if (...),for (...)andwhile (...), beforeelse, inside an argument list, after&&, mixed with blank lines, at the top and bottom of the file, a trailing comment, and#in strings and regexes. A comment after a\line continuation is still an error, as in gawk. Two things I noticed are left alone here because they fail the same way without comments:do ... whiledoesn't parse yet, andprintf/for (k in a)aren't implemented.The new test
comment_lines_between_statementsfails on main and passes with this change.cargo test --workspacepasses (275 tests), andcargo fmtandcargo clippy --workspace --all-targets -- -D warningsare clean.I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.