MCPcopy
hub / github.com/google/mtail / checkRegex

Method checkRegex

internal/runtime/compiler/checker/checker.go:882–919  ·  view source on GitHub ↗

checkRegex is a helper method to compile and check a regular expression, and to generate its capture groups as symbols.

(pattern string, n ast.Node)

Source from the content-addressed store, hash-verified

880// checkRegex is a helper method to compile and check a regular expression, and
881// to generate its capture groups as symbols.
882func (c *checker) checkRegex(pattern string, n ast.Node) {
883 plen := len(pattern)
884 if plen > c.maxRegexLength {
885 c.errors.Add(n.Pos(), fmt.Sprintf("Exceeded maximum regular expression pattern length of %d bytes with %d.\n\tExcessively long patterns are likely to cause compilation and runtime performance problems.", c.maxRegexLength, plen))
886 return
887 }
888 if reAst, err := types.ParseRegexp(pattern); err == nil {
889 if c.noRegexSymbols {
890 return
891 }
892
893 // We reserve the names of the capturing groups as declarations
894 // of those symbols, so that future CAPREF tokens parsed can
895 // retrieve their value. By recording them in the symbol table, we
896 // can warn the user about unknown capture group references.
897 for i, capref := range reAst.CapNames() {
898 sym := symbol.NewSymbol(fmt.Sprintf("%d", i), symbol.CaprefSymbol, n.Pos())
899 sym.Type = types.InferCaprefType(reAst, i)
900 sym.Binding = n
901 sym.Addr = i
902 if alt := c.scope.Insert(sym); alt != nil {
903 c.errors.Add(n.Pos(), fmt.Sprintf("Redeclaration of capture group `%s' previously declared at %s", sym.Name, alt.Pos))
904 // No return, let this loop collect all errors
905 }
906 if capref != "" {
907 sym.Name = capref
908 if alt := c.scope.InsertAlias(sym, capref); alt != nil {
909 c.errors.Add(n.Pos(), fmt.Sprintf("Redeclaration of capture group `%s' previously declared at %s", sym.Name, alt.Pos))
910 // No return, let this loop collect all errors
911 }
912 }
913 glog.V(2).Infof("Added capref %v to scope %v", sym, c.scope)
914 }
915 } else {
916 c.errors.Add(n.Pos(), err.Error())
917 return
918 }
919}
920
921// patternEvaluator is a helper that performs concatenation of pattern
922// fragments so that they can be compiled as whole regular expression patterns.

Callers 1

VisitAfterMethod · 0.95

Calls 8

ParseRegexpFunction · 0.92
NewSymbolFunction · 0.92
InferCaprefTypeFunction · 0.92
InsertMethod · 0.80
InsertAliasMethod · 0.80
PosMethod · 0.65
ErrorMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected