-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.go
348 lines (323 loc) · 7.98 KB
/
scanner.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
package awqlparse
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
"time"
)
// eof represents a marker rune for the end of the reader.
var eof = rune(0)
// Scanner represents a lexical scanner.
type Scanner struct {
r *bufio.Reader
}
// NewScanner returns a new instance of Scanner.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{r: bufio.NewReader(r)}
}
// Scan returns the next token and literal value.
func (s *Scanner) Scan() (Token, string) {
// Get the next rune.
r := s.read()
if isWhitespace(r) {
// Consume all contiguous whitespace.
s.unread()
return s.scanWhitespace()
} else if isQuote(r) {
// Consume as string.
s.unread()
return s.scanQuotedString()
} else if isLetter(r) {
// A keyword begins by a letter.
// Consume as an identifier or reserved word.
s.unread()
return s.scanIdentifier()
} else if isDigit(r) {
// Consume as a number.
s.unread()
return s.scanNumber()
}
// Otherwise read the individual character.
switch r {
case eof:
return EOF, ""
case '*':
return ASTERISK, string(r)
case ',':
return COMMA, string(r)
case '(':
return LEFT_PARENTHESIS, string(r)
case ')':
return RIGHT_PARENTHESIS, string(r)
case '[':
return LEFT_SQUARE_BRACKETS, string(r)
case ']':
return RIGHT_SQUARE_BRACKETS, string(r)
case '=':
return EQUAL, string(r)
case '!':
// Deal with !=
if r := s.read(); r == '=' {
return DIFFERENT, "!="
}
s.unread()
case '>':
// Deal with >=
if r := s.read(); r == '=' {
return SUPERIOR_OR_EQUAL, ">="
}
s.unread()
return SUPERIOR, string(r)
case '<':
// Deal with <=
if r := s.read(); r == '=' {
return INFERIOR_OR_EQUAL, "<="
}
s.unread()
return INFERIOR, string(r)
case '\\':
// Deal with \G or lowercase version.
if r := s.read(); r == 'G' || r == 'g' {
return G_MODIFIER, fmt.Sprintf("\\%c", r)
}
s.unread()
case ';':
return SEMICOLON, string(r)
}
return ILLEGAL, string(r)
}
// scanIdentifier consumes the current rune and all contiguous literal runes.
func (s *Scanner) scanIdentifier() (Token, string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(s.read())
// Read every subsequent character of this token into the buffer.
// Non-literal characters or EOF will cause the loop to exit.
var valueLiteral bool
for {
if r := s.read(); r == eof {
break
} else if !isValueLiteral(r) {
s.unread()
break
} else {
if r == '.' {
valueLiteral = true
}
buf.WriteRune(r)
}
}
// If the string is a value literal then return it.
if valueLiteral {
return VALUE_LITERAL, buf.String()
}
// If the string matches a reserved keyword then return it.
switch strings.ToUpper(buf.String()) {
case "DESCRIBE":
return DESCRIBE, buf.String()
case "SELECT":
return SELECT, buf.String()
case "CREATE":
return CREATE, buf.String()
case "REPLACE":
return REPLACE, buf.String()
case "VIEW":
return VIEW, buf.String()
case "SHOW":
return SHOW, buf.String()
case "FULL":
return FULL, buf.String()
case "TABLES":
return TABLES, buf.String()
case "DISTINCT":
return DISTINCT, buf.String()
case "AS":
return AS, buf.String()
case "FROM":
return FROM, buf.String()
case "WHERE":
return WHERE, buf.String()
case "LIKE":
return LIKE, buf.String()
case "WITH":
return WITH, buf.String()
case "AND":
return AND, buf.String()
case "OR":
return OR, buf.String()
case "IN":
return IN, buf.String()
case "NOT_IN":
return NOT_IN, buf.String()
case "STARTS_WITH":
return STARTS_WITH, buf.String()
case "STARTS_WITH_IGNORE_CASE":
return STARTS_WITH_IGNORE_CASE, buf.String()
case "CONTAINS":
return CONTAINS, buf.String()
case "CONTAINS_IGNORE_CASE":
return CONTAINS_IGNORE_CASE, buf.String()
case "DOES_NOT_CONTAIN":
return DOES_NOT_CONTAIN, buf.String()
case "DOES_NOT_CONTAIN_IGNORE_CASE":
return DOES_NOT_CONTAIN_IGNORE_CASE, buf.String()
case "DURING":
return DURING, buf.String()
case "GROUP":
return GROUP, buf.String()
case "ORDER":
return ORDER, buf.String()
case "BY":
return BY, buf.String()
case "ASC":
return ASC, buf.String()
case "DESC":
return DESC, buf.String()
case "LIMIT":
return LIMIT, buf.String()
}
return IDENTIFIER, buf.String()
}
// scanNumber consumes all digit or dot runes.
func (s *Scanner) scanNumber() (tk Token, str string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
for {
if r := s.read(); r == eof {
break
} else if !isDigit(r) && r != '.' {
s.unread()
break
} else {
buf.WriteRune(r)
}
}
// Check if it is a valid number.
str = buf.String()
if _, err := strconv.Atoi(str); err == nil {
tk = DIGIT
} else if _, err := strconv.ParseFloat(str, 64); err == nil {
tk = DECIMAL
}
return
}
// scanQuotedString consumes the current rune and all runes after it
// until the next unprotected quote character.
func (s *Scanner) scanQuotedString() (Token, string) {
// Create a buffer and add the single or double quote into it.
quote := s.read()
if quote != '\'' && quote != '"' {
return ILLEGAL, string(quote)
}
var buf bytes.Buffer
for {
r := s.read()
if r == eof {
return ILLEGAL, buf.String()
} else if r == '\\' {
buf.WriteRune(r)
// Only the character immediately after the escape can itself be a backslash or quote.
// Thus, we only need to protect the first character after the backslash.
buf.WriteRune(s.read())
} else if r != quote {
buf.WriteRune(r)
} else {
break
}
}
return STRING, buf.String()
}
// scanWhitespace consumes the current rune and all contiguous whitespace.
func (s *Scanner) scanWhitespace() (Token, string) {
var buf bytes.Buffer
for {
if r := s.read(); r == eof {
break
} else if !isWhitespace(r) {
s.unread()
break
} else {
buf.WriteRune(r)
}
}
return WHITE_SPACE, buf.String()
}
// read reads the next rune from the bufferred reader.
// Returns the rune(0) if an error occurs (or io.EOF is returned).
func (s *Scanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *Scanner) unread() {
_ = s.r.UnreadRune()
}
// isDate return true if the string is a date as expected by Adwords.
func isDate(s string) bool {
if _, err := time.Parse("20060102", s); err == nil {
return true
}
return false
}
// isDateRange return true if the string is a date range literal.
func isDateRangeLiteral(s string) bool {
switch s {
case "TODAY", "YESTERDAY",
"THIS_WEEK_SUN_TODAY", "THIS_WEEK_MON_TODAY",
"LAST_WEEK", "LAST_7_DAYS", "LAST_14_DAYS",
"LAST_30_DAYS", "LAST_BUSINESS_WEEK",
"LAST_WEEK_SUN_SAT", "THIS_MONTH":
return true
}
return false
}
// isDigit returns true if the rune is a digit.
func isDigit(r rune) bool {
return (r >= '0' && r <= '9')
}
// isFunction returns true if it is an aggregate function.
func isFunction(s string) bool {
switch strings.ToUpper(s) {
case "AVG", "COUNT", "MAX", "MIN", "SUM":
return true
}
return false
}
// isLetter returns true if the rune is a letter.
func isLetter(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
}
// isLiteral returns true if the rune is a literal [a-zA-Z0-9_]
func isLiteral(r rune) bool {
return r == '_' || isDigit(r) || isLetter(r)
}
// isOperator returns true if the token is an operator
func isOperator(tk Token) bool {
switch tk {
case EQUAL, DIFFERENT, SUPERIOR, SUPERIOR_OR_EQUAL,
INFERIOR, INFERIOR_OR_EQUAL, IN, NOT_IN,
STARTS_WITH, STARTS_WITH_IGNORE_CASE,
CONTAINS, CONTAINS_IGNORE_CASE,
DOES_NOT_CONTAIN, DOES_NOT_CONTAIN_IGNORE_CASE:
return true
}
return false
}
// isQuote returns if the rune is a single quote or double quote.
func isQuote(r rune) bool {
return r == '"' || r == '\''
}
// isValueLiteral returns true if the rune is a value literal [a-zA-Z0-9_.]
func isValueLiteral(r rune) bool {
return r == '.' || isLiteral(r)
}
// isWhitespace returns true if the rune is a space, tab, or newline.
func isWhitespace(r rune) bool {
return r == ' ' || r == '\t' || r == '\n'
}