-
Notifications
You must be signed in to change notification settings - Fork 5
/
lexer.go
312 lines (261 loc) · 6.61 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
package gosqlparser
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// lexer tokenizes the input and produces tokens.
type lexer struct {
input string // raw SQL query
start int // start position of the current token
position int // current position
width int // width of last rune read from input
tokens chan token
}
// token is an entity produced by tokenizer for parser that represents a smaller typed piece
// of input string.
type token struct {
tokenType tokenType
value string
}
type tokenType int
const (
tokenError tokenType = iota
tokenSpace // whitespace
tokenIdentifier // table or column name
tokenEnd // the end of the input
tokenEquals // "=="
tokenAssign // "="
tokenDelimeter // ','
tokenLeftParenthesis // '('
tokenRightParenthesis // ')'
tokenInteger // integer
tokenString // string including quotes
tokenAnd // AND
tokenInsert // INSERT
tokenInto // INTO
tokenSelect // SELECT
tokenDelete // DELETE
tokenFrom // FROM
tokenWhere // WHERE
tokenLimit // LIMIT
tokenValues // VALUES
tokenUpdate // UPDATE
tokenSet // SET
tokenCreate // CREATE
tokenDrop // DROP
tokenTable // TABLE
tokenTypeInteger // INTEGER
tokenTypeString // STRING
tokenEngine // ENGINE
tokenLSM // LSM
tokenBPTree // B+ tree
)
var tokenToString = map[tokenType]string{
tokenError: "error",
tokenSpace: "space",
tokenIdentifier: "identifier",
tokenEnd: "end",
tokenEquals: "equals",
tokenAssign: "assign",
tokenDelimeter: "delimeter",
tokenLeftParenthesis: "leftParenthesis",
tokenRightParenthesis: "rightParenthesis",
tokenInteger: "integer",
tokenString: "string",
tokenAnd: "AND",
tokenInsert: "INSERT",
tokenInto: "INTO",
tokenSelect: "SELECT",
tokenDelete: "DELETE",
tokenFrom: "FROM",
tokenWhere: "WHERE",
tokenLimit: "LIMIT",
tokenValues: "VALUES",
tokenUpdate: "UPDATE",
tokenSet: "SET",
tokenCreate: "CREATE",
tokenDrop: "DROP",
tokenTable: "TABLE",
tokenTypeInteger: "typeInteger",
tokenEngine: "ENGINE",
tokenLSM: "LSM",
tokenBPTree: "BPTree",
}
func (t tokenType) String() string {
s, defined := tokenToString[t]
if defined {
return s
}
return "unknown"
}
const (
keywordSelect = "SELECT"
keywordInsert = "INSERT"
keywordDelete = "DELETE"
keywordInto = "INTO"
keywordFrom = "FROM"
keywordWhere = "WHERE"
keywordLimit = "LIMIT"
keywordAnd = "AND"
keywordValues = "VALUES"
keywordUpdate = "UPDATE"
keywordSet = "SET"
keywordInteger = "INTEGER"
keywordString = "STRING"
keywordCreate = "CREATE"
keywordDrop = "DROP"
keywordTable = "TABLE"
keywordEngine = "ENGINE"
keywordLSM = "LSM"
keywordBPTree = "BPTREE"
)
var keywords = map[string]tokenType{
keywordSelect: tokenSelect,
keywordFrom: tokenFrom,
keywordWhere: tokenWhere,
keywordLimit: tokenLimit,
keywordAnd: tokenAnd,
keywordInsert: tokenInsert,
keywordInto: tokenInto,
keywordValues: tokenValues,
keywordDelete: tokenDelete,
keywordUpdate: tokenUpdate,
keywordSet: tokenSet,
keywordString: tokenTypeString,
keywordInteger: tokenTypeInteger,
keywordCreate: tokenCreate,
keywordTable: tokenTable,
keywordDrop: tokenDrop,
keywordEngine: tokenEngine,
keywordLSM: tokenLSM,
keywordBPTree: tokenBPTree,
}
const end = -1
type lexFunc func(*lexer) lexFunc
// newLexer returns an instance of the new lexer.
func newLexer(input string) *lexer {
l := &lexer{
input: input,
tokens: make(chan token),
}
return l
}
func (l *lexer) run() {
for state := lexStatement; state != nil; {
state = state(l)
}
close(l.tokens)
}
// nextToken consumes token and returns the next token.
func (l *lexer) nextToken() token {
return <-l.tokens
}
// produce sends the token.
func (l *lexer) produce(t tokenType) {
l.tokens <- token{t, l.input[l.start:l.position]}
l.start = l.position
}
func (l *lexer) next() rune {
if l.position >= len(l.input) {
l.width = 0
return end
}
r, w := utf8.DecodeRuneInString(l.input[l.position:])
l.width = w
l.position += w
return r
}
func (l *lexer) revert() {
l.position -= l.width
}
func (l *lexer) peek() rune {
r := l.next()
l.revert()
return r
}
func (l *lexer) drain() {
for range l.tokens {
}
}
func lexStatement(l *lexer) lexFunc {
r := l.next()
switch true {
case unicode.IsDigit(r):
return lexInteger
case isAlphaNumeric(r):
return lexIdentifier
case unicode.IsSpace(r):
l.produce(tokenSpace)
return lexStatement
case r == '(':
l.produce(tokenLeftParenthesis)
return lexStatement
case r == ')':
l.produce(tokenRightParenthesis)
return lexStatement
case r == '"':
return lexString
case r == ',':
l.produce(tokenDelimeter)
return lexStatement
case r == '=':
switch l.peek() {
case '=':
l.next()
l.produce(tokenEquals)
default:
l.produce(tokenAssign)
}
return lexStatement
case r == end:
l.produce(tokenEnd)
return nil
}
return l.errorf("unexpected rune")
}
// lexInteger lexes an integer.
func lexInteger(l *lexer) lexFunc {
r := l.peek()
if unicode.IsDigit(r) {
l.next()
return lexInteger
}
l.produce(tokenInteger)
return lexStatement
}
// lexString lexes a quoted string.
func lexString(l *lexer) lexFunc {
r := l.next()
switch r {
case '"':
l.produce(tokenString)
return lexStatement
case end:
return l.errorf("expected \"")
}
return lexString
}
func lexIdentifier(l *lexer) lexFunc {
r := l.next()
if isAlphaNumeric(r) {
// advance
return lexIdentifier
}
l.revert()
word := l.input[l.start:l.position]
if t, ok := keywords[strings.ToUpper(word)]; ok {
l.produce(t)
} else {
l.produce(tokenIdentifier)
}
return lexStatement
}
func (l *lexer) errorf(format string, args ...interface{}) lexFunc {
l.tokens <- token{tokenError, fmt.Sprintf(format, args...)}
return nil
}
func isAlphaNumeric(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
}