forked from jackc/sqlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
344 lines (292 loc) · 6.28 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
//go:generate -command yacc go tool yacc
//go:generate yacc -o sql.go sql.y
package sqlfmt
import (
"log"
"strings"
"unicode"
"unicode/utf8"
)
type stateFn func(*sqlLex) stateFn
type token struct {
typ int
src string
}
type sqlLex struct {
src string
start int
pos int
width int
state stateFn
tokens []token
stmt *SelectStmt
}
func (x *sqlLex) Lex(yylval *yySymType) int {
token := x.tokens[0]
x.tokens = x.tokens[1:]
yylval.str = token.src
return token.typ
}
// The parser calls this method on a parse error.
func (x *sqlLex) Error(s string) {
log.Printf("parse error: %s at character %d", s, x.start)
}
func NewSqlLexer(src string) *sqlLex {
x := &sqlLex{src: src,
tokens: make([]token, 0),
state: blankState,
}
for x.state != nil {
x.state = x.state(x)
}
x.append(token{typ: eof})
return x
}
func (l *sqlLex) append(t token) {
l.tokens = append(l.tokens, t)
if len(l.tokens) == 1 {
return
}
prevToken := &l.tokens[len(l.tokens)-2]
switch t.typ {
case BETWEEN, IN_P, LIKE, ILIKE, SIMILAR:
if prevToken.typ == NOT {
prevToken.typ = NOT_LA
}
case FIRST_P, LAST_P:
if prevToken.typ == NULLS_P {
prevToken.typ = NULLS_LA
}
case TIME, ORDINALITY:
if prevToken.typ == WITH {
prevToken.typ = WITH_LA
}
}
}
func (l *sqlLex) next() (r rune) {
if l.pos >= len(l.src) {
l.width = 0 // because backing up from having read eof should read eof again
return 0
}
r, l.width = utf8.DecodeRuneInString(l.src[l.pos:])
l.pos += l.width
return r
}
func (l *sqlLex) unnext() {
l.pos -= l.width
}
func (l *sqlLex) ignore() {
l.start = l.pos
}
func (l *sqlLex) acceptRunFunc(f func(rune) bool) {
for f(l.next()) {
}
l.unnext()
}
func blankState(l *sqlLex) stateFn {
switch r := l.next(); {
case r == 0:
return nil
case r == ',' || r == '(' || r == ')' || r == '[' || r == ']' || r == ';':
return lexSimple
case r == '\'':
return lexStringConst
case r == '"':
return lexQuotedIdentifier
case r == ':' || r == '.':
return lexAlmostOperator
case isOperator(r):
return lexOperator
case r == 'b' || r == 'B' || r == 'x' || r == 'X':
return lexPossibleBitString
case unicode.IsDigit(r):
return lexNumber
case isWhitespace(r):
l.skipWhitespace()
return blankState
case isAlphanumeric(r):
return lexAlphanumeric
}
return nil
}
func lexNumber(l *sqlLex) stateFn {
typ := ICONST
l.acceptRunFunc(unicode.IsDigit)
r := l.next()
if r == '.' {
l.acceptRunFunc(unicode.IsDigit)
typ = FCONST
} else {
l.unnext()
}
t := token{src: l.src[l.start:l.pos], typ: typ}
l.append(t)
l.start = l.pos
return blankState
}
func lexAlphanumeric(l *sqlLex) stateFn {
l.acceptRunFunc(isAlphanumeric)
t := token{src: l.src[l.start:l.pos]}
if typ, ok := keywords[strings.ToLower(t.src)]; ok {
t.typ = typ
} else {
t.typ = IDENT
}
l.append(t)
l.start = l.pos
return blankState
}
func lexStringConst(l *sqlLex) stateFn {
for {
var r rune
r = l.next()
if r == 0 {
return nil // error for EOF inside of string literal
}
if r == '\'' {
r = l.next()
if r != '\'' {
l.unnext()
t := token{src: l.src[l.start:l.pos]}
t.typ = SCONST
l.append(t)
l.start = l.pos
return blankState
}
}
}
}
func lexQuotedIdentifier(l *sqlLex) stateFn {
for {
var r rune
r = l.next()
if r == 0 {
return nil // error for EOF inside of string literal
}
if r == '"' {
r = l.next()
if r != '"' {
l.unnext()
t := token{src: l.src[l.start:l.pos]}
t.typ = IDENT
l.append(t)
l.start = l.pos
return blankState
}
}
}
}
// lexAlmostOperator is for operator-like ':' and '.' which aren't operators
func lexAlmostOperator(l *sqlLex) stateFn {
l.next()
t := token{src: l.src[l.start:l.pos]}
switch {
case t.src == "::":
t.typ = TYPECAST
case t.src == "..":
t.typ = DOT_DOT
case t.src == ":=":
t.typ = COLON_EQUALS
default:
l.unnext()
t.typ = int(t.src[0])
}
t.src = l.src[l.start:l.pos]
l.append(t)
l.start = l.pos
return blankState
}
func lexOperator(l *sqlLex) stateFn {
l.acceptRunFunc(isOperator)
if (l.pos-l.start) >= 2 && l.src[l.start:l.start+2] == "--" {
return lexDashDashComment
}
t := token{src: l.src[l.start:l.pos]}
switch {
case t.src == "+" || t.src == "-" || t.src == "*" || t.src == "/" || t.src == "%" || t.src == "^" || t.src == "<" || t.src == ">" || t.src == "=" || t.src == "[" || t.src == "]" || t.src == ":":
t.typ = int(t.src[0])
case t.src == "=>":
t.typ = EQUALS_GREATER
case t.src == "<=":
t.typ = LESS_EQUALS
case t.src == ">=":
t.typ = GREATER_EQUALS
case t.src == "<>", t.src == "!=":
t.typ = NOT_EQUALS
default:
t.typ = Op
}
l.append(t)
l.start = l.pos
return blankState
}
func lexSimple(l *sqlLex) stateFn {
l.append(token{int(l.src[l.start]), l.src[l.start:l.pos]})
l.start = l.pos
return blankState
}
func lexPossibleBitString(l *sqlLex) stateFn {
var rangeTable unicode.RangeTable
if l.src[l.start] == 'b' || l.src[l.start] == 'B' {
rangeTable = unicode.RangeTable{
R16: []unicode.Range16{
unicode.Range16{Lo: '0', Hi: '1', Stride: 1},
},
LatinOffset: 1,
}
} else {
rangeTable = unicode.RangeTable{
R16: []unicode.Range16{
unicode.Range16{Lo: '0', Hi: '9', Stride: 1},
unicode.Range16{Lo: 'A', Hi: 'F', Stride: 1},
unicode.Range16{Lo: 'a', Hi: 'f', Stride: 1},
},
LatinOffset: 3,
}
}
r := l.next()
if r == '\'' {
l.acceptRunFunc(func(r rune) bool {
return unicode.In(r, &rangeTable)
})
r = l.next()
if r == '\'' {
t := token{src: l.src[l.start:l.pos], typ: BCONST}
l.append(t)
l.start = l.pos
return blankState
}
return nil // lex error
} else {
l.unnext()
}
return lexAlphanumeric
}
func lexDashDashComment(l *sqlLex) stateFn {
var r rune
for r = l.next(); r != '\n'; r = l.next() {
}
// TODO - don't ignore comments
l.ignore()
return blankState
}
func (l *sqlLex) skipWhitespace() {
var r rune
for r = l.next(); isWhitespace(r); r = l.next() {
}
if r != 0 {
l.unnext()
}
l.ignore()
}
func isWhitespace(r rune) bool {
return unicode.IsSpace(r)
}
func isAlphanumeric(r rune) bool {
return r == '_' || unicode.In(r, unicode.Letter, unicode.Digit)
}
func isOperator(r rune) bool {
// list of operator characters from
// http://www.postgresql.org/docs/9.4/static/sql-createoperator.html
return strings.IndexRune("+-*/<>=~!@#%^&|`?", r) != -1
}