| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ** Compile and run this standalone program in order to generate code that | |
| 3 ** implements a function that will translate alphabetic identifiers into | |
| 4 ** parser token codes. | |
| 5 */ | |
| 6 #include <stdio.h> | |
| 7 #include <string.h> | |
| 8 #include <stdlib.h> | |
| 9 #include <assert.h> | |
| 10 | |
| 11 /* | |
| 12 ** A header comment placed at the beginning of generated code. | |
| 13 */ | |
| 14 static const char zHdr[] = | |
| 15 "/***** This file contains automatically generated code ******\n" | |
| 16 "**\n" | |
| 17 "** The code in this file has been automatically generated by\n" | |
| 18 "**\n" | |
| 19 "** sqlite/tool/mkkeywordhash.c\n" | |
| 20 "**\n" | |
| 21 "** The code in this file implements a function that determines whether\n" | |
| 22 "** or not a given identifier is really an SQL keyword. The same thing\n" | |
| 23 "** might be implemented more directly using a hand-written hash table.\n" | |
| 24 "** But by using this automatically generated code, the size of the code\n" | |
| 25 "** is substantially reduced. This is important for embedded applications\n" | |
| 26 "** on platforms with limited memory.\n" | |
| 27 "*/\n" | |
| 28 ; | |
| 29 | |
| 30 /* | |
| 31 ** All the keywords of the SQL language are stored in a hash | |
| 32 ** table composed of instances of the following structure. | |
| 33 */ | |
| 34 typedef struct Keyword Keyword; | |
| 35 struct Keyword { | |
| 36 char *zName; /* The keyword name */ | |
| 37 char *zTokenType; /* Token value for this keyword */ | |
| 38 int mask; /* Code this keyword if non-zero */ | |
| 39 int id; /* Unique ID for this record */ | |
| 40 int hash; /* Hash on the keyword */ | |
| 41 int offset; /* Offset to start of name string */ | |
| 42 int len; /* Length of this keyword, not counting final \000 */ | |
| 43 int prefix; /* Number of characters in prefix */ | |
| 44 int longestSuffix; /* Longest suffix that is a prefix on another word */ | |
| 45 int iNext; /* Index in aKeywordTable[] of next with same hash */ | |
| 46 int substrId; /* Id to another keyword this keyword is embedded in */ | |
| 47 int substrOffset; /* Offset into substrId for start of this keyword */ | |
| 48 char zOrigName[20]; /* Original keyword name before processing */ | |
| 49 }; | |
| 50 | |
| 51 /* | |
| 52 ** Define masks used to determine which keywords are allowed | |
| 53 */ | |
| 54 #ifdef SQLITE_OMIT_ALTERTABLE | |
| 55 # define ALTER 0 | |
| 56 #else | |
| 57 # define ALTER 0x00000001 | |
| 58 #endif | |
| 59 #define ALWAYS 0x00000002 | |
| 60 #ifdef SQLITE_OMIT_ANALYZE | |
| 61 # define ANALYZE 0 | |
| 62 #else | |
| 63 # define ANALYZE 0x00000004 | |
| 64 #endif | |
| 65 #ifdef SQLITE_OMIT_ATTACH | |
| 66 # define ATTACH 0 | |
| 67 #else | |
| 68 # define ATTACH 0x00000008 | |
| 69 #endif | |
| 70 #ifdef SQLITE_OMIT_AUTOINCREMENT | |
| 71 # define AUTOINCR 0 | |
| 72 #else | |
| 73 # define AUTOINCR 0x00000010 | |
| 74 #endif | |
| 75 #ifdef SQLITE_OMIT_CAST | |
| 76 # define CAST 0 | |
| 77 #else | |
| 78 # define CAST 0x00000020 | |
| 79 #endif | |
| 80 #ifdef SQLITE_OMIT_COMPOUND_SELECT | |
| 81 # define COMPOUND 0 | |
| 82 #else | |
| 83 # define COMPOUND 0x00000040 | |
| 84 #endif | |
| 85 #ifdef SQLITE_OMIT_CONFLICT_CLAUSE | |
| 86 # define CONFLICT 0 | |
| 87 #else | |
| 88 # define CONFLICT 0x00000080 | |
| 89 #endif | |
| 90 #ifdef SQLITE_OMIT_EXPLAIN | |
| 91 # define EXPLAIN 0 | |
| 92 #else | |
| 93 # define EXPLAIN 0x00000100 | |
| 94 #endif | |
| 95 #ifdef SQLITE_OMIT_FOREIGN_KEY | |
| 96 # define FKEY 0 | |
| 97 #else | |
| 98 # define FKEY 0x00000200 | |
| 99 #endif | |
| 100 #ifdef SQLITE_OMIT_PRAGMA | |
| 101 # define PRAGMA 0 | |
| 102 #else | |
| 103 # define PRAGMA 0x00000400 | |
| 104 #endif | |
| 105 #ifdef SQLITE_OMIT_REINDEX | |
| 106 # define REINDEX 0 | |
| 107 #else | |
| 108 # define REINDEX 0x00000800 | |
| 109 #endif | |
| 110 #ifdef SQLITE_OMIT_SUBQUERY | |
| 111 # define SUBQUERY 0 | |
| 112 #else | |
| 113 # define SUBQUERY 0x00001000 | |
| 114 #endif | |
| 115 #ifdef SQLITE_OMIT_TRIGGER | |
| 116 # define TRIGGER 0 | |
| 117 #else | |
| 118 # define TRIGGER 0x00002000 | |
| 119 #endif | |
| 120 #if defined(SQLITE_OMIT_AUTOVACUUM) && \ | |
| 121 (defined(SQLITE_OMIT_VACUUM) || defined(SQLITE_OMIT_ATTACH)) | |
| 122 # define VACUUM 0 | |
| 123 #else | |
| 124 # define VACUUM 0x00004000 | |
| 125 #endif | |
| 126 #ifdef SQLITE_OMIT_VIEW | |
| 127 # define VIEW 0 | |
| 128 #else | |
| 129 # define VIEW 0x00008000 | |
| 130 #endif | |
| 131 #ifdef SQLITE_OMIT_VIRTUALTABLE | |
| 132 # define VTAB 0 | |
| 133 #else | |
| 134 # define VTAB 0x00010000 | |
| 135 #endif | |
| 136 #ifdef SQLITE_OMIT_AUTOVACUUM | |
| 137 # define AUTOVACUUM 0 | |
| 138 #else | |
| 139 # define AUTOVACUUM 0x00020000 | |
| 140 #endif | |
| 141 #ifdef SQLITE_OMIT_CTE | |
| 142 # define CTE 0 | |
| 143 #else | |
| 144 # define CTE 0x00040000 | |
| 145 #endif | |
| 146 | |
| 147 /* | |
| 148 ** These are the keywords | |
| 149 */ | |
| 150 static Keyword aKeywordTable[] = { | |
| 151 { "ABORT", "TK_ABORT", CONFLICT|TRIGGER }, | |
| 152 { "ACTION", "TK_ACTION", FKEY }, | |
| 153 { "ADD", "TK_ADD", ALTER }, | |
| 154 { "AFTER", "TK_AFTER", TRIGGER }, | |
| 155 { "ALL", "TK_ALL", ALWAYS }, | |
| 156 { "ALTER", "TK_ALTER", ALTER }, | |
| 157 { "ANALYZE", "TK_ANALYZE", ANALYZE }, | |
| 158 { "AND", "TK_AND", ALWAYS }, | |
| 159 { "AS", "TK_AS", ALWAYS }, | |
| 160 { "ASC", "TK_ASC", ALWAYS }, | |
| 161 { "ATTACH", "TK_ATTACH", ATTACH }, | |
| 162 { "AUTOINCREMENT", "TK_AUTOINCR", AUTOINCR }, | |
| 163 { "BEFORE", "TK_BEFORE", TRIGGER }, | |
| 164 { "BEGIN", "TK_BEGIN", ALWAYS }, | |
| 165 { "BETWEEN", "TK_BETWEEN", ALWAYS }, | |
| 166 { "BY", "TK_BY", ALWAYS }, | |
| 167 { "CASCADE", "TK_CASCADE", FKEY }, | |
| 168 { "CASE", "TK_CASE", ALWAYS }, | |
| 169 { "CAST", "TK_CAST", CAST }, | |
| 170 { "CHECK", "TK_CHECK", ALWAYS }, | |
| 171 { "COLLATE", "TK_COLLATE", ALWAYS }, | |
| 172 { "COLUMN", "TK_COLUMNKW", ALTER }, | |
| 173 { "COMMIT", "TK_COMMIT", ALWAYS }, | |
| 174 { "CONFLICT", "TK_CONFLICT", CONFLICT }, | |
| 175 { "CONSTRAINT", "TK_CONSTRAINT", ALWAYS }, | |
| 176 { "CREATE", "TK_CREATE", ALWAYS }, | |
| 177 { "CROSS", "TK_JOIN_KW", ALWAYS }, | |
| 178 { "CURRENT_DATE", "TK_CTIME_KW", ALWAYS }, | |
| 179 { "CURRENT_TIME", "TK_CTIME_KW", ALWAYS }, | |
| 180 { "CURRENT_TIMESTAMP","TK_CTIME_KW", ALWAYS }, | |
| 181 { "DATABASE", "TK_DATABASE", ATTACH }, | |
| 182 { "DEFAULT", "TK_DEFAULT", ALWAYS }, | |
| 183 { "DEFERRED", "TK_DEFERRED", ALWAYS }, | |
| 184 { "DEFERRABLE", "TK_DEFERRABLE", FKEY }, | |
| 185 { "DELETE", "TK_DELETE", ALWAYS }, | |
| 186 { "DESC", "TK_DESC", ALWAYS }, | |
| 187 { "DETACH", "TK_DETACH", ATTACH }, | |
| 188 { "DISTINCT", "TK_DISTINCT", ALWAYS }, | |
| 189 { "DROP", "TK_DROP", ALWAYS }, | |
| 190 { "END", "TK_END", ALWAYS }, | |
| 191 { "EACH", "TK_EACH", TRIGGER }, | |
| 192 { "ELSE", "TK_ELSE", ALWAYS }, | |
| 193 { "ESCAPE", "TK_ESCAPE", ALWAYS }, | |
| 194 { "EXCEPT", "TK_EXCEPT", COMPOUND }, | |
| 195 { "EXCLUSIVE", "TK_EXCLUSIVE", ALWAYS }, | |
| 196 { "EXISTS", "TK_EXISTS", ALWAYS }, | |
| 197 { "EXPLAIN", "TK_EXPLAIN", EXPLAIN }, | |
| 198 { "FAIL", "TK_FAIL", CONFLICT|TRIGGER }, | |
| 199 { "FOR", "TK_FOR", TRIGGER }, | |
| 200 { "FOREIGN", "TK_FOREIGN", FKEY }, | |
| 201 { "FROM", "TK_FROM", ALWAYS }, | |
| 202 { "FULL", "TK_JOIN_KW", ALWAYS }, | |
| 203 { "GLOB", "TK_LIKE_KW", ALWAYS }, | |
| 204 { "GROUP", "TK_GROUP", ALWAYS }, | |
| 205 { "HAVING", "TK_HAVING", ALWAYS }, | |
| 206 { "IF", "TK_IF", ALWAYS }, | |
| 207 { "IGNORE", "TK_IGNORE", CONFLICT|TRIGGER }, | |
| 208 { "IMMEDIATE", "TK_IMMEDIATE", ALWAYS }, | |
| 209 { "IN", "TK_IN", ALWAYS }, | |
| 210 { "INDEX", "TK_INDEX", ALWAYS }, | |
| 211 { "INDEXED", "TK_INDEXED", ALWAYS }, | |
| 212 { "INITIALLY", "TK_INITIALLY", FKEY }, | |
| 213 { "INNER", "TK_JOIN_KW", ALWAYS }, | |
| 214 { "INSERT", "TK_INSERT", ALWAYS }, | |
| 215 { "INSTEAD", "TK_INSTEAD", TRIGGER }, | |
| 216 { "INTERSECT", "TK_INTERSECT", COMPOUND }, | |
| 217 { "INTO", "TK_INTO", ALWAYS }, | |
| 218 { "IS", "TK_IS", ALWAYS }, | |
| 219 { "ISNULL", "TK_ISNULL", ALWAYS }, | |
| 220 { "JOIN", "TK_JOIN", ALWAYS }, | |
| 221 { "KEY", "TK_KEY", ALWAYS }, | |
| 222 { "LEFT", "TK_JOIN_KW", ALWAYS }, | |
| 223 { "LIKE", "TK_LIKE_KW", ALWAYS }, | |
| 224 { "LIMIT", "TK_LIMIT", ALWAYS }, | |
| 225 { "MATCH", "TK_MATCH", ALWAYS }, | |
| 226 { "NATURAL", "TK_JOIN_KW", ALWAYS }, | |
| 227 { "NO", "TK_NO", FKEY }, | |
| 228 { "NOT", "TK_NOT", ALWAYS }, | |
| 229 { "NOTNULL", "TK_NOTNULL", ALWAYS }, | |
| 230 { "NULL", "TK_NULL", ALWAYS }, | |
| 231 { "OF", "TK_OF", ALWAYS }, | |
| 232 { "OFFSET", "TK_OFFSET", ALWAYS }, | |
| 233 { "ON", "TK_ON", ALWAYS }, | |
| 234 { "OR", "TK_OR", ALWAYS }, | |
| 235 { "ORDER", "TK_ORDER", ALWAYS }, | |
| 236 { "OUTER", "TK_JOIN_KW", ALWAYS }, | |
| 237 { "PLAN", "TK_PLAN", EXPLAIN }, | |
| 238 { "PRAGMA", "TK_PRAGMA", PRAGMA }, | |
| 239 { "PRIMARY", "TK_PRIMARY", ALWAYS }, | |
| 240 { "QUERY", "TK_QUERY", EXPLAIN }, | |
| 241 { "RAISE", "TK_RAISE", TRIGGER }, | |
| 242 { "RECURSIVE", "TK_RECURSIVE", CTE }, | |
| 243 { "REFERENCES", "TK_REFERENCES", FKEY }, | |
| 244 { "REGEXP", "TK_LIKE_KW", ALWAYS }, | |
| 245 { "REINDEX", "TK_REINDEX", REINDEX }, | |
| 246 { "RELEASE", "TK_RELEASE", ALWAYS }, | |
| 247 { "RENAME", "TK_RENAME", ALTER }, | |
| 248 { "REPLACE", "TK_REPLACE", CONFLICT }, | |
| 249 { "RESTRICT", "TK_RESTRICT", FKEY }, | |
| 250 { "RIGHT", "TK_JOIN_KW", ALWAYS }, | |
| 251 { "ROLLBACK", "TK_ROLLBACK", ALWAYS }, | |
| 252 { "ROW", "TK_ROW", TRIGGER }, | |
| 253 { "SAVEPOINT", "TK_SAVEPOINT", ALWAYS }, | |
| 254 { "SELECT", "TK_SELECT", ALWAYS }, | |
| 255 { "SET", "TK_SET", ALWAYS }, | |
| 256 { "TABLE", "TK_TABLE", ALWAYS }, | |
| 257 { "TEMP", "TK_TEMP", ALWAYS }, | |
| 258 { "TEMPORARY", "TK_TEMP", ALWAYS }, | |
| 259 { "THEN", "TK_THEN", ALWAYS }, | |
| 260 { "TO", "TK_TO", ALWAYS }, | |
| 261 { "TRANSACTION", "TK_TRANSACTION", ALWAYS }, | |
| 262 { "TRIGGER", "TK_TRIGGER", TRIGGER }, | |
| 263 { "UNION", "TK_UNION", COMPOUND }, | |
| 264 { "UNIQUE", "TK_UNIQUE", ALWAYS }, | |
| 265 { "UPDATE", "TK_UPDATE", ALWAYS }, | |
| 266 { "USING", "TK_USING", ALWAYS }, | |
| 267 { "VACUUM", "TK_VACUUM", VACUUM }, | |
| 268 { "VALUES", "TK_VALUES", ALWAYS }, | |
| 269 { "VIEW", "TK_VIEW", VIEW }, | |
| 270 { "VIRTUAL", "TK_VIRTUAL", VTAB }, | |
| 271 { "WITH", "TK_WITH", CTE }, | |
| 272 { "WITHOUT", "TK_WITHOUT", ALWAYS }, | |
| 273 { "WHEN", "TK_WHEN", ALWAYS }, | |
| 274 { "WHERE", "TK_WHERE", ALWAYS }, | |
| 275 }; | |
| 276 | |
| 277 /* Number of keywords */ | |
| 278 static int nKeyword = (sizeof(aKeywordTable)/sizeof(aKeywordTable[0])); | |
| 279 | |
| 280 /* Map all alphabetic characters into the same case */ | |
| 281 #define charMap(X) (0x20|(X)) | |
| 282 | |
| 283 /* | |
| 284 ** Comparision function for two Keyword records | |
| 285 */ | |
| 286 static int keywordCompare1(const void *a, const void *b){ | |
| 287 const Keyword *pA = (Keyword*)a; | |
| 288 const Keyword *pB = (Keyword*)b; | |
| 289 int n = pA->len - pB->len; | |
| 290 if( n==0 ){ | |
| 291 n = strcmp(pA->zName, pB->zName); | |
| 292 } | |
| 293 assert( n!=0 ); | |
| 294 return n; | |
| 295 } | |
| 296 static int keywordCompare2(const void *a, const void *b){ | |
| 297 const Keyword *pA = (Keyword*)a; | |
| 298 const Keyword *pB = (Keyword*)b; | |
| 299 int n = pB->longestSuffix - pA->longestSuffix; | |
| 300 if( n==0 ){ | |
| 301 n = strcmp(pA->zName, pB->zName); | |
| 302 } | |
| 303 assert( n!=0 ); | |
| 304 return n; | |
| 305 } | |
| 306 static int keywordCompare3(const void *a, const void *b){ | |
| 307 const Keyword *pA = (Keyword*)a; | |
| 308 const Keyword *pB = (Keyword*)b; | |
| 309 int n = pA->offset - pB->offset; | |
| 310 if( n==0 ) n = pB->id - pA->id; | |
| 311 assert( n!=0 ); | |
| 312 return n; | |
| 313 } | |
| 314 | |
| 315 /* | |
| 316 ** Return a KeywordTable entry with the given id | |
| 317 */ | |
| 318 static Keyword *findById(int id){ | |
| 319 int i; | |
| 320 for(i=0; i<nKeyword; i++){ | |
| 321 if( aKeywordTable[i].id==id ) break; | |
| 322 } | |
| 323 return &aKeywordTable[i]; | |
| 324 } | |
| 325 | |
| 326 /* | |
| 327 ** This routine does the work. The generated code is printed on standard | |
| 328 ** output. | |
| 329 */ | |
| 330 int main(int argc, char **argv){ | |
| 331 int i, j, k, h; | |
| 332 int bestSize, bestCount; | |
| 333 int count; | |
| 334 int nChar; | |
| 335 int totalLen = 0; | |
| 336 int aHash[1000]; /* 1000 is much bigger than nKeyword */ | |
| 337 char zText[2000]; | |
| 338 | |
| 339 /* Remove entries from the list of keywords that have mask==0 */ | |
| 340 for(i=j=0; i<nKeyword; i++){ | |
| 341 if( aKeywordTable[i].mask==0 ) continue; | |
| 342 if( j<i ){ | |
| 343 aKeywordTable[j] = aKeywordTable[i]; | |
| 344 } | |
| 345 j++; | |
| 346 } | |
| 347 nKeyword = j; | |
| 348 | |
| 349 /* Fill in the lengths of strings and hashes for all entries. */ | |
| 350 for(i=0; i<nKeyword; i++){ | |
| 351 Keyword *p = &aKeywordTable[i]; | |
| 352 p->len = (int)strlen(p->zName); | |
| 353 assert( p->len<sizeof(p->zOrigName) ); | |
| 354 memcpy(p->zOrigName, p->zName, p->len+1); | |
| 355 totalLen += p->len; | |
| 356 p->hash = (charMap(p->zName[0])*4) ^ | |
| 357 (charMap(p->zName[p->len-1])*3) ^ (p->len*1); | |
| 358 p->id = i+1; | |
| 359 } | |
| 360 | |
| 361 /* Sort the table from shortest to longest keyword */ | |
| 362 qsort(aKeywordTable, nKeyword, sizeof(aKeywordTable[0]), keywordCompare1); | |
| 363 | |
| 364 /* Look for short keywords embedded in longer keywords */ | |
| 365 for(i=nKeyword-2; i>=0; i--){ | |
| 366 Keyword *p = &aKeywordTable[i]; | |
| 367 for(j=nKeyword-1; j>i && p->substrId==0; j--){ | |
| 368 Keyword *pOther = &aKeywordTable[j]; | |
| 369 if( pOther->substrId ) continue; | |
| 370 if( pOther->len<=p->len ) continue; | |
| 371 for(k=0; k<=pOther->len-p->len; k++){ | |
| 372 if( memcmp(p->zName, &pOther->zName[k], p->len)==0 ){ | |
| 373 p->substrId = pOther->id; | |
| 374 p->substrOffset = k; | |
| 375 break; | |
| 376 } | |
| 377 } | |
| 378 } | |
| 379 } | |
| 380 | |
| 381 /* Compute the longestSuffix value for every word */ | |
| 382 for(i=0; i<nKeyword; i++){ | |
| 383 Keyword *p = &aKeywordTable[i]; | |
| 384 if( p->substrId ) continue; | |
| 385 for(j=0; j<nKeyword; j++){ | |
| 386 Keyword *pOther; | |
| 387 if( j==i ) continue; | |
| 388 pOther = &aKeywordTable[j]; | |
| 389 if( pOther->substrId ) continue; | |
| 390 for(k=p->longestSuffix+1; k<p->len && k<pOther->len; k++){ | |
| 391 if( memcmp(&p->zName[p->len-k], pOther->zName, k)==0 ){ | |
| 392 p->longestSuffix = k; | |
| 393 } | |
| 394 } | |
| 395 } | |
| 396 } | |
| 397 | |
| 398 /* Sort the table into reverse order by length */ | |
| 399 qsort(aKeywordTable, nKeyword, sizeof(aKeywordTable[0]), keywordCompare2); | |
| 400 | |
| 401 /* Fill in the offset for all entries */ | |
| 402 nChar = 0; | |
| 403 for(i=0; i<nKeyword; i++){ | |
| 404 Keyword *p = &aKeywordTable[i]; | |
| 405 if( p->offset>0 || p->substrId ) continue; | |
| 406 p->offset = nChar; | |
| 407 nChar += p->len; | |
| 408 for(k=p->len-1; k>=1; k--){ | |
| 409 for(j=i+1; j<nKeyword; j++){ | |
| 410 Keyword *pOther = &aKeywordTable[j]; | |
| 411 if( pOther->offset>0 || pOther->substrId ) continue; | |
| 412 if( pOther->len<=k ) continue; | |
| 413 if( memcmp(&p->zName[p->len-k], pOther->zName, k)==0 ){ | |
| 414 p = pOther; | |
| 415 p->offset = nChar - k; | |
| 416 nChar = p->offset + p->len; | |
| 417 p->zName += k; | |
| 418 p->len -= k; | |
| 419 p->prefix = k; | |
| 420 j = i; | |
| 421 k = p->len; | |
| 422 } | |
| 423 } | |
| 424 } | |
| 425 } | |
| 426 for(i=0; i<nKeyword; i++){ | |
| 427 Keyword *p = &aKeywordTable[i]; | |
| 428 if( p->substrId ){ | |
| 429 p->offset = findById(p->substrId)->offset + p->substrOffset; | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 /* Sort the table by offset */ | |
| 434 qsort(aKeywordTable, nKeyword, sizeof(aKeywordTable[0]), keywordCompare3); | |
| 435 | |
| 436 /* Figure out how big to make the hash table in order to minimize the | |
| 437 ** number of collisions */ | |
| 438 bestSize = nKeyword; | |
| 439 bestCount = nKeyword*nKeyword; | |
| 440 for(i=nKeyword/2; i<=2*nKeyword; i++){ | |
| 441 for(j=0; j<i; j++) aHash[j] = 0; | |
| 442 for(j=0; j<nKeyword; j++){ | |
| 443 h = aKeywordTable[j].hash % i; | |
| 444 aHash[h] *= 2; | |
| 445 aHash[h]++; | |
| 446 } | |
| 447 for(j=count=0; j<i; j++) count += aHash[j]; | |
| 448 if( count<bestCount ){ | |
| 449 bestCount = count; | |
| 450 bestSize = i; | |
| 451 } | |
| 452 } | |
| 453 | |
| 454 /* Compute the hash */ | |
| 455 for(i=0; i<bestSize; i++) aHash[i] = 0; | |
| 456 for(i=0; i<nKeyword; i++){ | |
| 457 h = aKeywordTable[i].hash % bestSize; | |
| 458 aKeywordTable[i].iNext = aHash[h]; | |
| 459 aHash[h] = i+1; | |
| 460 } | |
| 461 | |
| 462 /* Begin generating code */ | |
| 463 printf("%s", zHdr); | |
| 464 printf("/* Hash score: %d */\n", bestCount); | |
| 465 printf("static int keywordCode(const char *z, int n, int *pType){\n"); | |
| 466 printf(" /* zText[] encodes %d bytes of keywords in %d bytes */\n", | |
| 467 totalLen + nKeyword, nChar+1 ); | |
| 468 for(i=j=k=0; i<nKeyword; i++){ | |
| 469 Keyword *p = &aKeywordTable[i]; | |
| 470 if( p->substrId ) continue; | |
| 471 memcpy(&zText[k], p->zName, p->len); | |
| 472 k += p->len; | |
| 473 if( j+p->len>70 ){ | |
| 474 printf("%*s */\n", 74-j, ""); | |
| 475 j = 0; | |
| 476 } | |
| 477 if( j==0 ){ | |
| 478 printf(" /* "); | |
| 479 j = 8; | |
| 480 } | |
| 481 printf("%s", p->zName); | |
| 482 j += p->len; | |
| 483 } | |
| 484 if( j>0 ){ | |
| 485 printf("%*s */\n", 74-j, ""); | |
| 486 } | |
| 487 printf(" static const char zText[%d] = {\n", nChar); | |
| 488 zText[nChar] = 0; | |
| 489 for(i=j=0; i<k; i++){ | |
| 490 if( j==0 ){ | |
| 491 printf(" "); | |
| 492 } | |
| 493 if( zText[i]==0 ){ | |
| 494 printf("0"); | |
| 495 }else{ | |
| 496 printf("'%c',", zText[i]); | |
| 497 } | |
| 498 j += 4; | |
| 499 if( j>68 ){ | |
| 500 printf("\n"); | |
| 501 j = 0; | |
| 502 } | |
| 503 } | |
| 504 if( j>0 ) printf("\n"); | |
| 505 printf(" };\n"); | |
| 506 | |
| 507 printf(" static const unsigned char aHash[%d] = {\n", bestSize); | |
| 508 for(i=j=0; i<bestSize; i++){ | |
| 509 if( j==0 ) printf(" "); | |
| 510 printf(" %3d,", aHash[i]); | |
| 511 j++; | |
| 512 if( j>12 ){ | |
| 513 printf("\n"); | |
| 514 j = 0; | |
| 515 } | |
| 516 } | |
| 517 printf("%s };\n", j==0 ? "" : "\n"); | |
| 518 | |
| 519 printf(" static const unsigned char aNext[%d] = {\n", nKeyword); | |
| 520 for(i=j=0; i<nKeyword; i++){ | |
| 521 if( j==0 ) printf(" "); | |
| 522 printf(" %3d,", aKeywordTable[i].iNext); | |
| 523 j++; | |
| 524 if( j>12 ){ | |
| 525 printf("\n"); | |
| 526 j = 0; | |
| 527 } | |
| 528 } | |
| 529 printf("%s };\n", j==0 ? "" : "\n"); | |
| 530 | |
| 531 printf(" static const unsigned char aLen[%d] = {\n", nKeyword); | |
| 532 for(i=j=0; i<nKeyword; i++){ | |
| 533 if( j==0 ) printf(" "); | |
| 534 printf(" %3d,", aKeywordTable[i].len+aKeywordTable[i].prefix); | |
| 535 j++; | |
| 536 if( j>12 ){ | |
| 537 printf("\n"); | |
| 538 j = 0; | |
| 539 } | |
| 540 } | |
| 541 printf("%s };\n", j==0 ? "" : "\n"); | |
| 542 | |
| 543 printf(" static const unsigned short int aOffset[%d] = {\n", nKeyword); | |
| 544 for(i=j=0; i<nKeyword; i++){ | |
| 545 if( j==0 ) printf(" "); | |
| 546 printf(" %3d,", aKeywordTable[i].offset); | |
| 547 j++; | |
| 548 if( j>12 ){ | |
| 549 printf("\n"); | |
| 550 j = 0; | |
| 551 } | |
| 552 } | |
| 553 printf("%s };\n", j==0 ? "" : "\n"); | |
| 554 | |
| 555 printf(" static const unsigned char aCode[%d] = {\n", nKeyword); | |
| 556 for(i=j=0; i<nKeyword; i++){ | |
| 557 char *zToken = aKeywordTable[i].zTokenType; | |
| 558 if( j==0 ) printf(" "); | |
| 559 printf("%s,%*s", zToken, (int)(14-strlen(zToken)), ""); | |
| 560 j++; | |
| 561 if( j>=5 ){ | |
| 562 printf("\n"); | |
| 563 j = 0; | |
| 564 } | |
| 565 } | |
| 566 printf("%s };\n", j==0 ? "" : "\n"); | |
| 567 | |
| 568 printf(" int h, i;\n"); | |
| 569 printf(" if( n>=2 ){\n"); | |
| 570 printf(" h = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) %% %d;\n", | |
| 571 bestSize); | |
| 572 printf(" for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){\n"); | |
| 573 printf(" if( aLen[i]==n &&" | |
| 574 " sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){\n"); | |
| 575 for(i=0; i<nKeyword; i++){ | |
| 576 printf(" testcase( i==%d ); /* %s */\n", | |
| 577 i, aKeywordTable[i].zOrigName); | |
| 578 } | |
| 579 printf(" *pType = aCode[i];\n"); | |
| 580 printf(" break;\n"); | |
| 581 printf(" }\n"); | |
| 582 printf(" }\n"); | |
| 583 printf(" }\n"); | |
| 584 printf(" return n;\n"); | |
| 585 printf("}\n"); | |
| 586 printf("int sqlite3KeywordCode(const unsigned char *z, int n){\n"); | |
| 587 printf(" int id = TK_ID;\n"); | |
| 588 printf(" keywordCode((char*)z, n, &id);\n"); | |
| 589 printf(" return id;\n"); | |
| 590 printf("}\n"); | |
| 591 printf("#define SQLITE_N_KEYWORD %d\n", nKeyword); | |
| 592 | |
| 593 return 0; | |
| 594 } | |
| OLD | NEW |