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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
package main
import (
"strings"
)
type Parser struct {
repl *PrefixReplacer
}
func NewParser(s string) *Parser {
return &Parser{NewPrefixReplacer(s)}
}
func (p *Parser) EOF() bool {
return p.repl.rest == ""
}
func (p *Parser) Rest() string {
return p.repl.rest
}
func (p *Parser) PkgbasePattern() (pkgbase string) {
repl := p.repl
for {
if repl.AdvanceRegexp(`^\$\{\w+\}`) ||
repl.AdvanceRegexp(`^[\w.*+,{}]+`) ||
repl.AdvanceRegexp(`^\[[\d-]+\]`) {
pkgbase += repl.m[0]
continue
}
mark := repl.Mark()
if repl.AdvanceStr("-") {
if repl.AdvanceRegexp(`^\d`) ||
repl.AdvanceRegexp(`^\$\{\w*VER\w*\}`) ||
repl.AdvanceStr("[") {
repl.Reset(mark)
return
}
pkgbase += "-"
} else {
return
}
}
}
func (p *Parser) Dependency() *DependencyPattern {
repl := p.repl
var dp DependencyPattern
mark := repl.Mark()
dp.pkgbase = p.PkgbasePattern()
if dp.pkgbase == "" {
return nil
}
mark2 := repl.Mark()
if repl.AdvanceStr(">=") || repl.AdvanceStr(">") {
op := repl.s
if repl.AdvanceRegexp(`^(?:(?:\$\{\w+\})+|\d[\w.]*)`) {
dp.lowerOp = op
dp.lower = repl.m[0]
} else {
repl.Reset(mark2)
}
}
if repl.AdvanceStr("<=") || repl.AdvanceStr("<") {
op := repl.s
if repl.AdvanceRegexp(`^(?:(?:\$\{\w+\})+|\d[\w.]*)`) {
dp.upperOp = op
dp.upper = repl.m[0]
} else {
repl.Reset(mark2)
}
}
if dp.lowerOp != "" || dp.upperOp != "" {
return &dp
}
if repl.AdvanceStr("-") && repl.rest != "" {
dp.wildcard = repl.AdvanceRest()
return &dp
}
if hasPrefix(dp.pkgbase, "${") && hasSuffix(dp.pkgbase, "}") {
return &dp
}
if hasSuffix(dp.pkgbase, "-*") {
dp.pkgbase = strings.TrimSuffix(dp.pkgbase, "-*")
dp.wildcard = "*"
return &dp
}
repl.Reset(mark)
return nil
}
type MkToken struct {
literal string
varuse MkVarUse
}
type MkVarUse struct {
varname string
modifiers []string
}
func (p *Parser) MkTokens() []*MkToken {
repl := p.repl
var tokens []*MkToken
for !p.EOF() {
if varuse := p.VarUse(); varuse != nil {
tokens = append(tokens, &MkToken{varuse: *varuse})
continue
}
mark := repl.Mark()
needsReplace := false
again:
dollar := strings.IndexByte(repl.rest, '$')
if dollar == -1 {
dollar = len(repl.rest)
}
repl.Skip(dollar)
if repl.AdvanceStr("$$") {
needsReplace = true
goto again
}
literal := repl.Since(mark)
if needsReplace {
literal = strings.Replace(literal, "$$", "$", -1)
}
if literal != "" {
tokens = append(tokens, &MkToken{literal: literal})
continue
}
break
}
return tokens
}
func (p *Parser) Varname() string {
repl := p.repl
mark := repl.Mark()
repl.AdvanceStr(".")
for p.VarUse() != nil || repl.AdvanceBytes(0x00000000, 0x03ff6800, 0x87fffffe, 0x07fffffe, `[\w+\-.]`) {
}
return repl.Since(mark)
}
func (p *Parser) VarUse() *MkVarUse {
repl := p.repl
mark := repl.Mark()
if repl.AdvanceStr("${") || repl.AdvanceStr("$(") {
closing := "}"
if repl.Since(mark) == "$(" {
closing = ")"
}
varnameMark := repl.Mark()
varname := p.Varname()
if varname != "" {
modifiers := p.VarUseModifiers(closing)
if repl.AdvanceStr(closing) {
return &MkVarUse{varname, modifiers}
}
}
for p.VarUse() != nil || repl.AdvanceRegexp(`^([^$:`+closing+`]|\$\$)+`) {
}
rest := p.Rest()
if hasPrefix(rest, ":L") || hasPrefix(rest, ":sh") || hasPrefix(rest, ":?") {
varexpr := repl.Since(varnameMark)
modifiers := p.VarUseModifiers(closing)
if repl.AdvanceStr(closing) {
return &MkVarUse{varexpr, modifiers}
}
}
repl.Reset(mark)
}
return nil
}
func (p *Parser) VarUseModifiers(closing string) []string {
repl := p.repl
var modifiers []string
mayOmitColon := false
for repl.AdvanceStr(":") || mayOmitColon {
mayOmitColon = false
modifierMark := repl.Mark()
switch repl.PeekByte() {
case 'E', 'H', 'L', 'O', 'Q', 'R', 'T', 's', 't', 'u':
if repl.AdvanceRegexp(`^(E|H|L|Ox?|Q|R|T|sh|tA|tW|tl|tu|tw|u)`) {
modifiers = append(modifiers, repl.Since(modifierMark))
continue
}
if repl.AdvanceStr("ts") {
rest := repl.rest
if len(rest) >= 2 && (rest[1] == closing[0] || rest[1] == ':') {
repl.Skip(1)
} else if len(rest) >= 1 && (rest[0] == closing[0] || rest[0] == ':') {
} else if repl.AdvanceRegexp(`^\\\d+`) {
} else {
break
}
modifiers = append(modifiers, repl.Since(modifierMark))
continue
}
case '=', 'D', 'M', 'N', 'U':
if repl.AdvanceRegexp(`^[=DMNU]`) {
for p.VarUse() != nil || repl.AdvanceRegexp(`^([^$:`+closing+`]|\$\$)+`) {
}
modifiers = append(modifiers, repl.Since(modifierMark))
continue
}
case 'C', 'S':
if repl.AdvanceRegexp(`^[CS]([%,/:;@^|])`) {
separator := repl.m[1]
repl.AdvanceStr("^")
re := `^([^\` + separator + `$` + closing + `\\]|\$\$|\\.)+`
for p.VarUse() != nil || repl.AdvanceRegexp(re) {
}
repl.AdvanceStr("$")
if repl.AdvanceStr(separator) {
for p.VarUse() != nil || repl.AdvanceRegexp(re) {
}
if repl.AdvanceStr(separator) {
repl.AdvanceRegexp(`^[1gW]`)
modifiers = append(modifiers, repl.Since(modifierMark))
mayOmitColon = true
continue
}
}
}
case '@':
if repl.AdvanceRegexp(`^@([\w.]+)@`) {
for p.VarUse() != nil || repl.AdvanceRegexp(`^([^$:@`+closing+`\\]|\$\$|\\.)+`) {
}
if repl.AdvanceStr("@") {
modifiers = append(modifiers, repl.Since(modifierMark))
continue
}
}
case '[':
if repl.AdvanceRegexp(`^\[[-.\d]+\]`) {
modifiers = append(modifiers, repl.Since(modifierMark))
continue
}
case '?':
repl.AdvanceStr("?")
re := `^([^$:` + closing + `]|\$\$)+`
for p.VarUse() != nil || repl.AdvanceRegexp(re) {
}
if repl.AdvanceStr(":") {
for p.VarUse() != nil || repl.AdvanceRegexp(re) {
}
modifiers = append(modifiers, repl.Since(modifierMark))
continue
}
}
repl.Reset(modifierMark)
for p.VarUse() != nil || repl.AdvanceRegexp(`^([^:$`+closing+`]|\$\$)+`) {
}
if suffixSubst := repl.Since(modifierMark); contains(suffixSubst, "=") {
modifiers = append(modifiers, suffixSubst)
continue
}
}
return modifiers
}
|