Skip to content

fix(lexers): avoid numeric prefixes in YAML plain scalars - #1533

Merged
github-actions[bot] merged 8 commits into
alecthomas:masterfrom
wozhenbang2004:patch-1
Nov 23, 2026
Merged

github-actions[bot] merged 8 commits into
alecthomas:masterfrom
wozhenbang2004:patch-1

Conversation

@wozhenbang2004

@wozhenbang2004 wozhenbang2004 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

The YAML number rule ends with a word boundary, so it accepts the 1.0 prefix in 1.0-beta and the 2 prefix in 2 val. Require a numeric value to end at a line boundary, a flow separator, or a whitespace-separated # comment instead.

This keeps numeric-looking plain strings together, including 1#suffix and 1 // not a comment. The existing phone-number and complex-value fixtures also become whole Literal tokens. Signed numbers, fractions, exponents, hex/octal values, .inf, .nan, tags, anchors, quoted strings, and numeric flow sequences are covered by a dedicated fixture.

Rejecting a numeric prefix sends the complete value through the plain/comment fallback. Its greedy .* followed by ( )+ can repeatedly partition a long space run and hit the lexer's match timeout. Replace ( )+ with ( ): the greedy scalar capture already keeps all but the final separator space, so this preserves successful token captures while removing the repeated partitions. A fixture covers four 10,000-space cases, including literal hashes and real comments, without adding timing thresholds.

The decimal alternatives also group the optional fractional part to avoid ambiguous splits of a rejected digit run. Raw CRLF/CR input is covered as well as normalized line endings.

Related to discussion #1328, and the YAML plain-scalar rules. The scope is numeric recognition and its adjacent fallback; discussion #1379 covers the broader flow-collection parser work.

Validation with Go 1.27.4:

  • All three YAML fixtures pass, including all four long-whitespace regressions. The fixtures fail with the exact base lexer; the long-whitespace regression also catches the incomplete numeric-boundary-only fix.
  • go test -count=1 ./... passes.
  • go generate ./lexers leaves lexers/lexers_gen.go unchanged.
  • CI-pinned golangci-lint 2.15.0 passes.
  • A separate probe covers line endings, comments, suffixes, numeric flow values, and 1,000/10,000/100,000-digit prefixes. All preserve their expected complete tokens.

Prepared with AI assistance (ChatGPT, GPT-6 Astra Pro).

Require complete numeric values and remove redundant whitespace backtracking in the adjacent comment fallback.

Prepared with AI assistance (ChatGPT, GPT-6 Astra Pro).
Update YAML fixture expectations for complete plain scalar tokens.

Prepared with AI assistance (ChatGPT, GPT-6 Astra Pro).
Cover numeric-looking plain scalars and complete numeric values.

Prepared with AI assistance (ChatGPT, GPT-6 Astra Pro).
Record complete numeric and plain scalar token expectations.

Prepared with AI assistance (ChatGPT, GPT-6 Astra Pro).
Cover four 10,000-space scalars and comments without timing thresholds.

Prepared with AI assistance (ChatGPT, GPT-6 Astra Pro).
@github-actions

Copy link
Copy Markdown

Lexer and style changes should be tested with lexers/testdata files, not Go tests. Add a .actual input, generate its .expected output with RECORD=true go test ./lexers, and remove:

  • lexers/yaml_test.go

Preserve all four existing 10,000-space regression inputs in the standard lexer fixture format.
Use the same 24 expected tokens as the original four Go regression cases.
The four long-whitespace regressions now live in lexers/testdata, following the project review.
@github-actions
github-actions Bot enabled auto-merge (squash) November 23, 2026 11:02
@wozhenbang2004

Copy link
Copy Markdown
Contributor Author

Moved the four long-whitespace regressions into lexers/testdata and removed yaml_test.go. The generated fixture preserves all four 10,000-space inputs and the original expected tokens. It reproduces the timeout with the numeric-boundary-only change and passes with the final rules. go test ./..., the pinned linter, and generated-code checks pass. Updated in 7a693ed.

@github-actions
github-actions Bot merged commit c72aa6f into alecthomas:master Nov 23, 2026
4 checks passed
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.

1 participant