forked from perkeep/perkeep
/
lexer.go
315 lines (275 loc) · 5.28 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
/*
Copyright 2014 The Camlistore Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This is the lexer for search expressions (see expr.go).
package search
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type tokenType int
const (
tokenAnd tokenType = iota
tokenArg
tokenClose
tokenColon
tokenEOF
tokenError
tokenLiteral
tokenNot
tokenOpen
tokenOr
tokenPredicate
tokenQuotedArg
tokenQuotedLiteral
)
const (
eof = -1 // -1 is unused in utf8
whitespace = "\t\n\f\v\r "
opBound = whitespace + "("
)
// IsSearchWordRune defines the runes that can be used in unquoted predicate arguments
// or unquoted literals. These are all non-space unicode characters except ':' which is
// used for predicate marking, and '(', ')', which are used for predicate grouping.
func isSearchWordRune(r rune) bool {
switch r {
case ':', ')', '(', eof:
return false
}
return !unicode.IsSpace(r)
}
type token struct {
typ tokenType
val string
start int
}
func (t token) String() string {
switch t.typ {
case tokenEOF:
return "EOF"
case tokenError:
return fmt.Sprintf("{err:%q at pos: %d}", t.val, t.start)
}
return fmt.Sprintf("{t:%v,%q (col: %d)}", t.typ, t.val, t.start)
}
type lexer struct {
input string
start int
pos int
width int
tokens chan token
state stateFn
}
func (l *lexer) emit(typ tokenType) {
l.tokens <- token{typ, l.input[l.start:l.pos], l.start}
l.start = l.pos
}
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
}
func (l *lexer) ignore() {
l.start = l.pos
}
func (l *lexer) backup() {
l.pos -= l.width
}
func (l *lexer) peek() rune {
r := l.next()
l.backup()
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) acceptString(s string) bool {
for _, r := range s {
if l.next() != r {
l.backup()
return false
}
}
return true
}
func (l *lexer) acceptRun(valid string) {
for strings.IndexRune(valid, l.next()) >= 0 {
}
l.backup()
}
func (l *lexer) acceptRunFn(valid func(rune) bool) {
for valid(l.next()) {
}
l.backup()
}
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.tokens <- token{
typ: tokenError,
val: fmt.Sprintf(format, args...),
start: l.start,
}
return nil
}
func lex(input string) (*lexer, chan token) {
l := &lexer{
input: input,
tokens: make(chan token),
state: readExp,
}
go l.run()
return l, l.tokens
}
func (l *lexer) run() {
for {
if l.state == nil {
close(l.tokens)
return
}
l.state = l.state(l)
}
}
//
// State functions
//
type stateFn func(*lexer) stateFn
func readNeg(l *lexer) stateFn {
l.accept("-")
l.emit(tokenNot)
return readExp
}
func readClose(l *lexer) stateFn {
l.accept(")")
l.emit(tokenClose)
return readOperator
}
func readOpen(l *lexer) stateFn {
l.accept("(")
l.emit(tokenOpen)
return readExp
}
func readColon(l *lexer) stateFn {
l.accept(":")
l.emit(tokenColon)
return readArg
}
func readPredicate(l *lexer) stateFn {
l.acceptRunFn(unicode.IsLetter)
switch l.peek() {
case ':':
l.emit(tokenPredicate)
return readColon
}
return readLiteral
}
func readLiteral(l *lexer) stateFn {
l.acceptRunFn(isSearchWordRune)
l.emit(tokenLiteral)
return readOperator
}
func readArg(l *lexer) stateFn {
if l.peek() == '"' {
return readQuotedArg
}
l.acceptRunFn(isSearchWordRune)
l.emit(tokenArg)
if l.peek() == ':' {
return readColon
}
return readOperator
}
func readAND(l *lexer) stateFn {
if l.acceptString("and") && l.accept(opBound) {
l.backup()
l.emit(tokenAnd)
return readExp
} else {
return readPredicate
}
}
func readOR(l *lexer) stateFn {
if l.acceptString("or") && l.accept(opBound) {
l.backup()
l.emit(tokenOr)
return readExp
} else {
return readPredicate
}
}
func runQuoted(l *lexer) bool {
l.accept("\"")
for {
r := l.next()
switch r {
case eof:
return false
case '\\':
l.next()
case '"':
return true
}
}
}
func readQuotedLiteral(l *lexer) stateFn {
if !runQuoted(l) {
return l.errorf("Unclosed quote")
}
l.emit(tokenQuotedLiteral)
return readOperator
}
func readQuotedArg(l *lexer) stateFn {
if !runQuoted(l) {
return l.errorf("Unclosed quote")
}
l.emit(tokenQuotedArg)
if l.peek() == ':' {
return readColon
}
return readOperator
}
func readExp(l *lexer) stateFn {
l.acceptRun(whitespace)
l.ignore()
switch l.peek() {
case eof:
return nil
case '(':
return readOpen
case ')':
return readClose
case '-':
return readNeg
case '"':
return readQuotedLiteral
}
return readPredicate
}
func readOperator(l *lexer) stateFn {
l.acceptRun(whitespace)
l.ignore()
switch l.peek() {
case 'a':
return readAND
case 'o':
return readOR
}
return readExp
}