-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
340 lines (279 loc) · 6.12 KB
/
lexer.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
package equ
import (
"fmt"
"strings"
"unicode/utf8"
)
type lexerStateFn func(l *lexer) lexerStateFn
const errorContextLength = 5
const (
tokenFiltersStart = "["
tokenFiltersEnd = "]"
tokenOr = "|"
tokenAnd = ","
tokenBundleStart = "("
tokenBundleEnd = ")"
tokenFilterIs = ":"
tokenValidPath = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._`
tokenValidFilterType = `abcdefghijklmnopqrstuvwxyz`
tokenValidNumberStart = `.+-0123456789`
tokenValidNumberDigit = `0123456789`
eof = rune(0)
)
type lexer struct {
input string
items chan Item
done chan bool
width int
start int
pos int
cancel chan bool
}
func (l *lexer) run() {
state := lexFilters
for state != nil {
state = state(l)
}
l.done <- true
}
func (l *lexer) emit(t ItemType) bool {
value := l.input[l.start:l.pos]
// If we got a string, we need to clear escaped characters
if t == itemString {
value = strings.ReplaceAll(value, `\\`, `\`)
value = strings.ReplaceAll(value, `\"`, `"`)
}
select {
case l.items <- Item{t, value, l.start}:
l.start = l.pos
return true
case <-l.cancel:
return false
}
}
func (l *lexer) next() (r rune) {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width
return r
}
func (l *lexer) accept(valid string) bool {
if strings.IndexRune(valid, l.next()) >= 0 {
return true
}
l.backup()
return false
}
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
func (l *lexer) acceptRun(valid string) {
for strings.IndexRune(valid, l.next()) >= 0 {
}
l.backup()
}
func (l *lexer) acceptRunUntil(rs ...rune) bool {
for {
next := l.next()
if next == eof {
l.backup()
return false
}
for _, r := range rs {
if next == r {
l.backup()
return true
}
}
}
}
func (l *lexer) ignore() {
l.start = l.pos
}
func (l *lexer) backup() {
l.pos -= l.width
}
func lexFilters(l *lexer) lexerStateFn {
if string(l.peek()) == tokenBundleStart {
return lexToken(tokenBundleStart, itemBundleStart, lexFilters)
}
l.acceptRun(tokenValidPath)
if l.pos <= l.start {
return l.errorf(l.currentContext(), itemPath.String())
}
if !l.emit(itemPath) {
return nil
}
if string(l.peek()) != tokenFiltersStart {
return l.errorf(l.currentContext(), tokenFiltersStart)
}
return lexToken(tokenFiltersStart, itemFiltersStart, lexInsideFilters)
}
func lexInsideFilters(l *lexer) lexerStateFn {
if string(l.peek()) == tokenBundleStart {
return lexToken(tokenBundleStart, itemBundleStart, lexInsideFilters)
}
l.acceptRun(tokenValidFilterType)
if !l.emit(itemFilter) {
return nil
}
if string(l.next()) != tokenFilterIs {
return l.errorf(l.currentContext(), tokenFilterIs)
}
// Skip the :
l.ignore()
return lexFilterValue
}
func lexFilterValue(l *lexer) lexerStateFn {
next := l.peek()
if strings.IndexRune(tokenValidNumberStart, next) >= 0 {
return lexNumberValue
} else if next == '"' {
// Skip the opening quotes
l.next()
l.ignore()
return lexStringValue
}
return l.errorf(l.currentContext(), itemFilter.String())
}
func lexStringValue(l *lexer) lexerStateFn {
for {
found := l.acceptRunUntil('\\', '"')
if !found {
return l.errorf(l.currentContext(), itemString.String())
}
// If the next rune is only ", the string is closed
next := l.peek()
if next == '"' {
if !l.emit(itemString) {
return nil
}
// Skip the closing quotes
l.next()
l.ignore()
return lexAfterFilterValue
}
if next == '\\' {
// Check what we're escaping. First, skip the \
l.next()
next = l.peek()
// If we're escaping a \ or ", just keep going, they're filtered
// out when we emit
if next == '\\' || next == '"' {
l.next()
} else {
return l.errorf(`\`+string(next), "escapeSequence")
}
}
}
}
func lexNumberValue(l *lexer) lexerStateFn {
l.accept("+-")
// We can only have one decimal dot
decimalOccured := false
if l.accept(".") {
// Found it!
decimalOccured = true
}
l.acceptRun(tokenValidNumberDigit)
if l.accept(".") {
if decimalOccured {
// Found a second decimal point
return l.errorf(l.input[l.start:l.pos], itemNumber.String())
}
l.acceptRun(tokenValidNumberDigit)
}
if !l.emit(itemNumber) {
return nil
}
return lexAfterFilterValue
}
func lexAfterFilterValue(l *lexer) lexerStateFn {
next := l.next()
switch string(next) {
case tokenOr:
if !l.emit(itemOr) {
return nil
}
case tokenAnd:
if !l.emit(itemAnd) {
return nil
}
case tokenFiltersEnd:
l.backup()
return lexToken(tokenFiltersEnd, itemFiltersEnd, lexAfterFilter)
case tokenBundleEnd:
l.backup()
return lexToken(tokenBundleEnd, itemBundleEnd, lexAfterFilterValue)
default:
return l.errorf(string(next), "combinator")
}
return lexInsideFilters
}
func lexAfterFilter(l *lexer) lexerStateFn {
next := l.next()
if next == eof {
if !l.emit(itemEOF) {
return nil
}
return nil
}
switch string(next) {
case tokenOr:
if !l.emit(itemOr) {
return nil
}
case tokenAnd:
if !l.emit(itemAnd) {
return nil
}
case tokenBundleEnd:
l.backup()
return lexToken(tokenBundleEnd, itemBundleEnd, lexAfterFilter)
default:
return l.errorf(string(next), "combinator")
}
return lexFilters
}
func lexToken(tv string, it ItemType, next lexerStateFn) func(l *lexer) lexerStateFn {
return func(l *lexer) lexerStateFn {
l.pos += len(tv)
if !l.emit(it) {
return nil
}
return next
}
}
func (l *lexer) errorf(context string, expected ...string) lexerStateFn {
var msg string
if len(expected) == 1 {
msg = fmt.Sprintf("expected: `%s`", expected[0])
} else {
msg = fmt.Sprintf("expected one of: `%s`", strings.Join(expected, ","))
}
msg = fmt.Sprintf("%s, got `%s`", msg, context)
l.items <- Item{itemError, msg, l.pos}
return nil
}
func (l *lexer) currentContext() string {
context := l.input[l.start:]
if len(context) > errorContextLength {
return context[:errorContextLength]
}
return context
}
func lex(input string) *lexer {
l := &lexer{
input: input,
items: make(chan Item, 5),
done: make(chan bool, 1),
cancel: make(chan bool, 1),
}
go l.run()
return l
}