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*588a9a1aSdrh ** $Id: tokenize.c,v 1.152 2008/09/01 15:52:11 drh Exp $ 1975897234Sdrh */ 2075897234Sdrh #include "sqliteInt.h" 2175897234Sdrh #include <ctype.h> 22dcc581ccSdrh #include <stdlib.h> 2375897234Sdrh 2475897234Sdrh /* 259b8f447bSdrh ** The charMap() macro maps alphabetic characters into their 269b8f447bSdrh ** lower-case ASCII equivalent. On ASCII machines, this is just 279b8f447bSdrh ** an upper-to-lower case map. On EBCDIC machines we also need 289b8f447bSdrh ** to adjust the encoding. Only alphabetic characters and underscores 299b8f447bSdrh ** need to be translated. 309b8f447bSdrh */ 319b8f447bSdrh #ifdef SQLITE_ASCII 329b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X] 339b8f447bSdrh #endif 349b8f447bSdrh #ifdef SQLITE_EBCDIC 359b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X] 369b8f447bSdrh const unsigned char ebcdicToAscii[] = { 379b8f447bSdrh /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 389b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 399b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 409b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 419b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ 429b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ 439b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ 449b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ 459b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 469b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ 479b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ 489b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ 499b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 509b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ 519b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ 529b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ 539b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ 549b8f447bSdrh }; 559b8f447bSdrh #endif 569b8f447bSdrh 579b8f447bSdrh /* 5852fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if 5952fb6d71Sdrh ** it is a keyword. If it is a keyword, the token code of that keyword is 6075897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 612090a0edSdrh ** 622090a0edSdrh ** The implementation of this routine was generated by a program, 6373b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution. 6452fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file 6573b211abSdrh ** named keywordhash.h and then included into this source file by 6652fb6d71Sdrh ** the #include below. 6775897234Sdrh */ 6873b211abSdrh #include "keywordhash.h" 6975897234Sdrh 7040f20f7dSdrh 7198808babSdrh /* 729b8f447bSdrh ** If X is a character that can be used in an identifier then 739b8f447bSdrh ** IdChar(X) will be true. Otherwise it is false. 7498808babSdrh ** 759b8f447bSdrh ** For ASCII, any character with the high-order bit set is 769b8f447bSdrh ** allowed in an identifier. For 7-bit characters, 779b8f447bSdrh ** sqlite3IsIdChar[X] must be 1. 789b8f447bSdrh ** 799b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same 809b8f447bSdrh ** end result. 81a0d1f663Sdrh ** 82a0d1f663Sdrh ** Ticket #1066. the SQL standard does not allow '$' in the 83a0d1f663Sdrh ** middle of identfiers. But many SQL implementations do. 84a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility. 85a0d1f663Sdrh ** But the feature is undocumented. 8698808babSdrh */ 879b8f447bSdrh #ifdef SQLITE_ASCII 8846c99e0fSdrh const char sqlite3IsAsciiIdChar[] = { 8998808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 90a0d1f663Sdrh 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 9198808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ 9298808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 9398808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ 9498808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 9598808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ 9698808babSdrh }; 9746c99e0fSdrh #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsAsciiIdChar[c-0x20])) 989b8f447bSdrh #endif 999b8f447bSdrh #ifdef SQLITE_EBCDIC 10046c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = { 1019b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 1029b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ 1039b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ 1049b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ 1059b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 1069b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ 1079b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ 1089b8f447bSdrh 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ 1099b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 1109b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ 1119b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ 1129b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ 1139b8f447bSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ 1149b8f447bSdrh }; 11546c99e0fSdrh #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) 1169b8f447bSdrh #endif 1179b8f447bSdrh 11898808babSdrh 11975897234Sdrh /* 12061b487d0Sdrh ** Return the length of the token that begins at z[0]. 12161b487d0Sdrh ** Store the token type in *tokenType before returning. 12275897234Sdrh */ 12335b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){ 124aa756b09Sdrh int i, c; 12575897234Sdrh switch( *z ){ 12630cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 12732eb7b47Sdrh for(i=1; isspace(z[i]); i++){} 12875897234Sdrh *tokenType = TK_SPACE; 12975897234Sdrh return i; 13075897234Sdrh } 13175897234Sdrh case '-': { 13275897234Sdrh if( z[1]=='-' ){ 133aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 134200a81dcSdrh *tokenType = TK_SPACE; 13575897234Sdrh return i; 13675897234Sdrh } 13775897234Sdrh *tokenType = TK_MINUS; 13875897234Sdrh return 1; 13975897234Sdrh } 14075897234Sdrh case '(': { 14175897234Sdrh *tokenType = TK_LP; 14275897234Sdrh return 1; 14375897234Sdrh } 14475897234Sdrh case ')': { 14575897234Sdrh *tokenType = TK_RP; 14675897234Sdrh return 1; 14775897234Sdrh } 14875897234Sdrh case ';': { 14975897234Sdrh *tokenType = TK_SEMI; 15075897234Sdrh return 1; 15175897234Sdrh } 15275897234Sdrh case '+': { 15375897234Sdrh *tokenType = TK_PLUS; 15475897234Sdrh return 1; 15575897234Sdrh } 15675897234Sdrh case '*': { 15775897234Sdrh *tokenType = TK_STAR; 15875897234Sdrh return 1; 15975897234Sdrh } 16075897234Sdrh case '/': { 16166105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 16275897234Sdrh *tokenType = TK_SLASH; 16375897234Sdrh return 1; 16475897234Sdrh } 165aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 166aa756b09Sdrh if( c ) i++; 167200a81dcSdrh *tokenType = TK_SPACE; 16866105a8eSdrh return i; 16966105a8eSdrh } 170bf4133cbSdrh case '%': { 171bf4133cbSdrh *tokenType = TK_REM; 172bf4133cbSdrh return 1; 173bf4133cbSdrh } 17475897234Sdrh case '=': { 17575897234Sdrh *tokenType = TK_EQ; 17675897234Sdrh return 1 + (z[1]=='='); 17775897234Sdrh } 17875897234Sdrh case '<': { 179aa756b09Sdrh if( (c=z[1])=='=' ){ 18075897234Sdrh *tokenType = TK_LE; 18175897234Sdrh return 2; 182aa756b09Sdrh }else if( c=='>' ){ 18375897234Sdrh *tokenType = TK_NE; 18475897234Sdrh return 2; 185aa756b09Sdrh }else if( c=='<' ){ 186bf4133cbSdrh *tokenType = TK_LSHIFT; 187bf4133cbSdrh return 2; 18875897234Sdrh }else{ 18975897234Sdrh *tokenType = TK_LT; 19075897234Sdrh return 1; 19175897234Sdrh } 19275897234Sdrh } 19375897234Sdrh case '>': { 194aa756b09Sdrh if( (c=z[1])=='=' ){ 19575897234Sdrh *tokenType = TK_GE; 19675897234Sdrh return 2; 197aa756b09Sdrh }else if( c=='>' ){ 198bf4133cbSdrh *tokenType = TK_RSHIFT; 199bf4133cbSdrh return 2; 20075897234Sdrh }else{ 20175897234Sdrh *tokenType = TK_GT; 20275897234Sdrh return 1; 20375897234Sdrh } 20475897234Sdrh } 20575897234Sdrh case '!': { 20675897234Sdrh if( z[1]!='=' ){ 20775897234Sdrh *tokenType = TK_ILLEGAL; 208c837e709Sdrh return 2; 20975897234Sdrh }else{ 21075897234Sdrh *tokenType = TK_NE; 21175897234Sdrh return 2; 21275897234Sdrh } 21375897234Sdrh } 2140040077dSdrh case '|': { 2150040077dSdrh if( z[1]!='|' ){ 216bf4133cbSdrh *tokenType = TK_BITOR; 2170040077dSdrh return 1; 2180040077dSdrh }else{ 2190040077dSdrh *tokenType = TK_CONCAT; 2200040077dSdrh return 2; 2210040077dSdrh } 2220040077dSdrh } 22375897234Sdrh case ',': { 22475897234Sdrh *tokenType = TK_COMMA; 22575897234Sdrh return 1; 22675897234Sdrh } 227bf4133cbSdrh case '&': { 228bf4133cbSdrh *tokenType = TK_BITAND; 229bf4133cbSdrh return 1; 230bf4133cbSdrh } 231bf4133cbSdrh case '~': { 232bf4133cbSdrh *tokenType = TK_BITNOT; 233bf4133cbSdrh return 1; 234bf4133cbSdrh } 2353d94662aSdrh case '`': 2363d94662aSdrh case '\'': 2373d94662aSdrh case '"': { 23875897234Sdrh int delim = z[0]; 239aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 240aa756b09Sdrh if( c==delim ){ 24175897234Sdrh if( z[i+1]==delim ){ 24275897234Sdrh i++; 24375897234Sdrh }else{ 24475897234Sdrh break; 24575897234Sdrh } 24675897234Sdrh } 24775897234Sdrh } 248a33cb5f7Sdrh if( c=='\'' ){ 24975897234Sdrh *tokenType = TK_STRING; 250eef8b558Sdrh return i+1; 251a33cb5f7Sdrh }else if( c!=0 ){ 252a33cb5f7Sdrh *tokenType = TK_ID; 253a33cb5f7Sdrh return i+1; 254eef8b558Sdrh }else{ 255eef8b558Sdrh *tokenType = TK_ILLEGAL; 25675897234Sdrh return i; 25775897234Sdrh } 258eef8b558Sdrh } 25975897234Sdrh case '.': { 2607681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2617681618cSdrh if( !isdigit(z[1]) ) 2627681618cSdrh #endif 2637681618cSdrh { 26475897234Sdrh *tokenType = TK_DOT; 26575897234Sdrh return 1; 26675897234Sdrh } 2677681618cSdrh /* If the next character is a digit, this is a floating point 2687681618cSdrh ** number that begins with ".". Fall thru into the next case */ 2697681618cSdrh } 27075897234Sdrh case '0': case '1': case '2': case '3': case '4': 27175897234Sdrh case '5': case '6': case '7': case '8': case '9': { 272c837e709Sdrh *tokenType = TK_INTEGER; 2737681618cSdrh for(i=0; isdigit(z[i]); i++){} 274b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2757681618cSdrh if( z[i]=='.' ){ 2767681618cSdrh i++; 27732eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 278c837e709Sdrh *tokenType = TK_FLOAT; 279c837e709Sdrh } 28075897234Sdrh if( (z[i]=='e' || z[i]=='E') && 28175897234Sdrh ( isdigit(z[i+1]) 28275897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 28375897234Sdrh ) 28475897234Sdrh ){ 28575897234Sdrh i += 2; 28632eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 28775897234Sdrh *tokenType = TK_FLOAT; 28875897234Sdrh } 289b7f9164eSdrh #endif 29067dd9011Sdrh while( IdChar(z[i]) ){ 29167dd9011Sdrh *tokenType = TK_ILLEGAL; 29267dd9011Sdrh i++; 29367dd9011Sdrh } 29475897234Sdrh return i; 29575897234Sdrh } 2962f4392ffSdrh case '[': { 297aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 2984b2f9368Sdrh *tokenType = c==']' ? TK_ID : TK_ILLEGAL; 2992f4392ffSdrh return i; 3002f4392ffSdrh } 3017c972decSdrh case '?': { 30250457896Sdrh *tokenType = TK_VARIABLE; 303fa6bc000Sdrh for(i=1; isdigit(z[i]); i++){} 304fa6bc000Sdrh return i; 30550457896Sdrh } 306288d37f1Sdrh case '#': { 307288d37f1Sdrh for(i=1; isdigit(z[i]); i++){} 308288d37f1Sdrh if( i>1 ){ 309288d37f1Sdrh /* Parameters of the form #NNN (where NNN is a number) are used 310288d37f1Sdrh ** internally by sqlite3NestedParse. */ 311288d37f1Sdrh *tokenType = TK_REGISTER; 312895d7472Sdrh return i; 313895d7472Sdrh } 314288d37f1Sdrh /* Fall through into the next case if the '#' is not followed by 315288d37f1Sdrh ** a digit. Try to match #AAAA where AAAA is a parameter name. */ 316288d37f1Sdrh } 317b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 318288d37f1Sdrh case '$': 319288d37f1Sdrh #endif 3200b2a5eeaSdrh case '@': /* For compatibility with MS SQL Server */ 321288d37f1Sdrh case ':': { 322895d7472Sdrh int n = 0; 323288d37f1Sdrh *tokenType = TK_VARIABLE; 324895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 325288d37f1Sdrh if( IdChar(c) ){ 326895d7472Sdrh n++; 327288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 328895d7472Sdrh }else if( c=='(' && n>0 ){ 329895d7472Sdrh do{ 330895d7472Sdrh i++; 331895d7472Sdrh }while( (c=z[i])!=0 && !isspace(c) && c!=')' ); 332895d7472Sdrh if( c==')' ){ 333895d7472Sdrh i++; 334895d7472Sdrh }else{ 335895d7472Sdrh *tokenType = TK_ILLEGAL; 336895d7472Sdrh } 337895d7472Sdrh break; 338895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 339895d7472Sdrh i++; 340288d37f1Sdrh #endif 341895d7472Sdrh }else{ 342895d7472Sdrh break; 343895d7472Sdrh } 344895d7472Sdrh } 3459d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 346895d7472Sdrh return i; 347895d7472Sdrh } 348b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 349c572ef7fSdanielk1977 case 'x': case 'X': { 3504b2f9368Sdrh if( z[1]=='\'' ){ 3513fd0a736Sdanielk1977 *tokenType = TK_BLOB; 3524b2f9368Sdrh for(i=2; (c=z[i])!=0 && c!='\''; i++){ 353aa756b09Sdrh if( !isxdigit(c) ){ 3543fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 3553fd0a736Sdanielk1977 } 356c572ef7fSdanielk1977 } 3574b2f9368Sdrh if( i%2 || !c ) *tokenType = TK_ILLEGAL; 358aa756b09Sdrh if( c ) i++; 359c572ef7fSdanielk1977 return i; 360c572ef7fSdanielk1977 } 361c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 362c572ef7fSdanielk1977 } 363b7f9164eSdrh #endif 36498808babSdrh default: { 3659a087a99Sdrh if( !IdChar(*z) ){ 36698808babSdrh break; 36798808babSdrh } 368aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 369c60e9b82Sdanielk1977 *tokenType = keywordCode((char*)z, i); 37075897234Sdrh return i; 37175897234Sdrh } 37275897234Sdrh } 37375897234Sdrh *tokenType = TK_ILLEGAL; 37475897234Sdrh return 1; 37575897234Sdrh } 37675897234Sdrh 37775897234Sdrh /* 37875897234Sdrh ** Run the parser on the given SQL string. The parser structure is 379b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 380fb45d8c5Sdrh ** then an and attempt is made to write an error message into 381fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that 382fb45d8c5Sdrh ** error message. 38375897234Sdrh */ 3844adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 38575897234Sdrh int nErr = 0; 38675897234Sdrh int i; 38775897234Sdrh void *pEngine; 388b86ccfb2Sdrh int tokenType; 389b86ccfb2Sdrh int lastTokenParsed = -1; 3909bb575fdSdrh sqlite3 *db = pParse->db; 391bb4957f8Sdrh int mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 39275897234Sdrh 39315ca1df1Sdrh if( db->activeVdbeCnt==0 ){ 394881feaa0Sdrh db->u1.isInterrupted = 0; 39515ca1df1Sdrh } 3964c504391Sdrh pParse->rc = SQLITE_OK; 3974b1d66c1Sdrh pParse->zTail = pParse->zSql = zSql; 39875897234Sdrh i = 0; 399fb45d8c5Sdrh assert( pzErrMsg!=0 ); 400e5ae5735Sdrh pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc); 40175897234Sdrh if( pEngine==0 ){ 402f3a65f7eSdrh db->mallocFailed = 1; 403defc9972Sdrh return SQLITE_NOMEM; 40475897234Sdrh } 405fa6bc000Sdrh assert( pParse->sLastToken.dyn==0 ); 406fa6bc000Sdrh assert( pParse->pNewTable==0 ); 407fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 408fa6bc000Sdrh assert( pParse->nVar==0 ); 409fa6bc000Sdrh assert( pParse->nVarExpr==0 ); 410fa6bc000Sdrh assert( pParse->nVarExprAlloc==0 ); 411fa6bc000Sdrh assert( pParse->apVarExpr==0 ); 41217435752Sdrh while( !db->mallocFailed && zSql[i]!=0 ){ 41332eb7b47Sdrh assert( i>=0 ); 4142646da7eSdrh pParse->sLastToken.z = (u8*)&zSql[i]; 41532eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 41635b5a33eSdrh pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); 41775897234Sdrh i += pParse->sLastToken.n; 418bb4957f8Sdrh if( i>mxSqlLen ){ 419e5c941b8Sdrh pParse->rc = SQLITE_TOOBIG; 420e5c941b8Sdrh break; 421e5c941b8Sdrh } 42275897234Sdrh switch( tokenType ){ 423200a81dcSdrh case TK_SPACE: { 424881feaa0Sdrh if( db->u1.isInterrupted ){ 42532eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 426f089aa45Sdrh sqlite3SetString(pzErrMsg, db, "interrupt"); 42732eb7b47Sdrh goto abort_parse; 42832eb7b47Sdrh } 42975897234Sdrh break; 43075897234Sdrh } 43132eb7b47Sdrh case TK_ILLEGAL: { 432633e6d57Sdrh sqlite3DbFree(db, *pzErrMsg); 4331e536953Sdanielk1977 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"", 434ae29ffbeSdrh &pParse->sLastToken); 43575897234Sdrh nErr++; 436caec2f12Sdrh goto abort_parse; 43732eb7b47Sdrh } 438326dce74Sdrh case TK_SEMI: { 439326dce74Sdrh pParse->zTail = &zSql[i]; 440326dce74Sdrh /* Fall thru into the default case */ 441326dce74Sdrh } 44232eb7b47Sdrh default: { 4434adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 444b86ccfb2Sdrh lastTokenParsed = tokenType; 445b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 44632eb7b47Sdrh goto abort_parse; 44775897234Sdrh } 44875897234Sdrh break; 44975897234Sdrh } 45075897234Sdrh } 45132eb7b47Sdrh } 45232eb7b47Sdrh abort_parse: 453b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 454b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 4554adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 456326dce74Sdrh pParse->zTail = &zSql[i]; 45775897234Sdrh } 4584adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 45975897234Sdrh } 460ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH 461ec424a5bSdrh sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, 462ec424a5bSdrh sqlite3ParserStackPeak(pEngine) 463ec424a5bSdrh ); 464ec424a5bSdrh #endif /* YYDEBUG */ 46517435752Sdrh sqlite3ParserFree(pEngine, sqlite3_free); 46617435752Sdrh if( db->mallocFailed ){ 46771c697efSdrh pParse->rc = SQLITE_NOMEM; 46871c697efSdrh } 469b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 470f089aa45Sdrh sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc)); 471b86ccfb2Sdrh } 47275897234Sdrh if( pParse->zErrMsg ){ 473fb45d8c5Sdrh if( *pzErrMsg==0 ){ 47475897234Sdrh *pzErrMsg = pParse->zErrMsg; 47575897234Sdrh }else{ 476633e6d57Sdrh sqlite3DbFree(db, pParse->zErrMsg); 47775897234Sdrh } 478b86ccfb2Sdrh pParse->zErrMsg = 0; 4794b2f9368Sdrh nErr++; 48075897234Sdrh } 4812958a4e6Sdrh if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 4824adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 48375897234Sdrh pParse->pVdbe = 0; 48475897234Sdrh } 485c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE 486c00da105Sdanielk1977 if( pParse->nested==0 ){ 487633e6d57Sdrh sqlite3DbFree(db, pParse->aTableLock); 488c00da105Sdanielk1977 pParse->aTableLock = 0; 489c00da105Sdanielk1977 pParse->nTableLock = 0; 490c00da105Sdanielk1977 } 491c00da105Sdanielk1977 #endif 4924f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 493633e6d57Sdrh sqlite3DbFree(db, pParse->apVtabLock); 4944f3dd150Sdrh #endif 4957e6ebfb2Sdanielk1977 4967e6ebfb2Sdanielk1977 if( !IN_DECLARE_VTAB ){ 4977e6ebfb2Sdanielk1977 /* If the pParse->declareVtab flag is set, do not delete any table 4987e6ebfb2Sdanielk1977 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 4997e6ebfb2Sdanielk1977 ** will take responsibility for freeing the Table structure. 5007e6ebfb2Sdanielk1977 */ 501a04a34ffSdanielk1977 sqlite3DeleteTable(pParse->pNewTable); 5027e6ebfb2Sdanielk1977 } 5037e6ebfb2Sdanielk1977 504633e6d57Sdrh sqlite3DeleteTrigger(db, pParse->pNewTrigger); 505633e6d57Sdrh sqlite3DbFree(db, pParse->apVarExpr); 5068b213899Sdrh sqlite3DbFree(db, pParse->aAlias); 507*588a9a1aSdrh while( pParse->pZombieTab ){ 508*588a9a1aSdrh Table *p = pParse->pZombieTab; 509*588a9a1aSdrh pParse->pZombieTab = p->pNextZombie; 510*588a9a1aSdrh sqlite3DeleteTable(p); 511*588a9a1aSdrh } 512b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 5134c504391Sdrh pParse->rc = SQLITE_ERROR; 5144c504391Sdrh } 51575897234Sdrh return nErr; 51675897234Sdrh } 517