xref: /sqlite-3.40.0/src/tokenize.c (revision b7916a78)
175897234Sdrh /*
2b19a2bc6Sdrh ** 2001 September 15
375897234Sdrh **
4b19a2bc6Sdrh ** The author disclaims copyright to this source code.  In place of
5b19a2bc6Sdrh ** a legal notice, here is a blessing:
675897234Sdrh **
7b19a2bc6Sdrh **    May you do good and not evil.
8b19a2bc6Sdrh **    May you find forgiveness for yourself and forgive others.
9b19a2bc6Sdrh **    May you share freely, never taking more than you give.
1075897234Sdrh **
1175897234Sdrh *************************************************************************
1275897234Sdrh ** An tokenizer for SQL
1375897234Sdrh **
1475897234Sdrh ** This file contains C code that splits an SQL input string up into
1575897234Sdrh ** individual tokens and sends those tokens one-by-one over to the
1675897234Sdrh ** parser for analysis.
1775897234Sdrh **
18*b7916a78Sdrh ** $Id: tokenize.c,v 1.157 2009/05/27 10:31:29 drh Exp $
1975897234Sdrh */
2075897234Sdrh #include "sqliteInt.h"
21dcc581ccSdrh #include <stdlib.h>
2275897234Sdrh 
2375897234Sdrh /*
249b8f447bSdrh ** The charMap() macro maps alphabetic characters into their
259b8f447bSdrh ** lower-case ASCII equivalent.  On ASCII machines, this is just
269b8f447bSdrh ** an upper-to-lower case map.  On EBCDIC machines we also need
279b8f447bSdrh ** to adjust the encoding.  Only alphabetic characters and underscores
289b8f447bSdrh ** need to be translated.
299b8f447bSdrh */
309b8f447bSdrh #ifdef SQLITE_ASCII
319b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
329b8f447bSdrh #endif
339b8f447bSdrh #ifdef SQLITE_EBCDIC
349b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X]
359b8f447bSdrh const unsigned char ebcdicToAscii[] = {
369b8f447bSdrh /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
379b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
389b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
399b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
409b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
419b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
429b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
439b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
449b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
459b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
469b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
479b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
489b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
499b8f447bSdrh    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
509b8f447bSdrh    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
519b8f447bSdrh    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
529b8f447bSdrh    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
539b8f447bSdrh };
549b8f447bSdrh #endif
559b8f447bSdrh 
569b8f447bSdrh /*
5752fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if
5852fb6d71Sdrh ** it is a keyword.  If it is a keyword, the token code of that keyword is
5975897234Sdrh ** returned.  If the input is not a keyword, TK_ID is returned.
602090a0edSdrh **
612090a0edSdrh ** The implementation of this routine was generated by a program,
6273b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
6352fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file
6473b211abSdrh ** named keywordhash.h and then included into this source file by
6552fb6d71Sdrh ** the #include below.
6675897234Sdrh */
6773b211abSdrh #include "keywordhash.h"
6875897234Sdrh 
6940f20f7dSdrh 
7098808babSdrh /*
719b8f447bSdrh ** If X is a character that can be used in an identifier then
729b8f447bSdrh ** IdChar(X) will be true.  Otherwise it is false.
7398808babSdrh **
749b8f447bSdrh ** For ASCII, any character with the high-order bit set is
759b8f447bSdrh ** allowed in an identifier.  For 7-bit characters,
769b8f447bSdrh ** sqlite3IsIdChar[X] must be 1.
779b8f447bSdrh **
789b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same
799b8f447bSdrh ** end result.
80a0d1f663Sdrh **
81a0d1f663Sdrh ** Ticket #1066.  the SQL standard does not allow '$' in the
82a0d1f663Sdrh ** middle of identfiers.  But many SQL implementations do.
83a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility.
84a0d1f663Sdrh ** But the feature is undocumented.
8598808babSdrh */
869b8f447bSdrh #ifdef SQLITE_ASCII
8746c99e0fSdrh const char sqlite3IsAsciiIdChar[] = {
8898808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
89a0d1f663Sdrh     0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
9098808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
9198808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
9298808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
9398808babSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
9498808babSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
9598808babSdrh };
9646c99e0fSdrh #define IdChar(C)  (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20]))
979b8f447bSdrh #endif
989b8f447bSdrh #ifdef SQLITE_EBCDIC
9946c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = {
1009b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
1019b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
1029b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
1039b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
1049b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
1059b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
1069b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
1079b8f447bSdrh     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
1089b8f447bSdrh     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
1099b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
1109b8f447bSdrh     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
1119b8f447bSdrh     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
1129b8f447bSdrh     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
1139b8f447bSdrh };
11446c99e0fSdrh #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
1159b8f447bSdrh #endif
1169b8f447bSdrh 
11798808babSdrh 
11875897234Sdrh /*
11961b487d0Sdrh ** Return the length of the token that begins at z[0].
12061b487d0Sdrh ** Store the token type in *tokenType before returning.
12175897234Sdrh */
12235b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){
123aa756b09Sdrh   int i, c;
12475897234Sdrh   switch( *z ){
12530cab809Sdrh     case ' ': case '\t': case '\n': case '\f': case '\r': {
12678ca0e7eSdanielk1977       for(i=1; sqlite3Isspace(z[i]); i++){}
12775897234Sdrh       *tokenType = TK_SPACE;
12875897234Sdrh       return i;
12975897234Sdrh     }
13075897234Sdrh     case '-': {
13175897234Sdrh       if( z[1]=='-' ){
132aa756b09Sdrh         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
133200a81dcSdrh         *tokenType = TK_SPACE;
13475897234Sdrh         return i;
13575897234Sdrh       }
13675897234Sdrh       *tokenType = TK_MINUS;
13775897234Sdrh       return 1;
13875897234Sdrh     }
13975897234Sdrh     case '(': {
14075897234Sdrh       *tokenType = TK_LP;
14175897234Sdrh       return 1;
14275897234Sdrh     }
14375897234Sdrh     case ')': {
14475897234Sdrh       *tokenType = TK_RP;
14575897234Sdrh       return 1;
14675897234Sdrh     }
14775897234Sdrh     case ';': {
14875897234Sdrh       *tokenType = TK_SEMI;
14975897234Sdrh       return 1;
15075897234Sdrh     }
15175897234Sdrh     case '+': {
15275897234Sdrh       *tokenType = TK_PLUS;
15375897234Sdrh       return 1;
15475897234Sdrh     }
15575897234Sdrh     case '*': {
15675897234Sdrh       *tokenType = TK_STAR;
15775897234Sdrh       return 1;
15875897234Sdrh     }
15975897234Sdrh     case '/': {
16066105a8eSdrh       if( z[1]!='*' || z[2]==0 ){
16175897234Sdrh         *tokenType = TK_SLASH;
16275897234Sdrh         return 1;
16375897234Sdrh       }
164aa756b09Sdrh       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
165aa756b09Sdrh       if( c ) i++;
166200a81dcSdrh       *tokenType = TK_SPACE;
16766105a8eSdrh       return i;
16866105a8eSdrh     }
169bf4133cbSdrh     case '%': {
170bf4133cbSdrh       *tokenType = TK_REM;
171bf4133cbSdrh       return 1;
172bf4133cbSdrh     }
17375897234Sdrh     case '=': {
17475897234Sdrh       *tokenType = TK_EQ;
17575897234Sdrh       return 1 + (z[1]=='=');
17675897234Sdrh     }
17775897234Sdrh     case '<': {
178aa756b09Sdrh       if( (c=z[1])=='=' ){
17975897234Sdrh         *tokenType = TK_LE;
18075897234Sdrh         return 2;
181aa756b09Sdrh       }else if( c=='>' ){
18275897234Sdrh         *tokenType = TK_NE;
18375897234Sdrh         return 2;
184aa756b09Sdrh       }else if( c=='<' ){
185bf4133cbSdrh         *tokenType = TK_LSHIFT;
186bf4133cbSdrh         return 2;
18775897234Sdrh       }else{
18875897234Sdrh         *tokenType = TK_LT;
18975897234Sdrh         return 1;
19075897234Sdrh       }
19175897234Sdrh     }
19275897234Sdrh     case '>': {
193aa756b09Sdrh       if( (c=z[1])=='=' ){
19475897234Sdrh         *tokenType = TK_GE;
19575897234Sdrh         return 2;
196aa756b09Sdrh       }else if( c=='>' ){
197bf4133cbSdrh         *tokenType = TK_RSHIFT;
198bf4133cbSdrh         return 2;
19975897234Sdrh       }else{
20075897234Sdrh         *tokenType = TK_GT;
20175897234Sdrh         return 1;
20275897234Sdrh       }
20375897234Sdrh     }
20475897234Sdrh     case '!': {
20575897234Sdrh       if( z[1]!='=' ){
20675897234Sdrh         *tokenType = TK_ILLEGAL;
207c837e709Sdrh         return 2;
20875897234Sdrh       }else{
20975897234Sdrh         *tokenType = TK_NE;
21075897234Sdrh         return 2;
21175897234Sdrh       }
21275897234Sdrh     }
2130040077dSdrh     case '|': {
2140040077dSdrh       if( z[1]!='|' ){
215bf4133cbSdrh         *tokenType = TK_BITOR;
2160040077dSdrh         return 1;
2170040077dSdrh       }else{
2180040077dSdrh         *tokenType = TK_CONCAT;
2190040077dSdrh         return 2;
2200040077dSdrh       }
2210040077dSdrh     }
22275897234Sdrh     case ',': {
22375897234Sdrh       *tokenType = TK_COMMA;
22475897234Sdrh       return 1;
22575897234Sdrh     }
226bf4133cbSdrh     case '&': {
227bf4133cbSdrh       *tokenType = TK_BITAND;
228bf4133cbSdrh       return 1;
229bf4133cbSdrh     }
230bf4133cbSdrh     case '~': {
231bf4133cbSdrh       *tokenType = TK_BITNOT;
232bf4133cbSdrh       return 1;
233bf4133cbSdrh     }
2343d94662aSdrh     case '`':
2353d94662aSdrh     case '\'':
2363d94662aSdrh     case '"': {
23775897234Sdrh       int delim = z[0];
238aa756b09Sdrh       for(i=1; (c=z[i])!=0; i++){
239aa756b09Sdrh         if( c==delim ){
24075897234Sdrh           if( z[i+1]==delim ){
24175897234Sdrh             i++;
24275897234Sdrh           }else{
24375897234Sdrh             break;
24475897234Sdrh           }
24575897234Sdrh         }
24675897234Sdrh       }
247a33cb5f7Sdrh       if( c=='\'' ){
24875897234Sdrh         *tokenType = TK_STRING;
249eef8b558Sdrh         return i+1;
250a33cb5f7Sdrh       }else if( c!=0 ){
251a33cb5f7Sdrh         *tokenType = TK_ID;
252a33cb5f7Sdrh         return i+1;
253eef8b558Sdrh       }else{
254eef8b558Sdrh         *tokenType = TK_ILLEGAL;
25575897234Sdrh         return i;
25675897234Sdrh       }
257eef8b558Sdrh     }
25875897234Sdrh     case '.': {
2597681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
26078ca0e7eSdanielk1977       if( !sqlite3Isdigit(z[1]) )
2617681618cSdrh #endif
2627681618cSdrh       {
26375897234Sdrh         *tokenType = TK_DOT;
26475897234Sdrh         return 1;
26575897234Sdrh       }
2667681618cSdrh       /* If the next character is a digit, this is a floating point
2677681618cSdrh       ** number that begins with ".".  Fall thru into the next case */
2687681618cSdrh     }
26975897234Sdrh     case '0': case '1': case '2': case '3': case '4':
27075897234Sdrh     case '5': case '6': case '7': case '8': case '9': {
271c837e709Sdrh       *tokenType = TK_INTEGER;
27278ca0e7eSdanielk1977       for(i=0; sqlite3Isdigit(z[i]); i++){}
273b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT
2747681618cSdrh       if( z[i]=='.' ){
2757681618cSdrh         i++;
27678ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
277c837e709Sdrh         *tokenType = TK_FLOAT;
278c837e709Sdrh       }
27975897234Sdrh       if( (z[i]=='e' || z[i]=='E') &&
28078ca0e7eSdanielk1977            ( sqlite3Isdigit(z[i+1])
28178ca0e7eSdanielk1977             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
28275897234Sdrh            )
28375897234Sdrh       ){
28475897234Sdrh         i += 2;
28578ca0e7eSdanielk1977         while( sqlite3Isdigit(z[i]) ){ i++; }
28675897234Sdrh         *tokenType = TK_FLOAT;
28775897234Sdrh       }
288b7f9164eSdrh #endif
28967dd9011Sdrh       while( IdChar(z[i]) ){
29067dd9011Sdrh         *tokenType = TK_ILLEGAL;
29167dd9011Sdrh         i++;
29267dd9011Sdrh       }
29375897234Sdrh       return i;
29475897234Sdrh     }
2952f4392ffSdrh     case '[': {
296aa756b09Sdrh       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
2974b2f9368Sdrh       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
2982f4392ffSdrh       return i;
2992f4392ffSdrh     }
3007c972decSdrh     case '?': {
30150457896Sdrh       *tokenType = TK_VARIABLE;
30278ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
303fa6bc000Sdrh       return i;
30450457896Sdrh     }
305288d37f1Sdrh     case '#': {
30678ca0e7eSdanielk1977       for(i=1; sqlite3Isdigit(z[i]); i++){}
307288d37f1Sdrh       if( i>1 ){
308288d37f1Sdrh         /* Parameters of the form #NNN (where NNN is a number) are used
309288d37f1Sdrh         ** internally by sqlite3NestedParse.  */
310288d37f1Sdrh         *tokenType = TK_REGISTER;
311895d7472Sdrh         return i;
312895d7472Sdrh       }
313288d37f1Sdrh       /* Fall through into the next case if the '#' is not followed by
314288d37f1Sdrh       ** a digit. Try to match #AAAA where AAAA is a parameter name. */
315288d37f1Sdrh     }
316b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
317288d37f1Sdrh     case '$':
318288d37f1Sdrh #endif
3190b2a5eeaSdrh     case '@':  /* For compatibility with MS SQL Server */
320288d37f1Sdrh     case ':': {
321895d7472Sdrh       int n = 0;
322288d37f1Sdrh       *tokenType = TK_VARIABLE;
323895d7472Sdrh       for(i=1; (c=z[i])!=0; i++){
324288d37f1Sdrh         if( IdChar(c) ){
325895d7472Sdrh           n++;
326288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE
327895d7472Sdrh         }else if( c=='(' && n>0 ){
328895d7472Sdrh           do{
329895d7472Sdrh             i++;
33078ca0e7eSdanielk1977           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
331895d7472Sdrh           if( c==')' ){
332895d7472Sdrh             i++;
333895d7472Sdrh           }else{
334895d7472Sdrh             *tokenType = TK_ILLEGAL;
335895d7472Sdrh           }
336895d7472Sdrh           break;
337895d7472Sdrh         }else if( c==':' && z[i+1]==':' ){
338895d7472Sdrh           i++;
339288d37f1Sdrh #endif
340895d7472Sdrh         }else{
341895d7472Sdrh           break;
342895d7472Sdrh         }
343895d7472Sdrh       }
3449d74b4c5Sdrh       if( n==0 ) *tokenType = TK_ILLEGAL;
345895d7472Sdrh       return i;
346895d7472Sdrh     }
347b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL
348c572ef7fSdanielk1977     case 'x': case 'X': {
3494b2f9368Sdrh       if( z[1]=='\'' ){
3503fd0a736Sdanielk1977         *tokenType = TK_BLOB;
3514b2f9368Sdrh         for(i=2; (c=z[i])!=0 && c!='\''; i++){
35278ca0e7eSdanielk1977           if( !sqlite3Isxdigit(c) ){
3533fd0a736Sdanielk1977             *tokenType = TK_ILLEGAL;
3543fd0a736Sdanielk1977           }
355c572ef7fSdanielk1977         }
3564b2f9368Sdrh         if( i%2 || !c ) *tokenType = TK_ILLEGAL;
357aa756b09Sdrh         if( c ) i++;
358c572ef7fSdanielk1977         return i;
359c572ef7fSdanielk1977       }
360c572ef7fSdanielk1977       /* Otherwise fall through to the next case */
361c572ef7fSdanielk1977     }
362b7f9164eSdrh #endif
36398808babSdrh     default: {
3649a087a99Sdrh       if( !IdChar(*z) ){
36598808babSdrh         break;
36698808babSdrh       }
367aa756b09Sdrh       for(i=1; IdChar(z[i]); i++){}
368c60e9b82Sdanielk1977       *tokenType = keywordCode((char*)z, i);
36975897234Sdrh       return i;
37075897234Sdrh     }
37175897234Sdrh   }
37275897234Sdrh   *tokenType = TK_ILLEGAL;
37375897234Sdrh   return 1;
37475897234Sdrh }
37575897234Sdrh 
37675897234Sdrh /*
37775897234Sdrh ** Run the parser on the given SQL string.  The parser structure is
378b19a2bc6Sdrh ** passed in.  An SQLITE_ status code is returned.  If an error occurs
379fb45d8c5Sdrh ** then an and attempt is made to write an error message into
380fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
381fb45d8c5Sdrh ** error message.
38275897234Sdrh */
3834adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
384d9da78a2Sdrh   int nErr = 0;                   /* Number of errors encountered */
385d9da78a2Sdrh   int i;                          /* Loop counter */
386d9da78a2Sdrh   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
387d9da78a2Sdrh   int tokenType;                  /* type of the next token */
388d9da78a2Sdrh   int lastTokenParsed = -1;       /* type of the previous token */
389b08a67a7Sshane   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
390d9da78a2Sdrh   sqlite3 *db = pParse->db;       /* The database connection */
391d9da78a2Sdrh   int mxSqlLen;                   /* Max length of an SQL string */
39275897234Sdrh 
393d9da78a2Sdrh 
394d9da78a2Sdrh   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
39515ca1df1Sdrh   if( db->activeVdbeCnt==0 ){
396881feaa0Sdrh     db->u1.isInterrupted = 0;
39715ca1df1Sdrh   }
3984c504391Sdrh   pParse->rc = SQLITE_OK;
3994b1d66c1Sdrh   pParse->zTail = pParse->zSql = zSql;
40075897234Sdrh   i = 0;
401fb45d8c5Sdrh   assert( pzErrMsg!=0 );
402e5ae5735Sdrh   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
40375897234Sdrh   if( pEngine==0 ){
404f3a65f7eSdrh     db->mallocFailed = 1;
405defc9972Sdrh     return SQLITE_NOMEM;
40675897234Sdrh   }
407fa6bc000Sdrh   assert( pParse->pNewTable==0 );
408fa6bc000Sdrh   assert( pParse->pNewTrigger==0 );
409fa6bc000Sdrh   assert( pParse->nVar==0 );
410fa6bc000Sdrh   assert( pParse->nVarExpr==0 );
411fa6bc000Sdrh   assert( pParse->nVarExprAlloc==0 );
412fa6bc000Sdrh   assert( pParse->apVarExpr==0 );
413d9da78a2Sdrh   enableLookaside = db->lookaside.bEnabled;
414d9da78a2Sdrh   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
41517435752Sdrh   while( !db->mallocFailed && zSql[i]!=0 ){
41632eb7b47Sdrh     assert( i>=0 );
417*b7916a78Sdrh     pParse->sLastToken.z = &zSql[i];
41835b5a33eSdrh     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
41975897234Sdrh     i += pParse->sLastToken.n;
420bb4957f8Sdrh     if( i>mxSqlLen ){
421e5c941b8Sdrh       pParse->rc = SQLITE_TOOBIG;
422e5c941b8Sdrh       break;
423e5c941b8Sdrh     }
42475897234Sdrh     switch( tokenType ){
425200a81dcSdrh       case TK_SPACE: {
426881feaa0Sdrh         if( db->u1.isInterrupted ){
42732eb7b47Sdrh           pParse->rc = SQLITE_INTERRUPT;
428f089aa45Sdrh           sqlite3SetString(pzErrMsg, db, "interrupt");
42932eb7b47Sdrh           goto abort_parse;
43032eb7b47Sdrh         }
43175897234Sdrh         break;
43275897234Sdrh       }
43332eb7b47Sdrh       case TK_ILLEGAL: {
434633e6d57Sdrh         sqlite3DbFree(db, *pzErrMsg);
4351e536953Sdanielk1977         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
436ae29ffbeSdrh                         &pParse->sLastToken);
43775897234Sdrh         nErr++;
438caec2f12Sdrh         goto abort_parse;
43932eb7b47Sdrh       }
440326dce74Sdrh       case TK_SEMI: {
441326dce74Sdrh         pParse->zTail = &zSql[i];
442326dce74Sdrh         /* Fall thru into the default case */
443326dce74Sdrh       }
44432eb7b47Sdrh       default: {
4454adee20fSdanielk1977         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
446b86ccfb2Sdrh         lastTokenParsed = tokenType;
447b86ccfb2Sdrh         if( pParse->rc!=SQLITE_OK ){
44832eb7b47Sdrh           goto abort_parse;
44975897234Sdrh         }
45075897234Sdrh         break;
45175897234Sdrh       }
45275897234Sdrh     }
45332eb7b47Sdrh   }
45432eb7b47Sdrh abort_parse:
455b86ccfb2Sdrh   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
456b86ccfb2Sdrh     if( lastTokenParsed!=TK_SEMI ){
4574adee20fSdanielk1977       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
458326dce74Sdrh       pParse->zTail = &zSql[i];
45975897234Sdrh     }
4604adee20fSdanielk1977     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
46175897234Sdrh   }
462ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH
463ec424a5bSdrh   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
464ec424a5bSdrh       sqlite3ParserStackPeak(pEngine)
465ec424a5bSdrh   );
466ec424a5bSdrh #endif /* YYDEBUG */
46717435752Sdrh   sqlite3ParserFree(pEngine, sqlite3_free);
468d9da78a2Sdrh   db->lookaside.bEnabled = enableLookaside;
46917435752Sdrh   if( db->mallocFailed ){
47071c697efSdrh     pParse->rc = SQLITE_NOMEM;
47171c697efSdrh   }
472b86ccfb2Sdrh   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
473f089aa45Sdrh     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
474b86ccfb2Sdrh   }
47575897234Sdrh   if( pParse->zErrMsg ){
476fb45d8c5Sdrh     if( *pzErrMsg==0 ){
47775897234Sdrh       *pzErrMsg = pParse->zErrMsg;
47875897234Sdrh     }else{
479633e6d57Sdrh       sqlite3DbFree(db, pParse->zErrMsg);
48075897234Sdrh     }
481b86ccfb2Sdrh     pParse->zErrMsg = 0;
4824b2f9368Sdrh     nErr++;
48375897234Sdrh   }
4842958a4e6Sdrh   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
4854adee20fSdanielk1977     sqlite3VdbeDelete(pParse->pVdbe);
48675897234Sdrh     pParse->pVdbe = 0;
48775897234Sdrh   }
488c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE
489c00da105Sdanielk1977   if( pParse->nested==0 ){
490633e6d57Sdrh     sqlite3DbFree(db, pParse->aTableLock);
491c00da105Sdanielk1977     pParse->aTableLock = 0;
492c00da105Sdanielk1977     pParse->nTableLock = 0;
493c00da105Sdanielk1977   }
494c00da105Sdanielk1977 #endif
4954f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE
496633e6d57Sdrh   sqlite3DbFree(db, pParse->apVtabLock);
4974f3dd150Sdrh #endif
4987e6ebfb2Sdanielk1977 
4997e6ebfb2Sdanielk1977   if( !IN_DECLARE_VTAB ){
5007e6ebfb2Sdanielk1977     /* If the pParse->declareVtab flag is set, do not delete any table
5017e6ebfb2Sdanielk1977     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
5027e6ebfb2Sdanielk1977     ** will take responsibility for freeing the Table structure.
5037e6ebfb2Sdanielk1977     */
504a04a34ffSdanielk1977     sqlite3DeleteTable(pParse->pNewTable);
5057e6ebfb2Sdanielk1977   }
5067e6ebfb2Sdanielk1977 
507633e6d57Sdrh   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
508633e6d57Sdrh   sqlite3DbFree(db, pParse->apVarExpr);
5098b213899Sdrh   sqlite3DbFree(db, pParse->aAlias);
510588a9a1aSdrh   while( pParse->pZombieTab ){
511588a9a1aSdrh     Table *p = pParse->pZombieTab;
512588a9a1aSdrh     pParse->pZombieTab = p->pNextZombie;
513588a9a1aSdrh     sqlite3DeleteTable(p);
514588a9a1aSdrh   }
515b86ccfb2Sdrh   if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){
5164c504391Sdrh     pParse->rc = SQLITE_ERROR;
5174c504391Sdrh   }
51875897234Sdrh   return nErr;
51975897234Sdrh }
520