2004-10-07 23:03:01 +04:00
|
|
|
/*
|
|
|
|
** Compile and run this standalone program in order to generate code that
|
|
|
|
** implements a function that will translate alphabetic identifiers into
|
|
|
|
** parser token codes.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2009-06-09 18:27:41 +04:00
|
|
|
#include <assert.h>
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2007-02-21 19:44:33 +03:00
|
|
|
/*
|
|
|
|
** A header comment placed at the beginning of generated code.
|
|
|
|
*/
|
|
|
|
static const char zHdr[] =
|
|
|
|
"/***** This file contains automatically generated code ******\n"
|
|
|
|
"**\n"
|
|
|
|
"** The code in this file has been automatically generated by\n"
|
|
|
|
"**\n"
|
2009-11-03 16:08:16 +03:00
|
|
|
"** sqlite/tool/mkkeywordhash.c\n"
|
2007-02-21 19:44:33 +03:00
|
|
|
"**\n"
|
|
|
|
"** The code in this file implements a function that determines whether\n"
|
|
|
|
"** or not a given identifier is really an SQL keyword. The same thing\n"
|
|
|
|
"** might be implemented more directly using a hand-written hash table.\n"
|
|
|
|
"** But by using this automatically generated code, the size of the code\n"
|
|
|
|
"** is substantially reduced. This is important for embedded applications\n"
|
|
|
|
"** on platforms with limited memory.\n"
|
|
|
|
"*/\n"
|
|
|
|
;
|
|
|
|
|
2004-10-07 23:03:01 +04:00
|
|
|
/*
|
2008-12-10 23:11:01 +03:00
|
|
|
** All the keywords of the SQL language are stored in a hash
|
2004-10-07 23:03:01 +04:00
|
|
|
** table composed of instances of the following structure.
|
|
|
|
*/
|
|
|
|
typedef struct Keyword Keyword;
|
|
|
|
struct Keyword {
|
|
|
|
char *zName; /* The keyword name */
|
|
|
|
char *zTokenType; /* Token value for this keyword */
|
2004-11-03 06:59:57 +03:00
|
|
|
int mask; /* Code this keyword if non-zero */
|
2004-10-23 09:10:18 +04:00
|
|
|
int id; /* Unique ID for this record */
|
2004-10-07 23:03:01 +04:00
|
|
|
int hash; /* Hash on the keyword */
|
|
|
|
int offset; /* Offset to start of name string */
|
|
|
|
int len; /* Length of this keyword, not counting final \000 */
|
2004-10-23 09:10:18 +04:00
|
|
|
int prefix; /* Number of characters in prefix */
|
2007-05-04 21:07:52 +04:00
|
|
|
int longestSuffix; /* Longest suffix that is a prefix on another word */
|
2004-10-07 23:03:01 +04:00
|
|
|
int iNext; /* Index in aKeywordTable[] of next with same hash */
|
2004-10-23 09:10:18 +04:00
|
|
|
int substrId; /* Id to another keyword this keyword is embedded in */
|
|
|
|
int substrOffset; /* Offset into substrId for start of this keyword */
|
2009-06-09 18:27:41 +04:00
|
|
|
char zOrigName[20]; /* Original keyword name before processing */
|
2004-10-07 23:03:01 +04:00
|
|
|
};
|
|
|
|
|
2004-11-03 06:59:57 +03:00
|
|
|
/*
|
|
|
|
** Define masks used to determine which keywords are allowed
|
|
|
|
*/
|
2004-11-13 06:59:25 +03:00
|
|
|
#ifdef SQLITE_OMIT_ALTERTABLE
|
2004-11-03 06:59:57 +03:00
|
|
|
# define ALTER 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define ALTER 0x00000001
|
|
|
|
#endif
|
|
|
|
#define ALWAYS 0x00000002
|
|
|
|
#ifdef SQLITE_OMIT_ANALYZE
|
|
|
|
# define ANALYZE 0
|
|
|
|
#else
|
|
|
|
# define ANALYZE 0x00000004
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_ATTACH
|
|
|
|
# define ATTACH 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define ATTACH 0x00000008
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_AUTOINCREMENT
|
|
|
|
# define AUTOINCR 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define AUTOINCR 0x00000010
|
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_CAST
|
|
|
|
# define CAST 0
|
|
|
|
#else
|
|
|
|
# define CAST 0x00000020
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_COMPOUND_SELECT
|
|
|
|
# define COMPOUND 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define COMPOUND 0x00000040
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_CONFLICT_CLAUSE
|
|
|
|
# define CONFLICT 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define CONFLICT 0x00000080
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_EXPLAIN
|
|
|
|
# define EXPLAIN 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define EXPLAIN 0x00000100
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_FOREIGN_KEY
|
|
|
|
# define FKEY 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define FKEY 0x00000200
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_PRAGMA
|
|
|
|
# define PRAGMA 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define PRAGMA 0x00000400
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_REINDEX
|
|
|
|
# define REINDEX 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define REINDEX 0x00000800
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
2005-01-20 16:36:19 +03:00
|
|
|
#ifdef SQLITE_OMIT_SUBQUERY
|
|
|
|
# define SUBQUERY 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define SUBQUERY 0x00001000
|
2005-01-20 16:36:19 +03:00
|
|
|
#endif
|
2004-11-03 06:59:57 +03:00
|
|
|
#ifdef SQLITE_OMIT_TRIGGER
|
|
|
|
# define TRIGGER 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define TRIGGER 0x00002000
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
2007-04-26 18:42:34 +04:00
|
|
|
#if defined(SQLITE_OMIT_AUTOVACUUM) && \
|
|
|
|
(defined(SQLITE_OMIT_VACUUM) || defined(SQLITE_OMIT_ATTACH))
|
2004-11-03 06:59:57 +03:00
|
|
|
# define VACUUM 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define VACUUM 0x00004000
|
2004-11-03 06:59:57 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SQLITE_OMIT_VIEW
|
|
|
|
# define VIEW 0
|
|
|
|
#else
|
2005-07-08 16:13:04 +04:00
|
|
|
# define VIEW 0x00008000
|
2005-06-25 22:42:14 +04:00
|
|
|
#endif
|
2006-06-10 17:29:31 +04:00
|
|
|
#ifdef SQLITE_OMIT_VIRTUALTABLE
|
|
|
|
# define VTAB 0
|
|
|
|
#else
|
|
|
|
# define VTAB 0x00010000
|
|
|
|
#endif
|
2007-04-26 18:42:34 +04:00
|
|
|
#ifdef SQLITE_OMIT_AUTOVACUUM
|
|
|
|
# define AUTOVACUUM 0
|
|
|
|
#else
|
|
|
|
# define AUTOVACUUM 0x00020000
|
|
|
|
#endif
|
2014-01-11 17:22:17 +04:00
|
|
|
#ifdef SQLITE_OMIT_CTE
|
|
|
|
# define CTE 0
|
|
|
|
#else
|
|
|
|
# define CTE 0x00040000
|
|
|
|
#endif
|
2018-04-12 18:43:05 +03:00
|
|
|
#ifdef SQLITE_OMIT_UPSERT
|
|
|
|
# define UPSERT 0
|
|
|
|
#else
|
|
|
|
# define UPSERT 0x00080000
|
|
|
|
#endif
|
2004-11-03 06:59:57 +03:00
|
|
|
|
2004-10-07 23:03:01 +04:00
|
|
|
/*
|
|
|
|
** These are the keywords
|
|
|
|
*/
|
|
|
|
static Keyword aKeywordTable[] = {
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "ABORT", "TK_ABORT", CONFLICT|TRIGGER },
|
2009-09-21 20:06:03 +04:00
|
|
|
{ "ACTION", "TK_ACTION", FKEY },
|
2005-03-17 08:06:28 +03:00
|
|
|
{ "ADD", "TK_ADD", ALTER },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "AFTER", "TK_AFTER", TRIGGER },
|
|
|
|
{ "ALL", "TK_ALL", ALWAYS },
|
|
|
|
{ "ALTER", "TK_ALTER", ALTER },
|
2005-07-08 16:13:04 +04:00
|
|
|
{ "ANALYZE", "TK_ANALYZE", ANALYZE },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "AND", "TK_AND", ALWAYS },
|
|
|
|
{ "AS", "TK_AS", ALWAYS },
|
|
|
|
{ "ASC", "TK_ASC", ALWAYS },
|
|
|
|
{ "ATTACH", "TK_ATTACH", ATTACH },
|
|
|
|
{ "AUTOINCREMENT", "TK_AUTOINCR", AUTOINCR },
|
|
|
|
{ "BEFORE", "TK_BEFORE", TRIGGER },
|
|
|
|
{ "BEGIN", "TK_BEGIN", ALWAYS },
|
|
|
|
{ "BETWEEN", "TK_BETWEEN", ALWAYS },
|
|
|
|
{ "BY", "TK_BY", ALWAYS },
|
|
|
|
{ "CASCADE", "TK_CASCADE", FKEY },
|
|
|
|
{ "CASE", "TK_CASE", ALWAYS },
|
2005-06-25 22:42:14 +04:00
|
|
|
{ "CAST", "TK_CAST", CAST },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "CHECK", "TK_CHECK", ALWAYS },
|
|
|
|
{ "COLLATE", "TK_COLLATE", ALWAYS },
|
2005-03-17 08:06:28 +03:00
|
|
|
{ "COLUMN", "TK_COLUMNKW", ALTER },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "COMMIT", "TK_COMMIT", ALWAYS },
|
|
|
|
{ "CONFLICT", "TK_CONFLICT", CONFLICT },
|
|
|
|
{ "CONSTRAINT", "TK_CONSTRAINT", ALWAYS },
|
|
|
|
{ "CREATE", "TK_CREATE", ALWAYS },
|
|
|
|
{ "CROSS", "TK_JOIN_KW", ALWAYS },
|
2005-05-23 21:26:51 +04:00
|
|
|
{ "CURRENT_DATE", "TK_CTIME_KW", ALWAYS },
|
|
|
|
{ "CURRENT_TIME", "TK_CTIME_KW", ALWAYS },
|
|
|
|
{ "CURRENT_TIMESTAMP","TK_CTIME_KW", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "DATABASE", "TK_DATABASE", ATTACH },
|
|
|
|
{ "DEFAULT", "TK_DEFAULT", ALWAYS },
|
2004-11-03 19:27:01 +03:00
|
|
|
{ "DEFERRED", "TK_DEFERRED", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "DEFERRABLE", "TK_DEFERRABLE", FKEY },
|
|
|
|
{ "DELETE", "TK_DELETE", ALWAYS },
|
|
|
|
{ "DESC", "TK_DESC", ALWAYS },
|
|
|
|
{ "DETACH", "TK_DETACH", ATTACH },
|
|
|
|
{ "DISTINCT", "TK_DISTINCT", ALWAYS },
|
2018-04-12 18:43:05 +03:00
|
|
|
{ "DO", "TK_DO", UPSERT },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "DROP", "TK_DROP", ALWAYS },
|
|
|
|
{ "END", "TK_END", ALWAYS },
|
|
|
|
{ "EACH", "TK_EACH", TRIGGER },
|
|
|
|
{ "ELSE", "TK_ELSE", ALWAYS },
|
2004-11-17 19:41:29 +03:00
|
|
|
{ "ESCAPE", "TK_ESCAPE", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "EXCEPT", "TK_EXCEPT", COMPOUND },
|
|
|
|
{ "EXCLUSIVE", "TK_EXCLUSIVE", ALWAYS },
|
2006-01-14 11:02:28 +03:00
|
|
|
{ "EXISTS", "TK_EXISTS", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "EXPLAIN", "TK_EXPLAIN", EXPLAIN },
|
|
|
|
{ "FAIL", "TK_FAIL", CONFLICT|TRIGGER },
|
2005-02-04 07:07:16 +03:00
|
|
|
{ "FOR", "TK_FOR", TRIGGER },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "FOREIGN", "TK_FOREIGN", FKEY },
|
|
|
|
{ "FROM", "TK_FROM", ALWAYS },
|
|
|
|
{ "FULL", "TK_JOIN_KW", ALWAYS },
|
2005-05-23 21:26:51 +04:00
|
|
|
{ "GLOB", "TK_LIKE_KW", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "GROUP", "TK_GROUP", ALWAYS },
|
|
|
|
{ "HAVING", "TK_HAVING", ALWAYS },
|
2005-12-29 04:11:36 +03:00
|
|
|
{ "IF", "TK_IF", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "IGNORE", "TK_IGNORE", CONFLICT|TRIGGER },
|
2004-11-03 19:27:01 +03:00
|
|
|
{ "IMMEDIATE", "TK_IMMEDIATE", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "IN", "TK_IN", ALWAYS },
|
|
|
|
{ "INDEX", "TK_INDEX", ALWAYS },
|
2008-10-06 09:32:18 +04:00
|
|
|
{ "INDEXED", "TK_INDEXED", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "INITIALLY", "TK_INITIALLY", FKEY },
|
|
|
|
{ "INNER", "TK_JOIN_KW", ALWAYS },
|
|
|
|
{ "INSERT", "TK_INSERT", ALWAYS },
|
|
|
|
{ "INSTEAD", "TK_INSTEAD", TRIGGER },
|
|
|
|
{ "INTERSECT", "TK_INTERSECT", COMPOUND },
|
|
|
|
{ "INTO", "TK_INTO", ALWAYS },
|
|
|
|
{ "IS", "TK_IS", ALWAYS },
|
|
|
|
{ "ISNULL", "TK_ISNULL", ALWAYS },
|
|
|
|
{ "JOIN", "TK_JOIN", ALWAYS },
|
|
|
|
{ "KEY", "TK_KEY", ALWAYS },
|
|
|
|
{ "LEFT", "TK_JOIN_KW", ALWAYS },
|
2005-05-23 21:26:51 +04:00
|
|
|
{ "LIKE", "TK_LIKE_KW", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "LIMIT", "TK_LIMIT", ALWAYS },
|
|
|
|
{ "MATCH", "TK_MATCH", ALWAYS },
|
|
|
|
{ "NATURAL", "TK_JOIN_KW", ALWAYS },
|
2009-09-21 20:06:03 +04:00
|
|
|
{ "NO", "TK_NO", FKEY },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "NOT", "TK_NOT", ALWAYS },
|
2018-04-12 18:43:05 +03:00
|
|
|
{ "NOTHING", "TK_NOTHING", UPSERT },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "NOTNULL", "TK_NOTNULL", ALWAYS },
|
|
|
|
{ "NULL", "TK_NULL", ALWAYS },
|
|
|
|
{ "OF", "TK_OF", ALWAYS },
|
|
|
|
{ "OFFSET", "TK_OFFSET", ALWAYS },
|
|
|
|
{ "ON", "TK_ON", ALWAYS },
|
|
|
|
{ "OR", "TK_OR", ALWAYS },
|
|
|
|
{ "ORDER", "TK_ORDER", ALWAYS },
|
|
|
|
{ "OUTER", "TK_JOIN_KW", ALWAYS },
|
2005-09-10 20:46:12 +04:00
|
|
|
{ "PLAN", "TK_PLAN", EXPLAIN },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "PRAGMA", "TK_PRAGMA", PRAGMA },
|
|
|
|
{ "PRIMARY", "TK_PRIMARY", ALWAYS },
|
2005-09-10 20:46:12 +04:00
|
|
|
{ "QUERY", "TK_QUERY", EXPLAIN },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "RAISE", "TK_RAISE", TRIGGER },
|
2014-01-11 17:22:17 +04:00
|
|
|
{ "RECURSIVE", "TK_RECURSIVE", CTE },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "REFERENCES", "TK_REFERENCES", FKEY },
|
2005-05-23 21:26:51 +04:00
|
|
|
{ "REGEXP", "TK_LIKE_KW", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "REINDEX", "TK_REINDEX", REINDEX },
|
2008-12-17 20:30:26 +03:00
|
|
|
{ "RELEASE", "TK_RELEASE", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "RENAME", "TK_RENAME", ALTER },
|
|
|
|
{ "REPLACE", "TK_REPLACE", CONFLICT },
|
|
|
|
{ "RESTRICT", "TK_RESTRICT", FKEY },
|
|
|
|
{ "RIGHT", "TK_JOIN_KW", ALWAYS },
|
|
|
|
{ "ROLLBACK", "TK_ROLLBACK", ALWAYS },
|
|
|
|
{ "ROW", "TK_ROW", TRIGGER },
|
2008-12-17 20:30:26 +03:00
|
|
|
{ "SAVEPOINT", "TK_SAVEPOINT", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "SELECT", "TK_SELECT", ALWAYS },
|
|
|
|
{ "SET", "TK_SET", ALWAYS },
|
|
|
|
{ "TABLE", "TK_TABLE", ALWAYS },
|
|
|
|
{ "TEMP", "TK_TEMP", ALWAYS },
|
|
|
|
{ "TEMPORARY", "TK_TEMP", ALWAYS },
|
|
|
|
{ "THEN", "TK_THEN", ALWAYS },
|
2009-02-01 03:00:45 +03:00
|
|
|
{ "TO", "TK_TO", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "TRANSACTION", "TK_TRANSACTION", ALWAYS },
|
|
|
|
{ "TRIGGER", "TK_TRIGGER", TRIGGER },
|
|
|
|
{ "UNION", "TK_UNION", COMPOUND },
|
|
|
|
{ "UNIQUE", "TK_UNIQUE", ALWAYS },
|
|
|
|
{ "UPDATE", "TK_UPDATE", ALWAYS },
|
|
|
|
{ "USING", "TK_USING", ALWAYS },
|
|
|
|
{ "VACUUM", "TK_VACUUM", VACUUM },
|
|
|
|
{ "VALUES", "TK_VALUES", ALWAYS },
|
|
|
|
{ "VIEW", "TK_VIEW", VIEW },
|
2006-06-10 17:29:31 +04:00
|
|
|
{ "VIRTUAL", "TK_VIRTUAL", VTAB },
|
2014-01-11 17:22:17 +04:00
|
|
|
{ "WITH", "TK_WITH", CTE },
|
2013-10-21 06:14:45 +04:00
|
|
|
{ "WITHOUT", "TK_WITHOUT", ALWAYS },
|
2004-11-03 06:59:57 +03:00
|
|
|
{ "WHEN", "TK_WHEN", ALWAYS },
|
|
|
|
{ "WHERE", "TK_WHERE", ALWAYS },
|
2004-10-07 23:03:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Number of keywords */
|
2007-05-04 21:07:52 +04:00
|
|
|
static int nKeyword = (sizeof(aKeywordTable)/sizeof(aKeywordTable[0]));
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2016-02-08 22:15:48 +03:00
|
|
|
/* Map all alphabetic characters into lower-case for hashing. This is
|
|
|
|
** only valid for alphabetics. In particular it does not work for '_'
|
|
|
|
** and so the hash cannot be on a keyword position that might be an '_'.
|
|
|
|
*/
|
2015-11-10 06:30:51 +03:00
|
|
|
#define charMap(X) (0x20|(X))
|
2004-10-07 23:03:01 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Comparision function for two Keyword records
|
|
|
|
*/
|
2004-10-23 09:10:18 +04:00
|
|
|
static int keywordCompare1(const void *a, const void *b){
|
2004-10-07 23:03:01 +04:00
|
|
|
const Keyword *pA = (Keyword*)a;
|
|
|
|
const Keyword *pB = (Keyword*)b;
|
2004-10-23 09:10:18 +04:00
|
|
|
int n = pA->len - pB->len;
|
|
|
|
if( n==0 ){
|
|
|
|
n = strcmp(pA->zName, pB->zName);
|
|
|
|
}
|
2009-11-03 16:08:16 +03:00
|
|
|
assert( n!=0 );
|
2004-10-23 09:10:18 +04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
static int keywordCompare2(const void *a, const void *b){
|
|
|
|
const Keyword *pA = (Keyword*)a;
|
|
|
|
const Keyword *pB = (Keyword*)b;
|
2007-05-04 21:07:52 +04:00
|
|
|
int n = pB->longestSuffix - pA->longestSuffix;
|
|
|
|
if( n==0 ){
|
|
|
|
n = strcmp(pA->zName, pB->zName);
|
|
|
|
}
|
2009-11-03 16:08:16 +03:00
|
|
|
assert( n!=0 );
|
2004-10-23 09:10:18 +04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
static int keywordCompare3(const void *a, const void *b){
|
|
|
|
const Keyword *pA = (Keyword*)a;
|
|
|
|
const Keyword *pB = (Keyword*)b;
|
|
|
|
int n = pA->offset - pB->offset;
|
2009-11-03 16:08:16 +03:00
|
|
|
if( n==0 ) n = pB->id - pA->id;
|
|
|
|
assert( n!=0 );
|
2004-10-23 09:10:18 +04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return a KeywordTable entry with the given id
|
|
|
|
*/
|
|
|
|
static Keyword *findById(int id){
|
|
|
|
int i;
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=0; i<nKeyword; i++){
|
2004-10-23 09:10:18 +04:00
|
|
|
if( aKeywordTable[i].id==id ) break;
|
|
|
|
}
|
|
|
|
return &aKeywordTable[i];
|
2004-10-07 23:03:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This routine does the work. The generated code is printed on standard
|
|
|
|
** output.
|
|
|
|
*/
|
|
|
|
int main(int argc, char **argv){
|
2015-11-17 22:16:39 +03:00
|
|
|
int i, j, k, h;
|
2004-10-07 23:03:01 +04:00
|
|
|
int bestSize, bestCount;
|
|
|
|
int count;
|
|
|
|
int nChar;
|
2007-05-04 21:07:52 +04:00
|
|
|
int totalLen = 0;
|
2017-07-06 19:33:14 +03:00
|
|
|
int aKWHash[1000]; /* 1000 is much bigger than nKeyword */
|
|
|
|
char zKWText[2000];
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2004-11-03 06:59:57 +03:00
|
|
|
/* Remove entries from the list of keywords that have mask==0 */
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=j=0; i<nKeyword; i++){
|
2004-11-03 06:59:57 +03:00
|
|
|
if( aKeywordTable[i].mask==0 ) continue;
|
|
|
|
if( j<i ){
|
|
|
|
aKeywordTable[j] = aKeywordTable[i];
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
2007-05-04 21:07:52 +04:00
|
|
|
nKeyword = j;
|
2004-11-03 06:59:57 +03:00
|
|
|
|
2004-10-23 09:10:18 +04:00
|
|
|
/* Fill in the lengths of strings and hashes for all entries. */
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=0; i<nKeyword; i++){
|
2004-10-07 23:03:01 +04:00
|
|
|
Keyword *p = &aKeywordTable[i];
|
2012-04-18 14:13:20 +04:00
|
|
|
p->len = (int)strlen(p->zName);
|
2009-06-09 18:27:41 +04:00
|
|
|
assert( p->len<sizeof(p->zOrigName) );
|
2014-05-31 19:39:53 +04:00
|
|
|
memcpy(p->zOrigName, p->zName, p->len+1);
|
2007-05-04 21:07:52 +04:00
|
|
|
totalLen += p->len;
|
2015-11-10 06:30:51 +03:00
|
|
|
p->hash = (charMap(p->zName[0])*4) ^
|
|
|
|
(charMap(p->zName[p->len-1])*3) ^ (p->len*1);
|
2004-10-23 09:10:18 +04:00
|
|
|
p->id = i+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the table from shortest to longest keyword */
|
2007-05-04 21:07:52 +04:00
|
|
|
qsort(aKeywordTable, nKeyword, sizeof(aKeywordTable[0]), keywordCompare1);
|
2004-10-23 09:10:18 +04:00
|
|
|
|
|
|
|
/* Look for short keywords embedded in longer keywords */
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=nKeyword-2; i>=0; i--){
|
2004-10-23 09:10:18 +04:00
|
|
|
Keyword *p = &aKeywordTable[i];
|
2007-05-04 21:07:52 +04:00
|
|
|
for(j=nKeyword-1; j>i && p->substrId==0; j--){
|
2004-10-23 09:10:18 +04:00
|
|
|
Keyword *pOther = &aKeywordTable[j];
|
|
|
|
if( pOther->substrId ) continue;
|
|
|
|
if( pOther->len<=p->len ) continue;
|
|
|
|
for(k=0; k<=pOther->len-p->len; k++){
|
|
|
|
if( memcmp(p->zName, &pOther->zName[k], p->len)==0 ){
|
|
|
|
p->substrId = pOther->id;
|
|
|
|
p->substrOffset = k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-04 21:07:52 +04:00
|
|
|
/* Compute the longestSuffix value for every word */
|
|
|
|
for(i=0; i<nKeyword; i++){
|
|
|
|
Keyword *p = &aKeywordTable[i];
|
|
|
|
if( p->substrId ) continue;
|
|
|
|
for(j=0; j<nKeyword; j++){
|
|
|
|
Keyword *pOther;
|
|
|
|
if( j==i ) continue;
|
|
|
|
pOther = &aKeywordTable[j];
|
|
|
|
if( pOther->substrId ) continue;
|
|
|
|
for(k=p->longestSuffix+1; k<p->len && k<pOther->len; k++){
|
|
|
|
if( memcmp(&p->zName[p->len-k], pOther->zName, k)==0 ){
|
|
|
|
p->longestSuffix = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the table into reverse order by length */
|
|
|
|
qsort(aKeywordTable, nKeyword, sizeof(aKeywordTable[0]), keywordCompare2);
|
2004-10-23 09:10:18 +04:00
|
|
|
|
|
|
|
/* Fill in the offset for all entries */
|
|
|
|
nChar = 0;
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=0; i<nKeyword; i++){
|
2004-10-23 09:10:18 +04:00
|
|
|
Keyword *p = &aKeywordTable[i];
|
|
|
|
if( p->offset>0 || p->substrId ) continue;
|
2004-10-07 23:03:01 +04:00
|
|
|
p->offset = nChar;
|
2004-10-23 09:10:18 +04:00
|
|
|
nChar += p->len;
|
|
|
|
for(k=p->len-1; k>=1; k--){
|
2007-05-04 21:07:52 +04:00
|
|
|
for(j=i+1; j<nKeyword; j++){
|
2004-10-23 09:10:18 +04:00
|
|
|
Keyword *pOther = &aKeywordTable[j];
|
|
|
|
if( pOther->offset>0 || pOther->substrId ) continue;
|
|
|
|
if( pOther->len<=k ) continue;
|
|
|
|
if( memcmp(&p->zName[p->len-k], pOther->zName, k)==0 ){
|
|
|
|
p = pOther;
|
|
|
|
p->offset = nChar - k;
|
|
|
|
nChar = p->offset + p->len;
|
|
|
|
p->zName += k;
|
|
|
|
p->len -= k;
|
|
|
|
p->prefix = k;
|
|
|
|
j = i;
|
|
|
|
k = p->len;
|
|
|
|
}
|
|
|
|
}
|
2004-10-07 23:03:01 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=0; i<nKeyword; i++){
|
2004-10-23 09:10:18 +04:00
|
|
|
Keyword *p = &aKeywordTable[i];
|
|
|
|
if( p->substrId ){
|
|
|
|
p->offset = findById(p->substrId)->offset + p->substrOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sort the table by offset */
|
2007-05-04 21:07:52 +04:00
|
|
|
qsort(aKeywordTable, nKeyword, sizeof(aKeywordTable[0]), keywordCompare3);
|
2004-10-07 23:03:01 +04:00
|
|
|
|
|
|
|
/* Figure out how big to make the hash table in order to minimize the
|
|
|
|
** number of collisions */
|
2007-05-04 21:07:52 +04:00
|
|
|
bestSize = nKeyword;
|
|
|
|
bestCount = nKeyword*nKeyword;
|
|
|
|
for(i=nKeyword/2; i<=2*nKeyword; i++){
|
2017-07-06 19:33:14 +03:00
|
|
|
for(j=0; j<i; j++) aKWHash[j] = 0;
|
2007-05-04 21:07:52 +04:00
|
|
|
for(j=0; j<nKeyword; j++){
|
2004-10-07 23:03:01 +04:00
|
|
|
h = aKeywordTable[j].hash % i;
|
2017-07-06 19:33:14 +03:00
|
|
|
aKWHash[h] *= 2;
|
|
|
|
aKWHash[h]++;
|
2004-10-07 23:03:01 +04:00
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
for(j=count=0; j<i; j++) count += aKWHash[j];
|
2004-10-07 23:03:01 +04:00
|
|
|
if( count<bestCount ){
|
|
|
|
bestCount = count;
|
|
|
|
bestSize = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the hash */
|
2017-07-06 19:33:14 +03:00
|
|
|
for(i=0; i<bestSize; i++) aKWHash[i] = 0;
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=0; i<nKeyword; i++){
|
2004-10-07 23:03:01 +04:00
|
|
|
h = aKeywordTable[i].hash % bestSize;
|
2017-07-06 19:33:14 +03:00
|
|
|
aKeywordTable[i].iNext = aKWHash[h];
|
|
|
|
aKWHash[h] = i+1;
|
2004-10-07 23:03:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Begin generating code */
|
2007-02-21 19:44:33 +03:00
|
|
|
printf("%s", zHdr);
|
2005-02-01 04:40:44 +03:00
|
|
|
printf("/* Hash score: %d */\n", bestCount);
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* zKWText[] encodes %d bytes of keyword text in %d bytes */\n",
|
2007-05-04 21:07:52 +04:00
|
|
|
totalLen + nKeyword, nChar+1 );
|
2008-12-08 21:19:17 +03:00
|
|
|
for(i=j=k=0; i<nKeyword; i++){
|
2004-10-07 23:03:01 +04:00
|
|
|
Keyword *p = &aKeywordTable[i];
|
2004-10-23 09:10:18 +04:00
|
|
|
if( p->substrId ) continue;
|
2017-07-06 19:33:14 +03:00
|
|
|
memcpy(&zKWText[k], p->zName, p->len);
|
2008-12-08 21:19:17 +03:00
|
|
|
k += p->len;
|
|
|
|
if( j+p->len>70 ){
|
|
|
|
printf("%*s */\n", 74-j, "");
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
if( j==0 ){
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* ");
|
2008-12-08 21:19:17 +03:00
|
|
|
j = 8;
|
|
|
|
}
|
2004-10-07 23:03:01 +04:00
|
|
|
printf("%s", p->zName);
|
|
|
|
j += p->len;
|
2008-12-08 21:19:17 +03:00
|
|
|
}
|
|
|
|
if( j>0 ){
|
|
|
|
printf("%*s */\n", 74-j, "");
|
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("static const char zKWText[%d] = {\n", nChar);
|
|
|
|
zKWText[nChar] = 0;
|
2009-01-01 00:52:40 +03:00
|
|
|
for(i=j=0; i<k; i++){
|
2008-12-08 21:19:17 +03:00
|
|
|
if( j==0 ){
|
2017-07-06 19:33:14 +03:00
|
|
|
printf(" ");
|
2008-12-08 21:19:17 +03:00
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
if( zKWText[i]==0 ){
|
2008-12-08 21:19:17 +03:00
|
|
|
printf("0");
|
|
|
|
}else{
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("'%c',", zKWText[i]);
|
2008-12-08 21:19:17 +03:00
|
|
|
}
|
|
|
|
j += 4;
|
|
|
|
if( j>68 ){
|
|
|
|
printf("\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 21:19:17 +03:00
|
|
|
if( j>0 ) printf("\n");
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("};\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* aKWHash[i] is the hash value for the i-th keyword */\n");
|
|
|
|
printf("static const unsigned char aKWHash[%d] = {\n", bestSize);
|
2004-10-07 23:03:01 +04:00
|
|
|
for(i=j=0; i<bestSize; i++){
|
2017-07-06 19:33:14 +03:00
|
|
|
if( j==0 ) printf(" ");
|
|
|
|
printf(" %3d,", aKWHash[i]);
|
2004-10-07 23:03:01 +04:00
|
|
|
j++;
|
|
|
|
if( j>12 ){
|
|
|
|
printf("\n");
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("%s};\n", j==0 ? "" : "\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* aKWNext[] forms the hash collision chain. If aKWHash[i]==0\n");
|
|
|
|
printf("** then the i-th keyword has no more hash collisions. Otherwise,\n");
|
|
|
|
printf("** the next keyword with the same hash is aKWHash[i]-1. */\n");
|
|
|
|
printf("static const unsigned char aKWNext[%d] = {\n", nKeyword);
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=j=0; i<nKeyword; i++){
|
2017-07-06 19:33:14 +03:00
|
|
|
if( j==0 ) printf(" ");
|
2004-10-07 23:03:01 +04:00
|
|
|
printf(" %3d,", aKeywordTable[i].iNext);
|
|
|
|
j++;
|
|
|
|
if( j>12 ){
|
|
|
|
printf("\n");
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("%s};\n", j==0 ? "" : "\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* aKWLen[i] is the length (in bytes) of the i-th keyword */\n");
|
|
|
|
printf("static const unsigned char aKWLen[%d] = {\n", nKeyword);
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=j=0; i<nKeyword; i++){
|
2017-07-06 19:33:14 +03:00
|
|
|
if( j==0 ) printf(" ");
|
2004-10-23 09:10:18 +04:00
|
|
|
printf(" %3d,", aKeywordTable[i].len+aKeywordTable[i].prefix);
|
2004-10-07 23:03:01 +04:00
|
|
|
j++;
|
|
|
|
if( j>12 ){
|
|
|
|
printf("\n");
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("%s};\n", j==0 ? "" : "\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* aKWOffset[i] is the index into zKWText[] of the start of\n");
|
|
|
|
printf("** the text for the i-th keyword. */\n");
|
|
|
|
printf("static const unsigned short int aKWOffset[%d] = {\n", nKeyword);
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=j=0; i<nKeyword; i++){
|
2017-07-06 19:33:14 +03:00
|
|
|
if( j==0 ) printf(" ");
|
2004-10-07 23:03:01 +04:00
|
|
|
printf(" %3d,", aKeywordTable[i].offset);
|
|
|
|
j++;
|
|
|
|
if( j>12 ){
|
|
|
|
printf("\n");
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("%s};\n", j==0 ? "" : "\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("/* aKWCode[i] is the parser symbol code for the i-th keyword */\n");
|
|
|
|
printf("static const unsigned char aKWCode[%d] = {\n", nKeyword);
|
2007-05-04 21:07:52 +04:00
|
|
|
for(i=j=0; i<nKeyword; i++){
|
2004-10-07 23:03:01 +04:00
|
|
|
char *zToken = aKeywordTable[i].zTokenType;
|
2017-07-06 19:33:14 +03:00
|
|
|
if( j==0 ) printf(" ");
|
2004-10-07 23:03:01 +04:00
|
|
|
printf("%s,%*s", zToken, (int)(14-strlen(zToken)), "");
|
|
|
|
j++;
|
|
|
|
if( j>=5 ){
|
|
|
|
printf("\n");
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf("%s};\n", j==0 ? "" : "\n");
|
|
|
|
printf("/* Check to see if z[0..n-1] is a keyword. If it is, write the\n");
|
|
|
|
printf("** parser symbol code for that keyword into *pType. Always\n");
|
|
|
|
printf("** return the integer n (the length of the token). */\n");
|
|
|
|
printf("static int keywordCode(const char *z, int n, int *pType){\n");
|
2016-02-08 22:15:48 +03:00
|
|
|
printf(" int i, j;\n");
|
2016-02-08 06:23:46 +03:00
|
|
|
printf(" const char *zKW;\n");
|
2015-11-10 06:30:51 +03:00
|
|
|
printf(" if( n>=2 ){\n");
|
2016-02-08 22:15:48 +03:00
|
|
|
printf(" i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) %% %d;\n",
|
2015-11-10 06:30:51 +03:00
|
|
|
bestSize);
|
2017-07-06 19:33:14 +03:00
|
|
|
printf(" for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){\n");
|
|
|
|
printf(" if( aKWLen[i]!=n ) continue;\n");
|
2016-02-08 06:23:46 +03:00
|
|
|
printf(" j = 0;\n");
|
2017-07-06 19:33:14 +03:00
|
|
|
printf(" zKW = &zKWText[aKWOffset[i]];\n");
|
2016-02-08 22:15:48 +03:00
|
|
|
printf("#ifdef SQLITE_ASCII\n");
|
2016-02-08 06:23:46 +03:00
|
|
|
printf(" while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }\n");
|
2016-02-08 22:15:48 +03:00
|
|
|
printf("#endif\n");
|
|
|
|
printf("#ifdef SQLITE_EBCDIC\n");
|
|
|
|
printf(" while( j<n && toupper(z[j])==zKW[j] ){ j++; }\n");
|
|
|
|
printf("#endif\n");
|
2016-02-08 06:23:46 +03:00
|
|
|
printf(" if( j<n ) continue;\n");
|
2009-01-01 00:52:40 +03:00
|
|
|
for(i=0; i<nKeyword; i++){
|
2016-02-08 06:23:46 +03:00
|
|
|
printf(" testcase( i==%d ); /* %s */\n",
|
2009-06-09 18:27:41 +04:00
|
|
|
i, aKeywordTable[i].zOrigName);
|
2009-01-01 00:52:40 +03:00
|
|
|
}
|
2017-07-06 19:33:14 +03:00
|
|
|
printf(" *pType = aKWCode[i];\n");
|
2016-02-08 06:23:46 +03:00
|
|
|
printf(" break;\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
printf(" }\n");
|
|
|
|
printf(" }\n");
|
2015-11-10 06:30:51 +03:00
|
|
|
printf(" return n;\n");
|
2004-10-07 23:03:01 +04:00
|
|
|
printf("}\n");
|
2005-12-09 23:02:05 +03:00
|
|
|
printf("int sqlite3KeywordCode(const unsigned char *z, int n){\n");
|
2015-11-10 06:30:51 +03:00
|
|
|
printf(" int id = TK_ID;\n");
|
|
|
|
printf(" keywordCode((char*)z, n, &id);\n");
|
|
|
|
printf(" return id;\n");
|
2005-01-31 15:42:29 +03:00
|
|
|
printf("}\n");
|
2010-01-02 06:21:35 +03:00
|
|
|
printf("#define SQLITE_N_KEYWORD %d\n", nKeyword);
|
2004-10-07 23:03:01 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|