-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlex.go
251 lines (225 loc) · 3.76 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
package qs
import (
"strings"
"unicode"
"unicode/utf8"
)
// TODO: handle escaping special characters:
type tokType int
const (
tEOF tokType = iota
tERROR
tLITERAL
tQUOTED
tPLUS
tMINUS
tCOLON
tEQUAL
tGREATER
tLESS
tOR
tAND
tNOT
tLPAREN
tRPAREN
tLSQUARE
tRSQUARE
tLBRACE
tRBRACE
tTO
tBOOST
tFUZZY
)
func (t tokType) String() string {
tokTypes := map[tokType]string{
tEOF: "tEOF",
tERROR: "tERROR",
tLITERAL: "tLITERAL",
tQUOTED: "tQUOTED",
tOR: "tOR",
tAND: "tAND",
tNOT: "tNOT",
tTO: "tTO",
tPLUS: "tPLUS",
tMINUS: "tMINUS",
tCOLON: "tCOLON",
tEQUAL: "tEQUAL",
tGREATER: "tGREATER",
tLESS: "tLESS",
tLPAREN: "tLPAREN",
tRPAREN: "tRPAREN",
tLSQUARE: "tLSQUARE",
tRSQUARE: "tRSQUARE",
tLBRACE: "tLBRACE",
tRBRACE: "tRBRACE",
tBOOST: "tBOOST",
tFUZZY: "tFUZZY",
}
return tokTypes[t]
}
// some single-rune tokens
var singles = map[rune]tokType{
'(': tLPAREN,
')': tRPAREN,
'[': tLSQUARE,
']': tRSQUARE,
'{': tLBRACE,
'}': tRBRACE,
':': tCOLON,
'+': tPLUS,
'-': tMINUS,
'=': tEQUAL,
'>': tGREATER,
'<': tLESS,
}
type token struct {
typ tokType
val string
pos int
}
type stateFn func(*lexer) stateFn
type lexer struct {
input string
tokens []token
pos int
prevpos int
start int
}
// lex takes an input string and breaks it up into an array of tokens.
// The last token will be an tEOF, unless an error occurs, in which case
// it will be a tERROR.
func lex(input string) []token {
l := &lexer{
input: input,
tokens: []token{},
}
// run state machine - each state returns the next state, or nil when finished
for state := lexDefault; state != nil; {
state = state(l)
}
return l.tokens
}
func (l *lexer) next() rune {
l.prevpos = l.pos
if l.eof() {
return 0
}
r, w := utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += w
return r
}
func (l *lexer) eof() bool {
return l.pos >= len(l.input)
}
func (l *lexer) ignore() {
l.start = l.pos
}
func (l *lexer) backup() {
l.pos = l.prevpos
}
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
func (l *lexer) emit(t tokType) {
l.tokens = append(l.tokens, token{t, l.input[l.start:l.pos], l.start})
l.start = l.pos
}
func (l *lexer) emitError(msg string) {
l.tokens = append(l.tokens, token{tERROR, msg, l.start})
l.start = l.pos
}
func lexDefault(l *lexer) stateFn {
// skip space
for {
if l.eof() {
l.emit(tEOF)
return nil
}
r := l.next()
if !unicode.IsSpace(r) {
l.backup()
l.ignore()
if r == '~' || r == '^' {
return lexSuffix
}
if typ, got := singles[r]; got {
l.next()
l.emit(typ)
return lexDefault
}
if r == '"' || r == '\'' {
return lexQuoted
}
return lexText
}
}
}
func lexText(l *lexer) stateFn {
// non-space characters which terminate a literal
//stopChars := `&|:(){}[]^~`
stopChars := `:(){}[]^~`
for {
if l.eof() {
break
}
r := l.next()
if unicode.IsSpace(r) || strings.ContainsRune(stopChars, r) {
l.backup()
break
}
}
switch l.input[l.start:l.pos] {
case "OR":
l.emit(tOR)
case "AND":
l.emit(tAND)
case "NOT":
l.emit(tNOT)
case "TO":
l.emit(tTO)
default:
l.emit(tLITERAL)
}
return lexDefault
}
func lexQuoted(l *lexer) stateFn {
q := l.next()
for {
if l.eof() {
l.emitError("unclosed quote")
return nil
}
r := l.next()
if r == q {
break
}
}
l.emit(tQUOTED)
return lexDefault
}
func lexSuffix(l *lexer) stateFn {
kind := l.next() // '^' or '~'
// number (optional)
for {
if l.eof() {
break
}
r := l.next()
if unicode.IsSpace(r) {
l.backup()
break
}
if !strings.ContainsRune("0123456789.", r) {
l.emitError("bad number")
}
}
switch kind {
case '~':
l.emit(tFUZZY)
case '^':
l.emit(tBOOST)
}
return lexDefault
}