-
Notifications
You must be signed in to change notification settings - Fork 6
/
lex.go
380 lines (342 loc) · 9.55 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package mql
import (
"bufio"
"bytes"
"fmt"
"strings"
"unicode"
)
// Delimiter used to quote strings
type Delimiter rune
const (
DoubleQuote Delimiter = '"'
SingleQuote Delimiter = '\''
Backtick Delimiter = '`'
backslash = '\\'
)
type lexStateFunc func(*lexer) (lexStateFunc, error)
type lexer struct {
source *bufio.Reader
current stack[rune]
tokens chan token
state lexStateFunc
}
func newLexer(s string) *lexer {
l := &lexer{
source: bufio.NewReader(strings.NewReader(s)),
state: lexStartState,
tokens: make(chan token, 1), // define a ring buffer for emitted tokens
}
return l
}
// nextToken is the external api for the lexer and it simply returns the next
// token or an error. If EOF is encountered while scanning, nextToken will keep
// returning an eofToken no matter how many times you call nextToken.
func (l *lexer) nextToken() (token, error) {
for {
select {
case tk := <-l.tokens: // return a token if one has been emitted
return tk, nil
default: // otherwise, keep scanning via the next state
var err error
if l.state, err = l.state(l); err != nil {
return token{}, err
}
}
}
}
// lexStartState is the start state. It doesn't emit tokens, but rather
// transitions to other states. Other states typically transition back to
// lexStartState after they emit a token.
func lexStartState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexStartState", "lexer")
r := l.read()
switch {
// wait, if it's eof we're done
case r == eof:
l.emit(eofToken, "")
return lexEofState, nil
// start with finding all tokens that can have a trailing "="
case r == '>':
return lexGreaterState, nil
case r == '<':
return lexLesserState, nil
// now, we can just look at the next rune...
case r == '%':
return lexContainsState, nil
case r == '=':
return lexEqualState, nil
case r == '!':
return lexNotEqualState, nil
case r == ')':
return lexRightParenState, nil
case r == '(':
return lexLeftParenState, nil
case isSpace(r):
return lexWhitespaceState, nil
case unicode.IsDigit(r) || r == '.':
l.unread()
return lexNumberState, nil
case isDelimiter(r):
l.unread()
return lexStringState, nil
default:
l.unread()
return lexSymbolState, nil
}
}
// lexStringState scans for strings and can emit a stringToken
func lexStringState(l *lexer) (lexStateFunc, error) {
const op = "mql.lexStringState"
panicIfNil(l, "lexStringState", "lexer")
defer l.current.clear()
// we'll push the runes we read into this buffer and when appropriate will
// emit tokens using the buffer's data.
var tokenBuf bytes.Buffer
// before we start looping, let's found out if we're scanning a quoted string
r := l.read()
delimiter := r
if !isDelimiter(delimiter) {
return nil, fmt.Errorf("%s: %w %q", op, ErrInvalidDelimiter, delimiter)
}
finalDelimiter := false
WriteToBuf:
// keep reading runes into the buffer until we encounter eof or the final delimiter.
for {
r = l.read()
switch {
case r == eof:
break WriteToBuf
case r == backslash:
nextR := l.read()
switch {
case nextR == eof:
tokenBuf.WriteRune(r)
return nil, fmt.Errorf("%s: %w in %q", op, ErrInvalidTrailingBackslash, tokenBuf.String())
case nextR == backslash:
tokenBuf.WriteRune(nextR)
case nextR == delimiter:
tokenBuf.WriteRune(nextR)
default:
tokenBuf.WriteRune(r)
tokenBuf.WriteRune(nextR)
}
case r == delimiter: // end of the quoted string we're scanning
finalDelimiter = true
break WriteToBuf
default: // otherwise, write the rune into the keyword buffer
tokenBuf.WriteRune(r)
}
}
switch {
case !finalDelimiter:
return nil, fmt.Errorf("%s: %w for \"%s", op, ErrMissingEndOfStringTokenDelimiter, tokenBuf.String())
default:
l.emit(stringToken, tokenBuf.String())
return lexStartState, nil
}
}
// lexSymbolState scans for strings and can emit the following tokens:
// orToken, andToken, containsToken
func lexSymbolState(l *lexer) (lexStateFunc, error) {
const op = "mql.lexSymbolState"
panicIfNil(l, "lexSymbolState", "lexer")
defer l.current.clear()
ReadRunes:
// keep reading runes into the buffer until we encounter eof of non-text runes.
for {
r := l.read()
switch {
case r == eof:
break ReadRunes
case (isSpace(r) || isSpecial(r)): // whitespace or a special char
l.unread()
break ReadRunes
default:
continue ReadRunes
}
}
switch strings.ToLower(runesToString(l.current)) {
case "and":
l.emit(andToken, "and")
return lexStartState, nil
case "or":
l.emit(orToken, "or")
return lexStartState, nil
default:
l.emit(symbolToken, runesToString(l.current))
return lexStartState, nil
}
}
func lexNumberState(l *lexer) (lexStateFunc, error) {
const op = "mql.lexNumberState"
defer l.current.clear()
isFloat := false
// we'll push the runes we read into this buffer and when appropriate will
// emit tokens using the buffer's data.
var buf []rune
WriteToBuf:
// keep reading runes into the buffer until we encounter eof of non-number runes.
for {
r := l.read()
switch {
case r == eof:
break WriteToBuf
case r == '.' && isFloat:
buf = append(buf, r)
return nil, fmt.Errorf("%s: %w in %q", op, ErrInvalidNumber, string(buf))
case r == '.' && !isFloat:
isFloat = true
buf = append(buf, r)
case unicode.IsDigit(r) || (r == '.' && len(buf) == 0):
buf = append(buf, r)
default:
l.unread()
break WriteToBuf
}
}
l.emit(numberToken, string(buf))
return lexStartState, nil
}
// lexContainsState emits an containsToken and returns to the lexStartState
func lexContainsState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexContainsState", "lexer")
defer l.current.clear()
l.emit(containsToken, "%")
return lexStartState, nil
}
// lexEqualState emits an equalToken and returns to the lexStartState
func lexEqualState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexEqualState", "lexer")
defer l.current.clear()
l.emit(equalToken, "=")
return lexStartState, nil
}
// lexNotEqualState scans for a notEqualToken and return either to the lexStartState or
// lexErrorState
func lexNotEqualState(l *lexer) (lexStateFunc, error) {
const op = "mql.lexNotEqualState"
panicIfNil(l, "lexNotEqualState", "lexer")
defer l.current.clear()
nextRune := l.read()
switch nextRune {
case '=':
l.emit(notEqualToken, "!=")
return lexStartState, nil
default:
return nil, fmt.Errorf("%s: %w, got %q", op, ErrInvalidNotEqual, fmt.Sprintf("%s%s", "!", string(nextRune)))
}
}
// lexLeftParenState emits a startLogicalExprToken and returns to the
// lexStartState
func lexLeftParenState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexLeftParenState", "lexer")
defer l.current.clear()
l.emit(startLogicalExprToken, runesToString(l.current))
return lexStartState, nil
}
// lexRightParenState emits an endLogicalExprToken and returns to the
// lexStartState
func lexRightParenState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexRightParenState", "lexer")
defer l.current.clear()
l.emit(endLogicalExprToken, runesToString(l.current))
return lexStartState, nil
}
// lexWhitespaceState emits a whitespaceToken and returns to the lexStartState
func lexWhitespaceState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexWhitespaceState", "lexer")
defer l.current.clear()
ReadWhitespace:
for {
ch := l.read()
switch {
case ch == eof:
break ReadWhitespace
case !isSpace(ch):
l.unread()
break ReadWhitespace
}
}
l.emit(whitespaceToken, "")
return lexStartState, nil
}
// lexGreaterState will emit either a greaterThanToken or a
// greaterThanOrEqualToken and return to the lexStartState
func lexGreaterState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexGreaterState", "lexer")
defer l.current.clear()
next := l.read()
switch next {
case '=':
l.emit(greaterThanOrEqualToken, ">=")
return lexStartState, nil
default:
l.unread()
l.emit(greaterThanToken, ">")
return lexStartState, nil
}
}
// lexLesserState will emit either a lessThanToken or a lessThanOrEqualToken and
// return to the lexStartState
func lexLesserState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexLesserState", "lexer")
defer l.current.clear()
next := l.read()
switch next {
case '=':
l.emit(lessThanOrEqualToken, "<=")
return lexStartState, nil
default:
l.unread()
l.emit(lessThanToken, "<")
return lexStartState, nil
}
}
// lexEofState will emit an eofToken and returns right back to the lexEofState
func lexEofState(l *lexer) (lexStateFunc, error) {
panicIfNil(l, "lexEofState", "lexer")
l.emit(eofToken, "")
return lexEofState, nil
}
// emit send a token to the lexer's token channel
func (l *lexer) emit(t tokenType, v string) {
l.tokens <- token{
Type: t,
Value: v,
}
}
// isSpace reports if r is a space
func isSpace(r rune) bool {
return r == ' ' || r == '\t' || r == '\r' || r == '\n'
}
// isSpecial reports r is special rune
func isSpecial(r rune) bool {
return r == '=' || r == '>' || r == '!' || r == '<' || r == '(' || r == ')' || r == '%'
}
// read the next rune
func (l *lexer) read() rune {
ch, _, err := l.source.ReadRune()
if err != nil {
return eof
}
l.current.push(ch)
return ch
}
// unread the last rune read which means that rune will be returned the next
// time lexer.read() is called. unread also removes the last rune from the
// lexer's stack of current runes
func (l *lexer) unread() {
_ = l.source.UnreadRune() // error ignore which only occurs when nothing has been previously read
_, _ = l.current.pop()
}
func isDelimiter(r rune) bool {
switch Delimiter(r) {
case DoubleQuote, SingleQuote, Backtick:
return true
default:
return false
}
}