From 319e630aaf64ca2fc69e9c06ebf1adf170f4f609 Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Sun, 25 Dec 2022 11:58:23 +0100 Subject: [PATCH] code-gen: syntax-aware generation of vendor regex --- internal/code-generator/assets/vendor.go.tmpl | 8 ++++++-- internal/code-generator/generator/generator.go | 1 + internal/code-generator/generator/heuristics.go | 2 +- internal/code-generator/generator/vendor.go | 10 ++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/code-generator/assets/vendor.go.tmpl b/internal/code-generator/assets/vendor.go.tmpl index 4b5274e..a5d0c9f 100644 --- a/internal/code-generator/assets/vendor.go.tmpl +++ b/internal/code-generator/assets/vendor.go.tmpl @@ -3,8 +3,12 @@ package data import "github.com/go-enry/go-enry/v2/regex" var VendorMatchers = []regex.EnryRegexp{ - {{range $regexp := . -}} - regex.MustCompile(`{{ $regexp }}`), + {{range $re := . -}} + {{ if isRE2 $re -}} + regex.MustCompile({{ $re | stringVal }}), + {{- else -}} + regex.MustCompileRuby({{ $re | stringVal }}), + {{ end }} {{end -}} } diff --git a/internal/code-generator/generator/generator.go b/internal/code-generator/generator/generator.go index 3733f5c..ce0bdd4 100644 --- a/internal/code-generator/generator/generator.go +++ b/internal/code-generator/generator/generator.go @@ -45,6 +45,7 @@ func executeTemplate(w io.Writer, name, path, commit string, fmap template.FuncM } fmap["getCommit"] = getCommit fmap["stringVal"] = stringVal + fmap["isRE2"] = isRE2 const headerTmpl = "header.go.tmpl" headerPath := filepath.Join(filepath.Dir(path), headerTmpl) diff --git a/internal/code-generator/generator/heuristics.go b/internal/code-generator/generator/heuristics.go index ac89b97..65df2a1 100644 --- a/internal/code-generator/generator/heuristics.go +++ b/internal/code-generator/generator/heuristics.go @@ -90,7 +90,7 @@ func loadRule(namedPatterns map[string]StringArray, rule *Rule) *LanguagePattern } if !isRE2(result.Pattern) { - log.Printf("RE2 incompatible rule: language:'%s', rule:'%s'\n", rule.Languages, result.Pattern) + log.Printf("RE2 incompatible syntax for heuristic language:'%s', rule:'%s'\n", rule.Languages, result.Pattern) } return result } diff --git a/internal/code-generator/generator/vendor.go b/internal/code-generator/generator/vendor.go index e239d10..cc3facc 100644 --- a/internal/code-generator/generator/vendor.go +++ b/internal/code-generator/generator/vendor.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "sort" "strings" "text/template" @@ -25,6 +26,12 @@ func Vendor(fileToParse, samplesDir, outPath, tmplPath, tmplName, commit string) return fmt.Errorf("failed to parse YAML %s, %q", fileToParse, err) } + for _, re := range regexps { + if !isRE2(re) { + log.Printf("RE2 incompatible syntax for vendor:'%s'\n", re) + } + } + buf := &bytes.Buffer{} if err := executeVendorTemplate(buf, regexps, tmplPath, tmplName, commit); err != nil { return err @@ -84,6 +91,9 @@ func collateAllMatchers(regexps []string) string { var caretPrefixed, caretOrSlashPrefixed, theRest []string // Check prefix, add to the respective group slices for _, re := range regexps { + if !isRE2(re) { + continue + } if strings.HasPrefix(re, caret) { caretPrefixed = append(caretPrefixed, re[len(caret):]) } else if strings.HasPrefix(re, caretOrSlash) {