Add regex command implementation

This commit is contained in:
Rogee
2025-10-31 10:12:02 +08:00
parent a0d7084c28
commit d436970848
55 changed files with 2115 additions and 9 deletions

View File

@@ -0,0 +1,30 @@
package regex
import "fmt"
// ValidateTemplate ensures the parsed template does not reference capture groups beyond the
// pattern's capabilities and returns a descriptive error for CLI presentation.
func ValidateTemplate(engine *Engine, tmpl template) error {
if engine == nil {
return fmt.Errorf("internal error: regex engine not initialized")
}
max := 0
for _, segment := range tmpl.segments {
if segment.group > max {
max = segment.group
}
}
if max > engine.groups {
return ErrTemplateGroupOutOfRange{Group: max, Available: engine.groups}
}
return nil
}
// ErrUndefinedPlaceholder indicates that the template references a group with no match result.
type ErrUndefinedPlaceholder struct {
Index int
}
func (e ErrUndefinedPlaceholder) Error() string {
return fmt.Sprintf("template references @%d but the pattern did not produce that group", e.Index)
}