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*fb45d8c5Sdrh ** $Id: tokenize.c,v 1.145 2008/07/08 00:06:50 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++){} 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++){} 2954b2f9368Sdrh *tokenType = c==']' ? TK_ID : TK_ILLEGAL; 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': { 3474b2f9368Sdrh if( z[1]=='\'' ){ 3483fd0a736Sdanielk1977 *tokenType = TK_BLOB; 3494b2f9368Sdrh for(i=2; (c=z[i])!=0 && c!='\''; i++){ 350aa756b09Sdrh if( !isxdigit(c) ){ 3513fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 3523fd0a736Sdanielk1977 } 353c572ef7fSdanielk1977 } 3544b2f9368Sdrh if( i%2 || !c ) *tokenType = TK_ILLEGAL; 355aa756b09Sdrh if( c ) i++; 356c572ef7fSdanielk1977 return i; 357c572ef7fSdanielk1977 } 358c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 359c572ef7fSdanielk1977 } 360b7f9164eSdrh #endif 36198808babSdrh default: { 3629a087a99Sdrh if( !IdChar(*z) ){ 36398808babSdrh break; 36498808babSdrh } 365aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 366c60e9b82Sdanielk1977 *tokenType = keywordCode((char*)z, i); 36775897234Sdrh return i; 36875897234Sdrh } 36975897234Sdrh } 37075897234Sdrh *tokenType = TK_ILLEGAL; 37175897234Sdrh return 1; 37275897234Sdrh } 37375897234Sdrh 37475897234Sdrh /* 37575897234Sdrh ** Run the parser on the given SQL string. The parser structure is 376b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 377*fb45d8c5Sdrh ** then an and attempt is made to write an error message into 378*fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that 379*fb45d8c5Sdrh ** error message. 38075897234Sdrh */ 3814adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 38275897234Sdrh int nErr = 0; 38375897234Sdrh int i; 38475897234Sdrh void *pEngine; 385b86ccfb2Sdrh int tokenType; 386b86ccfb2Sdrh int lastTokenParsed = -1; 3879bb575fdSdrh sqlite3 *db = pParse->db; 388bb4957f8Sdrh int mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 38975897234Sdrh 39015ca1df1Sdrh if( db->activeVdbeCnt==0 ){ 391881feaa0Sdrh db->u1.isInterrupted = 0; 39215ca1df1Sdrh } 3934c504391Sdrh pParse->rc = SQLITE_OK; 3944b1d66c1Sdrh pParse->zTail = pParse->zSql = zSql; 39575897234Sdrh i = 0; 396*fb45d8c5Sdrh assert( pzErrMsg!=0 ); 397e5ae5735Sdrh pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc); 39875897234Sdrh if( pEngine==0 ){ 399f3a65f7eSdrh db->mallocFailed = 1; 400defc9972Sdrh return SQLITE_NOMEM; 40175897234Sdrh } 402fa6bc000Sdrh assert( pParse->sLastToken.dyn==0 ); 403fa6bc000Sdrh assert( pParse->pNewTable==0 ); 404fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 405fa6bc000Sdrh assert( pParse->nVar==0 ); 406fa6bc000Sdrh assert( pParse->nVarExpr==0 ); 407fa6bc000Sdrh assert( pParse->nVarExprAlloc==0 ); 408fa6bc000Sdrh assert( pParse->apVarExpr==0 ); 40917435752Sdrh while( !db->mallocFailed && zSql[i]!=0 ){ 41032eb7b47Sdrh assert( i>=0 ); 4112646da7eSdrh pParse->sLastToken.z = (u8*)&zSql[i]; 41232eb7b47Sdrh assert( pParse->sLastToken.dyn==0 ); 41335b5a33eSdrh pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); 41475897234Sdrh i += pParse->sLastToken.n; 415bb4957f8Sdrh if( i>mxSqlLen ){ 416e5c941b8Sdrh pParse->rc = SQLITE_TOOBIG; 417e5c941b8Sdrh break; 418e5c941b8Sdrh } 41975897234Sdrh switch( tokenType ){ 42075897234Sdrh case TK_SPACE: 42175897234Sdrh case TK_COMMENT: { 422881feaa0Sdrh if( db->u1.isInterrupted ){ 42332eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 4244adee20fSdanielk1977 sqlite3SetString(pzErrMsg, "interrupt", (char*)0); 42532eb7b47Sdrh goto abort_parse; 42632eb7b47Sdrh } 42775897234Sdrh break; 42875897234Sdrh } 42932eb7b47Sdrh case TK_ILLEGAL: { 43017435752Sdrh sqlite3_free(*pzErrMsg); 4311e536953Sdanielk1977 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"", 432ae29ffbeSdrh &pParse->sLastToken); 43375897234Sdrh nErr++; 434caec2f12Sdrh goto abort_parse; 43532eb7b47Sdrh } 436326dce74Sdrh case TK_SEMI: { 437326dce74Sdrh pParse->zTail = &zSql[i]; 438326dce74Sdrh /* Fall thru into the default case */ 439326dce74Sdrh } 44032eb7b47Sdrh default: { 4414adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 442b86ccfb2Sdrh lastTokenParsed = tokenType; 443b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 44432eb7b47Sdrh goto abort_parse; 44575897234Sdrh } 44675897234Sdrh break; 44775897234Sdrh } 44875897234Sdrh } 44932eb7b47Sdrh } 45032eb7b47Sdrh abort_parse: 451b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 452b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 4534adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 454326dce74Sdrh pParse->zTail = &zSql[i]; 45575897234Sdrh } 4564adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 45775897234Sdrh } 45817435752Sdrh sqlite3ParserFree(pEngine, sqlite3_free); 45917435752Sdrh if( db->mallocFailed ){ 46071c697efSdrh pParse->rc = SQLITE_NOMEM; 46171c697efSdrh } 462b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 463261919ccSdanielk1977 sqlite3SetString(&pParse->zErrMsg, sqlite3ErrStr(pParse->rc), (char*)0); 464b86ccfb2Sdrh } 46575897234Sdrh if( pParse->zErrMsg ){ 466*fb45d8c5Sdrh if( *pzErrMsg==0 ){ 46775897234Sdrh *pzErrMsg = pParse->zErrMsg; 46875897234Sdrh }else{ 46917435752Sdrh sqlite3_free(pParse->zErrMsg); 47075897234Sdrh } 471b86ccfb2Sdrh pParse->zErrMsg = 0; 4724b2f9368Sdrh nErr++; 47375897234Sdrh } 4742958a4e6Sdrh if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 4754adee20fSdanielk1977 sqlite3VdbeDelete(pParse->pVdbe); 47675897234Sdrh pParse->pVdbe = 0; 47775897234Sdrh } 478c00da105Sdanielk1977 #ifndef SQLITE_OMIT_SHARED_CACHE 479c00da105Sdanielk1977 if( pParse->nested==0 ){ 48017435752Sdrh sqlite3_free(pParse->aTableLock); 481c00da105Sdanielk1977 pParse->aTableLock = 0; 482c00da105Sdanielk1977 pParse->nTableLock = 0; 483c00da105Sdanielk1977 } 484c00da105Sdanielk1977 #endif 4854f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 4864f3dd150Sdrh sqlite3_free(pParse->apVtabLock); 4874f3dd150Sdrh #endif 4887e6ebfb2Sdanielk1977 4897e6ebfb2Sdanielk1977 if( !IN_DECLARE_VTAB ){ 4907e6ebfb2Sdanielk1977 /* If the pParse->declareVtab flag is set, do not delete any table 4917e6ebfb2Sdanielk1977 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 4927e6ebfb2Sdanielk1977 ** will take responsibility for freeing the Table structure. 4937e6ebfb2Sdanielk1977 */ 494a04a34ffSdanielk1977 sqlite3DeleteTable(pParse->pNewTable); 4957e6ebfb2Sdanielk1977 } 4967e6ebfb2Sdanielk1977 4974adee20fSdanielk1977 sqlite3DeleteTrigger(pParse->pNewTrigger); 49817435752Sdrh sqlite3_free(pParse->apVarExpr); 499b86ccfb2Sdrh if( nErr>0 && (pParse->rc==SQLITE_OK || pParse->rc==SQLITE_DONE) ){ 5004c504391Sdrh pParse->rc = SQLITE_ERROR; 5014c504391Sdrh } 50275897234Sdrh return nErr; 50375897234Sdrh } 504