Skip to content

lexer: build a working scanner with flex 2.6.16 and later - #1473

Merged
thefallentree merged 2 commits into
fluffos:masterfrom
gesslar:fix/flex-2.7
Nov 23, 2026
Merged

thefallentree merged 2 commits into
fluffos:masterfrom
gesslar:fix/flex-2.7

Conversation

@gesslar

@gesslar gesslar commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

macOS CI has been failing on master: every macOS runner image since 20261111 ships Homebrew flex 2.6.16+, CI regenerates the lexer with it, and the resulting driver can't load the testsuite's simul_efun. A function-like macro call loses the character after its ) to a stray (, so return to_string(value); in /std/json.lpc compiles as return ("" + (value)) (.

Two flex changes are involved; the fix is all in lexer.l and works with stock flex from 2.6.4 through 2.7.2. The pin stays at 2.6.4.

Held character moved (flex 2.6.16, "end yytext at the token after input()"). The NUL that terminates yytext now stays at the end of the token, recorded in a new yy_hold_ptr, while yyinput() advances yy_c_buf_p past it. lpc_lex_push_prepared_buffer() flushed the held character at yy_c_buf_p, so after the macro-argument collector's raw reads it overwrote the next unread byte. LPC_YY_HOLD_PTR() picks the right field by the generated scanner's version macros; the push and lpc_lex_buffer_extents() (which lpc_lex_peek() and the diagnostic snippets read) use it.

NUL jam no longer backs up under never-interactive (flex bug since 2.6.5, westes/flex#885, fix pending in westes/flex#886). When a NUL has no transition, the skeleton should back up to the last accepting position before yy_find_action. In cpp-flex.skl/c99-flex.skl that code is guarded by M4_NOT_MODE_INTERACTIVE, but flex only defines M4_MODE_NO_INTERACTIVE, so it is never emitted. The token before a NUL swallows it and the embedded-NUL rule never fires: embedded_nul_hang.lpc's NUL-in-comment, NUL-in-dead-#if and NUL-after-identifier cases compiled cleanly. Reproduced with a three-rule standalone scanner, where never-interactive alone triggers it. The fix drops %option never-interactive: every buffer the lexer scans is in memory and already non-interactive, so the option only selected that code path. The one buffer created from yyin is pinned non-interactive so a terminal on stdin can't change that.

The committed lexer.autogen.cc is regenerated with stock flex 2.6.4 built from the release tarball (distro-patched 2.6.4 builds emit different tables).

Testing

  • Stock flex 2.6.4: GTest 496/496, LPC suite clean.
  • Stock flex 2.7.2: GTest 496/496, LPC suite clean (failed to boot before; embedded_nul_hang.lpc failed with only the first fix).
  • ASan/UBSan RelWithDebInfo with flex 2.7.2: GTest and LPC suite clean.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T17:27:55.444658Z 3bb9e90 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

macOS CI regenerates the lexer with Homebrew's flex, which moved past
2.6.4, and every test that boots the driver failed: a function-like macro
call lost the character after its ')' to a stray '(' (`to_string(value);`
in /std/json.lpc compiled as `("" + (value)) (`), so the testsuite's
simul_efun could not load.

Two flex changes are involved.

flex 2.6.16 ("end yytext at the token after input()") keeps the NUL that
terminates yytext at the end of the token, in a new yy_hold_ptr, while
yyinput() advances yy_c_buf_p past it. lpc_lex_push_prepared_buffer()
flushed the held character at yy_c_buf_p, so after the macro collector's
raw reads it overwrote the next unread byte. It, and the held character
lpc_lex_buffer_extents() reports, now use yy_hold_ptr on 2.6.16+ and
yy_c_buf_p before.

Since flex 2.6.5 (westes/flex#885), a never-interactive scanner no longer backs up
to the last accepting position when a NUL has no transition: the skeleton
guards that code with M4_NOT_MODE_INTERACTIVE, which flex never defines.
The token before a NUL swallowed it, and embedded_nul_hang.lpc's NUL in a
comment, a dead #if branch or after an identifier compiled cleanly. Drop
%option never-interactive: every buffer is in memory and non-interactive
already, so it only selected that code path. The one buffer created from
yyin is pinned non-interactive so a terminal on stdin cannot change that.

The committed scanner is still generated by stock flex 2.6.4 (the pin).
Verified with stock flex 2.6.4 and 2.7.2: GTest and the LPC suite pass on
both, and on an ASan/UBSan build with 2.7.2.
Since flex 2.6.5, YYLMAX is substituted into the skeleton at generation
time from `%option yylmax` (default BUFSIZ), so lexer.l's
`#define YYLMAX 65536` no longer reaches the scanner. The token cap fell
to 8K on glibc and to 1K on macOS, where a 5000-byte string literal
aborted the compile (LexerTest.String_TooLong_Error and
TemplateLiteral_OverflowDuringMiddleFragment_DoesNotLeakNesting on
Homebrew flex 2.7).

flex 2.6.4 rejects the option, so lexer.l can't carry it. For 2.6.5+,
scan a build-dir copy with `yylmax=65536` appended to the `%option array`
line (same line count, so #line numbers still match lexer.l). The copy-back
stamp still hashes the real lexer.l.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@thefallentree thefallentree left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed; macOS now green with the yylmax follow-up. All 18 checks pass.

@thefallentree
thefallentree merged commit 1707c48 into fluffos:master Nov 23, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants