-
Notifications
You must be signed in to change notification settings - Fork 5
/
lex.go
285 lines (257 loc) · 6.36 KB
/
lex.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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// based on the lexer from: src/pkg/text/template/parse/lex.go (golang source)
package pkgbuild
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// pos is a position in input being scanned
type pos int
type item struct {
typ itemType
pos pos
val string
}
func (i item) String() string {
switch {
case i.typ == itemEOF:
return "EOF"
case i.typ == itemError:
return i.val
case len(i.val) > 10:
return fmt.Sprintf("%.10q...", i.val)
}
return fmt.Sprintf("%q", i.val)
}
type itemType int
const (
itemError itemType = iota
itemEOF
itemVariable
itemValue
itemEndSplit
// PKGBUILD variables
itemPkgname // pkgname variable
itemPkgver // pkgver variable
itemPkgrel // pkgrel variable
itemPkgdir // pkgdir variable
itemEpoch // epoch variable
itemPkgbase // pkgbase variable
itemPkgdesc // pkgdesc variable
itemArch // arch variable
itemURL // url variable
itemLicense // license variable
itemGroups // groups variable
itemDepends // depends variable
itemOptdepends // optdepends variable
itemMakedepends // makedepends variable
itemCheckdepends // checkdepends variable
itemProvides // provides variable
itemConflicts // conflicts variable
itemReplaces // replaces variable
itemBackup // backup variable
itemOptions // options variable
itemInstall // install variable
itemChangelog // changelog variable
itemSource // source variable
itemNoextract // noextract variable
itemMd5sums // md5sums variable
itemSha1sums // sha1sums variable
itemSha224sums // sha224sums variable
itemSha256sums // sha256sums variable
itemSha384sums // sha384sums variable
itemSha512sums // sha512sums variable
itemB2sums // b2sums variable
itemValidpgpkeys // validpgpkeys variable
)
// PKGBUILD variables
var variables = map[string]itemType{
"pkgname": itemPkgname,
"pkgver": itemPkgver,
"pkgrel": itemPkgrel,
"pkgdir": itemPkgdir,
"epoch": itemEpoch,
"pkgbase": itemPkgbase,
"pkgdesc": itemPkgdesc,
"arch": itemArch,
"url": itemURL,
"license": itemLicense,
"groups": itemGroups,
"depends": itemDepends,
"optdepends": itemOptdepends,
"makedepends": itemMakedepends,
"checkdepends": itemCheckdepends,
"provides": itemProvides,
"conflicts": itemConflicts,
"replaces": itemReplaces,
"backup": itemBackup,
"options": itemOptions,
"install": itemInstall,
"changelog": itemChangelog,
"source": itemSource,
"noextract": itemNoextract,
"md5sums": itemMd5sums,
"sha1sums": itemSha1sums,
"sha224sums": itemSha224sums,
"sha256sums": itemSha256sums,
"sha384sums": itemSha384sums,
"sha512sums": itemSha512sums,
"b2sums": itemB2sums,
"validpgpkeys": itemValidpgpkeys,
}
const eof = -1
// stateFn represents the state of the scanner as a function that returns the next state
type stateFn func(*lexer) stateFn
// lexer holds the state of the scanner
type lexer struct {
input string
state stateFn
pos pos
start pos
width pos
lastPos pos
items chan item // channel of scanned items
}
// next returns the next rune in the input
func (l *lexer) next() rune {
if int(l.pos) >= len(l.input) {
l.width = 0
return eof
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = pos(w)
l.pos += l.width
return r
}
// peek returns but does not consume the next rune in the input
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
// backup steps back one rune. Can only be called once per call of next
func (l *lexer) backup() {
l.pos -= l.width
}
// emit passes an item back to the client
func (l *lexer) emit(t itemType) {
l.items <- item{t, l.start, l.input[l.start:l.pos]}
l.start = l.pos
}
// ignore skips over the pending input before this point
func (l *lexer) ignore() {
l.start = l.pos
}
// errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.nextItem.
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.items <- item{itemError, l.start, fmt.Sprintf(format, args...)}
return nil
}
// nextItem returns the next item from the input.
func (l *lexer) nextItem() item {
item := <-l.items
l.lastPos = item.pos
return item
}
func lex(input string) *lexer {
l := &lexer{
input: input,
items: make(chan item),
}
go l.run()
return l
}
func (l *lexer) run() {
for l.state = lexEnv; l.state != nil; {
l.state = l.state(l)
}
}
func lexEnv(l *lexer) stateFn {
var r rune
for {
switch r = l.next(); {
case r == eof:
l.emit(itemEOF)
return nil
case isAlphaNumericUnderscore(r):
return lexVariable
case r == '\n':
buffer := l.input[l.start:l.pos]
if buffer == "\n" {
if l.peek() == '\n' {
l.next()
l.emit(itemEndSplit)
}
l.ignore()
}
case r == '\t':
l.ignore()
case r == ' ':
l.ignore()
case r == '#':
return lexComment
default:
l.errorf("unable to parse character: %c", r)
}
}
}
func lexComment(l *lexer) stateFn {
for {
switch l.next() {
case '\n':
l.ignore()
return lexEnv
case eof:
l.emit(itemEOF)
return nil
}
}
}
func lexVariable(l *lexer) stateFn {
for {
switch r := l.next(); {
case isAlphaNumericUnderscore(r):
// absorb
case r == ' ' && l.peek() == '=':
l.backup()
variable := l.input[l.start:l.pos]
// strip arch from source_arch like constructs
witharch := strings.SplitN(variable, "_", 2)
if len(witharch) == 2 {
variable = witharch[0]
}
if _, ok := variables[variable]; ok {
l.emit(variables[variable])
// TODO to cut off ' = '
l.next()
l.next()
l.next()
l.ignore()
return lexValue
}
return l.errorf("invalid variable: %s", variable)
default:
pattern := l.input[l.start:l.pos]
return l.errorf("invalid pattern: %s", pattern)
}
}
}
func lexValue(l *lexer) stateFn {
for {
switch l.next() {
case '\n':
l.backup()
l.emit(itemValue)
return lexEnv
}
}
}
// isAlphaNumericUnderscore reports whether r is an alphabetic, digit, or underscore.
func isAlphaNumericUnderscore(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
}