forked from rqlite/sql
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lexer.go
365 lines (323 loc) · 6.76 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
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
package sqlparser
import (
"bufio"
"bytes"
"io"
"strings"
"unicode"
)
type Lexer struct {
r io.RuneReader
buf bytes.Buffer
ch rune
pos Pos
full bool
}
func NewLexer(r io.Reader) *Lexer {
return &Lexer{
r: bufio.NewReader(r),
pos: Pos{Offset: -1, Line: 1},
}
}
func (l *Lexer) Lex() (pos Pos, token Token, lit string) {
for {
if ch := l.peek(); ch == -1 {
return l.pos, EOF, ""
} else if unicode.IsSpace(ch) {
l.read()
continue
} else if isDigit(ch) || ch == '.' {
return l.lexNumber()
} else if ch == 'x' || ch == 'X' {
return l.lexBlob()
} else if isAlpha(ch) || ch == '_' {
return l.lexUnquotedIdent(l.pos, "")
} else if ch == '"' || ch == '`' {
return l.lexQuotedIdent(ch)
} else if ch == '\'' {
return l.lexString()
} else if ch == '?' || ch == ':' || ch == '@' || ch == '$' {
return l.lexBind()
}
switch ch, pos := l.read(); ch {
case ';':
return pos, SEMI, ";"
case '(':
return pos, LP, "("
case ')':
return pos, RP, ")"
case ',':
return pos, COMMA, ","
case '!':
if l.peek() == '=' {
l.read()
return pos, NE, "!="
}
return pos, BITNOT, "!"
case '=':
return pos, EQ, "="
case '<':
if l.peek() == '=' {
l.read()
return pos, LE, "<="
} else if l.peek() == '<' {
l.read()
return pos, LSHIFT, "<<"
} else if l.peek() == '>' {
l.read()
return pos, LG, "<>"
}
return pos, LT, "<"
case '>':
if l.peek() == '=' {
l.read()
return pos, GE, ">="
} else if l.peek() == '>' {
l.read()
return pos, RSHIFT, ">>"
}
return pos, GT, ">"
case '&':
return pos, BITAND, "&"
case '|':
if l.peek() == '|' {
l.read()
return pos, CONCAT, "||"
}
return pos, BITOR, "|"
case '+':
return pos, PLUS, "+"
case '-':
return pos, MINUS, "-"
case '*':
return pos, STAR, "*"
case '/':
if l.peek() == '*' {
return l.lexMultilineComment()
}
return pos, SLASH, "/"
case '%':
return pos, REM, "%"
default:
return pos, ILLEGAL, string(ch)
}
}
}
func (l *Lexer) lexUnquotedIdent(pos Pos, prefix string) (Pos, Token, string) {
assert(isUnquotedIdent(l.peek()))
l.buf.Reset()
l.buf.WriteString(prefix)
for ch, _ := l.read(); isUnquotedIdent(ch); ch, _ = l.read() {
l.buf.WriteRune(ch)
}
l.unread()
lit := l.buf.String()
tok := Lookup(lit)
return pos, tok, lit
}
func (l *Lexer) lexQuotedIdent(char rune) (Pos, Token, string) {
ch, pos := l.read()
assert(ch == char)
l.buf.Reset()
l.buf.WriteRune(char)
for {
ch, _ := l.read()
if ch == -1 {
return pos, ILLEGAL, l.buf.String()
} else if ch == char {
if l.peek() == char { // escaped quote
l.read()
l.buf.WriteRune(char)
continue
}
l.buf.WriteRune(char)
return pos, QIDENT, l.buf.String()
}
l.buf.WriteRune(ch)
}
}
func (l *Lexer) lexString() (Pos, Token, string) {
ch, pos := l.read()
assert(ch == '\'')
l.buf.Reset()
for {
ch, _ := l.read()
if ch == -1 {
return pos, ILLEGAL, `'` + l.buf.String()
} else if ch == '\'' {
if l.peek() == '\'' { // escaped quote
l.read()
l.buf.WriteRune('\'')
continue
}
return pos, STRING, l.buf.String()
}
l.buf.WriteRune(ch)
}
}
func (l *Lexer) lexMultilineComment() (Pos, Token, string) {
ch, pos := l.read()
assert(ch == '*')
l.buf.Reset()
for {
ch, _ := l.read()
if ch == -1 {
return pos, ILLEGAL, `/*` + l.buf.String()
} else if ch == '*' {
if l.peek() == '/' {
l.read()
l.read()
return pos, MLCOMMENT, strings.Trim(l.buf.String(), " ")
}
}
l.buf.WriteRune(ch)
}
}
func (l *Lexer) lexBind() (Pos, Token, string) {
start, pos := l.read()
l.buf.Reset()
l.buf.WriteRune(start)
// Question mark starts a numeric bind.
if start == '?' {
for isDigit(l.peek()) {
ch, _ := l.read()
l.buf.WriteRune(ch)
}
return pos, BIND, l.buf.String()
}
// All other characters start an alphanumeric bind.
assert(start == ':' || start == '@' || start == '$')
for isUnquotedIdent(l.peek()) {
ch, _ := l.read()
l.buf.WriteRune(ch)
}
return pos, BIND, l.buf.String()
}
func (l *Lexer) lexBlob() (Pos, Token, string) {
start, pos := l.read()
assert(start == 'x' || start == 'X')
// If the next character is not a quote, it's an IDENT.
if isUnquotedIdent(l.peek()) {
return l.lexUnquotedIdent(pos, string(start))
} else if l.peek() != '\'' {
return pos, IDENT, string(start)
}
ch, _ := l.read()
assert(ch == '\'')
l.buf.Reset()
for i := 0; ; i++ {
ch, _ := l.read()
if ch == '\'' {
return pos, BLOB, l.buf.String()
} else if ch == -1 {
return pos, ILLEGAL, string(start) + `'` + l.buf.String()
} else if !isHex(ch) {
return pos, ILLEGAL, string(start) + `'` + l.buf.String() + string(ch)
}
l.buf.WriteRune(ch)
}
}
func (l *Lexer) lexNumber() (Pos, Token, string) {
assert(isDigit(l.peek()) || l.peek() == '.')
pos := l.pos
tok := INTEGER
l.buf.Reset()
// Read whole number if starting with a digit.
if isDigit(l.peek()) {
for isDigit(l.peek()) {
ch, _ := l.read()
l.buf.WriteRune(ch)
}
}
// Read decimal and successive digitl.
if l.peek() == '.' {
tok = FLOAT
ch, _ := l.read()
l.buf.WriteRune(ch)
for isDigit(l.peek()) {
ch, _ := l.read()
l.buf.WriteRune(ch)
}
}
// Read exponent with optional +/- sign.
if ch := l.peek(); ch == 'e' || ch == 'E' {
tok = FLOAT
ch, _ := l.read()
l.buf.WriteRune(ch)
if l.peek() == '+' || l.peek() == '-' {
ch, _ := l.read()
l.buf.WriteRune(ch)
if !isDigit(l.peek()) {
return pos, ILLEGAL, l.buf.String()
}
for isDigit(l.peek()) {
ch, _ := l.read()
l.buf.WriteRune(ch)
}
} else if isDigit(l.peek()) {
for isDigit(l.peek()) {
ch, _ := l.read()
l.buf.WriteRune(ch)
}
} else {
return pos, ILLEGAL, l.buf.String()
}
}
lit := l.buf.String()
if lit == "." {
return pos, DOT, lit
}
return pos, tok, lit
}
func (l *Lexer) read() (rune, Pos) {
if l.full {
l.full = false
return l.ch, l.pos
}
var err error
l.ch, _, err = l.r.ReadRune()
if err != nil {
l.ch = -1
return l.ch, l.pos
}
l.pos.Offset++
if l.ch == '\n' {
l.pos.Line++
l.pos.Column = 0
} else {
l.pos.Column++
}
return l.ch, l.pos
}
func (l *Lexer) peek() rune {
if !l.full {
l.read()
l.unread()
}
return l.ch
}
func (l *Lexer) unread() {
assert(!l.full)
l.full = true
}
func isDigit(ch rune) bool {
return ch >= '0' && ch <= '9'
}
func isAlpha(ch rune) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func isHex(ch rune) bool {
return isDigit(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
}
func isUnquotedIdent(ch rune) bool {
return isAlpha(ch) || isDigit(ch) || ch == '_'
}
// IsInteger returns true if s only contains digits.
func IsInteger(s string) bool {
for _, ch := range s {
if !isDigit(ch) {
return false
}
}
return s != ""
}