xref: /sqlite-3.40.0/src/tokenize.c (revision e7952263)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** An tokenizer for SQL
13 **
14 ** This file contains C code that splits an SQL input string up into
15 ** individual tokens and sends those tokens one-by-one over to the
16 ** parser for analysis.
17 */
18 #include "sqliteInt.h"
19 #include <stdlib.h>
20 
21 /* Character classes for tokenizing
22 **
23 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
24 ** using a lookup table, whereas a switch() directly on c uses a binary search.
25 ** The lookup table is much faster.  To maximize speed, and to ensure that
26 ** a lookup table is used, all of the classes need to be small integers and
27 ** all of them need to be used within the switch.
28 */
29 #define CC_X          0    /* The letter 'x', or start of BLOB literal */
30 #define CC_KYWD       1    /* Alphabetics or '_'.  Usable in a keyword */
31 #define CC_ID         2    /* unicode characters usable in IDs */
32 #define CC_DIGIT      3    /* Digits */
33 #define CC_DOLLAR     4    /* '$' */
34 #define CC_VARALPHA   5    /* '@', '#', ':'.  Alphabetic SQL variables */
35 #define CC_VARNUM     6    /* '?'.  Numeric SQL variables */
36 #define CC_SPACE      7    /* Space characters */
37 #define CC_QUOTE      8    /* '"', '\'', or '`'.  String literals, quoted ids */
38 #define CC_QUOTE2     9    /* '['.   [...] style quoted ids */
39 #define CC_PIPE      10    /* '|'.   Bitwise OR or concatenate */
40 #define CC_MINUS     11    /* '-'.  Minus or SQL-style comment */
41 #define CC_LT        12    /* '<'.  Part of < or <= or <> */
42 #define CC_GT        13    /* '>'.  Part of > or >= */
43 #define CC_EQ        14    /* '='.  Part of = or == */
44 #define CC_BANG      15    /* '!'.  Part of != */
45 #define CC_SLASH     16    /* '/'.  / or c-style comment */
46 #define CC_LP        17    /* '(' */
47 #define CC_RP        18    /* ')' */
48 #define CC_SEMI      19    /* ';' */
49 #define CC_PLUS      20    /* '+' */
50 #define CC_STAR      21    /* '*' */
51 #define CC_PERCENT   22    /* '%' */
52 #define CC_COMMA     23    /* ',' */
53 #define CC_AND       24    /* '&' */
54 #define CC_TILDA     25    /* '~' */
55 #define CC_DOT       26    /* '.' */
56 #define CC_ILLEGAL   27    /* Illegal character */
57 #define CC_NUL       28    /* 0x00 */
58 
59 static const unsigned char aiClass[] = {
60 #ifdef SQLITE_ASCII
61 /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
62 /* 0x */   28, 27, 27, 27, 27, 27, 27, 27, 27,  7,  7, 27,  7,  7, 27, 27,
63 /* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
64 /* 2x */    7, 15,  8,  5,  4, 22, 24,  8, 17, 18, 21, 20, 23, 11, 26, 16,
65 /* 3x */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  5, 19, 12, 14, 13,  6,
66 /* 4x */    5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
67 /* 5x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1,  9, 27, 27, 27,  1,
68 /* 6x */    8,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
69 /* 7x */    1,  1,  1,  1,  1,  1,  1,  1,  0,  1,  1, 27, 10, 27, 25, 27,
70 /* 8x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
71 /* 9x */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
72 /* Ax */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
73 /* Bx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
74 /* Cx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
75 /* Dx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
76 /* Ex */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
77 /* Fx */    2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2
78 #endif
79 #ifdef SQLITE_EBCDIC
80 /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
81 /* 0x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27,  7,  7, 27, 27,
82 /* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
83 /* 2x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
84 /* 3x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
85 /* 4x */    7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
86 /* 5x */   24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15,  4, 21, 18, 19, 27,
87 /* 6x */   11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22,  1, 13,  6,
88 /* 7x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  8,  5,  5,  5,  8, 14,  8,
89 /* 8x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
90 /* 9x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
91 /* Ax */   27, 25,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
92 /* Bx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27,  9, 27, 27, 27, 27, 27,
93 /* Cx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
94 /* Dx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
95 /* Ex */   27, 27,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
96 /* Fx */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 27, 27, 27, 27, 27, 27,
97 #endif
98 };
99 
100 /*
101 ** The charMap() macro maps alphabetic characters (only) into their
102 ** lower-case ASCII equivalent.  On ASCII machines, this is just
103 ** an upper-to-lower case map.  On EBCDIC machines we also need
104 ** to adjust the encoding.  The mapping is only valid for alphabetics
105 ** which are the only characters for which this feature is used.
106 **
107 ** Used by keywordhash.h
108 */
109 #ifdef SQLITE_ASCII
110 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
111 #endif
112 #ifdef SQLITE_EBCDIC
113 # define charMap(X) ebcdicToAscii[(unsigned char)X]
114 const unsigned char ebcdicToAscii[] = {
115 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
116    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
117    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
118    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
119    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
120    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
121    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
122    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
123    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
124    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
125    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
126    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
127    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
128    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
129    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
130    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
131    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
132 };
133 #endif
134 
135 /*
136 ** The sqlite3KeywordCode function looks up an identifier to determine if
137 ** it is a keyword.  If it is a keyword, the token code of that keyword is
138 ** returned.  If the input is not a keyword, TK_ID is returned.
139 **
140 ** The implementation of this routine was generated by a program,
141 ** mkkeywordhash.c, located in the tool subdirectory of the distribution.
142 ** The output of the mkkeywordhash.c program is written into a file
143 ** named keywordhash.h and then included into this source file by
144 ** the #include below.
145 */
146 #include "keywordhash.h"
147 
148 
149 /*
150 ** If X is a character that can be used in an identifier then
151 ** IdChar(X) will be true.  Otherwise it is false.
152 **
153 ** For ASCII, any character with the high-order bit set is
154 ** allowed in an identifier.  For 7-bit characters,
155 ** sqlite3IsIdChar[X] must be 1.
156 **
157 ** For EBCDIC, the rules are more complex but have the same
158 ** end result.
159 **
160 ** Ticket #1066.  the SQL standard does not allow '$' in the
161 ** middle of identifiers.  But many SQL implementations do.
162 ** SQLite will allow '$' in identifiers for compatibility.
163 ** But the feature is undocumented.
164 */
165 #ifdef SQLITE_ASCII
166 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
167 #endif
168 #ifdef SQLITE_EBCDIC
169 const char sqlite3IsEbcdicIdChar[] = {
170 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
171     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
172     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
173     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
174     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
175     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
176     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
177     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
178     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
179     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
180     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
181     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
182     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
183 };
184 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
185 #endif
186 
187 /* Make the IdChar function accessible from ctime.c */
188 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
189 int sqlite3IsIdChar(u8 c){ return IdChar(c); }
190 #endif
191 
192 #ifndef SQLITE_OMIT_WINDOWFUNC
193 /*
194 ** Return the id of the next token in string (*pz). Before returning, set
195 ** (*pz) to point to the byte following the parsed token.
196 */
197 static int getToken(const unsigned char **pz){
198   const unsigned char *z = *pz;
199   int t;                          /* Token type to return */
200   do {
201     z += sqlite3GetToken(z, &t);
202   }while( t==TK_SPACE );
203   if( t==TK_ID
204    || t==TK_STRING
205    || t==TK_JOIN_KW
206    || t==TK_WINDOW
207    || t==TK_OVER
208    || sqlite3ParserFallback(t)==TK_ID
209   ){
210     t = TK_ID;
211   }
212   *pz = z;
213   return t;
214 }
215 
216 /*
217 ** The following three functions are called immediately after the tokenizer
218 ** reads the keywords WINDOW, OVER and FILTER, respectively, to determine
219 ** whether the token should be treated as a keyword or an SQL identifier.
220 ** This cannot be handled by the usual lemon %fallback method, due to
221 ** the ambiguity in some constructions. e.g.
222 **
223 **   SELECT sum(x) OVER ...
224 **
225 ** In the above, "OVER" might be a keyword, or it might be an alias for the
226 ** sum(x) expression. If a "%fallback ID OVER" directive were added to
227 ** grammar, then SQLite would always treat "OVER" as an alias, making it
228 ** impossible to call a window-function without a FILTER clause.
229 **
230 ** WINDOW is treated as a keyword if:
231 **
232 **   * the following token is an identifier, or a keyword that can fallback
233 **     to being an identifier, and
234 **   * the token after than one is TK_AS.
235 **
236 ** OVER is a keyword if:
237 **
238 **   * the previous token was TK_RP, and
239 **   * the next token is either TK_LP or an identifier.
240 **
241 ** FILTER is a keyword if:
242 **
243 **   * the previous token was TK_RP, and
244 **   * the next token is TK_LP.
245 */
246 static int analyzeWindowKeyword(const unsigned char *z){
247   int t;
248   t = getToken(&z);
249   if( t!=TK_ID ) return TK_ID;
250   t = getToken(&z);
251   if( t!=TK_AS ) return TK_ID;
252   return TK_WINDOW;
253 }
254 static int analyzeOverKeyword(const unsigned char *z, int lastToken){
255   if( lastToken==TK_RP ){
256     int t = getToken(&z);
257     if( t==TK_LP || t==TK_ID ) return TK_OVER;
258   }
259   return TK_ID;
260 }
261 static int analyzeFilterKeyword(const unsigned char *z, int lastToken){
262   if( lastToken==TK_RP && getToken(&z)==TK_LP ){
263     return TK_FILTER;
264   }
265   return TK_ID;
266 }
267 #endif /* SQLITE_OMIT_WINDOWFUNC */
268 
269 /*
270 ** Return the length (in bytes) of the token that begins at z[0].
271 ** Store the token type in *tokenType before returning.
272 */
273 int sqlite3GetToken(const unsigned char *z, int *tokenType){
274   int i, c;
275   switch( aiClass[*z] ){  /* Switch on the character-class of the first byte
276                           ** of the token. See the comment on the CC_ defines
277                           ** above. */
278     case CC_SPACE: {
279       testcase( z[0]==' ' );
280       testcase( z[0]=='\t' );
281       testcase( z[0]=='\n' );
282       testcase( z[0]=='\f' );
283       testcase( z[0]=='\r' );
284       for(i=1; sqlite3Isspace(z[i]); i++){}
285       *tokenType = TK_SPACE;
286       return i;
287     }
288     case CC_MINUS: {
289       if( z[1]=='-' ){
290         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
291         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
292         return i;
293       }
294       *tokenType = TK_MINUS;
295       return 1;
296     }
297     case CC_LP: {
298       *tokenType = TK_LP;
299       return 1;
300     }
301     case CC_RP: {
302       *tokenType = TK_RP;
303       return 1;
304     }
305     case CC_SEMI: {
306       *tokenType = TK_SEMI;
307       return 1;
308     }
309     case CC_PLUS: {
310       *tokenType = TK_PLUS;
311       return 1;
312     }
313     case CC_STAR: {
314       *tokenType = TK_STAR;
315       return 1;
316     }
317     case CC_SLASH: {
318       if( z[1]!='*' || z[2]==0 ){
319         *tokenType = TK_SLASH;
320         return 1;
321       }
322       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
323       if( c ) i++;
324       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
325       return i;
326     }
327     case CC_PERCENT: {
328       *tokenType = TK_REM;
329       return 1;
330     }
331     case CC_EQ: {
332       *tokenType = TK_EQ;
333       return 1 + (z[1]=='=');
334     }
335     case CC_LT: {
336       if( (c=z[1])=='=' ){
337         *tokenType = TK_LE;
338         return 2;
339       }else if( c=='>' ){
340         *tokenType = TK_NE;
341         return 2;
342       }else if( c=='<' ){
343         *tokenType = TK_LSHIFT;
344         return 2;
345       }else{
346         *tokenType = TK_LT;
347         return 1;
348       }
349     }
350     case CC_GT: {
351       if( (c=z[1])=='=' ){
352         *tokenType = TK_GE;
353         return 2;
354       }else if( c=='>' ){
355         *tokenType = TK_RSHIFT;
356         return 2;
357       }else{
358         *tokenType = TK_GT;
359         return 1;
360       }
361     }
362     case CC_BANG: {
363       if( z[1]!='=' ){
364         *tokenType = TK_ILLEGAL;
365         return 1;
366       }else{
367         *tokenType = TK_NE;
368         return 2;
369       }
370     }
371     case CC_PIPE: {
372       if( z[1]!='|' ){
373         *tokenType = TK_BITOR;
374         return 1;
375       }else{
376         *tokenType = TK_CONCAT;
377         return 2;
378       }
379     }
380     case CC_COMMA: {
381       *tokenType = TK_COMMA;
382       return 1;
383     }
384     case CC_AND: {
385       *tokenType = TK_BITAND;
386       return 1;
387     }
388     case CC_TILDA: {
389       *tokenType = TK_BITNOT;
390       return 1;
391     }
392     case CC_QUOTE: {
393       int delim = z[0];
394       testcase( delim=='`' );
395       testcase( delim=='\'' );
396       testcase( delim=='"' );
397       for(i=1; (c=z[i])!=0; i++){
398         if( c==delim ){
399           if( z[i+1]==delim ){
400             i++;
401           }else{
402             break;
403           }
404         }
405       }
406       if( c=='\'' ){
407         *tokenType = TK_STRING;
408         return i+1;
409       }else if( c!=0 ){
410         *tokenType = TK_ID;
411         return i+1;
412       }else{
413         *tokenType = TK_ILLEGAL;
414         return i;
415       }
416     }
417     case CC_DOT: {
418 #ifndef SQLITE_OMIT_FLOATING_POINT
419       if( !sqlite3Isdigit(z[1]) )
420 #endif
421       {
422         *tokenType = TK_DOT;
423         return 1;
424       }
425       /* If the next character is a digit, this is a floating point
426       ** number that begins with ".".  Fall thru into the next case */
427     }
428     case CC_DIGIT: {
429       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
430       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
431       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
432       testcase( z[0]=='9' );
433       *tokenType = TK_INTEGER;
434 #ifndef SQLITE_OMIT_HEX_INTEGER
435       if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){
436         for(i=3; sqlite3Isxdigit(z[i]); i++){}
437         return i;
438       }
439 #endif
440       for(i=0; sqlite3Isdigit(z[i]); i++){}
441 #ifndef SQLITE_OMIT_FLOATING_POINT
442       if( z[i]=='.' ){
443         i++;
444         while( sqlite3Isdigit(z[i]) ){ i++; }
445         *tokenType = TK_FLOAT;
446       }
447       if( (z[i]=='e' || z[i]=='E') &&
448            ( sqlite3Isdigit(z[i+1])
449             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
450            )
451       ){
452         i += 2;
453         while( sqlite3Isdigit(z[i]) ){ i++; }
454         *tokenType = TK_FLOAT;
455       }
456 #endif
457       while( IdChar(z[i]) ){
458         *tokenType = TK_ILLEGAL;
459         i++;
460       }
461       return i;
462     }
463     case CC_QUOTE2: {
464       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
465       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
466       return i;
467     }
468     case CC_VARNUM: {
469       *tokenType = TK_VARIABLE;
470       for(i=1; sqlite3Isdigit(z[i]); i++){}
471       return i;
472     }
473     case CC_DOLLAR:
474     case CC_VARALPHA: {
475       int n = 0;
476       testcase( z[0]=='$' );  testcase( z[0]=='@' );
477       testcase( z[0]==':' );  testcase( z[0]=='#' );
478       *tokenType = TK_VARIABLE;
479       for(i=1; (c=z[i])!=0; i++){
480         if( IdChar(c) ){
481           n++;
482 #ifndef SQLITE_OMIT_TCL_VARIABLE
483         }else if( c=='(' && n>0 ){
484           do{
485             i++;
486           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
487           if( c==')' ){
488             i++;
489           }else{
490             *tokenType = TK_ILLEGAL;
491           }
492           break;
493         }else if( c==':' && z[i+1]==':' ){
494           i++;
495 #endif
496         }else{
497           break;
498         }
499       }
500       if( n==0 ) *tokenType = TK_ILLEGAL;
501       return i;
502     }
503     case CC_KYWD: {
504       for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
505       if( IdChar(z[i]) ){
506         /* This token started out using characters that can appear in keywords,
507         ** but z[i] is a character not allowed within keywords, so this must
508         ** be an identifier instead */
509         i++;
510         break;
511       }
512       *tokenType = TK_ID;
513       return keywordCode((char*)z, i, tokenType);
514     }
515     case CC_X: {
516 #ifndef SQLITE_OMIT_BLOB_LITERAL
517       testcase( z[0]=='x' ); testcase( z[0]=='X' );
518       if( z[1]=='\'' ){
519         *tokenType = TK_BLOB;
520         for(i=2; sqlite3Isxdigit(z[i]); i++){}
521         if( z[i]!='\'' || i%2 ){
522           *tokenType = TK_ILLEGAL;
523           while( z[i] && z[i]!='\'' ){ i++; }
524         }
525         if( z[i] ) i++;
526         return i;
527       }
528 #endif
529       /* If it is not a BLOB literal, then it must be an ID, since no
530       ** SQL keywords start with the letter 'x'.  Fall through */
531     }
532     case CC_ID: {
533       i = 1;
534       break;
535     }
536     case CC_NUL: {
537       *tokenType = TK_ILLEGAL;
538       return 0;
539     }
540     default: {
541       *tokenType = TK_ILLEGAL;
542       return 1;
543     }
544   }
545   while( IdChar(z[i]) ){ i++; }
546   *tokenType = TK_ID;
547   return i;
548 }
549 
550 /*
551 ** Run the parser on the given SQL string.  The parser structure is
552 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
553 ** then an and attempt is made to write an error message into
554 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
555 ** error message.
556 */
557 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
558   int nErr = 0;                   /* Number of errors encountered */
559   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
560   int n = 0;                      /* Length of the next token token */
561   int tokenType;                  /* type of the next token */
562   int lastTokenParsed = -1;       /* type of the previous token */
563   sqlite3 *db = pParse->db;       /* The database connection */
564   int mxSqlLen;                   /* Max length of an SQL string */
565 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
566   yyParser sEngine;    /* Space to hold the Lemon-generated Parser object */
567 #endif
568 
569   assert( zSql!=0 );
570   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
571   if( db->nVdbeActive==0 ){
572     db->u1.isInterrupted = 0;
573   }
574   pParse->rc = SQLITE_OK;
575   pParse->zTail = zSql;
576   assert( pzErrMsg!=0 );
577   /* sqlite3ParserTrace(stdout, "parser: "); */
578 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
579   pEngine = &sEngine;
580   sqlite3ParserInit(pEngine, pParse);
581 #else
582   pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse);
583   if( pEngine==0 ){
584     sqlite3OomFault(db);
585     return SQLITE_NOMEM_BKPT;
586   }
587 #endif
588   assert( pParse->pNewTable==0 );
589   assert( pParse->pNewTrigger==0 );
590   assert( pParse->nVar==0 );
591   assert( pParse->pVList==0 );
592   while( 1 ){
593     n = sqlite3GetToken((u8*)zSql, &tokenType);
594     mxSqlLen -= n;
595     if( mxSqlLen<0 ){
596       pParse->rc = SQLITE_TOOBIG;
597       break;
598     }
599 #ifndef SQLITE_OMIT_WINDOWFUNC
600     if( tokenType>=TK_WINDOW ){
601       assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER
602            || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW
603       );
604 #else
605     if( tokenType>=TK_SPACE ){
606       assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
607 #endif /* SQLITE_OMIT_WINDOWFUNC */
608       if( db->u1.isInterrupted ){
609         pParse->rc = SQLITE_INTERRUPT;
610         break;
611       }
612       if( tokenType==TK_SPACE ){
613         zSql += n;
614         continue;
615       }
616       if( zSql[0]==0 ){
617         /* Upon reaching the end of input, call the parser two more times
618         ** with tokens TK_SEMI and 0, in that order. */
619         if( lastTokenParsed==TK_SEMI ){
620           tokenType = 0;
621         }else if( lastTokenParsed==0 ){
622           break;
623         }else{
624           tokenType = TK_SEMI;
625         }
626         n = 0;
627 #ifndef SQLITE_OMIT_WINDOWFUNC
628       }else if( tokenType==TK_WINDOW ){
629         assert( n==6 );
630         tokenType = analyzeWindowKeyword((const u8*)&zSql[6]);
631       }else if( tokenType==TK_OVER ){
632         assert( n==4 );
633         tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed);
634       }else if( tokenType==TK_FILTER ){
635         assert( n==6 );
636         tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed);
637 #endif /* SQLITE_OMIT_WINDOWFUNC */
638       }else{
639         sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql);
640         break;
641       }
642     }
643     pParse->sLastToken.z = zSql;
644     pParse->sLastToken.n = n;
645     sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
646     lastTokenParsed = tokenType;
647     zSql += n;
648     if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break;
649   }
650   assert( nErr==0 );
651 #ifdef YYTRACKMAXSTACKDEPTH
652   sqlite3_mutex_enter(sqlite3MallocMutex());
653   sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK,
654       sqlite3ParserStackPeak(pEngine)
655   );
656   sqlite3_mutex_leave(sqlite3MallocMutex());
657 #endif /* YYDEBUG */
658 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK
659   sqlite3ParserFinalize(pEngine);
660 #else
661   sqlite3ParserFree(pEngine, sqlite3_free);
662 #endif
663   if( db->mallocFailed ){
664     pParse->rc = SQLITE_NOMEM_BKPT;
665   }
666   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
667     pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
668   }
669   assert( pzErrMsg!=0 );
670   if( pParse->zErrMsg ){
671     *pzErrMsg = pParse->zErrMsg;
672     sqlite3_log(pParse->rc, "%s in \"%s\"",
673                 *pzErrMsg, pParse->zTail);
674     pParse->zErrMsg = 0;
675     nErr++;
676   }
677   pParse->zTail = zSql;
678   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
679     sqlite3VdbeDelete(pParse->pVdbe);
680     pParse->pVdbe = 0;
681   }
682 #ifndef SQLITE_OMIT_SHARED_CACHE
683   if( pParse->nested==0 ){
684     sqlite3DbFree(db, pParse->aTableLock);
685     pParse->aTableLock = 0;
686     pParse->nTableLock = 0;
687   }
688 #endif
689 #ifndef SQLITE_OMIT_VIRTUALTABLE
690   sqlite3_free(pParse->apVtabLock);
691 #endif
692 
693   if( !IN_DECLARE_VTAB ){
694     /* If the pParse->declareVtab flag is set, do not delete any table
695     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
696     ** will take responsibility for freeing the Table structure.
697     */
698     sqlite3DeleteTable(db, pParse->pNewTable);
699   }
700 
701   if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree);
702   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
703   sqlite3DbFree(db, pParse->pVList);
704   while( pParse->pAinc ){
705     AutoincInfo *p = pParse->pAinc;
706     pParse->pAinc = p->pNext;
707     sqlite3DbFreeNN(db, p);
708   }
709   while( pParse->pZombieTab ){
710     Table *p = pParse->pZombieTab;
711     pParse->pZombieTab = p->pNextZombie;
712     sqlite3DeleteTable(db, p);
713   }
714   assert( nErr==0 || pParse->rc!=SQLITE_OK );
715   return nErr;
716 }
717