Name resource_api title pattern groups as Regexp#names does (#20)
resource_api names title-pattern captures with `Regexp.new(pattern).names`, which counts only real named groups. w10k matched `(?<name>` anywhere in the source, so an escaped paren, a character class or a comment added names puppet never emits. - scan patterns as Onigmo does: escapes, nested character classes, `(?#...)` and extended-mode comments hold no groups; names stay unique in first-appearance order - skip a pattern whose names or back references fall outside what w10k models (Unicode or hyphenated names, `\g`, forward `\k`) - bump gentypes.Version to 14 Reviewed-on: #20 Co-authored-by: unkin-agent <unkin-agent@unkin.net> Co-committed-by: unkin-agent <unkin-agent@unkin.net>
This commit is contained in:
@@ -1523,3 +1523,131 @@ func (p *parser) loadFeature(name string) error {
|
||||
}
|
||||
return p.load(srcs[0])
|
||||
}
|
||||
|
||||
// onigNames ports Onigmo's Regexp#names: named groups in first-appearance order, skipping escapes, character classes,
|
||||
// (?#...) comments and, in extended mode, # comments. A name outside [A-Za-z_][A-Za-z0-9_]* or a reference w10k
|
||||
// cannot resolve is an error.
|
||||
func onigNames(re regex) ([]string, error) {
|
||||
s := re.src
|
||||
var names []string
|
||||
ext := []bool{strings.Contains(re.flags, "x")}
|
||||
name := func(i int, end byte) (string, int, error) {
|
||||
j := strings.IndexByte(s[i:], end)
|
||||
if j < 0 || !rubyIdent.MatchString(s[i:i+j]) {
|
||||
return "", 0, fmt.Errorf("regex /%s/: unsupported group name", s)
|
||||
}
|
||||
return s[i : i+j], i + j + 1, nil
|
||||
}
|
||||
for i := 0; i < len(s); {
|
||||
c := s[i]
|
||||
switch {
|
||||
case c == '\\' && i+1 < len(s) && s[i+1] == 'k':
|
||||
if i+2 >= len(s) || s[i+2] != '<' {
|
||||
return nil, fmt.Errorf("regex /%s/: unsupported \\k", s)
|
||||
}
|
||||
n, j, err := name(i+3, '>')
|
||||
if err != nil || !slices.Contains(names, n) {
|
||||
return nil, fmt.Errorf("regex /%s/: unsupported back reference", s)
|
||||
}
|
||||
i = j
|
||||
case c == '\\' && i+1 < len(s) && s[i+1] == 'g':
|
||||
return nil, fmt.Errorf("regex /%s/: unsupported subexpression call", s)
|
||||
case c == '\\':
|
||||
_, n := utf8.DecodeRuneInString(s[min(i+1, len(s)):])
|
||||
i += 1 + n
|
||||
case c == '[':
|
||||
depth := 0
|
||||
for i < len(s) {
|
||||
switch s[i] {
|
||||
case '\\':
|
||||
i++
|
||||
case '[':
|
||||
depth++
|
||||
if i++; i < len(s) && s[i] == '^' {
|
||||
i++
|
||||
}
|
||||
if i < len(s) && s[i] == ']' {
|
||||
i++
|
||||
}
|
||||
continue
|
||||
case ']':
|
||||
depth--
|
||||
}
|
||||
i++
|
||||
if depth == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if depth != 0 {
|
||||
return nil, fmt.Errorf("regex /%s/: premature end of char-class", s)
|
||||
}
|
||||
case ext[len(ext)-1] && c == '#':
|
||||
for i < len(s) && s[i] != '\n' {
|
||||
i++
|
||||
}
|
||||
case c == '(' && strings.HasPrefix(s[i:], "(?#"):
|
||||
for i += 3; i < len(s) && s[i] != ')'; i++ {
|
||||
if s[i] == '\\' {
|
||||
i++
|
||||
}
|
||||
}
|
||||
if i++; i > len(s) {
|
||||
return nil, fmt.Errorf("regex /%s/: end pattern in group", s)
|
||||
}
|
||||
case c == '(' && (strings.HasPrefix(s[i:], "(?<") && !strings.HasPrefix(s[i:], "(?<=") && !strings.HasPrefix(s[i:], "(?<!") || strings.HasPrefix(s[i:], "(?'")):
|
||||
end := byte('>')
|
||||
if s[i+2] == '\'' {
|
||||
end = '\''
|
||||
}
|
||||
n, j, err := name(i+3, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !slices.Contains(names, n) {
|
||||
names = append(names, n)
|
||||
}
|
||||
ext = append(ext, ext[len(ext)-1])
|
||||
i = j
|
||||
case c == '(' && strings.HasPrefix(s[i:], "(?") && i+2 < len(s) && strings.IndexByte("imx-", s[i+2]) >= 0:
|
||||
on, x := true, ext[len(ext)-1]
|
||||
j := i + 2
|
||||
for ; j < len(s) && s[j] != ':' && s[j] != ')'; j++ {
|
||||
switch s[j] {
|
||||
case '-':
|
||||
on = false
|
||||
case 'x':
|
||||
x = on
|
||||
case 'i', 'm':
|
||||
default:
|
||||
return nil, fmt.Errorf("regex /%s/: unsupported group option", s)
|
||||
}
|
||||
}
|
||||
if j == len(s) {
|
||||
return nil, fmt.Errorf("regex /%s/: end pattern in group", s)
|
||||
}
|
||||
if s[j] == ')' {
|
||||
ext[len(ext)-1] = x
|
||||
} else {
|
||||
ext = append(ext, x)
|
||||
}
|
||||
i = j + 1
|
||||
case c == '(':
|
||||
ext = append(ext, ext[len(ext)-1])
|
||||
i++
|
||||
case c == ')':
|
||||
if len(ext) == 1 {
|
||||
return nil, fmt.Errorf("regex /%s/: unmatched close parenthesis", s)
|
||||
}
|
||||
ext = ext[:len(ext)-1]
|
||||
i++
|
||||
default:
|
||||
i++
|
||||
}
|
||||
}
|
||||
if len(ext) != 1 {
|
||||
return nil, fmt.Errorf("regex /%s/: end pattern with unmatched parenthesis", s)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
var rubyIdent = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
|
||||
@@ -337,3 +337,26 @@ func TestFileJoin(t *testing.T) {
|
||||
t.Errorf("fileJoin of %d rooted parts allocates %v times", len(parts), n)
|
||||
}
|
||||
}
|
||||
|
||||
// Each case is Ruby 3.3's Regexp#names, or an error where Ruby raises or w10k does not model the name.
|
||||
func TestOnigNames(t *testing.T) {
|
||||
for _, c := range []struct{ src, flags, want string }{
|
||||
{`(?<a>x)\(?<b>y\)[(?<c>)](?<a>z)`, "", "a"}, {`(?#c(?<a>x)(?<b>y)`, "", "b"}, {`(?#c\)(?<a>x)`, "", ""},
|
||||
{"(?x) # (?<n>c)\n(?<m>d)", "", "m"}, {"(?x:a # (?<n>c)\n)(?<m>d) # (?<o>e)", "", "m o"}, {"(?x:a)# (?<n>c)\n", "", "n"},
|
||||
{"(?x)[#](?<n>c)", "", "n"}, {`(?x)\#(?<n>c)`, "", "n"}, {`[]](?<a>x)`, "", "a"}, {`[^]](?<a>x)`, "", "a"},
|
||||
{`[a[b](?<c>x)]](?<a>x)`, "", "a"}, {`[[:alpha:]](?<a>x)`, "", "a"}, {`[a&&[^b]](?<a>x)`, "", "a"}, {`[\]](?<a>x)`, "", "a"},
|
||||
{`(?<=a)(?<!b)(?<c>d)`, "", "c"}, {`(?'q'x)(?<r>y)`, "", "q r"}, {`(?i)(?<a>x)`, "", "a"}, {`(?-x:#(?<a>x))`, "", "a"},
|
||||
{`(?<a>x)\k<a>`, "", "a"}, {`(?<a_1>x)(?<_b>y)`, "", "a_1 _b"}, {`(?<a>x)(?<b>(?<c>y))`, "", "a b c"}, {`(?~ab)(?<a>x)`, "", "a"},
|
||||
{`(?>a)(?<a>x)`, "", "a"}, {`\\(?<a>x)`, "", "a"}, {`\Q(?<a>x)`, "", "a"}, {"(?x)(?<a>x)#)\n(?<b>y)", "", "a b"},
|
||||
{"(?x)a(?-x)#(?<b>y)", "", "b"}, {"(?x:(?-x)#(?<a>x))#(?<b>y)", "", "a b"}, {`(?<b>x)(?<a>y)(?<b>z)`, "", "b a"},
|
||||
{"(?<a>x) # (?<b>y)\n (?<c>z)", "x", "a c"}, {`(x)`, "", ""},
|
||||
{`\k<a>(?<a>x)`, "", "error"}, {`\g<a>(?<a>x)`, "", "error"}, {`(?<1a>x)`, "", "error"}, {`(?<a-b>x)`, "", "error"},
|
||||
{`(?<>x)`, "", "error"}, {`(?<a>x`, "", "error"}, {`(?<a>x)\k<b>(?<b>y)`, "", "error"}, {`(?<a>x)\k<1>`, "", "error"},
|
||||
{`(?<a>x)\k<a+0>`, "", "error"}, {`(?<é>x)`, "", "error"}, {`a)`, "", "error"}, {`[a`, "", "error"}, {`(?#a`, "", "error"},
|
||||
} {
|
||||
got, err := onigNames(regex{c.src, c.flags})
|
||||
if g := strings.Join(got, " "); err != nil && c.want != "error" || err == nil && g != c.want {
|
||||
t.Errorf("onigNames(%q) = %q %v, want %q", c.src, g, err, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,3 +369,22 @@ func TestFidelityBlockCalls(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func rsapiPatterns(patterns string) map[string]string {
|
||||
return map[string]string{"m/lib/puppet/type/t.rb": rsapi(" title_patterns: [\n"+patterns+" ],\n", " owner: { type: 'String', desc: 'o', behaviour: :namevar },\n")}
|
||||
}
|
||||
|
||||
// Regexp#names counts only real named groups, so openvox 9.3.0 writes want; names w10k cannot model skip.
|
||||
func TestFidelityTitlePatternNames(t *testing.T) {
|
||||
env := fidelityEnv(t, rsapiPatterns(" { pattern: %r{^(?<name>[^-]*)\\(?<x>y\\)[(?<z>)]-(?<owner>.*)$}, desc: 'a' },\n"+
|
||||
" { pattern: %r{^(?<name>.*) # (?<q>c)\n $}x, desc: 'b' },\n"))
|
||||
want := " /^(?<name>[^-]*)\\(?<x>y\\)[(?<z>)]-(?<owner>.*)$/ => ['name', 'owner'],\n /(?x-mi:^(?<name>.*) # (?<q>c)\n $)/ => ['name']\n"
|
||||
if b := readFile(t, filepath.Join(env, OutDir, "t.pp")); !strings.Contains(b, want) {
|
||||
t.Fatalf("t.pp lacks %q:\n%s", want, b)
|
||||
}
|
||||
for _, re := range []string{`%r{^(?<name-x>.*)$}`, `%r{^\k<name>(?<name>.*)$}`, `%r{^(?<1name>.*)$}`} {
|
||||
if exists(filepath.Join(fidelityEnv(t, rsapiPatterns(" { pattern: "+re+", desc: 'a' },\n")), OutDir, "t.pp")) {
|
||||
t.Errorf("%s: t.pp written", re)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
const OutDir = ".resource_types"
|
||||
|
||||
// Version changes whenever generated output can change, so deploys rebuild typed environments.
|
||||
const Version = "13"
|
||||
const Version = "14"
|
||||
|
||||
var moduleName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
|
||||
|
||||
|
||||
@@ -1940,8 +1940,6 @@ var (
|
||||
rubyName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
|
||||
)
|
||||
|
||||
var namedGroup = regexp.MustCompile(`\(\?(?:<([A-Za-z_]\w*)>|'([A-Za-z_]\w*)')`)
|
||||
|
||||
const numericRe = `\A[[:blank:]]*([-+]?)[[:blank:]]*((0[xX][0-9A-Fa-f]+)|(0?\d+)((?:\.\d+)?(?:[eE]-?\d+)?))[[:blank:]]*\z`
|
||||
|
||||
func str(v rval, ok bool) (string, bool) {
|
||||
@@ -2073,13 +2071,11 @@ func resourceAPIType(def rval) (*rtype, error) {
|
||||
if e.k != vHash || !ok || re.k != vRegex {
|
||||
return nil, fmt.Errorf("title_patterns entries need a literal regex pattern")
|
||||
}
|
||||
pt := pattern{re: re.re}
|
||||
for _, m := range namedGroup.FindAllStringSubmatch(re.re.src, -1) {
|
||||
if n := m[1] + m[2]; !slices.Contains(pt.names, n) {
|
||||
pt.names = append(pt.names, n)
|
||||
}
|
||||
names, err := onigNames(re.re)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("title_patterns: %w", err)
|
||||
}
|
||||
t.patterns = append(t.patterns, pt)
|
||||
t.patterns = append(t.patterns, pattern{re: re.re, names: names})
|
||||
}
|
||||
} else {
|
||||
first := ""
|
||||
|
||||
Reference in New Issue
Block a user