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 */ 1875897234Sdrh #include "sqliteInt.h" 19dcc581ccSdrh #include <stdlib.h> 2075897234Sdrh 2175897234Sdrh /* 229b8f447bSdrh ** The charMap() macro maps alphabetic characters into their 239b8f447bSdrh ** lower-case ASCII equivalent. On ASCII machines, this is just 249b8f447bSdrh ** an upper-to-lower case map. On EBCDIC machines we also need 259b8f447bSdrh ** to adjust the encoding. Only alphabetic characters and underscores 269b8f447bSdrh ** need to be translated. 279b8f447bSdrh */ 289b8f447bSdrh #ifdef SQLITE_ASCII 299b8f447bSdrh # define charMap(X) sqlite3UpperToLower[(unsigned char)X] 309b8f447bSdrh #endif 319b8f447bSdrh #ifdef SQLITE_EBCDIC 329b8f447bSdrh # define charMap(X) ebcdicToAscii[(unsigned char)X] 339b8f447bSdrh const unsigned char ebcdicToAscii[] = { 349b8f447bSdrh /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 359b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 369b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 379b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 389b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ 399b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ 409b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ 419b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ 429b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 439b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ 449b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ 459b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ 469b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 479b8f447bSdrh 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ 489b8f447bSdrh 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ 499b8f447bSdrh 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ 509b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ 519b8f447bSdrh }; 529b8f447bSdrh #endif 539b8f447bSdrh 549b8f447bSdrh /* 5552fb6d71Sdrh ** The sqlite3KeywordCode function looks up an identifier to determine if 5652fb6d71Sdrh ** it is a keyword. If it is a keyword, the token code of that keyword is 5775897234Sdrh ** returned. If the input is not a keyword, TK_ID is returned. 582090a0edSdrh ** 592090a0edSdrh ** The implementation of this routine was generated by a program, 6073b211abSdrh ** mkkeywordhash.h, located in the tool subdirectory of the distribution. 6152fb6d71Sdrh ** The output of the mkkeywordhash.c program is written into a file 6273b211abSdrh ** named keywordhash.h and then included into this source file by 6352fb6d71Sdrh ** the #include below. 6475897234Sdrh */ 6573b211abSdrh #include "keywordhash.h" 6675897234Sdrh 6740f20f7dSdrh 6898808babSdrh /* 699b8f447bSdrh ** If X is a character that can be used in an identifier then 709b8f447bSdrh ** IdChar(X) will be true. Otherwise it is false. 7198808babSdrh ** 729b8f447bSdrh ** For ASCII, any character with the high-order bit set is 739b8f447bSdrh ** allowed in an identifier. For 7-bit characters, 749b8f447bSdrh ** sqlite3IsIdChar[X] must be 1. 759b8f447bSdrh ** 769b8f447bSdrh ** For EBCDIC, the rules are more complex but have the same 779b8f447bSdrh ** end result. 78a0d1f663Sdrh ** 79a0d1f663Sdrh ** Ticket #1066. the SQL standard does not allow '$' in the 8060ec914cSpeter.d.reid ** middle of identifiers. But many SQL implementations do. 81a0d1f663Sdrh ** SQLite will allow '$' in identifiers for compatibility. 82a0d1f663Sdrh ** But the feature is undocumented. 8398808babSdrh */ 849b8f447bSdrh #ifdef SQLITE_ASCII 85af2b5720Sdrh #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) 869b8f447bSdrh #endif 879b8f447bSdrh #ifdef SQLITE_EBCDIC 8846c99e0fSdrh const char sqlite3IsEbcdicIdChar[] = { 899b8f447bSdrh /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 909b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ 919b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ 929b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ 939b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 949b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ 959b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ 969b8f447bSdrh 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ 979b8f447bSdrh 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 989b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ 999b8f447bSdrh 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ 1009b8f447bSdrh 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ 1019b8f447bSdrh 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ 1029b8f447bSdrh }; 10346c99e0fSdrh #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) 1049b8f447bSdrh #endif 10597348b37Sdrh int sqlite3IsIdChar(u8 c){ return IdChar(c); } 1069b8f447bSdrh 10798808babSdrh 10875897234Sdrh /* 10961b487d0Sdrh ** Return the length of the token that begins at z[0]. 11061b487d0Sdrh ** Store the token type in *tokenType before returning. 11175897234Sdrh */ 11235b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){ 113aa756b09Sdrh int i, c; 11475897234Sdrh switch( *z ){ 11530cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 11619f81f6cSdrh testcase( z[0]==' ' ); 11719f81f6cSdrh testcase( z[0]=='\t' ); 11819f81f6cSdrh testcase( z[0]=='\n' ); 11919f81f6cSdrh testcase( z[0]=='\f' ); 12019f81f6cSdrh testcase( z[0]=='\r' ); 12178ca0e7eSdanielk1977 for(i=1; sqlite3Isspace(z[i]); i++){} 12275897234Sdrh *tokenType = TK_SPACE; 12375897234Sdrh return i; 12475897234Sdrh } 12575897234Sdrh case '-': { 12675897234Sdrh if( z[1]=='-' ){ 127aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 128d3479b94Sdrh *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 12975897234Sdrh return i; 13075897234Sdrh } 13175897234Sdrh *tokenType = TK_MINUS; 13275897234Sdrh return 1; 13375897234Sdrh } 13475897234Sdrh case '(': { 13575897234Sdrh *tokenType = TK_LP; 13675897234Sdrh return 1; 13775897234Sdrh } 13875897234Sdrh case ')': { 13975897234Sdrh *tokenType = TK_RP; 14075897234Sdrh return 1; 14175897234Sdrh } 14275897234Sdrh case ';': { 14375897234Sdrh *tokenType = TK_SEMI; 14475897234Sdrh return 1; 14575897234Sdrh } 14675897234Sdrh case '+': { 14775897234Sdrh *tokenType = TK_PLUS; 14875897234Sdrh return 1; 14975897234Sdrh } 15075897234Sdrh case '*': { 15175897234Sdrh *tokenType = TK_STAR; 15275897234Sdrh return 1; 15375897234Sdrh } 15475897234Sdrh case '/': { 15566105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 15675897234Sdrh *tokenType = TK_SLASH; 15775897234Sdrh return 1; 15875897234Sdrh } 159aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 160aa756b09Sdrh if( c ) i++; 161d3479b94Sdrh *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 16266105a8eSdrh return i; 16366105a8eSdrh } 164bf4133cbSdrh case '%': { 165bf4133cbSdrh *tokenType = TK_REM; 166bf4133cbSdrh return 1; 167bf4133cbSdrh } 16875897234Sdrh case '=': { 16975897234Sdrh *tokenType = TK_EQ; 17075897234Sdrh return 1 + (z[1]=='='); 17175897234Sdrh } 17275897234Sdrh case '<': { 173aa756b09Sdrh if( (c=z[1])=='=' ){ 17475897234Sdrh *tokenType = TK_LE; 17575897234Sdrh return 2; 176aa756b09Sdrh }else if( c=='>' ){ 17775897234Sdrh *tokenType = TK_NE; 17875897234Sdrh return 2; 179aa756b09Sdrh }else if( c=='<' ){ 180bf4133cbSdrh *tokenType = TK_LSHIFT; 181bf4133cbSdrh return 2; 18275897234Sdrh }else{ 18375897234Sdrh *tokenType = TK_LT; 18475897234Sdrh return 1; 18575897234Sdrh } 18675897234Sdrh } 18775897234Sdrh case '>': { 188aa756b09Sdrh if( (c=z[1])=='=' ){ 18975897234Sdrh *tokenType = TK_GE; 19075897234Sdrh return 2; 191aa756b09Sdrh }else if( c=='>' ){ 192bf4133cbSdrh *tokenType = TK_RSHIFT; 193bf4133cbSdrh return 2; 19475897234Sdrh }else{ 19575897234Sdrh *tokenType = TK_GT; 19675897234Sdrh return 1; 19775897234Sdrh } 19875897234Sdrh } 19975897234Sdrh case '!': { 20075897234Sdrh if( z[1]!='=' ){ 20175897234Sdrh *tokenType = TK_ILLEGAL; 202c837e709Sdrh return 2; 20375897234Sdrh }else{ 20475897234Sdrh *tokenType = TK_NE; 20575897234Sdrh return 2; 20675897234Sdrh } 20775897234Sdrh } 2080040077dSdrh case '|': { 2090040077dSdrh if( z[1]!='|' ){ 210bf4133cbSdrh *tokenType = TK_BITOR; 2110040077dSdrh return 1; 2120040077dSdrh }else{ 2130040077dSdrh *tokenType = TK_CONCAT; 2140040077dSdrh return 2; 2150040077dSdrh } 2160040077dSdrh } 21775897234Sdrh case ',': { 21875897234Sdrh *tokenType = TK_COMMA; 21975897234Sdrh return 1; 22075897234Sdrh } 221bf4133cbSdrh case '&': { 222bf4133cbSdrh *tokenType = TK_BITAND; 223bf4133cbSdrh return 1; 224bf4133cbSdrh } 225bf4133cbSdrh case '~': { 226bf4133cbSdrh *tokenType = TK_BITNOT; 227bf4133cbSdrh return 1; 228bf4133cbSdrh } 2293d94662aSdrh case '`': 2303d94662aSdrh case '\'': 2313d94662aSdrh case '"': { 23275897234Sdrh int delim = z[0]; 23319f81f6cSdrh testcase( delim=='`' ); 23419f81f6cSdrh testcase( delim=='\'' ); 23519f81f6cSdrh testcase( delim=='"' ); 236aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 237aa756b09Sdrh if( c==delim ){ 23875897234Sdrh if( z[i+1]==delim ){ 23975897234Sdrh i++; 24075897234Sdrh }else{ 24175897234Sdrh break; 24275897234Sdrh } 24375897234Sdrh } 24475897234Sdrh } 245a33cb5f7Sdrh if( c=='\'' ){ 24675897234Sdrh *tokenType = TK_STRING; 247eef8b558Sdrh return i+1; 248a33cb5f7Sdrh }else if( c!=0 ){ 249a33cb5f7Sdrh *tokenType = TK_ID; 250a33cb5f7Sdrh return i+1; 251eef8b558Sdrh }else{ 252eef8b558Sdrh *tokenType = TK_ILLEGAL; 25375897234Sdrh return i; 25475897234Sdrh } 255eef8b558Sdrh } 25675897234Sdrh case '.': { 2577681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 25878ca0e7eSdanielk1977 if( !sqlite3Isdigit(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': { 26919f81f6cSdrh testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); 27019f81f6cSdrh testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); 27119f81f6cSdrh testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); 27219f81f6cSdrh testcase( z[0]=='9' ); 273c837e709Sdrh *tokenType = TK_INTEGER; 27428e048c6Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 27528e048c6Sdrh if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ 27628e048c6Sdrh for(i=3; sqlite3Isxdigit(z[i]); i++){} 27728e048c6Sdrh return i; 27828e048c6Sdrh } 27928e048c6Sdrh #endif 28078ca0e7eSdanielk1977 for(i=0; sqlite3Isdigit(z[i]); i++){} 281b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2827681618cSdrh if( z[i]=='.' ){ 2837681618cSdrh i++; 28478ca0e7eSdanielk1977 while( sqlite3Isdigit(z[i]) ){ i++; } 285c837e709Sdrh *tokenType = TK_FLOAT; 286c837e709Sdrh } 28775897234Sdrh if( (z[i]=='e' || z[i]=='E') && 28878ca0e7eSdanielk1977 ( sqlite3Isdigit(z[i+1]) 28978ca0e7eSdanielk1977 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) 29075897234Sdrh ) 29175897234Sdrh ){ 29275897234Sdrh i += 2; 29378ca0e7eSdanielk1977 while( sqlite3Isdigit(z[i]) ){ i++; } 29475897234Sdrh *tokenType = TK_FLOAT; 29575897234Sdrh } 296b7f9164eSdrh #endif 29767dd9011Sdrh while( IdChar(z[i]) ){ 29867dd9011Sdrh *tokenType = TK_ILLEGAL; 29967dd9011Sdrh i++; 30067dd9011Sdrh } 30175897234Sdrh return i; 30275897234Sdrh } 3032f4392ffSdrh case '[': { 304aa756b09Sdrh for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 3054b2f9368Sdrh *tokenType = c==']' ? TK_ID : TK_ILLEGAL; 3062f4392ffSdrh return i; 3072f4392ffSdrh } 3087c972decSdrh case '?': { 30950457896Sdrh *tokenType = TK_VARIABLE; 31078ca0e7eSdanielk1977 for(i=1; sqlite3Isdigit(z[i]); i++){} 311fa6bc000Sdrh return i; 31250457896Sdrh } 313b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 314288d37f1Sdrh case '$': 315288d37f1Sdrh #endif 3160b2a5eeaSdrh case '@': /* For compatibility with MS SQL Server */ 317f59b12fbSdrh case '#': 318288d37f1Sdrh case ':': { 319895d7472Sdrh int n = 0; 320f59b12fbSdrh testcase( z[0]=='$' ); testcase( z[0]=='@' ); 321f59b12fbSdrh testcase( z[0]==':' ); testcase( z[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': { 34919f81f6cSdrh testcase( z[0]=='x' ); testcase( z[0]=='X' ); 3504b2f9368Sdrh if( z[1]=='\'' ){ 3513fd0a736Sdanielk1977 *tokenType = TK_BLOB; 3523a61a5a2Sdrh for(i=2; sqlite3Isxdigit(z[i]); i++){} 3533a61a5a2Sdrh if( z[i]!='\'' || i%2 ){ 3543fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 3553a61a5a2Sdrh while( z[i] && z[i]!='\'' ){ i++; } 3563fd0a736Sdanielk1977 } 3573a61a5a2Sdrh if( z[i] ) 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 39396c707a3Sdrh assert( zSql!=0 ); 394d9da78a2Sdrh mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 3954f7d3a5fSdrh if( db->nVdbeActive==0 ){ 396881feaa0Sdrh db->u1.isInterrupted = 0; 39715ca1df1Sdrh } 3984c504391Sdrh pParse->rc = SQLITE_OK; 399e7f3f3eeSdrh pParse->zTail = zSql; 40075897234Sdrh i = 0; 401fb45d8c5Sdrh assert( pzErrMsg!=0 ); 402fb046e76Sdrh pEngine = sqlite3ParserAlloc(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 ); 410124c0b49Sdrh assert( pParse->nzVar==0 ); 411124c0b49Sdrh assert( pParse->azVar==0 ); 412d9da78a2Sdrh enableLookaside = db->lookaside.bEnabled; 413d9da78a2Sdrh if( db->lookaside.pStart ) db->lookaside.bEnabled = 1; 41417435752Sdrh while( !db->mallocFailed && zSql[i]!=0 ){ 41532eb7b47Sdrh assert( i>=0 ); 416b7916a78Sdrh pParse->sLastToken.z = &zSql[i]; 41735b5a33eSdrh pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); 41875897234Sdrh i += pParse->sLastToken.n; 419bb4957f8Sdrh if( i>mxSqlLen ){ 420e5c941b8Sdrh pParse->rc = SQLITE_TOOBIG; 421e5c941b8Sdrh break; 422e5c941b8Sdrh } 42375897234Sdrh switch( tokenType ){ 424200a81dcSdrh case TK_SPACE: { 425881feaa0Sdrh if( db->u1.isInterrupted ){ 42619f81f6cSdrh sqlite3ErrorMsg(pParse, "interrupt"); 42732eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 42832eb7b47Sdrh goto abort_parse; 42932eb7b47Sdrh } 43075897234Sdrh break; 43175897234Sdrh } 43232eb7b47Sdrh case TK_ILLEGAL: { 433347bdc39Sdrh sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", 434ae29ffbeSdrh &pParse->sLastToken); 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: 4520be0cf67Sdrh assert( nErr==0 ); 453*9a9705deSdrh if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ 454b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 4554adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 456326dce74Sdrh pParse->zTail = &zSql[i]; 45775897234Sdrh } 458*9a9705deSdrh if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ 4594adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 46075897234Sdrh } 461*9a9705deSdrh } 462ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH 463af89fe66Sdrh sqlite3_mutex_enter(sqlite3MallocMutex()); 464ec424a5bSdrh sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, 465ec424a5bSdrh sqlite3ParserStackPeak(pEngine) 466ec424a5bSdrh ); 467af89fe66Sdrh sqlite3_mutex_leave(sqlite3MallocMutex()); 468ec424a5bSdrh #endif /* YYDEBUG */ 46917435752Sdrh sqlite3ParserFree(pEngine, sqlite3_free); 470d9da78a2Sdrh db->lookaside.bEnabled = enableLookaside; 47117435752Sdrh if( db->mallocFailed ){ 47271c697efSdrh pParse->rc = SQLITE_NOMEM; 47371c697efSdrh } 474b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 475f089aa45Sdrh sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc)); 476b86ccfb2Sdrh } 47719f81f6cSdrh assert( pzErrMsg!=0 ); 47875897234Sdrh if( pParse->zErrMsg ){ 47975897234Sdrh *pzErrMsg = pParse->zErrMsg; 480413c3d36Sdrh sqlite3_log(pParse->rc, "%s", *pzErrMsg); 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 496b975598eSdrh sqlite3_free(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 */ 5041feeaed2Sdan sqlite3DeleteTable(db, pParse->pNewTable); 5057e6ebfb2Sdanielk1977 } 5067e6ebfb2Sdanielk1977 507b290f117Sdan if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith); 508633e6d57Sdrh sqlite3DeleteTrigger(db, pParse->pNewTrigger); 509124c0b49Sdrh for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); 510124c0b49Sdrh sqlite3DbFree(db, pParse->azVar); 5110b9f50d8Sdrh while( pParse->pAinc ){ 5120b9f50d8Sdrh AutoincInfo *p = pParse->pAinc; 5130b9f50d8Sdrh pParse->pAinc = p->pNext; 5140b9f50d8Sdrh sqlite3DbFree(db, p); 5150b9f50d8Sdrh } 516588a9a1aSdrh while( pParse->pZombieTab ){ 517588a9a1aSdrh Table *p = pParse->pZombieTab; 518588a9a1aSdrh pParse->pZombieTab = p->pNextZombie; 5191feeaed2Sdan sqlite3DeleteTable(db, p); 520588a9a1aSdrh } 521f3151f0aSdrh assert( nErr==0 || pParse->rc!=SQLITE_OK ); 52275897234Sdrh return nErr; 52375897234Sdrh } 524