-
Notifications
You must be signed in to change notification settings - Fork 98
/
extglob.go
179 lines (161 loc) · 3.74 KB
/
extglob.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"regexp"
"regexp/syntax"
"strings"
)
func CompileExtGlob(extglob string) (*regexp.Regexp, error) {
ctx := globctx{glob: extglob}
ctx.compileGlobstarPrefix()
if err := ctx.compileExpression(); err != nil {
return nil, err
}
return regexp.Compile("^" + string(ctx.regexp) + "$")
}
type globctx struct {
glob string
regexp []byte
pos, depth int
}
func (c *globctx) compileExpression() error {
for c.pos < len(c.glob) {
switch curr := c.glob[c.pos]; curr {
case '\\':
if err := c.compileEscapeSequence(); err != nil {
return err
}
case '*':
if err := c.compileSubExpression("(?:", ")*", "[^/]*"); err != nil {
return err
}
case '?':
if err := c.compileSubExpression("(?:", ")?", "[^/]"); err != nil {
return err
}
case '+':
if err := c.compileSubExpression("(?:", ")+", "\\+"); err != nil {
return err
}
case '@':
if err := c.compileSubExpression("(?:", ")", "\\@"); err != nil {
return err
}
case '!':
if err := c.compileSubExpression("(?~", ")", "\\!"); err != nil {
return err
}
case ')':
if c.depth > 0 {
return nil
}
c.regexp = append(c.regexp, "\\)"...)
c.pos += 1
case '|':
if c.depth > 0 {
c.regexp = append(c.regexp, '|')
c.pos += 1
} else {
c.regexp = append(c.regexp, "\\|"...)
c.pos += 1
}
case '/':
if c.depth == 0 && (c.glob[c.pos:] == "/**" || strings.HasPrefix(c.glob[c.pos:], "/**/")) {
c.regexp = append(c.regexp, "(?:/.*)?"...)
c.pos += 3
} else {
c.regexp = append(c.regexp, '/')
c.pos += 1
}
case '[':
if err := c.compileCharacterClass(); err != nil {
return err
}
case '.', '^', '$', '(', '{':
c.regexp = append(c.regexp, '\\', curr)
c.pos += 1
default:
c.regexp = append(c.regexp, curr)
c.pos += 1
}
}
if c.depth > 0 {
return &syntax.Error{Code: syntax.ErrMissingParen, Expr: c.glob}
}
return nil
}
func (c *globctx) compileSubExpression(prefix string, suffix string, noexpr string) error {
if strings.HasPrefix(c.glob[c.pos+1:], "(") {
c.regexp = append(c.regexp, prefix...)
c.depth += 1
c.pos += 2
if err := c.compileExpression(); err != nil {
return err
}
c.regexp = append(c.regexp, suffix...)
c.depth -= 1
c.pos += 1
} else {
c.regexp = append(c.regexp, noexpr...)
c.pos += 1
}
return nil
}
func (c *globctx) compileCharacterClass() error {
c.regexp = append(c.regexp, '[')
c.pos += 1
if c.pos < len(c.glob) {
switch curr := c.glob[c.pos]; curr {
case ']', '-':
c.regexp = append(c.regexp, curr)
c.pos += 1
case '!', '^':
c.regexp = append(c.regexp, '^')
c.pos += 1
if strings.HasPrefix(c.glob[c.pos:], "]") {
c.regexp = append(c.regexp, ']')
c.pos += 1
}
}
}
for c.pos < len(c.glob) {
if s := c.glob[c.pos:]; strings.HasPrefix(s, "[:") {
if i := strings.Index(s[2:], ":]"); i >= 0 {
c.regexp = append(c.regexp, s[:4+i]...)
c.pos += 4 + i
continue
}
}
switch curr := c.glob[c.pos]; curr {
case '\\':
if err := c.compileEscapeSequence(); err != nil {
return err
}
case ']':
c.regexp = append(c.regexp, ']')
c.pos += 1
return nil
default:
c.regexp = append(c.regexp, curr)
c.pos += 1
}
}
return &syntax.Error{Code: syntax.ErrMissingBracket, Expr: c.glob}
}
func (c *globctx) compileEscapeSequence() error {
if c.pos+1 == len(c.glob) {
return &syntax.Error{Code: syntax.ErrTrailingBackslash, Expr: c.glob}
}
c.regexp = append(c.regexp, '\\', c.glob[c.pos+1])
c.pos += 2
return nil
}
func (c *globctx) compileGlobstarPrefix() {
for strings.HasPrefix(c.glob[c.pos:], "**/") {
c.regexp = append(c.regexp, "(?:[^/].*/)?"...)
c.pos += 3
}
if c.glob[c.pos:] == "**" {
c.regexp = append(c.regexp, "(?:[^/].*)?"...)
c.pos += 2
}
}