-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
187 lines (171 loc) · 6.56 KB
/
parse.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
package semver
import (
"errors"
)
// ParseMode is an enum-like type used with ParseAs.
type ParseMode int
var invalidSemverError = errors.New("invalid semantic version")
const (
// ParseModeStrict is the default parsing mode, requiring a strictly correct version string with
// all three required numeric components.
ParseModeStrict = iota
// ParseModeAllowMissingMinorAndPatch is a parsing mode in which the version string may omit the patch
// version component ("2.1"), or both the minor and patch version components ("2"), in which case
// they are assumed to be zero.
ParseModeAllowMissingMinorAndPatch = iota
)
// Parse attempts to parse a string into a Version. It only accepts strings that strictly match the
// specification, so extensions like a "v" prefix are not allowed.
//
// If parsing fails, it returns a non-nil error as the second return value, and Version{} as the first.
func Parse(s string) (Version, error) {
return ParseAs(s, ParseModeStrict)
}
// ParseAs attempts to parse a string into a Version, using the specified ParseMode.
//
// If parsing fails, it returns a non-nil error as the second return value, and Version{} as the first.
func ParseAs(s string, mode ParseMode) (Version, error) {
if mode != ParseModeStrict && mode != ParseModeAllowMissingMinorAndPatch {
return Version{}, errors.New("invalid ParseMode")
}
scanner := newSimpleASCIIScanner(s)
var result Version
var term int8
var ok bool
if mode == ParseModeAllowMissingMinorAndPatch {
result.major, term, ok = requirePositiveIntegerComponent(&scanner, dotOrHyphenOrPlusTerminator)
if !ok {
return Version{}, invalidSemverError
}
if term == '.' {
result.minor, term, ok = requirePositiveIntegerComponent(&scanner, dotOrHyphenOrPlusTerminator)
if !ok {
return Version{}, invalidSemverError
}
if term == '.' {
result.patch, term, ok = requirePositiveIntegerComponent(&scanner, hyphenOrPlusTerminator)
if !ok {
return Version{}, invalidSemverError
}
}
}
} else {
result.major, term, ok = requirePositiveIntegerComponent(&scanner, dotTerminator)
if !ok || term != '.' {
return Version{}, invalidSemverError
}
result.minor, term, ok = requirePositiveIntegerComponent(&scanner, dotTerminator)
if !ok || term != '.' {
return Version{}, invalidSemverError
}
result.patch, term, ok = requirePositiveIntegerComponent(&scanner, hyphenOrPlusTerminator)
if !ok {
return Version{}, invalidSemverError
}
}
if term == '-' {
result.prerelease, term = scanner.readUntil(plusTerminator)
if result.prerelease == "" || term == scannerNonASCII || !validatePrerelease(result.prerelease) {
return Version{}, invalidSemverError
}
}
if term == '+' {
result.build, term = scanner.readUntil(noTerminator)
if result.build == "" || term == scannerNonASCII || !validateBuild(result.build) {
return Version{}, invalidSemverError
}
}
return result, nil
}
func requirePositiveIntegerComponent(
scanner *simpleASCIIScanner,
terminatorFn func(rune) bool,
) (n int, terminatedBy int8, ok bool) {
// From spec:
// A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers, and
// MUST NOT contain leading zeroes.
substr, terminatedBy := scanner.readUntil(terminatorFn)
if terminatedBy == scannerNonASCII {
return 0, terminatedBy, false
}
if n, okNumber := parsePositiveNumericString(substr); okNumber {
return n, terminatedBy, true
}
return 0, terminatedBy, false
}
func validatePrerelease(s string) bool {
// BNF definition from spec:
// <pre-release> ::= <dot-separated pre-release identifiers>
// <dot-separated pre-release identifiers> ::=
// <pre-release identifier>
// | <pre-release identifier> "." <dot-separated pre-release identifiers>
// <pre-release identifier> ::= <alphanumeric identifier> | <numeric identifier>
// <alphanumeric identifier> ::=
// <non-digit>
// | <non-digit> <identifier characters>
// | <identifier characters> <non-digit>
// | <identifier characters> <non-digit> <identifier characters>
// <numeric identifier> ::=
// "0"
// | <positive digit>
// | <positive digit> <digits>
// <identifier characters> ::= <identifier character> | <identifier character> <identifier characters>
// <identifier character> ::= <digit> | <non-digit>
// <non-digit> ::= <letter> | "-"
// <digits> ::= <digit> | <digit> <digits>
// <digit> ::= "0" | <positive digit>
// <positive digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
// <letter> ::= [A-Za-z]
//
// Textual definition from spec:
// A pre-release version MAY be denoted by appending a hyphen and a series of dot separated identifiers
// immediately following the patch version. Identifiers MUST comprise only ASCII alphanumerics and hyphen
// [0-9A-Za-z-]. Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes.
// Pre-release versions have a lower precedence than the associated normal version. A pre-release version
// indicates that the version is unstable and might not satisfy the intended compatibility requirements as
// denoted by its associated normal version. Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7,
// 1.0.0-x.7.z.92.
scanner := newSimpleASCIIScanner(s)
for {
substr, terminatedBy := scanner.readUntil(dotTerminator)
if terminatedBy == scannerNonASCII || substr == "" {
return false
}
if !everyChar(substr, isAlphanumericOrHyphen) {
return false
}
if len(substr) > 1 && everyChar(substr, isDigit) && substr[0] == '0' {
// leading zero is not allowed in an all-numeric string, for prerelease (OK in build)
return false
}
if terminatedBy == scannerEOF {
break
}
}
return true
}
func validateBuild(s string) bool {
// BNF definition from spec (see validatePrerelease for basic definitions)
//
// <build> ::= <dot-separated build identifiers>
// <dot-separated build identifiers> ::=
// <build identifier>
// | <build identifier> "." <dot-separated build identifiers>
// <build identifier> ::= <alphanumeric identifier> | <digits>
//
// Textual definition from spec:
// Build metadata MAY be denoted by appending a plus sign and a series of dot separated identifiers
// immediately following the patch or pre-release version. Identifiers MUST comprise only ASCII
// alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty.
scanner := newSimpleASCIIScanner(s)
for {
substr, terminatedBy := scanner.readUntil(dotTerminator)
if terminatedBy == scannerNonASCII || substr == "" || !everyChar(substr, isAlphanumericOrHyphen) {
return false
}
if terminatedBy == scannerEOF {
break
}
}
return true
}