-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlb_tokenizer.go
373 lines (314 loc) · 8 KB
/
sqlb_tokenizer.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
package sqlb
import (
"fmt"
"io"
"strconv"
"strings"
"unicode/utf8"
)
/*
Partial SQL tokenizer used internally by `(*Prep).Parse` to parse queries, in
particular to convert named parameters into other expressions.
Goals:
* Correctly parse whitespace, comments, quoted strings and identifiers,
ordinal parameters, named parameters.
* Decently fast and allocation-free tokenization.
Non-goals:
* Full SQL parser.
Notable limitations:
* No special support for dollar-quoted strings, which are rarely if ever used
in dynamically-generated queries.
*/
type Tokenizer struct {
Source string
Transform func(Token) Token
cursor int
next Token
}
/*
Returns the next token if possible. When the tokenizer reaches the end, this
returns an empty `Token{}`. Call `Token.IsInvalid` to detect the end.
*/
func (self *Tokenizer) Next() Token {
for {
token := self.nextToken()
if token.IsInvalid() {
return Token{}
}
if self.Transform != nil {
token = self.Transform(token)
if token.IsInvalid() {
continue
}
}
return token
}
}
func (self *Tokenizer) nextToken() Token {
next := self.next
if !next.IsInvalid() {
self.next = Token{}
return next
}
start := self.cursor
for self.more() {
mid := self.cursor
if self.maybeWhitespace(); self.cursor > mid {
return self.choose(start, mid, TokenTypeWhitespace)
}
if self.maybeQuotedSingle(); self.cursor > mid {
return self.choose(start, mid, TokenTypeQuotedSingle)
}
if self.maybeQuotedDouble(); self.cursor > mid {
return self.choose(start, mid, TokenTypeQuotedDouble)
}
if self.maybeQuotedGrave(); self.cursor > mid {
return self.choose(start, mid, TokenTypeQuotedGrave)
}
if self.maybeCommentLine(); self.cursor > mid {
return self.choose(start, mid, TokenTypeCommentLine)
}
if self.maybeCommentBlock(); self.cursor > mid {
return self.choose(start, mid, TokenTypeCommentBlock)
}
if self.maybeDoubleColon(); self.cursor > mid {
return self.choose(start, mid, TokenTypeDoubleColon)
}
if self.maybeOrdinalParam(); self.cursor > mid {
return self.choose(start, mid, TokenTypeOrdinalParam)
}
if self.maybeNamedParam(); self.cursor > mid {
return self.choose(start, mid, TokenTypeNamedParam)
}
self.skipChar()
}
if self.cursor > start {
return Token{self.from(start), TokenTypeText}
}
return Token{}
}
func (self *Tokenizer) choose(start, mid int, typ TokenType) Token {
tok := Token{self.from(mid), typ}
if mid > start {
self.setNext(tok)
return Token{self.Source[start:mid], TokenTypeText}
}
return tok
}
func (self *Tokenizer) setNext(val Token) {
if !self.next.IsInvalid() {
panic(ErrInternal{Err{
`parsing SQL`,
errf(
`internal error: attempted to overwrite non-empty pending token %#v with %#v`,
self.next, val,
),
}})
}
self.next = val
}
func (self *Tokenizer) maybeWhitespace() {
for self.more() && charsetWhitespace.has(self.headByte()) {
self.skipBytes(1)
}
}
func (self *Tokenizer) maybeQuotedSingle() {
self.maybeStringBetweenBytes(quoteSingle, quoteSingle)
}
func (self *Tokenizer) maybeQuotedDouble() {
self.maybeStringBetweenBytes(quoteDouble, quoteDouble)
}
func (self *Tokenizer) maybeQuotedGrave() {
self.maybeStringBetweenBytes(quoteGrave, quoteGrave)
}
func (self *Tokenizer) maybeCommentLine() {
if !self.skippedString(commentLinePrefix) {
return
}
for self.more() && !self.skippedNewline() && self.skippedChar() {
}
}
// TODO support nested block comments, which are valid in SQL.
func (self *Tokenizer) maybeCommentBlock() {
self.maybeStringBetween(commentBlockPrefix, commentBlockSuffix)
}
func (self *Tokenizer) maybeDoubleColon() {
self.maybeString(doubleColonPrefix)
}
func (self *Tokenizer) maybeOrdinalParam() {
start := self.cursor
if !self.skippedByte(ordinalParamPrefix) {
return
}
if !self.skippedDigits() {
self.cursor = start
}
}
func (self *Tokenizer) maybeNamedParam() {
start := self.cursor
if !self.skippedByte(namedParamPrefix) {
return
}
if !self.skippedIdent() {
self.cursor = start
}
}
func (self *Tokenizer) maybeString(val string) {
_ = self.skippedString(val)
}
func (self *Tokenizer) skippedNewline() bool {
start := self.cursor
self.maybeNewline()
return self.cursor > start
}
func (self *Tokenizer) maybeNewline() {
self.skipBytes(leadingNewlineSize(self.rest()))
}
func (self *Tokenizer) skippedChar() bool {
start := self.cursor
self.skipChar()
return self.cursor > start
}
func (self *Tokenizer) skipChar() {
_, size := utf8.DecodeRuneInString(self.rest())
self.skipBytes(size)
}
func (self *Tokenizer) skippedDigits() bool {
start := self.cursor
self.maybeSkipDigits()
return self.cursor > start
}
func (self *Tokenizer) maybeSkipDigits() {
for self.more() && charsetDigitDec.has(self.headByte()) {
self.skipBytes(1)
}
}
func (self *Tokenizer) skippedIdent() bool {
start := self.cursor
self.maybeIdent()
return self.cursor > start
}
func (self *Tokenizer) maybeIdent() {
if !self.skippedByteFromCharset(charsetIdentStart) {
return
}
for self.more() && self.skippedByteFromCharset(charsetIdent) {
}
}
func (self *Tokenizer) maybeStringBetween(prefix, suffix string) {
if !self.skippedString(prefix) {
return
}
for self.more() {
if self.skippedString(suffix) {
return
}
self.skipChar()
}
panic(ErrUnexpectedEOF{Err{
`parsing SQL`,
fmt.Errorf(`expected closing %q, got unexpected %w`, suffix, io.EOF),
}})
}
func (self *Tokenizer) maybeStringBetweenBytes(prefix, suffix byte) {
if !self.skippedByte(prefix) {
return
}
for self.more() {
if self.skippedByte(suffix) {
return
}
self.skipChar()
}
panic(ErrUnexpectedEOF{Err{
`parsing SQL`,
fmt.Errorf(`expected closing %q, got unexpected %w`, rune(suffix), io.EOF),
}})
}
func (self *Tokenizer) skipBytes(val int) {
self.cursor += val
}
func (self *Tokenizer) more() bool {
return self.cursor < len(self.Source)
}
func (self *Tokenizer) rest() string {
return self.Source[self.cursor:]
}
func (self *Tokenizer) from(start int) string {
return self.Source[start:self.cursor]
}
func (self *Tokenizer) headByte() byte {
return self.Source[self.cursor]
}
func (self *Tokenizer) skippedByte(val byte) bool {
if self.headByte() == val {
self.skipBytes(1)
return true
}
return false
}
func (self *Tokenizer) skippedByteFromCharset(val *charset) bool {
if val.has(self.headByte()) {
self.skipBytes(1)
return true
}
return false
}
func (self *Tokenizer) skippedString(val string) bool {
if strings.HasPrefix(self.rest(), val) {
self.skipBytes(len(val))
return true
}
return false
}
const (
TokenTypeInvalid TokenType = iota
TokenTypeText
TokenTypeWhitespace
TokenTypeQuotedSingle
TokenTypeQuotedDouble
TokenTypeQuotedGrave
TokenTypeCommentLine
TokenTypeCommentBlock
TokenTypeDoubleColon
TokenTypeOrdinalParam
TokenTypeNamedParam
)
// Part of `Token`.
type TokenType byte
// Represents an arbitrary chunk of SQL text parsed by `Tokenizer`.
type Token struct {
Text string
Type TokenType
}
/*
True if the token's type is `TokenTypeInvalid`. This is used to detect end of
iteration when calling `(*Tokenizer).Next`.
*/
func (self Token) IsInvalid() bool {
return self.Type == TokenTypeInvalid
}
// Implement `fmt.Stringer` for debug purposes.
func (self Token) String() string { return self.Text }
/*
Assumes that the token has `TokenTypeOrdinalParam` and looks like a
Postgres-style ordinal param: "$1", "$2" and so on. Parses and returns the
number. Panics if the text had the wrong structure.
*/
func (self Token) ParseOrdinalParam() OrdinalParam {
rest, err := trimPrefixByte(self.Text, ordinalParamPrefix)
try(errOrdinal(err))
val, err := strconv.Atoi(rest)
try(errOrdinal(err))
return OrdinalParam(val)
}
/*
Assumes that the token has `TokenTypeNamedParam` and looks like a Postgres-style
named param: ":one", ":two" and so on. Parses and returns the parameter's name
without the leading ":". Panics if the text had the wrong structure.
*/
func (self Token) ParseNamedParam() NamedParam {
rest, err := trimPrefixByte(self.Text, namedParamPrefix)
try(errNamed(err))
return NamedParam(rest)
}