-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
58 lines (49 loc) · 1.16 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//go:generate git submodule foreach git pull origin master
//go:generate git submodule update --remote --checkout --recursive
package genx
import (
"regexp"
"sort"
"github.com/OneOfOne/xast"
)
func (g *GenX) OrderedRewriters() (out []string) {
for k, v := range g.rewriters {
out = append(out, k+"="+v)
}
sort.Strings(out)
return
}
var pkgWithTypeRE = regexp.MustCompile(`^(?:(.*)/([\w\d_-]+)(?:#([\w\d]+))?\.)?([*\w\d]+)$`)
func parsePackageWithType(v string) (name, pkg, sel string) {
p := pkgWithTypeRE.FindAllStringSubmatch(v, -1)
if len(p) != 1 {
return
}
parts := p[0]
pkg = parts[1] + "/" + parts[2]
if name = parts[3]; name != "" {
parts[2] = name
}
if parts[4][0] == '*' {
if parts[2] != "" {
sel = "*" + parts[2] + "." + parts[4][1:]
} else {
sel = "*" + parts[4][1:]
}
} else if parts[2] != "" {
sel = parts[2] + "." + parts[4]
} else {
sel = parts[4]
}
return
}
func regexpReplacer(src string, repl string) func(string) string {
re := regexp.MustCompile(src)
return func(in string) string {
return re.ReplaceAllString(in, repl)
}
}
func deleteWithParent(n *xast.Node) *xast.Node {
n.Parent().Delete()
return n.Delete()
}