-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
177 lines (156 loc) · 3.5 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
package semver
import (
"errors"
"strconv"
)
var (
expectedNumber = errors.New("semver.Parse: unexpected character, expected number")
expectedDashPlusEOF = errors.New("semver.Parse: unexpected character, expected dash, plus or end of string")
expectedDot = errors.New("semver.Parse: unexpected character, expected dot")
expectedDotEOF = errors.New("semver.Parse: unexpected character, expected dot or end of string")
expectedDotPlusEOF = errors.New("semver.Parse: unexpected character, expected dot, plus or end of string")
expectedIdentifier = errors.New("semver.Parse: unexpected character, expected identifier")
unexpectedEOF = errors.New("semver.Parse: unexpected end of string")
invalidCharacter = errors.New("semver.Parse: invalid character in string")
)
// Turn a version string into a Version. Will return an error if it can't
// recognize a proper (semantic) version number in this string.
func Parse(version string) (v Version, err error) {
l := lex(version)
if v.Major, err = parseNumber(l); err != nil {
return
}
if err = parseDot(l); err != nil {
return
}
if v.Minor, err = parseNumber(l); err != nil {
return
}
if err = parseDot(l); err != nil {
return
}
if v.Patch, err = parseNumber(l); err != nil {
return
}
v.PreRelease, v.Build, err = parseSecondPart(l)
return
}
func parseNumber(l *lexer) (n uint, err error) {
i := <-l.items
switch i.itemType {
case itemEOF:
return 0, unexpectedEOF
case itemInvalid:
return 0, invalidCharacter
case itemNumber:
n, err := strconv.ParseUint(i.value, 10, 64) // TODO: hard-code 64-bit here?
return uint(n), err
}
return 0, expectedNumber
}
func parseDot(l *lexer) error {
i := <-l.items
switch i.itemType {
case itemEOF:
return unexpectedEOF
case itemInvalid:
return invalidCharacter
case itemDot:
return nil
}
return expectedDot
}
func parseIdentifier(l *lexer) (identifier []byte, err error) {
i := <-l.items
switch i.itemType {
case itemEOF:
return nil, unexpectedEOF
case itemInvalid:
return nil, invalidCharacter
case itemIdentifier:
return []byte(i.value), nil
}
return nil, expectedIdentifier
}
func parseSecondPart(l *lexer) (pre, build [][]byte, err error) {
i := <-l.items
switch i.itemType {
case itemEOF:
return
case itemDash:
parsepreloop:
for {
i := <-l.items
switch i.itemType {
case itemEOF:
err = unexpectedEOF
return
case itemIdentifier:
pre = append(pre, []byte(i.value))
i = <-l.items
switch i.itemType {
case itemEOF:
return
case itemDot:
continue parsepreloop
case itemPlus:
build, err = parseBuild(l)
return
default:
break parsepreloop
}
default:
err = expectedIdentifier
return
}
if i.itemType == itemEOF {
break
}
if i.itemType == itemDot {
continue
}
break
}
err = expectedDotEOF
return
case itemPlus:
build, err = parseBuild(l)
return
}
err = expectedDashPlusEOF
return
}
func parseBuild(l *lexer) (build [][]byte, err error) {
parsebuildloop:
for {
i := <-l.items
switch i.itemType {
case itemEOF:
err = unexpectedEOF
return
case itemIdentifier:
build = append(build, []byte(i.value))
i = <-l.items
switch i.itemType {
case itemEOF:
return
case itemDot:
continue parsebuildloop
default:
break parsebuildloop
}
default:
err = expectedIdentifier
return
}
if i.itemType == itemEOF {
break
}
if i.itemType == itemDot {
continue
}
break
}
err = expectedDotEOF
return
}