4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** An tokenizer for SQL
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.
25 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msi);
33 ** All the keywords of the SQL language are stored as in a hash
34 ** table composed of instances of the following structure.
36 typedef struct Keyword Keyword;
38 const char *zName; /* The keyword name */
39 int tokenType; /* The token value for this keyword */
43 ** These are the keywords
45 static const Keyword aKeywordTable[] = {
46 { "ABORT", TK_ABORT },
47 { "AFTER", TK_AFTER },
52 { "BEFORE", TK_BEFORE },
53 { "BEGIN", TK_BEGIN },
54 { "BETWEEN", TK_BETWEEN },
56 { "CASCADE", TK_CASCADE },
59 { "CHARACTER", TK_CHAR },
60 { "CHECK", TK_CHECK },
61 { "CLUSTER", TK_CLUSTER },
62 { "COLLATE", TK_COLLATE },
63 { "COMMIT", TK_COMMIT },
64 { "CONFLICT", TK_CONFLICT },
65 { "CONSTRAINT", TK_CONSTRAINT },
67 { "CREATE", TK_CREATE },
68 { "CROSS", TK_JOIN_KW },
69 { "DEFAULT", TK_DEFAULT },
70 { "DEFERRED", TK_DEFERRED },
71 { "DEFERRABLE", TK_DEFERRABLE },
72 { "DELETE", TK_DELETE },
73 { "DELIMITERS", TK_DELIMITERS },
75 { "DISTINCT", TK_DISTINCT },
80 { "EXCEPT", TK_EXCEPT },
81 { "EXPLAIN", TK_EXPLAIN },
84 { "FOREIGN", TK_FOREIGN },
86 { "FULL", TK_JOIN_KW },
88 { "GROUP", TK_GROUP },
89 { "HAVING", TK_HAVING },
91 { "IGNORE", TK_IGNORE },
92 { "IMMEDIATE", TK_IMMEDIATE },
94 { "INDEX", TK_INDEX },
95 { "INITIALLY", TK_INITIALLY },
96 { "INNER", TK_JOIN_KW },
97 { "INSERT", TK_INSERT },
98 { "INSTEAD", TK_INSTEAD },
100 { "INTERSECT", TK_INTERSECT },
103 { "ISNULL", TK_ISNULL },
106 { "LEFT", TK_JOIN_KW },
108 { "LIMIT", TK_LIMIT },
109 { "LOCALIZABLE", TK_LOCALIZABLE },
111 { "LONGCHAR", TK_LONGCHAR },
112 { "MATCH", TK_MATCH },
113 { "NATURAL", TK_JOIN_KW },
115 { "NOTNULL", TK_NOTNULL },
117 { "OBJECT", TK_OBJECT },
119 { "OFFSET", TK_OFFSET },
122 { "ORDER", TK_ORDER },
123 { "OUTER", TK_JOIN_KW },
124 { "PRAGMA", TK_PRAGMA },
125 { "PRIMARY", TK_PRIMARY },
126 { "RAISE", TK_RAISE },
127 { "REFERENCES", TK_REFERENCES },
128 { "REPLACE", TK_REPLACE },
129 { "RESTRICT", TK_RESTRICT },
130 { "RIGHT", TK_JOIN_KW },
131 { "ROLLBACK", TK_ROLLBACK },
133 { "SELECT", TK_SELECT },
135 { "SHORT", TK_SHORT },
136 { "STATEMENT", TK_STATEMENT },
137 { "TABLE", TK_TABLE },
139 { "TEMPORARY", TK_TEMP },
141 { "TRANSACTION", TK_TRANSACTION },
142 { "TRIGGER", TK_TRIGGER },
143 { "UNION", TK_UNION },
144 { "UNIQUE", TK_UNIQUE },
145 { "UPDATE", TK_UPDATE },
146 { "USING", TK_USING },
147 { "VACUUM", TK_VACUUM },
148 { "VALUES", TK_VALUES },
151 { "WHERE", TK_WHERE },
154 #define KEYWORD_COUNT ( sizeof aKeywordTable/sizeof (Keyword) )
157 ** This function looks up an identifier to determine if it is a
158 ** keyword. If it is a keyword, the token code of that keyword is
159 ** returned. If the input is not a keyword, TK_ID is returned.
161 static int sqliteKeywordCode(const WCHAR *z, int n){
165 len = WideCharToMultiByte( CP_ACP, 0, z, n, buffer, sizeof buffer, NULL, NULL );
167 buffer[i] = toupper(buffer[i]);
168 for(i=0; i<KEYWORD_COUNT; i++)
170 if(memcmp(buffer, aKeywordTable[i].zName, len))
172 if(strlen(aKeywordTable[i].zName) == len )
173 return aKeywordTable[i].tokenType;
180 ** If X is a character that can be used in an identifier then
181 ** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
183 ** In this implementation, an identifier can be a string of
184 ** alphabetic characters, digits, and "_" plus any character
185 ** with the high-order bit set. The latter rule means that
186 ** any sequence of UTF-8 characters or characters taken from
187 ** an extended ISO8859 character set can form an identifier.
189 static const char isIdChar[] = {
190 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
192 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
194 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
195 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
196 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
197 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
198 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
199 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
200 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
201 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
202 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
203 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
204 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
205 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
211 ** Return the length of the token that begins at z[0]. Return
212 ** -1 if the token is (or might be) incomplete. Store the token
213 ** type in *tokenType before returning.
215 int sqliteGetToken(const WCHAR *z, int *tokenType){
218 case ' ': case '\t': case '\n': case '\f': case '\r': {
219 for(i=1; isspace(z[i]); i++){}
220 *tokenType = TK_SPACE;
224 if( z[1]==0 ) return -1;
226 for(i=2; z[i] && z[i]!='\n'; i++){}
227 *tokenType = TK_COMMENT;
230 *tokenType = TK_MINUS;
234 if( z[1]=='+' && z[2]==')' ){
235 *tokenType = TK_ORACLE_OUTER_JOIN;
247 *tokenType = TK_SEMI;
251 *tokenType = TK_PLUS;
255 *tokenType = TK_STAR;
259 if( z[1]!='*' || z[2]==0 ){
260 *tokenType = TK_SLASH;
263 for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
265 *tokenType = TK_COMMENT;
274 return 1 + (z[1]=='=');
280 }else if( z[1]=='>' ){
283 }else if( z[1]=='<' ){
284 *tokenType = TK_LSHIFT;
295 }else if( z[1]=='>' ){
296 *tokenType = TK_RSHIFT;
305 *tokenType = TK_ILLEGAL;
314 *tokenType = TK_BITOR;
317 *tokenType = TK_CONCAT;
322 *tokenType = TK_WILDCARD;
326 *tokenType = TK_COMMA;
330 *tokenType = TK_BITAND;
334 *tokenType = TK_BITNOT;
337 case '`': case '\'': case '"': {
352 *tokenType = TK_STRING;
356 if( !isdigit(z[1]) ){
360 /* Fall thru into the next case */
362 case '0': case '1': case '2': case '3': case '4':
363 case '5': case '6': case '7': case '8': case '9': {
364 *tokenType = TK_INTEGER;
365 for(i=1; isdigit(z[i]); i++){}
368 while( isdigit(z[i]) ){ i++; }
369 *tokenType = TK_FLOAT;
371 if( (z[i]=='e' || z[i]=='E') &&
373 || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
377 while( isdigit(z[i]) ){ i++; }
378 *tokenType = TK_FLOAT;
379 }else if( z[0]=='.' ){
380 *tokenType = TK_FLOAT;
385 for(i=1; z[i] && z[i-1]!=']'; i++){}
393 for(i=1; isIdChar[z[i]]; i++){}
394 *tokenType = sqliteKeywordCode(z, i);
398 *tokenType = TK_ILLEGAL;