Skip to content

Commit a69d1c0

Browse files
alecthomasclaude
andcommitted
fix: surface regex match errors instead of swallowing them
matchRules discarded errors from regexp2, notably the 250ms MatchTimeout, treating the rule as a plain non-match and silently degrading output. Errors now abort tokenisation via panic, which Formatters recover into a returned error per the Formatter contract. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 34ba3a7 commit a69d1c0

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

regexp.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ func (l *LexerState) Iterator() iter.Seq[Token] { // nolint: gocognit
209209
if l.Lexer.trace {
210210
start = time.Now()
211211
}
212-
ruleIndex, rule, groups, namedGroups := matchRules(l.Text, l.Pos, selectedRule)
212+
ruleIndex, rule, groups, namedGroups, err := matchRules(l.Text, l.Pos, selectedRule)
213+
if err != nil {
214+
// The iterator cannot return an error, so panic and let the Formatter recover it.
215+
panic(fmt.Errorf("%s: state %s: %w", l.Lexer.config.Name, l.State, err))
216+
}
213217
if l.Lexer.trace {
214218
var length int
215219
if groups != nil {
@@ -481,20 +485,23 @@ func (r *RegexLexer) MustRules() Rules {
481485
return rules
482486
}
483487

484-
func matchRules(text []rune, pos int, rules []*CompiledRule) (int, *CompiledRule, []string, map[string]string) {
488+
func matchRules(text []rune, pos int, rules []*CompiledRule) (int, *CompiledRule, []string, map[string]string, error) {
485489
for i, rule := range rules {
486490
match, err := rule.Regexp.FindRunesMatchStartingAt(text, pos)
487-
if match != nil && err == nil && match.RuneIndex == pos {
491+
if err != nil {
492+
return 0, nil, nil, nil, fmt.Errorf("matching %q: %w", rule.Pattern, err)
493+
}
494+
if match != nil && match.RuneIndex == pos {
488495
groups := []string{}
489496
namedGroups := make(map[string]string)
490497
for _, g := range match.Groups() {
491498
namedGroups[g.Name] = g.String()
492499
groups = append(groups, g.String())
493500
}
494-
return i, rule, groups, namedGroups
501+
return i, rule, groups, namedGroups, nil
495502
}
496503
}
497-
return 0, &CompiledRule{}, nil, nil
504+
return 0, &CompiledRule{}, nil, nil, nil
498505
}
499506

500507
// replace \r and \r\n with \n

0 commit comments

Comments
 (0)