-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.go
282 lines (239 loc) · 5.7 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
package lql
import (
"bytes"
"io"
"strconv"
"strings"
bsq "github.com/blugelabs/bluge"
)
type lex struct {
// Input string from the user
input *bytes.Reader
errs []string
// search is the end result of the lexer parser and what is given to the user
query bsq.Query
fields []string
// Hold current value of strings and other multi-byte operators (and, or, not, ...)
lval string
lastToken int
inQuote bool
inEscape bool
}
// Lex parses tokens and if needed sets the lval which will
// be what is used in the $1, $2, ..., $n vars in your goyacc
// parser.y file.
func (l *lex) Lex(lval *yySymType) int {
var b byte
var err error
for {
b, err = l.input.ReadByte()
if err == io.EOF {
// not a real error
return 0
}
if err != nil {
l.errs = append(l.errs, err.Error())
return 0
}
// Keep going until we get non whitespace
if l.byteToToken(b) != tWHITESPACE {
break
}
}
token := l.byteToToken(b)
if rtoken := l.singleByteTokenToRetVal(token); rtoken != 0 {
l.lastToken = rtoken
return rtoken
}
// Looks like we are in a string and we will now keep
// eating bytes until we are to the end of the string.
// Then hack off any whitespace which should only happen
// at the start of the string.
var retStr string
if token == tDOUBLEQUOTE {
l.inQuote = true
retStr = l.eatString()
} else if token == tBACKSLASH {
l.inEscape = true
retStr = l.eatString()
} else {
retStr = string(b) + l.eatString()
}
// DEBUG: fmt.Println("RET:", retStr)
// Always trim any space between the string and the next token
retStr = strings.TrimSpace(retStr)
// Check if this is a multi byte token such as and or or before just
// freely passing it back and calling it a string.
if rtoken := l.multiByteTokenToRetVal(retStr); rtoken != 0 {
l.lastToken = rtoken
return rtoken
}
// Check if the string should be converted to a number before being passed back
// something in a quote is always a string
var retToken int
if l.inQuote {
// Set the left val for the parser to use
lval.str = retStr
retToken = tSTRING
} else {
num, err := l.isStringValidNumber(retStr)
if err != nil {
lval.str = retStr
retToken = tSTRING
} else {
lval.num = num
retToken = tNUMBER
}
}
// Clear everything for next Lex call and if it's a tNUMBER or tSTRING just
// return tSTRING for now. This should be cleaned up.
l.lastToken = tSTRING
l.inQuote = false
l.inEscape = false
return retToken
}
// Add a field that will limit the amount of field returned
// from our search.
func (l *lex) addReturnField(field string) {
l.fields = append(l.fields, field)
}
// Final method that is called by the lexer to set
// the query that has been built.
func (l *lex) setSearchQuery(q bsq.Query) {
l.query = q
}
func (l *lex) eatString() string {
var retVal string
for {
b, err := l.input.ReadByte()
if err != nil {
return retVal
}
token := l.byteToToken(b)
if token == tBACKSLASH && !l.inEscape {
l.inEscape = true
} else {
// Hit the end of a double quote so cleanup and return
// without including the " in the retVal
if !l.inEscape {
if token == tDOUBLEQUOTE && l.inQuote {
break
}
// If we hit a known token we are no longer in a string
// and should backout the last read byte and break
if token != tSTRING && !l.inQuote {
l.input.UnreadByte()
break
}
}
retVal = retVal + string(b)
l.inEscape = false
}
}
return retVal
}
func (l *lex) multiByteTokenToRetVal(val string) int {
// check if we are on the right hand side of a k/v
// pair before testing the value. or k=and causes issues
switch l.lastToken {
case tEQUAL, tGREATERTHAN,
tLESSTHAN, tTILDE, tAND, tOR:
return 0
case 0: // Last token has not been set
return 0
}
switch val {
case "returns":
return tRETURNS
case "and":
return tAND
case "or":
return tOR
}
return 0
}
// given a single token decide what if anything should be returned
// to the parser.
func (l *lex) singleByteTokenToRetVal(t int) int {
if l.lastToken == tEQUAL && t == tLSQUAREBRACKET {
return tLSQUAREBRACKET
}
// Special case for <= and >=
if (l.lastToken == tGREATERTHAN || l.lastToken == tLESSTHAN) && t == tEQUAL {
return tEQUAL
}
// !~
if l.lastToken == tEXCLAMATION && t == tTILDE {
return tTILDE
}
// =~
if l.lastToken == tEQUAL && t == tTILDE {
return tTILDE
}
// check if we are on the right hand side of a k/v
// pair before testing the value. or k=\" causes issues
switch l.lastToken {
case tEQUAL, tGREATERTHAN,
tLESSTHAN, tTILDE:
return 0
}
// tSTRING and tDOUBLEQUOTE mean that we are about to enter
// into a string.
if t != tSTRING && t != tDOUBLEQUOTE {
return t
}
return 0
}
// token is for checking tokens that can be decided by only looking
// at a single byte and possibly some other context.
func (l *lex) byteToToken(b byte) int {
// always eat whitespace
if b == ' ' {
return tWHITESPACE
}
switch b {
case '~':
return tTILDE
case '*':
return tASTERISK
case '>':
return tGREATERTHAN
case '<':
return tLESSTHAN
case '(':
return tLBRACKET
case ')':
return tRBRACKET
case '=':
return tEQUAL
case '\\':
return tBACKSLASH
case '"':
return tDOUBLEQUOTE
case '!':
return tEXCLAMATION
case ',':
return tCOMMA
case '[':
return tLSQUAREBRACKET
case ']':
return tRSQUAREBRACKET
default:
return tSTRING
}
}
func (l *lex) Error(s string) {
l.errs = append(l.errs, s)
}
func (l *lex) isStringValidNumber(val string) (float64, error) {
// Special case to support decimals that use a comma which is common
// in many places.
if strings.Count(val, ",") == 1 {
val = strings.ReplaceAll(val, ",", ".")
}
lval, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, err
}
return lval, nil
}