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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
#include "lexer_low_lev.h"
#include "token.h"
#include "keywords.h"
#include "sqlite3_parse.h"
#include "common/utils.h"
#include "common/utils_sql.h"
#include "common/unused.h"
#include <QByteArray>
#include <QChar>
#include <QDebug>
//
// Low-level lexer routines based on tokenizer from SQLite 3.7.15.2
//
int lexerGetToken(const QString& z, TokenPtr& token, bool tolerant);
bool isIdChar(const QChar& c)
{
return c.isPrint() && !c.isSpace() && !doesObjectNeedWrapping(c);
}
// From SQLite source code:
/*
** The following three functions are called immediately after the tokenizer
** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
** whether the token should be treated as a keyword or an SQL identifier.
** This cannot be handled by the usual lemon %fallback method, due to
** the ambiguity in some constructions. e.g.
**
** SELECT sum(x) OVER ...
**
** In the above, "OVER" might be a keyword, or it might be an alias for the
** sum(x) expression. If a "%fallback ID OVER" directive were added to
** grammar, then SQLite would always treat "OVER" as an alias, making it
** impossible to call a window-function without a FILTER clause.
**
** WINDOW is treated as a keyword if:
**
** * the following token is an identifier, or a keyword that can fallback
** to being an identifier, and
** * the token after than one is TK_AS.
**
** OVER is a keyword if:
**
** * the previous token was TK_RP, and
** * the next token is either TK_LP or an identifier.
**
** FILTER is a keyword if:
**
** * the previous token was TK_RP, and
** * the next token is TK_LP.
*/
int sqlite3ParserFallback(int iToken); // defined in parse.y
int lexerWindowSpecificGetToken(const QString& z, TokenPtr& token, const TokenPtr& prevToken, bool tolerant)
{
int lgt = 0;
do
lgt += lexerGetToken(z.mid(lgt), token, prevToken, tolerant);
while (token->lemonType == TK3_SPACE);
if (
token->lemonType == TK3_ID ||
token->lemonType == TK3_STRING ||
token->lemonType == TK3_JOIN_KW ||
token->lemonType == TK3_WINDOW ||
token->lemonType == TK3_OVER ||
sqlite3ParserFallback(token->lemonType) == TK3_ID
) {
token->lemonType = TK3_ID;
token->type = Token::OTHER;
}
return lgt;
}
void lexerHandleWindowKeyword(const QString& z, TokenPtr& token, const TokenPtr& prevToken, bool tolerant)
{
UNUSED(prevToken);
TokenPtr firstAfter = TokenPtr::create();
int lgt = lexerWindowSpecificGetToken(z, firstAfter, token, tolerant);
if (firstAfter->lemonType != TK3_ID)
{
token->lemonType = TK3_ID;
token->type = Token::OTHER;
return;
}
TokenPtr secondAfter = TokenPtr::create();
lexerWindowSpecificGetToken(z.mid(lgt), secondAfter, firstAfter, tolerant);
if (secondAfter->lemonType != TK3_AS)
{
token->lemonType = TK3_ID;
token->type = Token::OTHER;
return;
}
}
void lexerHandleOverKeyword(const QString& z, TokenPtr& token, const TokenPtr& prevToken, bool tolerant)
{
if (prevToken && prevToken->lemonType == TK3_RP) {
TokenPtr firstAfter = TokenPtr::create();
lexerWindowSpecificGetToken(z, firstAfter, token, tolerant);
if (firstAfter->lemonType == TK3_LP || firstAfter->lemonType == TK3_ID)
return; // remains OVER keyword
}
token->lemonType = TK3_ID;
token->type = Token::OTHER;
}
void lexerHandleFilterKeyword(const QString& z, TokenPtr& token, const TokenPtr& prevToken, bool tolerant)
{
if (prevToken && prevToken->lemonType == TK3_RP) {
TokenPtr firstAfter = TokenPtr::create();
lexerWindowSpecificGetToken(z, firstAfter, token, tolerant);
if (firstAfter->lemonType == TK3_LP)
return; // remains FILTER keyword
}
token->lemonType = TK3_ID;
token->type = Token::OTHER;
}
int lexerGetToken(const QString& z, TokenPtr& token, const TokenPtr& prevToken, int sqliteVersion, bool tolerant)
{
UNUSED(sqliteVersion);
int lgt = lexerGetToken(z, token, tolerant);
if (token->lemonType == TK3_WINDOW)
lexerHandleWindowKeyword(z.mid(lgt), token, prevToken, tolerant);
else if (token->lemonType == TK3_OVER)
lexerHandleOverKeyword(z.mid(lgt), token, prevToken, tolerant);
else if (token->lemonType == TK3_FILTER)
lexerHandleFilterKeyword(z.mid(lgt), token, prevToken, tolerant);
return lgt;
}
int lexerGetToken(const QString& z, TokenPtr& token, bool tolerant)
{
if (tolerant && !token.dynamicCast<TolerantToken>())
{
qCritical() << "lexerGetToken() called with tolerant=true, but not a TolerantToken entity!";
return 0;
}
int i;
QChar c;
QChar z0 = charAt(z, 0);
for (;;)
{
if (z0.isSpace())
{
for(i=1; charAt(z, i).isSpace(); i++) {}
token->lemonType = TK3_SPACE;
token->type = Token::SPACE;
return i;
}
if (z0 == '-')
{
if (charAt(z, 1) == '-')
{
for (i=2; !(c = charAt(z, i)).isNull() && c != '\n'; i++) {}
token->lemonType = TK3_COMMENT;
token->type = Token::COMMENT;
return i;
}
else if (charAt(z, 1) == '>')
{
token->lemonType = TK3_PTR;
token->type = Token::OPERATOR;
return (charAt(z, 2) == '>') ? 3 : 2;
}
token->lemonType = TK3_MINUS;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '(')
{
token->lemonType = TK3_LP;
token->type = Token::PAR_LEFT;
return 1;
}
if (z0 == ')')
{
token->lemonType = TK3_RP;
token->type = Token::PAR_RIGHT;
return 1;
}
if (z0 == ';')
{
token->lemonType = TK3_SEMI;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '+')
{
token->lemonType = TK3_PLUS;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '*')
{
token->lemonType = TK3_STAR;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '/')
{
if ( charAt(z, 1) != '*' )
{
token->lemonType = TK3_SLASH;
token->type = Token::OPERATOR;
return 1;
}
if ( charAt(z, 2).isNull() )
{
token->lemonType = TK3_COMMENT;
token->type = Token::COMMENT;
if (tolerant)
token.dynamicCast<TolerantToken>()->invalid = true;
return 2;
}
for (i = 3, c = charAt(z, 2); (c != '*' || charAt(z, i) != '/') && !(c = charAt(z, i)).isNull(); i++) {}
if (tolerant && (c != '*' || charAt(z, i) != '/'))
token.dynamicCast<TolerantToken>()->invalid = true;
#if QT_VERSION >= 0x050800
if ( c.unicode() > 0 )
#else
if ( c > 0 )
#endif
i++;
token->lemonType = TK3_COMMENT;
token->type = Token::COMMENT;
return i;
}
if (z0 == '%')
{
token->lemonType = TK3_REM;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '=')
{
token->lemonType = TK3_EQ;
token->type = Token::OPERATOR;
return 1 + (charAt(z, 1) == '=');
}
if (z0 == '<')
{
if ( (c = charAt(z, 1)) == '=' )
{
token->lemonType = TK3_LE;
token->type = Token::OPERATOR;
return 2;
}
else if ( c == '>' )
{
token->lemonType = TK3_NE;
token->type = Token::OPERATOR;
return 2;
}
else if( c == '<' )
{
token->lemonType = TK3_LSHIFT;
token->type = Token::OPERATOR;
return 2;
}
else
{
token->lemonType = TK3_LT;
token->type = Token::OPERATOR;
return 1;
}
}
if (z0 == '>')
{
if ( (c = charAt(z, 1)) == '=' )
{
token->lemonType = TK3_GE;
token->type = Token::OPERATOR;
return 2;
}
else if ( c == '>' )
{
token->lemonType = TK3_RSHIFT;
token->type = Token::OPERATOR;
return 2;
}
else
{
token->lemonType = TK3_GT;
token->type = Token::OPERATOR;
return 1;
}
}
if (z0 == '!')
{
if ( charAt(z, 1) != '=' )
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
return 2;
}
else
{
token->lemonType = TK3_NE;
token->type = Token::OPERATOR;
return 2;
}
}
if (z0 == '|')
{
if( charAt(z, 1) != '|' )
{
token->lemonType = TK3_BITOR;
token->type = Token::OPERATOR;
return 1;
}
else
{
token->lemonType = TK3_CONCAT;
token->type = Token::OPERATOR;
return 2;
}
}
if (z0 == ',')
{
token->lemonType = TK3_COMMA;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '&')
{
token->lemonType = TK3_BITAND;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '~')
{
token->lemonType = TK3_BITNOT;
token->type = Token::OPERATOR;
return 1;
}
if (z0 == '`' ||
z0 == '\'' ||
z0 == '"')
{
QChar delim = z0;
for (i = 1; !(c = charAt(z, i)).isNull(); i++)
{
if ( c == delim )
{
if( charAt(z, i+1) == delim )
i++;
else
break;
}
}
if ( c == '\'' )
{
token->lemonType = TK3_STRING;
token->type = Token::STRING;
return i+1;
}
else if ( !c.isNull() )
{
token->lemonType = TK3_ID;
token->type = Token::OTHER;
return i+1;
}
else if (tolerant)
{
if (z0 == '\'')
{
token->lemonType = TK3_STRING;
token->type = Token::STRING;
}
else
{
token->lemonType = TK3_ID;
token->type = Token::OTHER;
}
token.dynamicCast<TolerantToken>()->invalid = true;
return i;
}
else
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
return i;
}
}
if (z0 == '.')
{
if( !charAt(z, 1).isDigit() )
{
token->lemonType = TK3_DOT;
token->type = Token::OPERATOR;
return 1;
}
/*
* If the next character is a digit, this is a floating point
* number that begins with ".". Fall thru into the next case
*/
}
if (z0.isDigit() || z0 == '.')
{
token->lemonType = TK3_INTEGER;
token->type = Token::INTEGER;
if (charAt(z, 0) == '0' && (charAt(z, 1) == 'x' || charAt(z, 1) == 'X') && isHex(charAt(z, 2)))
{
for (i=3; isHex(charAt(z, i)); i++) {}
return i;
}
for (i=0; charAt(z, i).isDigit(); i++) {}
if ( charAt(z, i) == '.' )
{
i++;
while ( charAt(z, i).isDigit() )
i++;
token->lemonType = TK3_FLOAT;
token->type = Token::FLOAT;
}
if ( (charAt(z, i) == 'e' || charAt(z, i) == 'E') &&
( charAt(z, i+1).isDigit()
|| ((charAt(z, i+1) == '+' || charAt(z, i+1) == '-') && charAt(z, i+2).isDigit())
)
)
{
i += 2;
while ( charAt(z, i).isDigit() )
i++;
token->lemonType = TK3_FLOAT;
token->type = Token::FLOAT;
}
while ( isIdChar(charAt(z, i)) )
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
i++;
}
return i;
}
if (z0 == '[')
{
for (i = 1, c = z0; c!=']' && !(c = charAt(z, i)).isNull(); i++) {}
if (c == ']')
{
token->lemonType = TK3_ID;
token->type = Token::OTHER;
}
else if (tolerant)
{
token->lemonType = TK3_ID;
token->type = Token::OTHER;
token.dynamicCast<TolerantToken>()->invalid = true;
}
else
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
}
return i;
}
if (z0 == '?')
{
token->lemonType = TK3_VARIABLE;
token->type = Token::BIND_PARAM;
for (i=1; charAt(z, i).isDigit(); i++) {}
return i;
}
if (z0 == '$' ||
z0 == '@' || /* For compatibility with MS SQL Server */
z0 == ':')
{
int n = 0;
token->lemonType = TK3_VARIABLE;
token->type = Token::BIND_PARAM;
for (i = 1; !(c = charAt(z, i)).isNull(); i++)
{
if ( isIdChar(c) )
{
n++;
}
else if ( c == '(' && n > 0 )
{
do
{
i++;
}
while ( !(c = charAt(z, i)).isNull() && !c.isSpace() && c != ')' );
if ( c==')' )
{
i++;
}
else
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
}
break;
}
else if ( c == ':' && charAt(z, i+1) == ':' )
{
i++;
}
else
{
break;
}
}
if( n == 0 )
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
}
return i;
}
if (z0 == 'x' || z0 == 'X')
{
if ( charAt(z, 1) == '\'' )
{
token->lemonType = TK3_BLOB;
token->type = Token::BLOB;
for (i = 2; isXDigit(charAt(z, i)); i++) {}
if (charAt(z, i) != '\'' || i%2)
{
if (tolerant)
{
token->lemonType = TK3_BLOB;
token->type = Token::BLOB;
token.dynamicCast<TolerantToken>()->invalid = true;
}
else
{
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
}
#if QT_VERSION >= 0x050800
while (charAt(z, i).unicode() > 0 && charAt(z, i).unicode() != '\'')
#else
while (charAt(z, i) > 0 && charAt(z, i) != '\'')
#endif
i++;
}
#if QT_VERSION >= 0x050800
if ( charAt(z, i).unicode() > 0 )
#else
if ( charAt(z, i) > 0 )
#endif
i++;
return i;
}
/* Otherwise fall through to the next case */
}
//default:
{
if (!isIdChar(z0))
break;
for (i = 1; isIdChar(charAt(z, i)); i++) {}
token->lemonType = getKeywordId3(z.mid(0, i));
if (token->lemonType == TK3_ID)
token->type = Token::OTHER;
else
token->type = Token::KEYWORD;
return i;
}
}
token->lemonType = TK3_ILLEGAL;
token->type = Token::INVALID;
return 1;
}
|