OLD | NEW |
(Empty) | |
| 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_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */ |
| 31 #define CC_ID 2 /* unicode characters usable in IDs */ |
| 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_ILLEGAL 27 /* Illegal character */ |
| 57 |
| 58 static const unsigned char aiClass[] = { |
| 59 #ifdef SQLITE_ASCII |
| 60 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
| 61 /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27, |
| 62 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 63 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16, |
| 64 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6, |
| 65 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 66 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1, |
| 67 /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 68 /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27, |
| 69 /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 70 /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 71 /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 72 /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 73 /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 74 /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 75 /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 76 /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 |
| 77 #endif |
| 78 #ifdef SQLITE_EBCDIC |
| 79 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ |
| 80 /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27, |
| 81 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 82 /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 83 /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, |
| 84 /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10, |
| 85 /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27, |
| 86 /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 6, |
| 87 /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8, |
| 88 /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 89 /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 90 /* Ax */ 27, 25, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, |
| 91 /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27, |
| 92 /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 93 /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27, |
| 94 /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27, |
| 95 /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27, |
| 96 #endif |
| 97 }; |
| 98 |
| 99 /* |
| 100 ** The charMap() macro maps alphabetic characters (only) into their |
| 101 ** lower-case ASCII equivalent. On ASCII machines, this is just |
| 102 ** an upper-to-lower case map. On EBCDIC machines we also need |
| 103 ** to adjust the encoding. The mapping is only valid for alphabetics |
| 104 ** which are the only characters for which this feature is used. |
| 105 ** |
| 106 ** Used by keywordhash.h |
| 107 */ |
| 108 #ifdef SQLITE_ASCII |
| 109 # define charMap(X) sqlite3UpperToLower[(unsigned char)X] |
| 110 #endif |
| 111 #ifdef SQLITE_EBCDIC |
| 112 # define charMap(X) ebcdicToAscii[(unsigned char)X] |
| 113 const unsigned char ebcdicToAscii[] = { |
| 114 /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ |
| 115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ |
| 116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ |
| 117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ |
| 118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ |
| 119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ |
| 121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ |
| 122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 123 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ |
| 124 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ |
| 125 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ |
| 126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 127 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ |
| 128 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ |
| 129 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ |
| 130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ |
| 131 }; |
| 132 #endif |
| 133 |
| 134 /* |
| 135 ** The sqlite3KeywordCode function looks up an identifier to determine if |
| 136 ** it is a keyword. If it is a keyword, the token code of that keyword is |
| 137 ** returned. If the input is not a keyword, TK_ID is returned. |
| 138 ** |
| 139 ** The implementation of this routine was generated by a program, |
| 140 ** mkkeywordhash.c, located in the tool subdirectory of the distribution. |
| 141 ** The output of the mkkeywordhash.c program is written into a file |
| 142 ** named keywordhash.h and then included into this source file by |
| 143 ** the #include below. |
| 144 */ |
| 145 #include "keywordhash.h" |
| 146 |
| 147 |
| 148 /* |
| 149 ** If X is a character that can be used in an identifier then |
| 150 ** IdChar(X) will be true. Otherwise it is false. |
| 151 ** |
| 152 ** For ASCII, any character with the high-order bit set is |
| 153 ** allowed in an identifier. For 7-bit characters, |
| 154 ** sqlite3IsIdChar[X] must be 1. |
| 155 ** |
| 156 ** For EBCDIC, the rules are more complex but have the same |
| 157 ** end result. |
| 158 ** |
| 159 ** Ticket #1066. the SQL standard does not allow '$' in the |
| 160 ** middle of identifiers. But many SQL implementations do. |
| 161 ** SQLite will allow '$' in identifiers for compatibility. |
| 162 ** But the feature is undocumented. |
| 163 */ |
| 164 #ifdef SQLITE_ASCII |
| 165 #define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) |
| 166 #endif |
| 167 #ifdef SQLITE_EBCDIC |
| 168 const char sqlite3IsEbcdicIdChar[] = { |
| 169 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ |
| 170 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ |
| 171 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ |
| 172 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ |
| 173 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ |
| 174 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ |
| 175 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ |
| 176 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ |
| 177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ |
| 178 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ |
| 179 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ |
| 180 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ |
| 181 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ |
| 182 }; |
| 183 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) |
| 184 #endif |
| 185 |
| 186 /* Make the IdChar function accessible from ctime.c */ |
| 187 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS |
| 188 int sqlite3IsIdChar(u8 c){ return IdChar(c); } |
| 189 #endif |
| 190 |
| 191 |
| 192 /* |
| 193 ** Return the length (in bytes) of the token that begins at z[0]. |
| 194 ** Store the token type in *tokenType before returning. |
| 195 */ |
| 196 int sqlite3GetToken(const unsigned char *z, int *tokenType){ |
| 197 int i, c; |
| 198 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte |
| 199 ** of the token. See the comment on the CC_ defines |
| 200 ** above. */ |
| 201 case CC_SPACE: { |
| 202 testcase( z[0]==' ' ); |
| 203 testcase( z[0]=='\t' ); |
| 204 testcase( z[0]=='\n' ); |
| 205 testcase( z[0]=='\f' ); |
| 206 testcase( z[0]=='\r' ); |
| 207 for(i=1; sqlite3Isspace(z[i]); i++){} |
| 208 *tokenType = TK_SPACE; |
| 209 return i; |
| 210 } |
| 211 case CC_MINUS: { |
| 212 if( z[1]=='-' ){ |
| 213 for(i=2; (c=z[i])!=0 && c!='\n'; i++){} |
| 214 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
| 215 return i; |
| 216 } |
| 217 *tokenType = TK_MINUS; |
| 218 return 1; |
| 219 } |
| 220 case CC_LP: { |
| 221 *tokenType = TK_LP; |
| 222 return 1; |
| 223 } |
| 224 case CC_RP: { |
| 225 *tokenType = TK_RP; |
| 226 return 1; |
| 227 } |
| 228 case CC_SEMI: { |
| 229 *tokenType = TK_SEMI; |
| 230 return 1; |
| 231 } |
| 232 case CC_PLUS: { |
| 233 *tokenType = TK_PLUS; |
| 234 return 1; |
| 235 } |
| 236 case CC_STAR: { |
| 237 *tokenType = TK_STAR; |
| 238 return 1; |
| 239 } |
| 240 case CC_SLASH: { |
| 241 if( z[1]!='*' || z[2]==0 ){ |
| 242 *tokenType = TK_SLASH; |
| 243 return 1; |
| 244 } |
| 245 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} |
| 246 if( c ) i++; |
| 247 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ |
| 248 return i; |
| 249 } |
| 250 case CC_PERCENT: { |
| 251 *tokenType = TK_REM; |
| 252 return 1; |
| 253 } |
| 254 case CC_EQ: { |
| 255 *tokenType = TK_EQ; |
| 256 return 1 + (z[1]=='='); |
| 257 } |
| 258 case CC_LT: { |
| 259 if( (c=z[1])=='=' ){ |
| 260 *tokenType = TK_LE; |
| 261 return 2; |
| 262 }else if( c=='>' ){ |
| 263 *tokenType = TK_NE; |
| 264 return 2; |
| 265 }else if( c=='<' ){ |
| 266 *tokenType = TK_LSHIFT; |
| 267 return 2; |
| 268 }else{ |
| 269 *tokenType = TK_LT; |
| 270 return 1; |
| 271 } |
| 272 } |
| 273 case CC_GT: { |
| 274 if( (c=z[1])=='=' ){ |
| 275 *tokenType = TK_GE; |
| 276 return 2; |
| 277 }else if( c=='>' ){ |
| 278 *tokenType = TK_RSHIFT; |
| 279 return 2; |
| 280 }else{ |
| 281 *tokenType = TK_GT; |
| 282 return 1; |
| 283 } |
| 284 } |
| 285 case CC_BANG: { |
| 286 if( z[1]!='=' ){ |
| 287 *tokenType = TK_ILLEGAL; |
| 288 return 1; |
| 289 }else{ |
| 290 *tokenType = TK_NE; |
| 291 return 2; |
| 292 } |
| 293 } |
| 294 case CC_PIPE: { |
| 295 if( z[1]!='|' ){ |
| 296 *tokenType = TK_BITOR; |
| 297 return 1; |
| 298 }else{ |
| 299 *tokenType = TK_CONCAT; |
| 300 return 2; |
| 301 } |
| 302 } |
| 303 case CC_COMMA: { |
| 304 *tokenType = TK_COMMA; |
| 305 return 1; |
| 306 } |
| 307 case CC_AND: { |
| 308 *tokenType = TK_BITAND; |
| 309 return 1; |
| 310 } |
| 311 case CC_TILDA: { |
| 312 *tokenType = TK_BITNOT; |
| 313 return 1; |
| 314 } |
| 315 case CC_QUOTE: { |
| 316 int delim = z[0]; |
| 317 testcase( delim=='`' ); |
| 318 testcase( delim=='\'' ); |
| 319 testcase( delim=='"' ); |
| 320 for(i=1; (c=z[i])!=0; i++){ |
| 321 if( c==delim ){ |
| 322 if( z[i+1]==delim ){ |
| 323 i++; |
| 324 }else{ |
| 325 break; |
| 326 } |
| 327 } |
| 328 } |
| 329 if( c=='\'' ){ |
| 330 *tokenType = TK_STRING; |
| 331 return i+1; |
| 332 }else if( c!=0 ){ |
| 333 *tokenType = TK_ID; |
| 334 return i+1; |
| 335 }else{ |
| 336 *tokenType = TK_ILLEGAL; |
| 337 return i; |
| 338 } |
| 339 } |
| 340 case CC_DOT: { |
| 341 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 342 if( !sqlite3Isdigit(z[1]) ) |
| 343 #endif |
| 344 { |
| 345 *tokenType = TK_DOT; |
| 346 return 1; |
| 347 } |
| 348 /* If the next character is a digit, this is a floating point |
| 349 ** number that begins with ".". Fall thru into the next case */ |
| 350 } |
| 351 case CC_DIGIT: { |
| 352 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); |
| 353 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); |
| 354 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); |
| 355 testcase( z[0]=='9' ); |
| 356 *tokenType = TK_INTEGER; |
| 357 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 358 if( z[0]=='0' && (z[1]=='x' || z[1]=='X') && sqlite3Isxdigit(z[2]) ){ |
| 359 for(i=3; sqlite3Isxdigit(z[i]); i++){} |
| 360 return i; |
| 361 } |
| 362 #endif |
| 363 for(i=0; sqlite3Isdigit(z[i]); i++){} |
| 364 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 365 if( z[i]=='.' ){ |
| 366 i++; |
| 367 while( sqlite3Isdigit(z[i]) ){ i++; } |
| 368 *tokenType = TK_FLOAT; |
| 369 } |
| 370 if( (z[i]=='e' || z[i]=='E') && |
| 371 ( sqlite3Isdigit(z[i+1]) |
| 372 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) |
| 373 ) |
| 374 ){ |
| 375 i += 2; |
| 376 while( sqlite3Isdigit(z[i]) ){ i++; } |
| 377 *tokenType = TK_FLOAT; |
| 378 } |
| 379 #endif |
| 380 while( IdChar(z[i]) ){ |
| 381 *tokenType = TK_ILLEGAL; |
| 382 i++; |
| 383 } |
| 384 return i; |
| 385 } |
| 386 case CC_QUOTE2: { |
| 387 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} |
| 388 *tokenType = c==']' ? TK_ID : TK_ILLEGAL; |
| 389 return i; |
| 390 } |
| 391 case CC_VARNUM: { |
| 392 *tokenType = TK_VARIABLE; |
| 393 for(i=1; sqlite3Isdigit(z[i]); i++){} |
| 394 return i; |
| 395 } |
| 396 case CC_DOLLAR: |
| 397 case CC_VARALPHA: { |
| 398 int n = 0; |
| 399 testcase( z[0]=='$' ); testcase( z[0]=='@' ); |
| 400 testcase( z[0]==':' ); testcase( z[0]=='#' ); |
| 401 *tokenType = TK_VARIABLE; |
| 402 for(i=1; (c=z[i])!=0; i++){ |
| 403 if( IdChar(c) ){ |
| 404 n++; |
| 405 #ifndef SQLITE_OMIT_TCL_VARIABLE |
| 406 }else if( c=='(' && n>0 ){ |
| 407 do{ |
| 408 i++; |
| 409 }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); |
| 410 if( c==')' ){ |
| 411 i++; |
| 412 }else{ |
| 413 *tokenType = TK_ILLEGAL; |
| 414 } |
| 415 break; |
| 416 }else if( c==':' && z[i+1]==':' ){ |
| 417 i++; |
| 418 #endif |
| 419 }else{ |
| 420 break; |
| 421 } |
| 422 } |
| 423 if( n==0 ) *tokenType = TK_ILLEGAL; |
| 424 return i; |
| 425 } |
| 426 case CC_KYWD: { |
| 427 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){} |
| 428 if( IdChar(z[i]) ){ |
| 429 /* This token started out using characters that can appear in keywords, |
| 430 ** but z[i] is a character not allowed within keywords, so this must |
| 431 ** be an identifier instead */ |
| 432 i++; |
| 433 break; |
| 434 } |
| 435 *tokenType = TK_ID; |
| 436 return keywordCode((char*)z, i, tokenType); |
| 437 } |
| 438 case CC_X: { |
| 439 #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 440 testcase( z[0]=='x' ); testcase( z[0]=='X' ); |
| 441 if( z[1]=='\'' ){ |
| 442 *tokenType = TK_BLOB; |
| 443 for(i=2; sqlite3Isxdigit(z[i]); i++){} |
| 444 if( z[i]!='\'' || i%2 ){ |
| 445 *tokenType = TK_ILLEGAL; |
| 446 while( z[i] && z[i]!='\'' ){ i++; } |
| 447 } |
| 448 if( z[i] ) i++; |
| 449 return i; |
| 450 } |
| 451 #endif |
| 452 /* If it is not a BLOB literal, then it must be an ID, since no |
| 453 ** SQL keywords start with the letter 'x'. Fall through */ |
| 454 } |
| 455 case CC_ID: { |
| 456 i = 1; |
| 457 break; |
| 458 } |
| 459 default: { |
| 460 *tokenType = TK_ILLEGAL; |
| 461 return 1; |
| 462 } |
| 463 } |
| 464 while( IdChar(z[i]) ){ i++; } |
| 465 *tokenType = TK_ID; |
| 466 return i; |
| 467 } |
| 468 |
| 469 /* |
| 470 ** Run the parser on the given SQL string. The parser structure is |
| 471 ** passed in. An SQLITE_ status code is returned. If an error occurs |
| 472 ** then an and attempt is made to write an error message into |
| 473 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that |
| 474 ** error message. |
| 475 */ |
| 476 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ |
| 477 int nErr = 0; /* Number of errors encountered */ |
| 478 int i; /* Loop counter */ |
| 479 void *pEngine; /* The LEMON-generated LALR(1) parser */ |
| 480 int tokenType; /* type of the next token */ |
| 481 int lastTokenParsed = -1; /* type of the previous token */ |
| 482 sqlite3 *db = pParse->db; /* The database connection */ |
| 483 int mxSqlLen; /* Max length of an SQL string */ |
| 484 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 485 unsigned char zSpace[sizeof(yyParser)]; /* Space for parser engine object */ |
| 486 #endif |
| 487 |
| 488 assert( zSql!=0 ); |
| 489 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; |
| 490 if( db->nVdbeActive==0 ){ |
| 491 db->u1.isInterrupted = 0; |
| 492 } |
| 493 pParse->rc = SQLITE_OK; |
| 494 pParse->zTail = zSql; |
| 495 i = 0; |
| 496 assert( pzErrMsg!=0 ); |
| 497 /* sqlite3ParserTrace(stdout, "parser: "); */ |
| 498 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 499 pEngine = zSpace; |
| 500 sqlite3ParserInit(pEngine); |
| 501 #else |
| 502 pEngine = sqlite3ParserAlloc(sqlite3Malloc); |
| 503 if( pEngine==0 ){ |
| 504 sqlite3OomFault(db); |
| 505 return SQLITE_NOMEM_BKPT; |
| 506 } |
| 507 #endif |
| 508 assert( pParse->pNewTable==0 ); |
| 509 assert( pParse->pNewTrigger==0 ); |
| 510 assert( pParse->nVar==0 ); |
| 511 assert( pParse->pVList==0 ); |
| 512 while( 1 ){ |
| 513 assert( i>=0 ); |
| 514 if( zSql[i]!=0 ){ |
| 515 pParse->sLastToken.z = &zSql[i]; |
| 516 pParse->sLastToken.n = sqlite3GetToken((u8*)&zSql[i],&tokenType); |
| 517 i += pParse->sLastToken.n; |
| 518 if( i>mxSqlLen ){ |
| 519 pParse->rc = SQLITE_TOOBIG; |
| 520 break; |
| 521 } |
| 522 }else{ |
| 523 /* Upon reaching the end of input, call the parser two more times |
| 524 ** with tokens TK_SEMI and 0, in that order. */ |
| 525 if( lastTokenParsed==TK_SEMI ){ |
| 526 tokenType = 0; |
| 527 }else if( lastTokenParsed==0 ){ |
| 528 break; |
| 529 }else{ |
| 530 tokenType = TK_SEMI; |
| 531 } |
| 532 } |
| 533 if( tokenType>=TK_SPACE ){ |
| 534 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL ); |
| 535 if( db->u1.isInterrupted ){ |
| 536 pParse->rc = SQLITE_INTERRUPT; |
| 537 break; |
| 538 } |
| 539 if( tokenType==TK_ILLEGAL ){ |
| 540 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", |
| 541 &pParse->sLastToken); |
| 542 break; |
| 543 } |
| 544 }else{ |
| 545 sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); |
| 546 lastTokenParsed = tokenType; |
| 547 if( pParse->rc!=SQLITE_OK || db->mallocFailed ) break; |
| 548 } |
| 549 } |
| 550 assert( nErr==0 ); |
| 551 pParse->zTail = &zSql[i]; |
| 552 #ifdef YYTRACKMAXSTACKDEPTH |
| 553 sqlite3_mutex_enter(sqlite3MallocMutex()); |
| 554 sqlite3StatusHighwater(SQLITE_STATUS_PARSER_STACK, |
| 555 sqlite3ParserStackPeak(pEngine) |
| 556 ); |
| 557 sqlite3_mutex_leave(sqlite3MallocMutex()); |
| 558 #endif /* YYDEBUG */ |
| 559 #ifdef sqlite3Parser_ENGINEALWAYSONSTACK |
| 560 sqlite3ParserFinalize(pEngine); |
| 561 #else |
| 562 sqlite3ParserFree(pEngine, sqlite3_free); |
| 563 #endif |
| 564 if( db->mallocFailed ){ |
| 565 pParse->rc = SQLITE_NOMEM_BKPT; |
| 566 } |
| 567 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ |
| 568 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc)); |
| 569 } |
| 570 assert( pzErrMsg!=0 ); |
| 571 if( pParse->zErrMsg ){ |
| 572 *pzErrMsg = pParse->zErrMsg; |
| 573 sqlite3_log(pParse->rc, "%s", *pzErrMsg); |
| 574 pParse->zErrMsg = 0; |
| 575 nErr++; |
| 576 } |
| 577 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ |
| 578 sqlite3VdbeDelete(pParse->pVdbe); |
| 579 pParse->pVdbe = 0; |
| 580 } |
| 581 #ifndef SQLITE_OMIT_SHARED_CACHE |
| 582 if( pParse->nested==0 ){ |
| 583 sqlite3DbFree(db, pParse->aTableLock); |
| 584 pParse->aTableLock = 0; |
| 585 pParse->nTableLock = 0; |
| 586 } |
| 587 #endif |
| 588 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 589 sqlite3_free(pParse->apVtabLock); |
| 590 #endif |
| 591 |
| 592 if( !IN_DECLARE_VTAB ){ |
| 593 /* If the pParse->declareVtab flag is set, do not delete any table |
| 594 ** structure built up in pParse->pNewTable. The calling code (see vtab.c) |
| 595 ** will take responsibility for freeing the Table structure. |
| 596 */ |
| 597 sqlite3DeleteTable(db, pParse->pNewTable); |
| 598 } |
| 599 |
| 600 if( pParse->pWithToFree ) sqlite3WithDelete(db, pParse->pWithToFree); |
| 601 sqlite3DeleteTrigger(db, pParse->pNewTrigger); |
| 602 sqlite3DbFree(db, pParse->pVList); |
| 603 while( pParse->pAinc ){ |
| 604 AutoincInfo *p = pParse->pAinc; |
| 605 pParse->pAinc = p->pNext; |
| 606 sqlite3DbFree(db, p); |
| 607 } |
| 608 while( pParse->pZombieTab ){ |
| 609 Table *p = pParse->pZombieTab; |
| 610 pParse->pZombieTab = p->pNextZombie; |
| 611 sqlite3DeleteTable(db, p); |
| 612 } |
| 613 assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
| 614 return nErr; |
| 615 } |
OLD | NEW |