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