1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** An tokenizer for SQL 13 ** 14 ** This file contains C code that splits an SQL input string up into 15 ** individual tokens and sends those tokens one-by-one over to the 16 ** parser for analysis. 17 */ 18 #include "sqliteInt.h" 19 #include <stdlib.h> 20 21 /* Character classes for tokenizing 22 ** 23 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented 24 ** using a lookup table, whereas a switch() directly on c uses a binary search. 25 ** The lookup table is much faster. To maximize speed, and to ensure that 26 ** a lookup table is used, all of the classes need to be small integers and 27 ** all of them need to be used within the switch. 28 */ 29 #define CC_X 0 /* The letter 'x', or start of BLOB literal */ 30 #define CC_KYWD0 1 /* First letter of a keyword */ 31 #define CC_KYWD 2 /* Alphabetics or '_'. Usable in a keyword */ 32 #define CC_DIGIT 3 /* Digits */ 33 #define CC_DOLLAR 4 /* '$' */ 34 #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */ 35 #define CC_VARNUM 6 /* '?'. Numeric SQL variables */ 36 #define CC_SPACE 7 /* Space characters */ 37 #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */ 38 #define CC_QUOTE2 9 /* '['. [...] style quoted ids */ 39 #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */ 40 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */ 41 #define CC_LT 12 /* '<'. Part of < or <= or <> */ 42 #define CC_GT 13 /* '>'. Part of > or >= */ 43 #define CC_EQ 14 /* '='. Part of = or == */ 44 #define CC_BANG 15 /* '!'. Part of != */ 45 #define CC_SLASH 16 /* '/'. / or c-style comment */ 46 #define CC_LP 17 /* '(' */ 47 #define CC_RP 18 /* ')' */ 48 #define CC_SEMI 19 /* ';' */ 49 #define CC_PLUS 20 /* '+' */ 50 #define CC_STAR 21 /* '*' */ 51 #define CC_PERCENT 22 /* '%' */ 52 #define CC_COMMA 23 /* ',' */ 53 #define CC_AND 24 /* '&' */ 54 #define CC_TILDA 25 /* '~' */ 55 #define CC_DOT 26 /* '.' */ 56 #define CC_ID 27 /* unicode characters usable in IDs */ 57 #define CC_ILLEGAL 28 /* Illegal character */ 58 #define CC_NUL 29 /* 0x00 */ 59 60 static const unsigned char aiClass[] = { 61 #ifdef SQLITE_ASCII 62 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ 63 /* 0x */ 29, 28, 28, 28, 28, 28, 28, 28, 28, 7, 7, 28, 7, 7, 28, 28, 64 /* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 65 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, 66 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, 67 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 68 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 9, 28, 28, 28, 2, 69 /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 70 /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 28, 10, 28, 25, 28, 71 /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 72 /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 73 /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 74 /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 75 /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 76 /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 77 /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 78 /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 79 #endif 80 #ifdef SQLITE_EBCDIC 81 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ 82 /* 0x */ 29, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 7, 7, 28, 28, 83 /* 1x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 84 /* 2x */ 28, 28, 28, 28, 28, 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 85 /* 3x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 86 /* 4x */ 7, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 26, 12, 17, 20, 10, 87 /* 5x */ 24, 28, 28, 28, 28, 28, 28, 28, 28, 28, 15, 4, 21, 18, 19, 28, 88 /* 6x */ 11, 16, 28, 28, 28, 28, 28, 28, 28, 28, 28, 23, 22, 2, 13, 6, 89 /* 7x */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 8, 5, 5, 5, 8, 14, 8, 90 /* 8x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, 91 /* 9x */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, 92 /* Ax */ 28, 25, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28, 93 /* Bx */ 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 9, 28, 28, 28, 28, 28, 94 /* Cx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, 95 /* Dx */ 28, 1, 1, 1, 1, 1, 1, 1, 1, 1, 28, 28, 28, 28, 28, 28, 96 /* Ex */ 28, 28, 1, 1, 1, 1, 1, 0, 2, 2, 28, 28, 28, 28, 28, 28, 97 /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 28, 28, 28, 28, 28, 28, 98 #endif 99 }; 100 101 /* 102 ** The charMap() macro maps alphabetic characters (only) into their 103 ** lower-case ASCII equivalent. On ASCII machines, this is just 104 ** an upper-to-lower case map. On EBCDIC machines we also need 105 ** to adjust the encoding. The mapping is only valid for alphabetics 106 ** which are the only characters for which this feature is used. 107 ** 108 ** Used by keywordhash.h 109 */ 110 #ifdef SQLITE_ASCII 111 # define charMap(X) sqlite3UpperToLower[(unsigned char)X] 112 #endif 113 #ifdef SQLITE_EBCDIC 114 # define charMap(X) ebcdicToAscii[(unsigned char)X] 115 const unsigned char ebcdicToAscii[] = { 116 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ 117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ 118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ 119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ 120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ 122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ 123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ 124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 125 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ 126 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ 127 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ 128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 129 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ 130 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ 131 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ 132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ 133 }; 134 #endif 135 136 /* 137 ** The sqlite3KeywordCode function looks up an identifier to determine if 138 ** it is a keyword. If it is a keyword, the token code of that keyword is 139 ** returned. If the input is not a keyword, TK_ID is returned. 140 ** 141 ** The implementation of this routine was generated by a program, 142 ** mkkeywordhash.c, located in the tool subdirectory of the distribution. 143 ** The output of the mkkeywordhash.c program is written into a file 144 ** named keywordhash.h and then included into this source file by 145 ** the #include below. 146 */ 147 #include "keywordhash.h" 148 149 150 /* 151 ** If X is a character that can be used in an identifier then 152 ** IdChar(X) will be true. Otherwise it is false. 153 ** 154 ** For ASCII, any character with the high-order bit set is 155 ** allowed in an identifier. For 7-bit characters, 156 ** sqlite3IsIdChar[X] must be 1. 157 ** 158 ** For EBCDIC, the rules are more complex but have the same 159 ** end result. 160 ** 161 ** Ticket #1066. the SQL standard does not allow '$' in the 162 ** middle of identifiers. But many SQL implementations do. 163 ** SQLite will allow '$' in identifiers for compatibility. 164 ** But the feature is undocumented. 165 */ 166 #ifdef SQLITE_ASCII 167 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) 168 #endif 169 #ifdef SQLITE_EBCDIC 170 const char sqlite3IsEbcdicIdChar[] = { 171 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ 172 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ 173 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ 174 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ 175 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ 176 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ 177 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ 178 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ 179 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ 180 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ 181 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ 182 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ 183 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ 184 }; 185 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) 186 #endif 187 188 /* Make the IdChar function accessible from ctime.c and alter.c */ 189 int sqlite3IsIdChar(u8 c){ return IdChar(c); } 190 191 #ifndef SQLITE_OMIT_WINDOWFUNC 192 /* 193 ** Return the id of the next token in string (*pz). Before returning, set 194 ** (*pz) to point to the byte following the parsed token. 195 */ 196 static int getToken(const unsigned char **pz){ 197 const unsigned char *z = *pz; 198 int t; /* Token type to return */ 199 do { 200 z += sqlite3GetToken(z, &t); 201 }while( t==TK_SPACE ); 202 if( t==TK_ID 203 || t==TK_STRING 204 || t==TK_JOIN_KW 205 || t==TK_WINDOW 206 || t==TK_OVER 207 || sqlite3ParserFallback(t)==TK_ID 208 ){ 209 t = TK_ID; 210 } 211 *pz = z; 212 return t; 213 } 214 215 /* 216 ** The following three functions are called immediately after the tokenizer 217 ** reads the keywords WINDOW, OVER and FILTER, respectively, to determine 218 ** whether the token should be treated as a keyword or an SQL identifier. 219 ** This cannot be handled by the usual lemon %fallback method, due to 220 ** the ambiguity in some constructions. e.g. 221 ** 222 ** SELECT sum(x) OVER ... 223 ** 224 ** In the above, "OVER" might be a keyword, or it might be an alias for the 225 ** sum(x) expression. If a "%fallback ID OVER" directive were added to 226 ** grammar, then SQLite would always treat "OVER" as an alias, making it 227 ** impossible to call a window-function without a FILTER clause. 228 ** 229 ** WINDOW is treated as a keyword if: 230 ** 231 ** * the following token is an identifier, or a keyword that can fallback 232 ** to being an identifier, and 233 ** * the token after than one is TK_AS. 234 ** 235 ** OVER is a keyword if: 236 ** 237 ** * the previous token was TK_RP, and 238 ** * the next token is either TK_LP or an identifier. 239 ** 240 ** FILTER is a keyword if: 241 ** 242 ** * the previous token was TK_RP, and 243 ** * the next token is TK_LP. 244 */ 245 static int analyzeWindowKeyword(const unsigned char *z){ 246 int t; 247 t = getToken(&z); 248 if( t!=TK_ID ) return TK_ID; 249 t = getToken(&z); 250 if( t!=TK_AS ) return TK_ID; 251 return TK_WINDOW; 252 } 253 static int analyzeOverKeyword(const unsigned char *z, int lastToken){ 254 if( lastToken==TK_RP ){ 255 int t = getToken(&z); 256 if( t==TK_LP || t==TK_ID ) return TK_OVER; 257 } 258 return TK_ID; 259 } 260 static int analyzeFilterKeyword(const unsigned char *z, int lastToken){ 261 if( lastToken==TK_RP && getToken(&z)==TK_LP ){ 262 return TK_FILTER; 263 } 264 return TK_ID; 265 } 266 #endif /* SQLITE_OMIT_WINDOWFUNC */ 267 268 /* 269 ** Return the length (in bytes) of the token that begins at z[0]. 270 ** Store the token type in *tokenType before returning. 271 */ 272 int sqlite3GetToken(const unsigned char *z, int *tokenType){ 273 int i, c; 274 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte 275 ** of the token. See the comment on the CC_ defines 276 ** above. */ 277 case CC_SPACE: { 278 testcase( z[0]==' ' ); 279 testcase( z[0]=='\t' ); 280 testcase( z[0]=='\n' ); 281 testcase( z[0]=='\f' ); 282 testcase( z[0]=='\r' ); 283 for(i=1; sqlite3Isspace(z[i]); i++){} 284 *tokenType = TK_SPACE; 285 return i; 286 } 287 case CC_MINUS: { 288 if( z[1]=='-' ){ 289 for(i=2; (c=z[i])!=0 && c!='\n'; i++){} 290 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 291 return i; 292 } 293 *tokenType = TK_MINUS; 294 return 1; 295 } 296 case CC_LP: { 297 *tokenType = TK_LP; 298 return 1; 299 } 300 case CC_RP: { 301 *tokenType = TK_RP; 302 return 1; 303 } 304 case CC_SEMI: { 305 *tokenType = TK_SEMI; 306 return 1; 307 } 308 case CC_PLUS: { 309 *tokenType = TK_PLUS; 310 return 1; 311 } 312 case CC_STAR: { 313 *tokenType = TK_STAR; 314 return 1; 315 } 316 case CC_SLASH: { 317 if( z[1]!='*' || z[2]==0 ){ 318 *tokenType = TK_SLASH; 319 return 1; 320 } 321 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} 322 if( c ) i++; 323 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ 324 return i; 325 } 326 case CC_PERCENT: { 327 *tokenType = TK_REM; 328 return 1; 329 } 330 case CC_EQ: { 331 *tokenType = TK_EQ; 332 return 1 + (z[1]=='='); 333 } 334 case CC_LT: { 335 if( (c=z[1])=='=' ){ 336 *tokenType = TK_LE; 337 return 2; 338 }else if( c=='>' ){ 339 *tokenType = TK_NE; 340 return 2; 341 }else if( c=='<' ){ 342 *tokenType = TK_LSHIFT; 343 return 2; 344 }else{ 345 *tokenType = TK_LT; 346 return 1; 347 } 348 } 349 case CC_GT: { 350 if( (c=z[1])=='=' ){ 351 *tokenType = TK_GE; 352 return 2; 353 }else if( c=='>' ){ 354 *tokenType = TK_RSHIFT; 355 return 2; 356 }else{ 357 *tokenType = TK_GT; 358 return 1; 359 } 360 } 361 case CC_BANG: { 362 if( z[1]!='=' ){ 363 *tokenType = TK_ILLEGAL; 364 return 1; 365 }else{ 366 *tokenType = TK_NE; 367 return 2; 368 } 369 } 370 case CC_PIPE: { 371 if( z[1]!='|' ){ 372 *tokenType = TK_BITOR; 373 return 1; 374 }else{ 375 *tokenType = TK_CONCAT; 376 return 2; 377 } 378 } 379 case CC_COMMA: { 380 *tokenType = TK_COMMA; 381 return 1; 382 } 383 case CC_AND: { 384 *tokenType = TK_BITAND; 385 return 1; 386 } 387 case CC_TILDA: { 388 *tokenType = TK_BITNOT; 389 return 1; 390 } 391 case CC_QUOTE: { 392 int delim = z[0]; 393 testcase( delim=='`' ); 394 testcase( delim=='\'' ); 395 testcase( delim=='"' ); 396 for(i=1; (c=z[i])!=0; i++){ 397 if( c==delim ){ 398 if( z[i+1]==delim ){ 399 i++; 400 }else{ 401 break; 402 } 403 } 404 } 405 if( c=='\'' ){ 406 *tokenType = TK_STRING; 407 return i+1; 408 }else if( c!=0 ){ 409 *tokenType = TK_ID; 410 return i+1; 411 }else{ 412 *tokenType = TK_ILLEGAL; 413 return i; 414 } 415 } 416 case CC_DOT: { 417 #ifndef SQLITE_OMIT_FLOATING_POINT 418 if( !sqlite3Isdigit(z[1]) ) 419 #endif 420 { 421 *tokenType = TK_DOT; 422 return 1; 423 } 424 /* If the next character is a digit, this is a floating point 425 ** number that begins with ".". Fall thru into the next case */ 426 /* no break */ deliberate_fall_through 427 } 428 case CC_DIGIT: { 429 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); 430 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); 431 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); 432 testcase( z[0]=='9' ); 433 *tokenType = TK_INTEGER; 434 #ifndef SQLITE_OMIT_HEX_INTEGER 435 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ 436 for(i=3; sqlite3Isxdigit(z[i]); i++){} 437 return i; 438 } 439 #endif 440 for(i=0; sqlite3Isdigit(z[i]); i++){} 441 #ifndef SQLITE_OMIT_FLOATING_POINT 442 if( z[i]=='.' ){ 443 i++; 444 while( sqlite3Isdigit(z[i]) ){ i++; } 445 *tokenType = TK_FLOAT; 446 } 447 if( (z[i]=='e' || z[i]=='E') && 448 ( sqlite3Isdigit(z[i+1]) 449 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) 450 ) 451 ){ 452 i += 2; 453 while( sqlite3Isdigit(z[i]) ){ i++; } 454 *tokenType = TK_FLOAT; 455 } 456 #endif 457 while( IdChar(z[i]) ){ 458 *tokenType = TK_ILLEGAL; 459 i++; 460 } 461 return i; 462 } 463 case CC_QUOTE2: { 464 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} 465 *tokenType = c==']' ? TK_ID : TK_ILLEGAL; 466 return i; 467 } 468 case CC_VARNUM: { 469 *tokenType = TK_VARIABLE; 470 for(i=1; sqlite3Isdigit(z[i]); i++){} 471 return i; 472 } 473 case CC_DOLLAR: 474 case CC_VARALPHA: { 475 int n = 0; 476 testcase( z[0]=='$' ); testcase( z[0]=='@' ); 477 testcase( z[0]==':' ); testcase( z[0]=='#' ); 478 *tokenType = TK_VARIABLE; 479 for(i=1; (c=z[i])!=0; i++){ 480 if( IdChar(c) ){ 481 n++; 482 #ifndef SQLITE_OMIT_TCL_VARIABLE 483 }else if( c=='(' && n>0 ){ 484 do{ 485 i++; 486 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); 487 if( c==')' ){ 488 i++; 489 }else{ 490 *tokenType = TK_ILLEGAL; 491 } 492 break; 493 }else if( c==':' && z[i+1]==':' ){ 494 i++; 495 #endif 496 }else{ 497 break; 498 } 499 } 500 if( n==0 ) *tokenType = TK_ILLEGAL; 501 return i; 502 } 503 case CC_KYWD0: { 504 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} 505 if( IdChar(z[i]) ){ 506 /* This token started out using characters that can appear in keywords, 507 ** but z[i] is a character not allowed within keywords, so this must 508 ** be an identifier instead */ 509 i++; 510 break; 511 } 512 *tokenType = TK_ID; 513 return keywordCode((char*)z, i, tokenType); 514 } 515 case CC_X: { 516 #ifndef SQLITE_OMIT_BLOB_LITERAL 517 testcase( z[0]=='x' ); testcase( z[0]=='X' ); 518 if( z[1]=='\'' ){ 519 *tokenType = TK_BLOB; 520 for(i=2; sqlite3Isxdigit(z[i]); i++){} 521 if( z[i]!='\'' || i%2 ){ 522 *tokenType = TK_ILLEGAL; 523 while( z[i] && z[i]!='\'' ){ i++; } 524 } 525 if( z[i] ) i++; 526 return i; 527 } 528 #endif 529 /* If it is not a BLOB literal, then it must be an ID, since no 530 ** SQL keywords start with the letter 'x'. Fall through */ 531 /* no break */ deliberate_fall_through 532 } 533 case CC_KYWD: 534 case CC_ID: { 535 i = 1; 536 break; 537 } 538 case CC_NUL: { 539 *tokenType = TK_ILLEGAL; 540 return 0; 541 } 542 default: { 543 *tokenType = TK_ILLEGAL; 544 return 1; 545 } 546 } 547 while( IdChar(z[i]) ){ i++; } 548 *tokenType = TK_ID; 549 return i; 550 } 551 552 /* 553 ** Run the parser on the given SQL string. The parser structure is 554 ** passed in. An SQLITE_ status code is returned. If an error occurs 555 ** then an and attempt is made to write an error message into 556 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that 557 ** error message. 558 */ 559 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ 560 int nErr = 0; /* Number of errors encountered */ 561 void *pEngine; /* The LEMON-generated LALR(1) parser */ 562 int n = 0; /* Length of the next token token */ 563 int tokenType; /* type of the next token */ 564 int lastTokenParsed = -1; /* type of the previous token */ 565 sqlite3 *db = pParse->db; /* The database connection */ 566 int mxSqlLen; /* Max length of an SQL string */ 567 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK 568 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */ 569 #endif 570 VVA_ONLY( u8 startedWithOom = db->mallocFailed ); 571 572 assert( zSql!=0 ); 573 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; 574 if( db->nVdbeActive==0 ){ 575 AtomicStore(&db->u1.isInterrupted, 0); 576 } 577 pParse->rc = SQLITE_OK; 578 pParse->zTail = zSql; 579 assert( pzErrMsg!=0 ); 580 #ifdef SQLITE_DEBUG 581 if( db->flags & SQLITE_ParserTrace ){ 582 printf("parser: [[[%s]]]\n", zSql); 583 sqlite3ParserTrace(stdout, "parser: "); 584 }else{ 585 sqlite3ParserTrace(0, 0); 586 } 587 #endif 588 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK 589 pEngine = &sEngine; 590 sqlite3ParserInit(pEngine, pParse); 591 #else 592 pEngine = sqlite3ParserAlloc(sqlite3Malloc, pParse); 593 if( pEngine==0 ){ 594 sqlite3OomFault(db); 595 return SQLITE_NOMEM_BKPT; 596 } 597 #endif 598 assert( pParse->pNewTable==0 ); 599 assert( pParse->pNewTrigger==0 ); 600 assert( pParse->nVar==0 ); 601 assert( pParse->pVList==0 ); 602 pParse->pParentParse = db->pParse; 603 db->pParse = pParse; 604 while( 1 ){ 605 n = sqlite3GetToken((u8*)zSql, &tokenType); 606 mxSqlLen -= n; 607 if( mxSqlLen<0 ){ 608 pParse->rc = SQLITE_TOOBIG; 609 break; 610 } 611 #ifndef SQLITE_OMIT_WINDOWFUNC 612 if( tokenType>=TK_WINDOW ){ 613 assert( tokenType==TK_SPACE || tokenType==TK_OVER || tokenType==TK_FILTER 614 || tokenType==TK_ILLEGAL || tokenType==TK_WINDOW 615 ); 616 #else 617 if( tokenType>=TK_SPACE ){ 618 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); 619 #endif /* SQLITE_OMIT_WINDOWFUNC */ 620 if( AtomicLoad(&db->u1.isInterrupted) ){ 621 pParse->rc = SQLITE_INTERRUPT; 622 break; 623 } 624 if( tokenType==TK_SPACE ){ 625 zSql += n; 626 continue; 627 } 628 if( zSql[0]==0 ){ 629 /* Upon reaching the end of input, call the parser two more times 630 ** with tokens TK_SEMI and 0, in that order. */ 631 if( lastTokenParsed==TK_SEMI ){ 632 tokenType = 0; 633 }else if( lastTokenParsed==0 ){ 634 break; 635 }else{ 636 tokenType = TK_SEMI; 637 } 638 n = 0; 639 #ifndef SQLITE_OMIT_WINDOWFUNC 640 }else if( tokenType==TK_WINDOW ){ 641 assert( n==6 ); 642 tokenType = analyzeWindowKeyword((const u8*)&zSql[6]); 643 }else if( tokenType==TK_OVER ){ 644 assert( n==4 ); 645 tokenType = analyzeOverKeyword((const u8*)&zSql[4], lastTokenParsed); 646 }else if( tokenType==TK_FILTER ){ 647 assert( n==6 ); 648 tokenType = analyzeFilterKeyword((const u8*)&zSql[6], lastTokenParsed); 649 #endif /* SQLITE_OMIT_WINDOWFUNC */ 650 }else{ 651 sqlite3ErrorMsg(pParse, "unrecognized token: \"%.*s\"", n, zSql); 652 break; 653 } 654 } 655 pParse->sLastToken.z = zSql; 656 pParse->sLastToken.n = n; 657 sqlite3Parser(pEngine, tokenType, pParse->sLastToken); 658 lastTokenParsed = tokenType; 659 zSql += n; 660 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom ); 661 if( pParse->rc!=SQLITE_OK ) break; 662 } 663 assert( nErr==0 ); 664 #ifdef YYTRACKMAXSTACKDEPTH 665 sqlite3_mutex_enter(sqlite3MallocMutex()); 666 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, 667 sqlite3ParserStackPeak(pEngine) 668 ); 669 sqlite3_mutex_leave(sqlite3MallocMutex()); 670 #endif /* YYDEBUG */ 671 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK 672 sqlite3ParserFinalize(pEngine); 673 #else 674 sqlite3ParserFree(pEngine, sqlite3_free); 675 #endif 676 if( db->mallocFailed ){ 677 pParse->rc = SQLITE_NOMEM_BKPT; 678 } 679 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ 680 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); 681 } 682 assert( pzErrMsg!=0 ); 683 if( pParse->zErrMsg ){ 684 *pzErrMsg = pParse->zErrMsg; 685 sqlite3_log(pParse->rc, "%s in \"%s\"", 686 *pzErrMsg, pParse->zTail); 687 pParse->zErrMsg = 0; 688 nErr++; 689 } 690 pParse->zTail = zSql; 691 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ 692 sqlite3VdbeDelete(pParse->pVdbe); 693 pParse->pVdbe = 0; 694 } 695 #ifndef SQLITE_OMIT_SHARED_CACHE 696 if( pParse->nested==0 ){ 697 sqlite3DbFree(db, pParse->aTableLock); 698 pParse->aTableLock = 0; 699 pParse->nTableLock = 0; 700 } 701 #endif 702 #ifndef SQLITE_OMIT_VIRTUALTABLE 703 sqlite3_free(pParse->apVtabLock); 704 #endif 705 706 if( !IN_SPECIAL_PARSE ){ 707 /* If the pParse->declareVtab flag is set, do not delete any table 708 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) 709 ** will take responsibility for freeing the Table structure. 710 */ 711 sqlite3DeleteTable(db, pParse->pNewTable); 712 } 713 if( !IN_RENAME_OBJECT ){ 714 sqlite3DeleteTrigger(db, pParse->pNewTrigger); 715 } 716 717 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree); 718 sqlite3DbFree(db, pParse->pVList); 719 while( pParse->pAinc ){ 720 AutoincInfo *p = pParse->pAinc; 721 pParse->pAinc = p->pNext; 722 sqlite3DbFreeNN(db, p); 723 } 724 while( pParse->pZombieTab ){ 725 Table *p = pParse->pZombieTab; 726 pParse->pZombieTab = p->pNextZombie; 727 sqlite3DeleteTable(db, p); 728 } 729 db->pParse = pParse->pParentParse; 730 pParse->pParentParse = 0; 731 assert( nErr==0 || pParse->rc!=SQLITE_OK ); 732 return nErr; 733 } 734 735 736 #ifdef SQLITE_ENABLE_NORMALIZE 737 /* 738 ** Insert a single space character into pStr if the current string 739 ** ends with an identifier 740 */ 741 static void addSpaceSeparator(sqlite3_str *pStr){ 742 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){ 743 sqlite3_str_append(pStr, " ", 1); 744 } 745 } 746 747 /* 748 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return 749 ** the normalization in space obtained from sqlite3DbMalloc(). Or return 750 ** NULL if anything goes wrong or if zSql is NULL. 751 */ 752 char *sqlite3Normalize( 753 Vdbe *pVdbe, /* VM being reprepared */ 754 const char *zSql /* The original SQL string */ 755 ){ 756 sqlite3 *db; /* The database connection */ 757 int i; /* Next unread byte of zSql[] */ 758 int n; /* length of current token */ 759 int tokenType; /* type of current token */ 760 int prevType = 0; /* Previous non-whitespace token */ 761 int nParen; /* Number of nested levels of parentheses */ 762 int iStartIN; /* Start of RHS of IN operator in z[] */ 763 int nParenAtIN; /* Value of nParent at start of RHS of IN operator */ 764 u32 j; /* Bytes of normalized SQL generated so far */ 765 sqlite3_str *pStr; /* The normalized SQL string under construction */ 766 767 db = sqlite3VdbeDb(pVdbe); 768 tokenType = -1; 769 nParen = iStartIN = nParenAtIN = 0; 770 pStr = sqlite3_str_new(db); 771 assert( pStr!=0 ); /* sqlite3_str_new() never returns NULL */ 772 for(i=0; zSql[i] && pStr->accError==0; i+=n){ 773 if( tokenType!=TK_SPACE ){ 774 prevType = tokenType; 775 } 776 n = sqlite3GetToken((unsigned char*)zSql+i, &tokenType); 777 if( NEVER(n<=0) ) break; 778 switch( tokenType ){ 779 case TK_SPACE: { 780 break; 781 } 782 case TK_NULL: { 783 if( prevType==TK_IS || prevType==TK_NOT ){ 784 sqlite3_str_append(pStr, " NULL", 5); 785 break; 786 } 787 /* Fall through */ 788 } 789 case TK_STRING: 790 case TK_INTEGER: 791 case TK_FLOAT: 792 case TK_VARIABLE: 793 case TK_BLOB: { 794 sqlite3_str_append(pStr, "?", 1); 795 break; 796 } 797 case TK_LP: { 798 nParen++; 799 if( prevType==TK_IN ){ 800 iStartIN = pStr->nChar; 801 nParenAtIN = nParen; 802 } 803 sqlite3_str_append(pStr, "(", 1); 804 break; 805 } 806 case TK_RP: { 807 if( iStartIN>0 && nParen==nParenAtIN ){ 808 assert( pStr->nChar>=(u32)iStartIN ); 809 pStr->nChar = iStartIN+1; 810 sqlite3_str_append(pStr, "?,?,?", 5); 811 iStartIN = 0; 812 } 813 nParen--; 814 sqlite3_str_append(pStr, ")", 1); 815 break; 816 } 817 case TK_ID: { 818 iStartIN = 0; 819 j = pStr->nChar; 820 if( sqlite3Isquote(zSql[i]) ){ 821 char *zId = sqlite3DbStrNDup(db, zSql+i, n); 822 int nId; 823 int eType = 0; 824 if( zId==0 ) break; 825 sqlite3Dequote(zId); 826 if( zSql[i]=='"' && sqlite3VdbeUsesDoubleQuotedString(pVdbe, zId) ){ 827 sqlite3_str_append(pStr, "?", 1); 828 sqlite3DbFree(db, zId); 829 break; 830 } 831 nId = sqlite3Strlen30(zId); 832 if( sqlite3GetToken((u8*)zId, &eType)==nId && eType==TK_ID ){ 833 addSpaceSeparator(pStr); 834 sqlite3_str_append(pStr, zId, nId); 835 }else{ 836 sqlite3_str_appendf(pStr, "\"%w\"", zId); 837 } 838 sqlite3DbFree(db, zId); 839 }else{ 840 addSpaceSeparator(pStr); 841 sqlite3_str_append(pStr, zSql+i, n); 842 } 843 while( j<pStr->nChar ){ 844 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]); 845 j++; 846 } 847 break; 848 } 849 case TK_SELECT: { 850 iStartIN = 0; 851 /* fall through */ 852 } 853 default: { 854 if( sqlite3IsIdChar(zSql[i]) ) addSpaceSeparator(pStr); 855 j = pStr->nChar; 856 sqlite3_str_append(pStr, zSql+i, n); 857 while( j<pStr->nChar ){ 858 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]); 859 j++; 860 } 861 break; 862 } 863 } 864 } 865 if( tokenType!=TK_SEMI ) sqlite3_str_append(pStr, ";", 1); 866 return sqlite3_str_finish(pStr); 867 } 868 #endif /* SQLITE_ENABLE_NORMALIZE */ 869