-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscanner.go
More file actions
291 lines (235 loc) · 4.54 KB
/
scanner.go
File metadata and controls
291 lines (235 loc) · 4.54 KB
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
package sqlread
import (
"io"
"strings"
"unicode/utf8"
)
type lexItemType uint8
//go:generate stringer -type=lexItemType
const (
TIllegal lexItemType = iota
TEof
TSemi
TComma
TComment
TDelim
TNull
TString
TNumber
THexLiteral
TIdentifier
TDropTableFullStmt
TLockTableFullStmt
TUnlockTablesFullStmt
TSetFullStmt
TLParen
TRParen
TCreateTable
TCreateTableDetail
TCreateTableExtraDetail
TColumnType
TColumnSize
TColumnEnumVal
TColumnDetails
TInsertInto
TInsertValues
// Values Below Are Specific to the interpreter
TIntpSelect
TIntpStar
TIntpFrom
TIntpIntoOutfile
TIntpShowTables
TIntpShowColumns
TIntpShowCreateTable
TIntpQuit
TBeginFullStmt
TCommitFullStmt
)
type LexItem struct {
Type lexItemType
Val string
Pos int64
}
type lexer struct {
input io.ReaderAt
start int64
pos int64
// width int
items chan LexItem
}
const (
eof = byte(0) // null
lf = byte(10) // \n
semi = byte(59) // semicolon ;
bs = byte(92) // backslash \
bt = byte(96) // backtick `
dot = byte(46) // period .
lprn = byte(40) // (
rprn = byte(41) // )
coma = byte(44) // ,
dash = byte(45) // -
sq = byte(39) // '
dq = byte(34) // "
letN = byte(78) // N
// letn = byte(39)
)
func (l *lexer) next() byte {
i, b := l.peek(1)
l.pos++
if i != 1 {
return eof
}
return b[0]
}
func (l *lexer) rewind() {
l.pos--
}
func (l *lexer) peek(s int) (int, []byte) {
b := make([]byte, s)
n, err := l.input.ReadAt(b, l.pos)
if err == io.EOF {
return n, b
} else if err != nil {
panic(err)
}
return n, b
}
func (l *lexer) hasPrefix(s string) bool {
x := []byte(s)
_, y := l.peek(len(x))
return string(x) == string(y)
}
func (l *lexer) hasPrefixI(s string) bool {
x := []byte(s)
_, y := l.peek(len(x))
return strings.EqualFold(string(x), string(y))
}
type state func(*lexer) state
var (
whitespace = []byte(" \t\r\n")
sep = []byte(" \t\r\n;")
numbers = []byte("0123456789")
hexNumbers = []byte("0123456789abcdefABCDEF")
letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
identifiers = append(letters, append(numbers, '$', '_')...)
)
func (l *lexer) Run(start state) {
for state := start; state != nil; {
state = state(l)
}
close(l.items)
}
func (l *lexer) accept(bs []byte) (c int) {
for {
n := l.next()
found := false
for _, b := range bs {
if b == n {
found = true
break
}
}
if !found {
l.rewind()
return c
}
c++
}
}
func (l *lexer) until(b byte) bool {
for {
n := l.next()
if n == eof {
return false
}
if n == b {
return true
}
}
}
func (l *lexer) acceptUnicodeRange(start, end rune) (c int) {
buf := make([]byte, 4) // Maximum UTF-8 character size is 4 bytes
for {
// Read the first byte
firstByte := l.next()
if firstByte == eof {
return c
}
// Store the first byte
buf[0] = firstByte
// Determine the width of the UTF-8 character
width := 1
if firstByte >= 0xF0 { // 4-byte character
width = 4
} else if firstByte >= 0xE0 { // 3-byte character
width = 3
} else if firstByte >= 0xC0 { // 2-byte character
width = 2
}
// Read the remaining bytes if it's a multi-byte character
for i := 1; i < width; i++ {
b := l.next()
if b == eof || b < 0x80 || b >= 0xC0 { // Invalid continuation byte
// Rewind what we've read so far
l.rewind() // Rewind the invalid byte
for j := 0; j < i; j++ {
l.rewind() // Rewind the bytes we've already read
}
return c
}
buf[i] = b
}
// Decode the UTF-8 bytes into a rune
r, _ := utf8.DecodeRune(buf[:width])
if r == utf8.RuneError && width > 1 {
// Invalid UTF-8 sequence, rewind all bytes
for i := 0; i < width; i++ {
l.rewind()
}
return c
}
// Check if the rune is within the specified range
if r >= start && r <= end {
c++
} else {
// Rewind the bytes we read
for i := 0; i < width; i++ {
l.rewind()
}
return c
}
}
}
func Lex(input io.ReaderAt) (*lexer, chan LexItem) {
l := &lexer{
input: input,
items: make(chan LexItem, 2000000),
}
return l, l.items
}
func LexSection(input io.ReaderAt, off, n int64) (*lexer, chan LexItem) {
l := &lexer{
input: io.NewSectionReader(input, off, n),
items: make(chan LexItem, 2000000),
}
return l, l.items
}
func (l *lexer) emit(t lexItemType) LexItem {
b := make([]byte, l.pos-l.start)
l.input.ReadAt(b, l.start)
li := LexItem{
Type: t,
Val: string(b),
Pos: l.start,
}
l.items <- li
return li
}
func in(b byte, bs []byte) bool {
for _, bb := range bs {
if b == bb {
return true
}
}
return false
}