-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstraintparser.go
404 lines (349 loc) · 8.32 KB
/
constraintparser.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package semver
import (
"fmt"
"pkg.package-operator.run/semver/internal"
"pkg.package-operator.run/semver/internal/ranges"
)
const maxUint64 = ^uint64(0)
// Parses the given string into a Version Constraint or panics.
func MustNewConstraint(data string) Constraint {
c, err := NewConstraint(data)
if err != nil {
panic(err)
}
return c
}
// Parses the given string into a Version Constraint.
func NewConstraint(data string) (Constraint, error) {
c, err := parseConstraint([]byte(data))
if err != nil {
return nil, err
}
return &originalInputConstraint{
Constraint: c,
original: data,
}, nil
}
// parseConstraint bytes into a Version Constraint.
func parseConstraint(data []byte) (Constraint, error) {
var p parserState
p.init(data)
c, err := p.parse()
if err != nil {
return nil, err
}
return c, nil
}
type parserState struct {
scanner ranges.Scanner
c Constraint
or or // active || combined ranges or And constraints
and and // active && combined ranges
operator ranges.Token // EQUAL,NOT_EQUAL, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL
expectingNumber bool // if we expect a number next
lastSemverPos int // previous position after versionClose()
semverPos int // 0=Major, 1=Minor, 2=Patch
version *Version // active version being parsed
max bool // false = min part of the range, true = max part of the range
activeRange *Range // active range being parsed
errors []string // scanner errors
}
func (p *parserState) init(src []byte) *parserState {
p.scanner.Init(src, func(pos internal.Position, msg string) {
p.errors = append(p.errors, fmt.Sprintf("%s: %s", pos, msg))
})
p.resetRange()
return p
}
func (p *parserState) addNumberToVersion(num uint64) {
if p.activeRange == nil {
p.activeRange = &Range{}
}
if p.max {
p.version = &p.activeRange.Max
} else {
p.version = &p.activeRange.Min
}
switch p.semverPos {
case 0:
p.version.Major = num
case 1:
p.version.Minor = num
case 2:
p.version.Patch = num
}
}
func (p *parserState) closeVersion(pos internal.Position) error {
if p.version == nil {
return nil
}
if p.expectingNumber {
// semver clause incomplete!
return fmt.Errorf("%s: semver clause incomplete", pos)
}
p.lastSemverPos = p.semverPos
p.semverPos = 0
if !p.max {
// move to max part of range next.
p.max = true
}
p.version = nil // no active version
return nil
}
func (p *parserState) closeRange(pos internal.Position) error {
if p.activeRange == nil {
return nil
}
if err := p.closeVersion(pos); err != nil {
return err
}
r := p.activeRange
switch p.operator {
case ranges.EQUAL, ranges.NOT_EQUAL:
r.Max = r.Min
switch p.lastSemverPos {
case 0:
r.Max.Minor = maxUint64
r.Max.Patch = maxUint64
case 1:
r.Max.Patch = maxUint64
}
case ranges.GREATER:
switch p.lastSemverPos {
// 1.x.x -> 2.x.x
case 0:
r.Min.Major++
// 1.2.x -> 1.3.x
case 1:
r.Min.Minor++
// 1.2.0 -> 1.2.1
default:
r.Min.Patch++
}
// x.x.x
r.Max = Version{Major: maxUint64, Minor: maxUint64, Patch: maxUint64}
case ranges.HYPHEN:
// x.0 => x.x
if r.Max.Major == maxUint64 {
r.Max.Minor = maxUint64
}
// 1.x.0 => 1.x.x
if r.Max.Minor == maxUint64 {
r.Max.Patch = maxUint64
}
case ranges.LESS:
r.Max = r.Min
switch {
// 1.2.0 => 1.1.x
case r.Max.Patch == 0 && r.Max.Minor > 0:
r.Max.Patch = maxUint64
r.Max.Minor--
// 1.0.0 => 0.x.x
case r.Max.Minor == 0:
r.Max.Patch = maxUint64
r.Max.Minor = maxUint64
r.Max.Major--
// 1.2.3 => 1.2.2
default:
r.Max.Patch--
}
r.Min = Version{} // 0.0.0
case ranges.LESS_EQUAL:
r.Max = r.Min
r.Min = Version{} // 0.0.0
case ranges.GREATER_EQUAL:
r.Max = Version{Major: maxUint64, Minor: maxUint64, Patch: maxUint64}
case ranges.TILDE:
r.Max = r.Min
r.Max.Patch = maxUint64
if r.Max.Minor == 0 {
r.Max.Minor = maxUint64
}
case ranges.CARET:
r.Max = r.Min
if r.Min.Major != 0 {
r.Max.Minor = maxUint64
}
r.Max.Patch = maxUint64
default:
return fmt.Errorf("%s: range closed without operator", pos)
}
var c Constraint
c = r
// negate result
if p.operator == ranges.NOT_EQUAL {
c = not{Range: *r}
}
p.and = append(p.and, c)
var err error
p.and, err = compactAndValidateLogicalAND(pos, p.and)
if err != nil {
return err
}
// reset
p.resetRange()
return nil
}
func (p *parserState) resetRange() {
p.max = false
p.activeRange = nil
p.operator = 0
}
func (p *parserState) close(pos internal.Position) error {
if err := p.closeRange(pos); err != nil {
return err
}
switch len(p.and) {
case 0:
case 1:
p.or = append(p.or, p.and[0])
default:
p.or = append(p.or, p.and)
}
switch len(p.or) {
case 0:
case 1:
p.c = p.or[0]
default:
p.c = p.or
}
return nil
}
func (p *parserState) parse() (Constraint, error) {
parse:
for {
pos, tok, lit := p.scanner.Scan()
if len(p.errors) > 0 {
return nil, fmt.Errorf(p.errors[0])
}
switch tok {
case ranges.ILLEGAL:
goto parse
case ranges.SPACE:
if err := p.closeVersion(pos); err != nil {
return nil, err
}
case ranges.EQUAL, ranges.NOT_EQUAL,
ranges.GREATER, ranges.GREATER_EQUAL,
ranges.LESS, ranges.LESS_EQUAL,
ranges.TILDE, ranges.CARET:
if err := p.closeRange(pos); err != nil {
return nil, err
}
p.operator = tok
case ranges.AND:
if p.activeRange == nil {
return nil, fmt.Errorf("%s: AND empty range constraint", pos)
}
if err := p.closeRange(pos); err != nil {
return nil, fmt.Errorf("%s: AND %w", pos, err)
}
case ranges.OR:
if p.activeRange == nil {
return nil, fmt.Errorf("%s: OR empty range constraint", pos)
}
if err := p.closeRange(pos); err != nil {
return nil, fmt.Errorf("%s: OR %w", pos, err)
}
// Shift current AND constraint into OR
if len(p.and) == 1 {
p.or = append(p.or, p.and[0])
} else {
p.or = append(p.or, p.and)
}
p.and = nil
case ranges.HYPHEN:
if p.operator == ranges.HYPHEN {
// we are already within a HYPON range.
// seeing a HYPON again is an error.
return nil, fmt.Errorf(`%s: double hyphen in range constraint`, pos)
}
if err := p.closeVersion(pos); err != nil {
return nil, err
}
p.operator = tok
p.max = true
p.expectingNumber = true
case ranges.EOF:
if err := p.close(pos); err != nil {
return nil, err
}
break parse
case ranges.NUMBER:
p.addNumberToVersion(lit)
p.expectingNumber = false
case ranges.WILDCARD:
if p.max {
p.addNumberToVersion(maxUint64)
} else {
p.addNumberToVersion(0)
}
if p.semverPos != 0 {
p.semverPos--
}
p.expectingNumber = false
case ranges.DOT:
p.expectingNumber = true
p.semverPos++
if p.semverPos > 2 {
return nil, fmt.Errorf("%s: found 3rd dot when parsing semver", pos)
}
}
}
if p.c == nil {
return nil, fmt.Errorf("%s: empty", internal.Position(1))
}
return p.c, nil
}
func compactAndValidateLogicalAND(pos internal.Position, and and) (and, error) {
if len(and) < 2 {
return and, nil
}
var fullyDefinedConstraints []Constraint
// find min version and max version
var (
max *Version
min *Version
)
for _, c := range and {
r, ok := c.(*Range)
switch {
case ok && isMinUnconstraint(*r):
if max != nil {
return nil, fmt.Errorf("%s: <=%s overlaps with <=%s in logical AND", pos, r.Max.String(), max)
}
max = &r.Max
case ok && isMaxUnconstraint(*r):
if min != nil {
return nil, fmt.Errorf("%s: >=%s overlaps with >=%s in logical AND", pos, r.Min.String(), min)
}
min = &r.Min
case ok:
if min != nil && max != nil {
existingRange := Range{Min: *min, Max: *max}
if !existingRange.Contains(r) {
return nil, fmt.Errorf("%s: non overlapping ranges %q and %q in logical AND", pos, r.String(), existingRange.String())
}
fullyDefinedConstraints = append(fullyDefinedConstraints, c)
} else {
min = &r.Min
max = &r.Max
}
default:
fullyDefinedConstraints = append(fullyDefinedConstraints, c)
}
}
if min != nil && max != nil {
fullyDefinedConstraints = append(fullyDefinedConstraints, &Range{
Min: *min,
Max: *max,
})
}
return fullyDefinedConstraints, nil
}
func isMinUnconstraint(r Range) bool {
return r.Min.Same(Version{})
}
func isMaxUnconstraint(r Range) bool {
return r.Max.Same(Version{Major: maxUint64, Minor: maxUint64, Patch: maxUint64})
}