fix: handle unmatched optional capture groups in stateful lexer actions - #464
Open
Pastalikek65 wants to merge 1 commit into
Open
fix: handle unmatched optional capture groups in stateful lexer actions#464Pastalikek65 wants to merge 1 commit into
Pastalikek65 wants to merge 1 commit into
Conversation
When a rule has an optional capture group that does not match, regexp reports it with indices of -1. Building action groups from those indices panicked with a slice bounds error. Unmatched groups are now passed to actions as empty strings, which also prevents backreferences from shifting to the wrong group (issue alecthomas#324). Fixes alecthomas#324.
Pastalikek65
force-pushed
the
fix-lexer-empty-capture-group
branch
from
August 9, 2026 17:41
d809289 to
f3464d1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a stateful lexer rule has an optional capture group that does not match (e.g.
[a-z]([a-z])?matching"a"),regexp.FindStringSubmatchIndexreports it with indices of-1. Building the groups passed toActions sliced the input with those indices, panicking withslice bounds out of range.The reported
%x + ycase from #324 is a real-world instance: the keyword/Ident design pushes a state on%, and the next rule's action (Pop) crashed on the unmatched capture group.Changes:
lexer/stateful.go: unmatched capture groups are passed to actions as empty strings instead of panicking. Keeping the group position (rather than dropping it) also preserves the meaning of\nbackreferences to later states — an unmatched group genuinely captures the empty string.lexer/stateful_test.go: two new table cases — "UnmatchedOptionalCaptureGroup" (panics before this fix, per Panic when stateful lexer's non-Root rule has optional group but captures nothing #324) and "BackrefMatchesEmptyCaptureGroup" (exercises the parent-state group hand-off).Verification:
go test ./...(root +_examples) green; golangci-lint — no new issues.Fixes #324.