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 80a0d1f663Sdrh ** middle of identfiers. 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 1059b8f447bSdrh 10698808babSdrh 10775897234Sdrh /* 10861b487d0Sdrh ** Return the length of the token that begins at z[0]. 10961b487d0Sdrh ** Store the token type in *tokenType before returning. 11075897234Sdrh */ 11135b5a33eSdrh int sqlite3GetToken(const unsigned char *z, int *tokenType){ 112aa756b09Sdrh int i, c; 11375897234Sdrh switch( *z ){ 11430cab809Sdrh case ' ': case '\t': case '\n': case '\f': case '\r': { 11519f81f6cSdrh testcase( z[0]==' ' ); 11619f81f6cSdrh testcase( z[0]=='\t' ); 11719f81f6cSdrh testcase( z[0]=='\n' ); 11819f81f6cSdrh testcase( z[0]=='\f' ); 11919f81f6cSdrh testcase( z[0]=='\r' ); 12078ca0e7eSdanielk1977 for(i=1; sqlite3Isspace(z[i]); i++){} 12175897234Sdrh *tokenType = TK_SPACE; 12275897234Sdrh return i; 12375897234Sdrh } 12475897234Sdrh case '-': { 12575897234Sdrh if( z[1]=='-' ){ 126aa756b09Sdrh for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 127d3479b94Sdrh *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 12875897234Sdrh return i; 12975897234Sdrh } 13075897234Sdrh *tokenType = TK_MINUS; 13175897234Sdrh return 1; 13275897234Sdrh } 13375897234Sdrh case '(': { 13475897234Sdrh *tokenType = TK_LP; 13575897234Sdrh return 1; 13675897234Sdrh } 13775897234Sdrh case ')': { 13875897234Sdrh *tokenType = TK_RP; 13975897234Sdrh return 1; 14075897234Sdrh } 14175897234Sdrh case ';': { 14275897234Sdrh *tokenType = TK_SEMI; 14375897234Sdrh return 1; 14475897234Sdrh } 14575897234Sdrh case '+': { 14675897234Sdrh *tokenType = TK_PLUS; 14775897234Sdrh return 1; 14875897234Sdrh } 14975897234Sdrh case '*': { 15075897234Sdrh *tokenType = TK_STAR; 15175897234Sdrh return 1; 15275897234Sdrh } 15375897234Sdrh case '/': { 15466105a8eSdrh if( z[1]!='*' || z[2]==0 ){ 15575897234Sdrh *tokenType = TK_SLASH; 15675897234Sdrh return 1; 15775897234Sdrh } 158aa756b09Sdrh for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 159aa756b09Sdrh if( c ) i++; 160d3479b94Sdrh *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 16166105a8eSdrh return i; 16266105a8eSdrh } 163bf4133cbSdrh case '%': { 164bf4133cbSdrh *tokenType = TK_REM; 165bf4133cbSdrh return 1; 166bf4133cbSdrh } 16775897234Sdrh case '=': { 16875897234Sdrh *tokenType = TK_EQ; 16975897234Sdrh return 1 + (z[1]=='='); 17075897234Sdrh } 17175897234Sdrh case '<': { 172aa756b09Sdrh if( (c=z[1])=='=' ){ 17375897234Sdrh *tokenType = TK_LE; 17475897234Sdrh return 2; 175aa756b09Sdrh }else if( c=='>' ){ 17675897234Sdrh *tokenType = TK_NE; 17775897234Sdrh return 2; 178aa756b09Sdrh }else if( c=='<' ){ 179bf4133cbSdrh *tokenType = TK_LSHIFT; 180bf4133cbSdrh return 2; 18175897234Sdrh }else{ 18275897234Sdrh *tokenType = TK_LT; 18375897234Sdrh return 1; 18475897234Sdrh } 18575897234Sdrh } 18675897234Sdrh case '>': { 187aa756b09Sdrh if( (c=z[1])=='=' ){ 18875897234Sdrh *tokenType = TK_GE; 18975897234Sdrh return 2; 190aa756b09Sdrh }else if( c=='>' ){ 191bf4133cbSdrh *tokenType = TK_RSHIFT; 192bf4133cbSdrh return 2; 19375897234Sdrh }else{ 19475897234Sdrh *tokenType = TK_GT; 19575897234Sdrh return 1; 19675897234Sdrh } 19775897234Sdrh } 19875897234Sdrh case '!': { 19975897234Sdrh if( z[1]!='=' ){ 20075897234Sdrh *tokenType = TK_ILLEGAL; 201c837e709Sdrh return 2; 20275897234Sdrh }else{ 20375897234Sdrh *tokenType = TK_NE; 20475897234Sdrh return 2; 20575897234Sdrh } 20675897234Sdrh } 2070040077dSdrh case '|': { 2080040077dSdrh if( z[1]!='|' ){ 209bf4133cbSdrh *tokenType = TK_BITOR; 2100040077dSdrh return 1; 2110040077dSdrh }else{ 2120040077dSdrh *tokenType = TK_CONCAT; 2130040077dSdrh return 2; 2140040077dSdrh } 2150040077dSdrh } 21675897234Sdrh case ',': { 21775897234Sdrh *tokenType = TK_COMMA; 21875897234Sdrh return 1; 21975897234Sdrh } 220bf4133cbSdrh case '&': { 221bf4133cbSdrh *tokenType = TK_BITAND; 222bf4133cbSdrh return 1; 223bf4133cbSdrh } 224bf4133cbSdrh case '~': { 225bf4133cbSdrh *tokenType = TK_BITNOT; 226bf4133cbSdrh return 1; 227bf4133cbSdrh } 2283d94662aSdrh case '`': 2293d94662aSdrh case '\'': 2303d94662aSdrh case '"': { 23175897234Sdrh int delim = z[0]; 23219f81f6cSdrh testcase( delim=='`' ); 23319f81f6cSdrh testcase( delim=='\'' ); 23419f81f6cSdrh testcase( delim=='"' ); 235aa756b09Sdrh for(i=1; (c=z[i])!=0; i++){ 236aa756b09Sdrh if( c==delim ){ 23775897234Sdrh if( z[i+1]==delim ){ 23875897234Sdrh i++; 23975897234Sdrh }else{ 24075897234Sdrh break; 24175897234Sdrh } 24275897234Sdrh } 24375897234Sdrh } 244a33cb5f7Sdrh if( c=='\'' ){ 24575897234Sdrh *tokenType = TK_STRING; 246eef8b558Sdrh return i+1; 247a33cb5f7Sdrh }else if( c!=0 ){ 248a33cb5f7Sdrh *tokenType = TK_ID; 249a33cb5f7Sdrh return i+1; 250eef8b558Sdrh }else{ 251eef8b558Sdrh *tokenType = TK_ILLEGAL; 25275897234Sdrh return i; 25375897234Sdrh } 254eef8b558Sdrh } 25575897234Sdrh case '.': { 2567681618cSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 25778ca0e7eSdanielk1977 if( !sqlite3Isdigit(z[1]) ) 2587681618cSdrh #endif 2597681618cSdrh { 26075897234Sdrh *tokenType = TK_DOT; 26175897234Sdrh return 1; 26275897234Sdrh } 2637681618cSdrh /* If the next character is a digit, this is a floating point 2647681618cSdrh ** number that begins with ".". Fall thru into the next case */ 2657681618cSdrh } 26675897234Sdrh case '0': case '1': case '2': case '3': case '4': 26775897234Sdrh case '5': case '6': case '7': case '8': case '9': { 26819f81f6cSdrh testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); 26919f81f6cSdrh testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); 27019f81f6cSdrh testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); 27119f81f6cSdrh testcase( z[0]=='9' ); 272c837e709Sdrh *tokenType = TK_INTEGER; 27378ca0e7eSdanielk1977 for(i=0; sqlite3Isdigit(z[i]); i++){} 274b7f9164eSdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2757681618cSdrh if( z[i]=='.' ){ 2767681618cSdrh i++; 27778ca0e7eSdanielk1977 while( sqlite3Isdigit(z[i]) ){ i++; } 278c837e709Sdrh *tokenType = TK_FLOAT; 279c837e709Sdrh } 28075897234Sdrh if( (z[i]=='e' || z[i]=='E') && 28178ca0e7eSdanielk1977 ( sqlite3Isdigit(z[i+1]) 28278ca0e7eSdanielk1977 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) 28375897234Sdrh ) 28475897234Sdrh ){ 28575897234Sdrh i += 2; 28678ca0e7eSdanielk1977 while( sqlite3Isdigit(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; 30378ca0e7eSdanielk1977 for(i=1; sqlite3Isdigit(z[i]); i++){} 304fa6bc000Sdrh return i; 30550457896Sdrh } 306b7f9164eSdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 307288d37f1Sdrh case '$': 308288d37f1Sdrh #endif 3090b2a5eeaSdrh case '@': /* For compatibility with MS SQL Server */ 310f59b12fbSdrh case '#': 311288d37f1Sdrh case ':': { 312895d7472Sdrh int n = 0; 313f59b12fbSdrh testcase( z[0]=='$' ); testcase( z[0]=='@' ); 314f59b12fbSdrh testcase( z[0]==':' ); testcase( z[0]=='#' ); 315288d37f1Sdrh *tokenType = TK_VARIABLE; 316895d7472Sdrh for(i=1; (c=z[i])!=0; i++){ 317288d37f1Sdrh if( IdChar(c) ){ 318895d7472Sdrh n++; 319288d37f1Sdrh #ifndef SQLITE_OMIT_TCL_VARIABLE 320895d7472Sdrh }else if( c=='(' && n>0 ){ 321895d7472Sdrh do{ 322895d7472Sdrh i++; 32378ca0e7eSdanielk1977 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); 324895d7472Sdrh if( c==')' ){ 325895d7472Sdrh i++; 326895d7472Sdrh }else{ 327895d7472Sdrh *tokenType = TK_ILLEGAL; 328895d7472Sdrh } 329895d7472Sdrh break; 330895d7472Sdrh }else if( c==':' && z[i+1]==':' ){ 331895d7472Sdrh i++; 332288d37f1Sdrh #endif 333895d7472Sdrh }else{ 334895d7472Sdrh break; 335895d7472Sdrh } 336895d7472Sdrh } 3379d74b4c5Sdrh if( n==0 ) *tokenType = TK_ILLEGAL; 338895d7472Sdrh return i; 339895d7472Sdrh } 340b7f9164eSdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 341c572ef7fSdanielk1977 case 'x': case 'X': { 34219f81f6cSdrh testcase( z[0]=='x' ); testcase( z[0]=='X' ); 3434b2f9368Sdrh if( z[1]=='\'' ){ 3443fd0a736Sdanielk1977 *tokenType = TK_BLOB; 3453a61a5a2Sdrh for(i=2; sqlite3Isxdigit(z[i]); i++){} 3463a61a5a2Sdrh if( z[i]!='\'' || i%2 ){ 3473fd0a736Sdanielk1977 *tokenType = TK_ILLEGAL; 3483a61a5a2Sdrh while( z[i] && z[i]!='\'' ){ i++; } 3493fd0a736Sdanielk1977 } 3503a61a5a2Sdrh if( z[i] ) i++; 351c572ef7fSdanielk1977 return i; 352c572ef7fSdanielk1977 } 353c572ef7fSdanielk1977 /* Otherwise fall through to the next case */ 354c572ef7fSdanielk1977 } 355b7f9164eSdrh #endif 35698808babSdrh default: { 3579a087a99Sdrh if( !IdChar(*z) ){ 35898808babSdrh break; 35998808babSdrh } 360aa756b09Sdrh for(i=1; IdChar(z[i]); i++){} 361c60e9b82Sdanielk1977 *tokenType = keywordCode((char*)z, i); 36275897234Sdrh return i; 36375897234Sdrh } 36475897234Sdrh } 36575897234Sdrh *tokenType = TK_ILLEGAL; 36675897234Sdrh return 1; 36775897234Sdrh } 36875897234Sdrh 36975897234Sdrh /* 37075897234Sdrh ** Run the parser on the given SQL string. The parser structure is 371b19a2bc6Sdrh ** passed in. An SQLITE_ status code is returned. If an error occurs 372fb45d8c5Sdrh ** then an and attempt is made to write an error message into 373fb45d8c5Sdrh ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that 374fb45d8c5Sdrh ** error message. 37575897234Sdrh */ 3764adee20fSdanielk1977 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 377d9da78a2Sdrh int nErr = 0; /* Number of errors encountered */ 378d9da78a2Sdrh int i; /* Loop counter */ 379d9da78a2Sdrh void *pEngine; /* The LEMON-generated LALR(1) parser */ 380d9da78a2Sdrh int tokenType; /* type of the next token */ 381d9da78a2Sdrh int lastTokenParsed = -1; /* type of the previous token */ 382b08a67a7Sshane u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */ 383d9da78a2Sdrh sqlite3 *db = pParse->db; /* The database connection */ 384d9da78a2Sdrh int mxSqlLen; /* Max length of an SQL string */ 38575897234Sdrh 386d9da78a2Sdrh 387d9da78a2Sdrh mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 3884f7d3a5fSdrh if( db->nVdbeActive==0 ){ 389881feaa0Sdrh db->u1.isInterrupted = 0; 39015ca1df1Sdrh } 3914c504391Sdrh pParse->rc = SQLITE_OK; 392e7f3f3eeSdrh pParse->zTail = zSql; 39375897234Sdrh i = 0; 394fb45d8c5Sdrh assert( pzErrMsg!=0 ); 395e5ae5735Sdrh pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc); 39675897234Sdrh if( pEngine==0 ){ 397f3a65f7eSdrh db->mallocFailed = 1; 398defc9972Sdrh return SQLITE_NOMEM; 39975897234Sdrh } 400fa6bc000Sdrh assert( pParse->pNewTable==0 ); 401fa6bc000Sdrh assert( pParse->pNewTrigger==0 ); 402fa6bc000Sdrh assert( pParse->nVar==0 ); 403124c0b49Sdrh assert( pParse->nzVar==0 ); 404124c0b49Sdrh assert( pParse->azVar==0 ); 405d9da78a2Sdrh enableLookaside = db->lookaside.bEnabled; 406d9da78a2Sdrh if( db->lookaside.pStart ) db->lookaside.bEnabled = 1; 40717435752Sdrh while( !db->mallocFailed && zSql[i]!=0 ){ 40832eb7b47Sdrh assert( i>=0 ); 409b7916a78Sdrh pParse->sLastToken.z = &zSql[i]; 41035b5a33eSdrh pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); 41175897234Sdrh i += pParse->sLastToken.n; 412bb4957f8Sdrh if( i>mxSqlLen ){ 413e5c941b8Sdrh pParse->rc = SQLITE_TOOBIG; 414e5c941b8Sdrh break; 415e5c941b8Sdrh } 41675897234Sdrh switch( tokenType ){ 417200a81dcSdrh case TK_SPACE: { 418881feaa0Sdrh if( db->u1.isInterrupted ){ 41919f81f6cSdrh sqlite3ErrorMsg(pParse, "interrupt"); 42032eb7b47Sdrh pParse->rc = SQLITE_INTERRUPT; 42132eb7b47Sdrh goto abort_parse; 42232eb7b47Sdrh } 42375897234Sdrh break; 42475897234Sdrh } 42532eb7b47Sdrh case TK_ILLEGAL: { 426633e6d57Sdrh sqlite3DbFree(db, *pzErrMsg); 4271e536953Sdanielk1977 *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"", 428ae29ffbeSdrh &pParse->sLastToken); 42975897234Sdrh nErr++; 430caec2f12Sdrh goto abort_parse; 43132eb7b47Sdrh } 432326dce74Sdrh case TK_SEMI: { 433326dce74Sdrh pParse->zTail = &zSql[i]; 434326dce74Sdrh /* Fall thru into the default case */ 435326dce74Sdrh } 43632eb7b47Sdrh default: { 4374adee20fSdanielk1977 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); 438b86ccfb2Sdrh lastTokenParsed = tokenType; 439b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK ){ 44032eb7b47Sdrh goto abort_parse; 44175897234Sdrh } 44275897234Sdrh break; 44375897234Sdrh } 44475897234Sdrh } 44532eb7b47Sdrh } 44632eb7b47Sdrh abort_parse: 447b86ccfb2Sdrh if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ 448b86ccfb2Sdrh if( lastTokenParsed!=TK_SEMI ){ 4494adee20fSdanielk1977 sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); 450326dce74Sdrh pParse->zTail = &zSql[i]; 45175897234Sdrh } 4524adee20fSdanielk1977 sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); 45375897234Sdrh } 454ec424a5bSdrh #ifdef YYTRACKMAXSTACKDEPTH 455ec424a5bSdrh sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, 456ec424a5bSdrh sqlite3ParserStackPeak(pEngine) 457ec424a5bSdrh ); 458ec424a5bSdrh #endif /* YYDEBUG */ 45917435752Sdrh sqlite3ParserFree(pEngine, sqlite3_free); 460d9da78a2Sdrh db->lookaside.bEnabled = enableLookaside; 46117435752Sdrh if( db->mallocFailed ){ 46271c697efSdrh pParse->rc = SQLITE_NOMEM; 46371c697efSdrh } 464b86ccfb2Sdrh if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 465f089aa45Sdrh sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc)); 466b86ccfb2Sdrh } 46719f81f6cSdrh assert( pzErrMsg!=0 ); 46875897234Sdrh if( pParse->zErrMsg ){ 46975897234Sdrh *pzErrMsg = pParse->zErrMsg; 470413c3d36Sdrh sqlite3_log(pParse->rc, "%s", *pzErrMsg); 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 ){ 480633e6d57Sdrh sqlite3DbFree(db, pParse->aTableLock); 481c00da105Sdanielk1977 pParse->aTableLock = 0; 482c00da105Sdanielk1977 pParse->nTableLock = 0; 483c00da105Sdanielk1977 } 484c00da105Sdanielk1977 #endif 4854f3dd150Sdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 486b975598eSdrh 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 */ 4941feeaed2Sdan sqlite3DeleteTable(db, pParse->pNewTable); 4957e6ebfb2Sdanielk1977 } 4967e6ebfb2Sdanielk1977 497*b290f117Sdan if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith); 498633e6d57Sdrh sqlite3DeleteTrigger(db, pParse->pNewTrigger); 499124c0b49Sdrh for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]); 500124c0b49Sdrh sqlite3DbFree(db, pParse->azVar); 5010b9f50d8Sdrh while( pParse->pAinc ){ 5020b9f50d8Sdrh AutoincInfo *p = pParse->pAinc; 5030b9f50d8Sdrh pParse->pAinc = p->pNext; 5040b9f50d8Sdrh sqlite3DbFree(db, p); 5050b9f50d8Sdrh } 506588a9a1aSdrh while( pParse->pZombieTab ){ 507588a9a1aSdrh Table *p = pParse->pZombieTab; 508588a9a1aSdrh pParse->pZombieTab = p->pNextZombie; 5091feeaed2Sdan sqlite3DeleteTable(db, p); 510588a9a1aSdrh } 511b5092d18Sdrh if( nErr>0 && pParse->rc==SQLITE_OK ){ 5124c504391Sdrh pParse->rc = SQLITE_ERROR; 5134c504391Sdrh } 51475897234Sdrh return nErr; 51575897234Sdrh } 516