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*f3a65f7eSdrh ** $Id: tokenize.c,v 1.135 2007/08/22 20:18:22 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 88a2b902d6Sdrh const char sqlite3IsIdChar[] = { 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 }; 97a2b902d6Sdrh #define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && sqlite3IsIdChar[c-0x20])) 989b8f447bSdrh #endif 999b8f447bSdrh #ifdef SQLITE_EBCDIC 1009b8f447bSdrh const char sqlite3IsIdChar[] = { 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 }; 1159b8f447bSdrh #define IdChar(C) (((c=C)>=0x42 && sqlite3IsIdChar[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 */ 123c60e9b82Sdanielk1977 static int getToken(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++){} 13475897234Sdrh *tokenType = TK_COMMENT; 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++; 16766105a8eSdrh *tokenType = TK_COMMENT; 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 } 248eef8b558Sdrh if( c ){ 24975897234Sdrh *tokenType = TK_STRING; 250eef8b558Sdrh return i+1; 251eef8b558Sdrh }else{ 252eef8b558Sdrh *tokenType = TK_ILLEGAL; 25375897234Sdrh return i; 25475897234Sdrh } 255eef8b558Sdrh } 25675897234Sdrh case '.': { 2577681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2587681618cSdrh if( !isdigit(z[1]) ) 2597681618cSdrh #endif 2607681618cSdrh { 26175897234Sdrh *tokenType = TK_DOT; 26275897234Sdrh return 1; 26375897234Sdrh } 2647681618cSdrh /* If the next character is a digit, this is a floating point 2657681618cSdrh ** number that begins with ".". Fall thru into the next case */ 2667681618cSdrh } 26775897234Sdrh case '0': case '1': case '2': case '3': case '4': 26875897234Sdrh case '5': case '6': case '7': case '8': case '9': { 269c837e709Sdrh *tokenType = TK_INTEGER; 2707681618cSdrh for(i=0; isdigit(z[i]); i++){} 271b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2727681618cSdrh if( z[i]=='.' ){ 2737681618cSdrh i++; 27432eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 275c837e709Sdrh *tokenType = TK_FLOAT; 276c837e709Sdrh } 27775897234Sdrh if( (z[i]=='e' || z[i]=='E') && 27875897234Sdrh ( isdigit(z[i+1]) 27975897234Sdrh || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2])) 28075897234Sdrh ) 28175897234Sdrh ){ 28275897234Sdrh i += 2; 28332eb7b47Sdrh while( isdigit(z[i]) ){ i++; } 28475897234Sdrh *tokenType = TK_FLOAT; 28575897234Sdrh } 286b7f9164eSdrh #endif 28767dd9011Sdrh while( IdChar(z[i]) ){ 28867dd9011Sdrh *tokenType = TK_ILLEGAL; 28967dd9011Sdrh i++; 29067dd9011Sdrh } 29175897234Sdrh return i; 29275897234Sdrh } 2932f4392ffSdrh case '[': { 294aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 2952f4392ffSdrh *tokenType = TK_ID; 2962f4392ffSdrh return i; 2972f4392ffSdrh } 2987c972decSdrh case '?': { 29950457896Sdrh *tokenType = TK_VARIABLE; 300fa6bc000Sdrh for(i=1; isdigit(z[i]); i++){} 301fa6bc000Sdrh return i; 30250457896Sdrh } 303288d37f1Sdrh case '#': { 304288d37f1Sdrh for(i=1; isdigit(z[i]); i++){} 305288d37f1Sdrh if( i>1 ){ 306288d37f1Sdrh /* Parameters of the form #NNN (where NNN is a number) are used 307288d37f1Sdrh ** internally by sqlite3NestedParse. */ 308288d37f1Sdrh *tokenType = TK_REGISTER; 309895d7472Sdrh return i; 310895d7472Sdrh } 311288d37f1Sdrh /* Fall through into the next case if the '#' is not followed by 312288d37f1Sdrh ** a digit. Try to match #AAAA where AAAA is a parameter name. */ 313288d37f1Sdrh } 314b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 315288d37f1Sdrh case '$': 316288d37f1Sdrh #endif 3170b2a5eeaSdrh case '@': /* For compatibility with MS SQL Server */ 318288d37f1Sdrh case ':': { 319895d7472Sdrh int n = 0; 320288d37f1Sdrh *tokenType = TK_VARIABLE; 321895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 322288d37f1Sdrh if( IdChar(c) ){ 323895d7472Sdrh n++; 324288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 325895d7472Sdrh }else if( c=='(' && n>0 ){ 326895d7472Sdrh do{ 327895d7472Sdrh i++; 328895d7472Sdrh }while( (c=z[i])!=0 && !isspace(c) && c!=')' ); 329895d7472Sdrh if( c==')' ){ 330895d7472Sdrh i++; 331895d7472Sdrh }else{ 332895d7472Sdrh *tokenType = TK_ILLEGAL; 333895d7472Sdrh } 334895d7472Sdrh break; 335895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 336895d7472Sdrh i++; 337288d37f1Sdrh #endif 338895d7472Sdrh }else{ 339895d7472Sdrh break; 340895d7472Sdrh } 341895d7472Sdrh } 3429d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 343895d7472Sdrh return i; 344895d7472Sdrh } 345b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 346c572ef7fSdanielk1977 case 'x': case 'X': { 347aa756b09Sdrh if( (c=z[1])=='\'' || c=='"' ){ 348aa756b09Sdrh int delim = c; 3493fd0a736Sdanielk1977 *tokenType = TK_BLOB; 350aa756b09Sdrh for(i=2; (c=z[i])!=0; i++){ 351aa756b09Sdrh if( c==delim ){ 3523fd0a736Sdanielk1977 if( i%2 ) *tokenType = TK_ILLEGAL; 353c572ef7fSdanielk1977 break; 354c572ef7fSdanielk1977 } 355aa756b09Sdrh if( !isxdigit(c) ){ 3563fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 3573fd0a736Sdanielk1977 return i; 3583fd0a736Sdanielk1977 } 359c572ef7fSdanielk1977 } 360aa756b09Sdrh if( c ) i++; 361c572ef7fSdanielk1977 return i; 362c572ef7fSdanielk1977 } 363c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 364c572ef7fSdanielk1977 } 365b7f9164eSdrh #endif 36698808babSdrh default: { 3679a087a99Sdrh if( !IdChar(*z) ){ 36898808babSdrh break; 36998808babSdrh } 370aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 371c60e9b82Sdanielk1977 *tokenType = keywordCode((char*)z, i); 37275897234Sdrh return i; 37375897234Sdrh } 37475897234Sdrh } 37575897234Sdrh *tokenType = TK_ILLEGAL; 37675897234Sdrh return 1; 37775897234Sdrh } 378c60e9b82Sdanielk1977 int sqlite3GetToken(const unsigned char *z, int *tokenType){ 379c60e9b82Sdanielk1977 return getToken(z, tokenType); 380c60e9b82Sdanielk1977 } 38175897234Sdrh 38275897234Sdrh /* 38375897234Sdrh ** Run the parser on the given SQL string. The parser structure is 384b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 385b19a2bc6Sdrh ** and pzErrMsg!=NULL then an error message might be written into 38617435752Sdrh ** memory obtained from sqlite3_malloc() and *pzErrMsg made to point to that 387b19a2bc6Sdrh ** error message. Or maybe not. 38875897234Sdrh */ 3894adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 39075897234Sdrh int nErr = 0; 39175897234Sdrh int i; 39275897234Sdrh void *pEngine; 393b86ccfb2Sdrh int tokenType; 394b86ccfb2Sdrh int lastTokenParsed = -1; 3959bb575fdSdrh sqlite3 *db = pParse->db; 39675897234Sdrh 39715ca1df1Sdrh if( db->activeVdbeCnt==0 ){ 398881feaa0Sdrh db->u1.isInterrupted = 0; 39915ca1df1Sdrh } 4004c504391Sdrh pParse->rc = SQLITE_OK; 40175897234Sdrh i = 0; 40217435752Sdrh pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3_malloc); 40375897234Sdrh if( pEngine==0 ){ 404*f3a65f7eSdrh db->mallocFailed = 1; 405defc9972Sdrh return SQLITE_NOMEM; 40675897234Sdrh } 407fa6bc000Sdrh assert( pParse->sLastToken.dyn==0 ); 408fa6bc000Sdrh assert( pParse->pNewTable==0 ); 409fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 410fa6bc000Sdrh assert( pParse->nVar==0 ); 411fa6bc000Sdrh assert( pParse->nVarExpr==0 ); 412fa6bc000Sdrh assert( pParse->nVarExprAlloc==0 ); 413fa6bc000Sdrh assert( pParse->apVarExpr==0 ); 4143f7d4e49Sdrh pParse->zTail = pParse->zSql = zSql; 41517435752Sdrh while( !db->mallocFailed && zSql[i]!=0 ){ 41632eb7b47Sdrh assert( i>=0 ); 4172646da7eSdrh pParse->sLastToken.z = (u8*)&zSql[i]; 41832eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 419c60e9b82Sdanielk1977 pParse->sLastToken.n = getToken((unsigned char*)&zSql[i],&tokenType); 42075897234Sdrh i += pParse->sLastToken.n; 421e5c941b8Sdrh if( i>SQLITE_MAX_SQL_LENGTH ){ 422e5c941b8Sdrh pParse->rc = SQLITE_TOOBIG; 423e5c941b8Sdrh break; 424e5c941b8Sdrh } 42575897234Sdrh switch( tokenType ){ 42675897234Sdrh case TK_SPACE: 42775897234Sdrh case TK_COMMENT: { 428881feaa0Sdrh if( db->u1.isInterrupted ){ 42932eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 4304adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "interrupt", (char*)0); 43132eb7b47Sdrh goto abort_parse; 43232eb7b47Sdrh } 43375897234Sdrh break; 43475897234Sdrh } 43532eb7b47Sdrh case TK_ILLEGAL: { 436ae29ffbeSdrh if( pzErrMsg ){ 43717435752Sdrh sqlite3_free(*pzErrMsg); 4381e536953Sdanielk1977 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"", 439ae29ffbeSdrh &pParse->sLastToken); 440ae29ffbeSdrh } 44175897234Sdrh nErr++; 442caec2f12Sdrh goto abort_parse; 44332eb7b47Sdrh } 444326dce74Sdrh case TK_SEMI: { 445326dce74Sdrh pParse->zTail = &zSql[i]; 446326dce74Sdrh /* Fall thru into the default case */ 447326dce74Sdrh } 44832eb7b47Sdrh default: { 4494adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 450b86ccfb2Sdrh lastTokenParsed = tokenType; 451b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 45232eb7b47Sdrh goto abort_parse; 45375897234Sdrh } 45475897234Sdrh break; 45575897234Sdrh } 45675897234Sdrh } 45732eb7b47Sdrh } 45832eb7b47Sdrh abort_parse: 459b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 460b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 4614adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 462326dce74Sdrh pParse->zTail = &zSql[i]; 46375897234Sdrh } 4644adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 46575897234Sdrh } 46617435752Sdrh sqlite3ParserFree(pEngine, sqlite3_free); 46717435752Sdrh if( db->mallocFailed ){ 46871c697efSdrh pParse->rc = SQLITE_NOMEM; 46971c697efSdrh } 470b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 471261919ccSdanielk1977 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0); 472b86ccfb2Sdrh } 47375897234Sdrh if( pParse->zErrMsg ){ 474b86ccfb2Sdrh if( pzErrMsg && *pzErrMsg==0 ){ 47575897234Sdrh *pzErrMsg = pParse->zErrMsg; 47675897234Sdrh }else{ 47717435752Sdrh sqlite3_free(pParse->zErrMsg); 47875897234Sdrh } 479b86ccfb2Sdrh pParse->zErrMsg = 0; 48075897234Sdrh if( !nErr ) nErr++; 48175897234Sdrh } 4822958a4e6Sdrh if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 4834adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 48475897234Sdrh pParse->pVdbe = 0; 48575897234Sdrh } 486c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE 487c00da105Sdanielk1977 if( pParse->nested==0 ){ 48817435752Sdrh sqlite3_free(pParse->aTableLock); 489c00da105Sdanielk1977 pParse->aTableLock = 0; 490c00da105Sdanielk1977 pParse->nTableLock = 0; 491c00da105Sdanielk1977 } 492c00da105Sdanielk1977 #endif 4937e6ebfb2Sdanielk1977 4947e6ebfb2Sdanielk1977 if( !IN_DECLARE_VTAB ){ 4957e6ebfb2Sdanielk1977 /* If the pParse->declareVtab flag is set, do not delete any table 4967e6ebfb2Sdanielk1977 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 4977e6ebfb2Sdanielk1977 ** will take responsibility for freeing the Table structure. 4987e6ebfb2Sdanielk1977 */ 499a04a34ffSdanielk1977 sqlite3DeleteTable(pParse->pNewTable); 5007e6ebfb2Sdanielk1977 } 5017e6ebfb2Sdanielk1977 5024adee20fSdanielk1977 sqlite3DeleteTrigger(pParse->pNewTrigger); 50317435752Sdrh sqlite3_free(pParse->apVarExpr); 504b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 5054c504391Sdrh pParse->rc = SQLITE_ERROR; 5064c504391Sdrh } 50775897234Sdrh return nErr; 50875897234Sdrh } 509