mkkeywordhash.c 26 KB

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