-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
377 lines (307 loc) · 8.88 KB
/
parser.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
package dibbi
import (
"errors"
"fmt"
)
func tokenFromKeyword(k keyword) token {
return token{
tokenType: KeywordType,
value: string(k),
}
}
func tokenFromSymbol(k symbol) token {
return token{
tokenType: SymbolType,
value: string(k),
}
}
func assertIsToken(tokens []*token, cursor uint, t token) bool {
if cursor >= uint(len(tokens)) {
return false
}
return t.Equals(tokens[cursor])
}
func printHelpMessage(tokens []*token, cursor uint, msg string) {
var c *token
if cursor < uint(len(tokens)) {
c = tokens[cursor]
} else {
c = tokens[cursor-1]
}
fmt.Printf("[%d,%d]: %s, got: %s\n", c.location.line, c.location.column, msg, c.value)
}
func parse(source string) (*ast, error) {
tokens, err := Lex(source)
if err != nil {
return nil, err
}
ast := ast{}
cursor := uint(0)
for cursor < uint(len(tokens)) {
stmt, newCursor, ok := parseStatement(tokens, cursor, tokenFromSymbol(SemicolonSymbol))
if !ok {
printHelpMessage(tokens, cursor, "Expected statement")
return nil, errors.New("failed to parse, expected statement")
}
cursor = newCursor
ast.Statements = append(ast.Statements, stmt)
// Finds a semicolon
semicolonIsPresent := false
for assertIsToken(tokens, cursor, tokenFromSymbol(SemicolonSymbol)) {
cursor++
semicolonIsPresent = true
}
if !semicolonIsPresent {
printHelpMessage(tokens, cursor, "Expected semi-colon delimiter between statements")
return nil, errors.New("missing semi-colon between statements")
}
}
return &ast, nil
}
func parseStatement(tokens []*token, initialCursor uint, delimiter token) (*statement, uint, bool) {
cursor := initialCursor
// Look for a SELECT statement
selectStmt, newCursor, ok := parseSelectStatement(tokens, cursor, delimiter)
if ok {
return &statement{
statementType: SelectType,
selectStatement: selectStmt,
}, newCursor, true
}
// Look for a INSERT statement
insertStmt, newCursor, ok := parseInsertStatement(tokens, cursor)
if ok {
return &statement{
statementType: InsertType,
insertStatement: insertStmt,
}, newCursor, true
}
// Look for a CREATE statement
createTableStmt, newCursor, ok := parseCreateTableStatement(tokens, cursor)
if ok {
return &statement{
statementType: CreateTableType,
createTableStatement: createTableStmt,
}, newCursor, true
}
return nil, initialCursor, false
}
func parseSelectStatement(tokens []*token, initialCursor uint, delimiter token) (*selectStatement, uint, bool) {
cursor := initialCursor
if !assertIsToken(tokens, cursor, tokenFromKeyword(SelectKeyword)) {
return nil, initialCursor, false
}
cursor++
selectStmt := selectStatement{}
expressions, newCursor, ok := parseExpressions(tokens, cursor, []token{tokenFromKeyword(FromKeyword), delimiter})
if !ok {
return nil, initialCursor, false
}
selectStmt.items = *expressions
cursor = newCursor
if assertIsToken(tokens, cursor, tokenFromKeyword(FromKeyword)) {
cursor++
from, newCursor, ok := parseToken(tokens, cursor, IdentifierType)
if !ok {
printHelpMessage(tokens, cursor, "Expected FROM token")
return nil, initialCursor, false
}
selectStmt.from = from
cursor = newCursor
}
return &selectStmt, cursor, true
}
func parseInsertStatement(tokens []*token, initialCursor uint) (*insertStatement, uint, bool) {
cursor := initialCursor
// Look for INSERT
if !assertIsToken(tokens, cursor, tokenFromKeyword(InsertKeyword)) {
return nil, initialCursor, false
}
cursor++
// Look for INTO
if !assertIsToken(tokens, cursor, tokenFromKeyword(IntoKeyword)) {
printHelpMessage(tokens, cursor, "Expected into")
return nil, initialCursor, false
}
cursor++
// Look for table name
table, newCursor, ok := parseToken(tokens, cursor, IdentifierType)
if !ok {
printHelpMessage(tokens, cursor, "Expected table name")
return nil, initialCursor, false
}
cursor = newCursor
// Look for VALUES
if !assertIsToken(tokens, cursor, tokenFromKeyword(ValuesKeyword)) {
printHelpMessage(tokens, cursor, "Expected VALUES")
return nil, initialCursor, false
}
cursor++
// Look for left paren
if !assertIsToken(tokens, cursor, tokenFromSymbol(LeftParenthesesSymbol)) {
printHelpMessage(tokens, cursor, "Expected left paren")
return nil, initialCursor, false
}
cursor++
// Look for expression list
values, newCursor, ok := parseExpressions(tokens, cursor, []token{tokenFromSymbol(RightParenthesesSymbol)})
if !ok {
return nil, initialCursor, false
}
cursor = newCursor
// Look for right paren
if !assertIsToken(tokens, cursor, tokenFromSymbol(RightParenthesesSymbol)) {
printHelpMessage(tokens, cursor, "Expected right paren")
return nil, initialCursor, false
}
cursor++
return &insertStatement{
Table: *table,
Values: values,
}, cursor, true
}
func parseCreateTableStatement(tokens []*token, initialCursor uint) (*createTableStatement, uint, bool) {
cursor := initialCursor
if !assertIsToken(tokens, cursor, tokenFromKeyword(CreateKeyword)) {
return nil, initialCursor, false
}
cursor++
if !assertIsToken(tokens, cursor, tokenFromKeyword(TableKeyword)) {
return nil, initialCursor, false
}
cursor++
name, newCursor, ok := parseToken(tokens, cursor, IdentifierType)
if !ok {
printHelpMessage(tokens, cursor, "Expected table name")
return nil, initialCursor, false
}
cursor = newCursor
if !assertIsToken(tokens, cursor, tokenFromSymbol(LeftParenthesesSymbol)) {
printHelpMessage(tokens, cursor, "Expected left parenthesis")
return nil, initialCursor, false
}
cursor++
cols, newCursor, ok := parsecolumnDefinitions(tokens, cursor, tokenFromSymbol(RightParenthesesSymbol))
if !ok {
return nil, initialCursor, false
}
cursor = newCursor
if !assertIsToken(tokens, cursor, tokenFromSymbol(RightParenthesesSymbol)) {
printHelpMessage(tokens, cursor, "Expected right parenthesis")
return nil, initialCursor, false
}
cursor++
return &createTableStatement{
Name: name,
Columns: cols,
}, cursor, true
}
func parseToken(tokens []*token, initialCursor uint, tokenType tokenType) (*token, uint, bool) {
cursor := initialCursor
if cursor >= uint(len(tokens)) {
return nil, initialCursor, false
}
current := tokens[cursor]
if current.tokenType == tokenType {
return current, cursor + 1, true
}
return nil, initialCursor, false
}
func parseExpressions(tokens []*token, initialCursor uint, delimiters []token) (*[]*expression, uint, bool) {
cursor := initialCursor
var exps []*expression
outer:
for {
if cursor >= uint(len(tokens)) {
return nil, initialCursor, false
}
// Look for delimiter
current := tokens[cursor]
for _, delimiter := range delimiters {
if delimiter.Equals(current) {
break outer
}
}
// Look for comma
if len(exps) > 0 {
if !assertIsToken(tokens, cursor, tokenFromSymbol(CommaSymbol)) {
printHelpMessage(tokens, cursor, "Expected comma")
return nil, initialCursor, false
}
cursor++
}
// Check if it's star
t, newCursor, ok := parseToken(tokens, cursor, SymbolType)
if ok {
exps = append(exps, &expression{
Literal: t,
ExpressionType: LiteralType,
})
return &exps, newCursor, true
}
// Look for expression
exp, newCursor, ok := parseExpression(tokens, cursor, tokenFromSymbol(CommaSymbol))
if !ok {
printHelpMessage(tokens, cursor, "Expected expression")
return nil, initialCursor, false
}
cursor = newCursor
exps = append(exps, exp)
}
return &exps, cursor, true
}
func parseExpression(tokens []*token, initialCursor uint, _ token) (*expression, uint, bool) {
cursor := initialCursor
kinds := []tokenType{IdentifierType, NumericType, StringType}
for _, kind := range kinds {
t, newCursor, ok := parseToken(tokens, cursor, kind)
if ok {
return &expression{
Literal: t,
ExpressionType: LiteralType,
}, newCursor, true
}
}
return nil, initialCursor, false
}
func parsecolumnDefinitions(tokens []*token, initialCursor uint, delimiter token) (*[]*columnDefinition, uint, bool) {
cursor := initialCursor
var cds []*columnDefinition
for {
if cursor >= uint(len(tokens)) {
return nil, initialCursor, false
}
// Look for a delimiter
current := tokens[cursor]
if delimiter.Equals(current) {
break
}
// Look for a comma
if len(cds) > 0 {
if !assertIsToken(tokens, cursor, tokenFromSymbol(CommaSymbol)) {
printHelpMessage(tokens, cursor, "Expected comma")
return nil, initialCursor, false
}
cursor++
}
// Look for a column name
id, newCursor, ok := parseToken(tokens, cursor, IdentifierType)
if !ok {
printHelpMessage(tokens, cursor, "Expected column name")
return nil, initialCursor, false
}
cursor = newCursor
// Look for a column Type
ty, newCursor, ok := parseToken(tokens, cursor, KeywordType)
if !ok {
printHelpMessage(tokens, cursor, "Expected column Type")
return nil, initialCursor, false
}
cursor = newCursor
cds = append(cds, &columnDefinition{
Name: id,
Datatype: ty,
})
}
return &cds, cursor, true
}