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*881feaa0Sdrh ** $Id: tokenize.c,v 1.122 2006/07/26 01:39:30 drh Exp $ 1975897234Sdrh */ 2075897234Sdrh #include "sqliteInt.h" 21ad75e987Sdrh #include "os.h" 2275897234Sdrh #include <ctype.h> 23dcc581ccSdrh #include <stdlib.h> 2475897234Sdrh 2575897234Sdrh /* 269b8f447bSdrh ** The charMap() macro maps alphabetic characters into their 279b8f447bSdrh ** lower-case ASCII equivalent. On ASCII machines, this is just 289b8f447bSdrh ** an upper-to-lower case map. On EBCDIC machines we also need 299b8f447bSdrh ** to adjust the encoding. Only alphabetic characters and underscores 309b8f447bSdrh ** need to be translated. 319b8f447bSdrh */ 329b8f447bSdrh #ifdef SQLITE_ASCII 339b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X] 349b8f447bSdrh #endif 359b8f447bSdrh #ifdef SQLITE_EBCDIC 369b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X] 379b8f447bSdrh const unsigned char ebcdicToAscii[] = { 389b8f447bSdrh /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 399b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 409b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 419b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 429b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ 439b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ 449b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ 459b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ 469b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 479b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ 489b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ 499b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ 509b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 519b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ 529b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ 539b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ 549b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ 559b8f447bSdrh }; 569b8f447bSdrh #endif 579b8f447bSdrh 589b8f447bSdrh /* 5952fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if 6052fb6d71Sdrh ** it is a keyword. If it is a keyword, the token code of that keyword is 6175897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 622090a0edSdrh ** 632090a0edSdrh ** The implementation of this routine was generated by a program, 6473b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution. 6552fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file 6673b211abSdrh ** named keywordhash.h and then included into this source file by 6752fb6d71Sdrh ** the #include below. 6875897234Sdrh */ 6973b211abSdrh #include "keywordhash.h" 7075897234Sdrh 7140f20f7dSdrh 7298808babSdrh /* 739b8f447bSdrh ** If X is a character that can be used in an identifier then 749b8f447bSdrh ** IdChar(X) will be true. Otherwise it is false. 7598808babSdrh ** 769b8f447bSdrh ** For ASCII, any character with the high-order bit set is 779b8f447bSdrh ** allowed in an identifier. For 7-bit characters, 789b8f447bSdrh ** sqlite3IsIdChar[X] must be 1. 799b8f447bSdrh ** 809b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same 819b8f447bSdrh ** end result. 82a0d1f663Sdrh ** 83a0d1f663Sdrh ** Ticket #1066. the SQL standard does not allow '$' in the 84a0d1f663Sdrh ** middle of identfiers. But many SQL implementations do. 85a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility. 86a0d1f663Sdrh ** But the feature is undocumented. 8798808babSdrh */ 889b8f447bSdrh #ifdef SQLITE_ASCII 89a2b902d6Sdrh const char sqlite3IsIdChar[] = { 9098808babSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 91a0d1f663Sdrh 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 9298808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ 9398808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ 9498808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ 9598808babSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ 9698808babSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ 9798808babSdrh }; 98a2b902d6Sdrh #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20])) 999b8f447bSdrh #endif 1009b8f447bSdrh #ifdef SQLITE_EBCDIC 1019b8f447bSdrh const char sqlite3IsIdChar[] = { 1029b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 1039b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ 1049b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ 1059b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ 1069b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 1079b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ 1089b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ 1099b8f447bSdrh 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ 1109b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 1119b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ 1129b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ 1139b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ 1149b8f447bSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ 1159b8f447bSdrh }; 1169b8f447bSdrh #define IdChar(C) (((c=C)>=0x42 && sqlite3IsIdChar[c-0x40])) 1179b8f447bSdrh #endif 1189b8f447bSdrh 11998808babSdrh 12075897234Sdrh /* 12161b487d0Sdrh ** Return the length of the token that begins at z[0]. 12261b487d0Sdrh ** Store the token type in *tokenType before returning. 12375897234Sdrh */ 124c60e9b82Sdanielk1977 static int getToken(const unsigned char *z, int *tokenType){ 125aa756b09Sdrh int i, c; 12675897234Sdrh switch( *z ){ 12730cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 12832eb7b47Sdrh for(i=1; isspace(z[i]); i++){} 12975897234Sdrh *tokenType = TK_SPACE; 13075897234Sdrh return i; 13175897234Sdrh } 13275897234Sdrh case '-': { 13375897234Sdrh if( z[1]=='-' ){ 134aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 13575897234Sdrh *tokenType = TK_COMMENT; 13675897234Sdrh return i; 13775897234Sdrh } 13875897234Sdrh *tokenType = TK_MINUS; 13975897234Sdrh return 1; 14075897234Sdrh } 14175897234Sdrh case '(': { 14275897234Sdrh *tokenType = TK_LP; 14375897234Sdrh return 1; 14475897234Sdrh } 14575897234Sdrh case ')': { 14675897234Sdrh *tokenType = TK_RP; 14775897234Sdrh return 1; 14875897234Sdrh } 14975897234Sdrh case ';': { 15075897234Sdrh *tokenType = TK_SEMI; 15175897234Sdrh return 1; 15275897234Sdrh } 15375897234Sdrh case '+': { 15475897234Sdrh *tokenType = TK_PLUS; 15575897234Sdrh return 1; 15675897234Sdrh } 15775897234Sdrh case '*': { 15875897234Sdrh *tokenType = TK_STAR; 15975897234Sdrh return 1; 16075897234Sdrh } 16175897234Sdrh case '/': { 16266105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 16375897234Sdrh *tokenType = TK_SLASH; 16475897234Sdrh return 1; 16575897234Sdrh } 166aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 167aa756b09Sdrh if( c ) i++; 16866105a8eSdrh *tokenType = TK_COMMENT; 16966105a8eSdrh return i; 17066105a8eSdrh } 171bf4133cbSdrh case '%': { 172bf4133cbSdrh *tokenType = TK_REM; 173bf4133cbSdrh return 1; 174bf4133cbSdrh } 17575897234Sdrh case '=': { 17675897234Sdrh *tokenType = TK_EQ; 17775897234Sdrh return 1 + (z[1]=='='); 17875897234Sdrh } 17975897234Sdrh case '<': { 180aa756b09Sdrh if( (c=z[1])=='=' ){ 18175897234Sdrh *tokenType = TK_LE; 18275897234Sdrh return 2; 183aa756b09Sdrh }else if( c=='>' ){ 18475897234Sdrh *tokenType = TK_NE; 18575897234Sdrh return 2; 186aa756b09Sdrh }else if( c=='<' ){ 187bf4133cbSdrh *tokenType = TK_LSHIFT; 188bf4133cbSdrh return 2; 18975897234Sdrh }else{ 19075897234Sdrh *tokenType = TK_LT; 19175897234Sdrh return 1; 19275897234Sdrh } 19375897234Sdrh } 19475897234Sdrh case '>': { 195aa756b09Sdrh if( (c=z[1])=='=' ){ 19675897234Sdrh *tokenType = TK_GE; 19775897234Sdrh return 2; 198aa756b09Sdrh }else if( c=='>' ){ 199bf4133cbSdrh *tokenType = TK_RSHIFT; 200bf4133cbSdrh return 2; 20175897234Sdrh }else{ 20275897234Sdrh *tokenType = TK_GT; 20375897234Sdrh return 1; 20475897234Sdrh } 20575897234Sdrh } 20675897234Sdrh case '!': { 20775897234Sdrh if( z[1]!='=' ){ 20875897234Sdrh *tokenType = TK_ILLEGAL; 209c837e709Sdrh return 2; 21075897234Sdrh }else{ 21175897234Sdrh *tokenType = TK_NE; 21275897234Sdrh return 2; 21375897234Sdrh } 21475897234Sdrh } 2150040077dSdrh case '|': { 2160040077dSdrh if( z[1]!='|' ){ 217bf4133cbSdrh *tokenType = TK_BITOR; 2180040077dSdrh return 1; 2190040077dSdrh }else{ 2200040077dSdrh *tokenType = TK_CONCAT; 2210040077dSdrh return 2; 2220040077dSdrh } 2230040077dSdrh } 22475897234Sdrh case ',': { 22575897234Sdrh *tokenType = TK_COMMA; 22675897234Sdrh return 1; 22775897234Sdrh } 228bf4133cbSdrh case '&': { 229bf4133cbSdrh *tokenType = TK_BITAND; 230bf4133cbSdrh return 1; 231bf4133cbSdrh } 232bf4133cbSdrh case '~': { 233bf4133cbSdrh *tokenType = TK_BITNOT; 234bf4133cbSdrh return 1; 235bf4133cbSdrh } 2363d94662aSdrh case '`': 2373d94662aSdrh case '\'': 2383d94662aSdrh case '"': { 23975897234Sdrh int delim = z[0]; 240aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 241aa756b09Sdrh if( c==delim ){ 24275897234Sdrh if( z[i+1]==delim ){ 24375897234Sdrh i++; 24475897234Sdrh }else{ 24575897234Sdrh break; 24675897234Sdrh } 24775897234Sdrh } 24875897234Sdrh } 249eef8b558Sdrh if( c ){ 25075897234Sdrh *tokenType = TK_STRING; 251eef8b558Sdrh return i+1; 252eef8b558Sdrh }else{ 253eef8b558Sdrh *tokenType = TK_ILLEGAL; 25475897234Sdrh return i; 25575897234Sdrh } 256eef8b558Sdrh } 25775897234Sdrh case '.': { 2587681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2597681618cSdrh if( !isdigit(z[1]) ) 2607681618cSdrh #endif 2617681618cSdrh { 26275897234Sdrh *tokenType = TK_DOT; 26375897234Sdrh return 1; 26475897234Sdrh } 2657681618cSdrh /* If the next character is a digit, this is a floating point 2667681618cSdrh ** number that begins with ".". Fall thru into the next case */ 2677681618cSdrh } 26875897234Sdrh case '0': case '1': case '2': case '3': case '4': 26975897234Sdrh case '5': case '6': case '7': case '8': case '9': { 270c837e709Sdrh *tokenType = TK_INTEGER; 2717681618cSdrh for(i=0; isdigit(z[i]); i++){} 272b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2737681618cSdrh if( z[i]=='.' ){ 2747681618cSdrh i++; 27532eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 276c837e709Sdrh *tokenType = TK_FLOAT; 277c837e709Sdrh } 27875897234Sdrh if( (z[i]=='e' || z[i]=='E') && 27975897234Sdrh ( isdigit(z[i+1]) 28075897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 28175897234Sdrh ) 28275897234Sdrh ){ 28375897234Sdrh i += 2; 28432eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 28575897234Sdrh *tokenType = TK_FLOAT; 28675897234Sdrh } 287b7f9164eSdrh #endif 28875897234Sdrh return i; 28975897234Sdrh } 2902f4392ffSdrh case '[': { 291aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 2922f4392ffSdrh *tokenType = TK_ID; 2932f4392ffSdrh return i; 2942f4392ffSdrh } 2957c972decSdrh case '?': { 29650457896Sdrh *tokenType = TK_VARIABLE; 297fa6bc000Sdrh for(i=1; isdigit(z[i]); i++){} 298fa6bc000Sdrh return i; 29950457896Sdrh } 300288d37f1Sdrh case '#': { 301288d37f1Sdrh for(i=1; isdigit(z[i]); i++){} 302288d37f1Sdrh if( i>1 ){ 303288d37f1Sdrh /* Parameters of the form #NNN (where NNN is a number) are used 304288d37f1Sdrh ** internally by sqlite3NestedParse. */ 305288d37f1Sdrh *tokenType = TK_REGISTER; 306895d7472Sdrh return i; 307895d7472Sdrh } 308288d37f1Sdrh /* Fall through into the next case if the '#' is not followed by 309288d37f1Sdrh ** a digit. Try to match #AAAA where AAAA is a parameter name. */ 310288d37f1Sdrh } 311b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 312288d37f1Sdrh case '$': 313288d37f1Sdrh #endif 3140b2a5eeaSdrh case '@': /* For compatibility with MS SQL Server */ 315288d37f1Sdrh case ':': { 316895d7472Sdrh int n = 0; 317288d37f1Sdrh *tokenType = TK_VARIABLE; 318895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 319288d37f1Sdrh if( IdChar(c) ){ 320895d7472Sdrh n++; 321288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 322895d7472Sdrh }else if( c=='(' && n>0 ){ 323895d7472Sdrh do{ 324895d7472Sdrh i++; 325895d7472Sdrh }while( (c=z[i])!=0 && !isspace(c) && c!=')' ); 326895d7472Sdrh if( c==')' ){ 327895d7472Sdrh i++; 328895d7472Sdrh }else{ 329895d7472Sdrh *tokenType = TK_ILLEGAL; 330895d7472Sdrh } 331895d7472Sdrh break; 332895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 333895d7472Sdrh i++; 334288d37f1Sdrh #endif 335895d7472Sdrh }else{ 336895d7472Sdrh break; 337895d7472Sdrh } 338895d7472Sdrh } 3399d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 340895d7472Sdrh return i; 341895d7472Sdrh } 342b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 343c572ef7fSdanielk1977 case 'x': case 'X': { 344aa756b09Sdrh if( (c=z[1])=='\'' || c=='"' ){ 345aa756b09Sdrh int delim = c; 3463fd0a736Sdanielk1977 *tokenType = TK_BLOB; 347aa756b09Sdrh for(i=2; (c=z[i])!=0; i++){ 348aa756b09Sdrh if( c==delim ){ 3493fd0a736Sdanielk1977 if( i%2 ) *tokenType = TK_ILLEGAL; 350c572ef7fSdanielk1977 break; 351c572ef7fSdanielk1977 } 352aa756b09Sdrh if( !isxdigit(c) ){ 3533fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 3543fd0a736Sdanielk1977 return i; 3553fd0a736Sdanielk1977 } 356c572ef7fSdanielk1977 } 357aa756b09Sdrh if( c ) i++; 358c572ef7fSdanielk1977 return i; 359c572ef7fSdanielk1977 } 360c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 361c572ef7fSdanielk1977 } 362b7f9164eSdrh #endif 36398808babSdrh default: { 364aa756b09Sdrh 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 } 375c60e9b82Sdanielk1977 int sqlite3GetToken(const unsigned char *z, int *tokenType){ 376c60e9b82Sdanielk1977 return getToken(z, tokenType); 377c60e9b82Sdanielk1977 } 37875897234Sdrh 37975897234Sdrh /* 38075897234Sdrh ** Run the parser on the given SQL string. The parser structure is 381b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 382b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into 383b19a2bc6Sdrh ** memory obtained from malloc() and *pzErrMsg made to point to that 384b19a2bc6Sdrh ** error message. Or maybe not. 38575897234Sdrh */ 3864adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 38775897234Sdrh int nErr = 0; 38875897234Sdrh int i; 38975897234Sdrh void *pEngine; 390b86ccfb2Sdrh int tokenType; 391b86ccfb2Sdrh int lastTokenParsed = -1; 3929bb575fdSdrh sqlite3 *db = pParse->db; 3934adee20fSdanielk1977 extern void *sqlite3ParserAlloc(void*(*)(int)); 3944adee20fSdanielk1977 extern void sqlite3ParserFree(void*, void(*)(void*)); 3954adee20fSdanielk1977 extern int sqlite3Parser(void*, int, Token, Parse*); 39675897234Sdrh 397*881feaa0Sdrh db->u1.isInterrupted = 0; 3984c504391Sdrh pParse->rc = SQLITE_OK; 39975897234Sdrh i = 0; 400132d8d6aSdrh pEngine = sqlite3ParserAlloc((void*(*)(int))sqlite3MallocX); 40175897234Sdrh if( pEngine==0 ){ 402defc9972Sdrh return SQLITE_NOMEM; 40375897234Sdrh } 404fa6bc000Sdrh assert( pParse->sLastToken.dyn==0 ); 405fa6bc000Sdrh assert( pParse->pNewTable==0 ); 406fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 407fa6bc000Sdrh assert( pParse->nVar==0 ); 408fa6bc000Sdrh assert( pParse->nVarExpr==0 ); 409fa6bc000Sdrh assert( pParse->nVarExprAlloc==0 ); 410fa6bc000Sdrh assert( pParse->apVarExpr==0 ); 4113f7d4e49Sdrh pParse->zTail = pParse->zSql = zSql; 4129e12800dSdanielk1977 while( !sqlite3MallocFailed() && zSql[i]!=0 ){ 41332eb7b47Sdrh assert( i>=0 ); 4142646da7eSdrh pParse->sLastToken.z = (u8*)&zSql[i]; 41532eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 416c60e9b82Sdanielk1977 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType); 41775897234Sdrh i += pParse->sLastToken.n; 41875897234Sdrh switch( tokenType ){ 41975897234Sdrh case TK_SPACE: 42075897234Sdrh case TK_COMMENT: { 421*881feaa0Sdrh if( db->u1.isInterrupted ){ 42232eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 4234adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "interrupt", (char*)0); 42432eb7b47Sdrh goto abort_parse; 42532eb7b47Sdrh } 42675897234Sdrh break; 42775897234Sdrh } 42832eb7b47Sdrh case TK_ILLEGAL: { 429ae29ffbeSdrh if( pzErrMsg ){ 430ae29ffbeSdrh sqliteFree(*pzErrMsg); 431ae29ffbeSdrh *pzErrMsg = sqlite3MPrintf("unrecognized token: \"%T\"", 432ae29ffbeSdrh &pParse->sLastToken); 433ae29ffbeSdrh } 43475897234Sdrh nErr++; 435caec2f12Sdrh goto abort_parse; 43632eb7b47Sdrh } 437326dce74Sdrh case TK_SEMI: { 438326dce74Sdrh pParse->zTail = &zSql[i]; 439326dce74Sdrh /* Fall thru into the default case */ 440326dce74Sdrh } 44132eb7b47Sdrh default: { 4424adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 443b86ccfb2Sdrh lastTokenParsed = tokenType; 444b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 44532eb7b47Sdrh goto abort_parse; 44675897234Sdrh } 44775897234Sdrh break; 44875897234Sdrh } 44975897234Sdrh } 45032eb7b47Sdrh } 45132eb7b47Sdrh abort_parse: 452b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 453b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 4544adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 455326dce74Sdrh pParse->zTail = &zSql[i]; 45675897234Sdrh } 4574adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 45875897234Sdrh } 459132d8d6aSdrh sqlite3ParserFree(pEngine, sqlite3FreeX); 4609e12800dSdanielk1977 if( sqlite3MallocFailed() ){ 46171c697efSdrh pParse->rc = SQLITE_NOMEM; 46271c697efSdrh } 463b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 464261919ccSdanielk1977 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0); 465b86ccfb2Sdrh } 46675897234Sdrh if( pParse->zErrMsg ){ 467b86ccfb2Sdrh if( pzErrMsg && *pzErrMsg==0 ){ 46875897234Sdrh *pzErrMsg = pParse->zErrMsg; 46975897234Sdrh }else{ 47075897234Sdrh sqliteFree(pParse->zErrMsg); 47175897234Sdrh } 472b86ccfb2Sdrh pParse->zErrMsg = 0; 47375897234Sdrh if( !nErr ) nErr++; 47475897234Sdrh } 4752958a4e6Sdrh if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 4764adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 47775897234Sdrh pParse->pVdbe = 0; 47875897234Sdrh } 479c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE 480c00da105Sdanielk1977 if( pParse->nested==0 ){ 481c00da105Sdanielk1977 sqliteFree(pParse->aTableLock); 482c00da105Sdanielk1977 pParse->aTableLock = 0; 483c00da105Sdanielk1977 pParse->nTableLock = 0; 484c00da105Sdanielk1977 } 485c00da105Sdanielk1977 #endif 4867e6ebfb2Sdanielk1977 4877e6ebfb2Sdanielk1977 if( !IN_DECLARE_VTAB ){ 4887e6ebfb2Sdanielk1977 /* If the pParse->declareVtab flag is set, do not delete any table 4897e6ebfb2Sdanielk1977 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 4907e6ebfb2Sdanielk1977 ** will take responsibility for freeing the Table structure. 4917e6ebfb2Sdanielk1977 */ 4924adee20fSdanielk1977 sqlite3DeleteTable(pParse->db, pParse->pNewTable); 4937e6ebfb2Sdanielk1977 } 4947e6ebfb2Sdanielk1977 4954adee20fSdanielk1977 sqlite3DeleteTrigger(pParse->pNewTrigger); 496fa6bc000Sdrh sqliteFree(pParse->apVarExpr); 497b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 4984c504391Sdrh pParse->rc = SQLITE_ERROR; 4994c504391Sdrh } 50075897234Sdrh return nErr; 50175897234Sdrh } 502