Fix the case of product and company names.
[wine] / dlls / msi / tokenize.c
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
19 #include <ctype.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wine/debug.h"
26 #include "winnls.h"
27 #include "query.h"
28 #include "y.tab.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msi);
31
32 /*
33 ** All the keywords of the SQL language are stored as in a hash
34 ** table composed of instances of the following structure.
35 */
36 typedef struct Keyword Keyword;
37 struct Keyword {
38   const char *zName;             /* The keyword name */
39   int tokenType;           /* The token value for this keyword */
40 };
41
42 /*
43 ** These are the keywords
44 */
45 static const Keyword aKeywordTable[] = {
46   { "ABORT", TK_ABORT },
47   { "AFTER", TK_AFTER },
48   { "ALL", TK_ALL },
49   { "AND", TK_AND },
50   { "AS", TK_AS },
51   { "ASC", TK_ASC },
52   { "BEFORE", TK_BEFORE },
53   { "BEGIN", TK_BEGIN },
54   { "BETWEEN", TK_BETWEEN },
55   { "BY", TK_BY },
56   { "CASCADE", TK_CASCADE },
57   { "CASE", TK_CASE },
58   { "CHECK", TK_CHECK },
59   { "CLUSTER", TK_CLUSTER },
60   { "COLLATE", TK_COLLATE },
61   { "COMMIT", TK_COMMIT },
62   { "CONFLICT", TK_CONFLICT },
63   { "CONSTRAINT", TK_CONSTRAINT },
64   { "COPY", TK_COPY },
65   { "CREATE", TK_CREATE },
66   { "CROSS", TK_JOIN_KW },
67   { "DEFAULT", TK_DEFAULT },
68   { "DEFERRED", TK_DEFERRED },
69   { "DEFERRABLE", TK_DEFERRABLE },
70   { "DELETE", TK_DELETE },
71   { "DELIMITERS", TK_DELIMITERS },
72   { "DESC", TK_DESC },
73   { "DISTINCT", TK_DISTINCT },
74   { "DROP", TK_DROP },
75   { "END", TK_END },
76   { "EACH", TK_EACH },
77   { "ELSE", TK_ELSE },
78   { "EXCEPT", TK_EXCEPT },
79   { "EXPLAIN", TK_EXPLAIN },
80   { "FAIL", TK_FAIL },
81   { "FOR", TK_FOR },
82   { "FOREIGN", TK_FOREIGN },
83   { "FROM", TK_FROM },
84   { "FULL", TK_JOIN_KW },
85   { "GLOB", TK_GLOB },
86   { "GROUP", TK_GROUP },
87   { "HAVING", TK_HAVING },
88   { "IGNORE", TK_IGNORE },
89   { "IMMEDIATE", TK_IMMEDIATE },
90   { "IN", TK_IN },
91   { "INDEX", TK_INDEX },
92   { "INITIALLY", TK_INITIALLY },
93   { "INNER", TK_JOIN_KW },
94   { "INSERT", TK_INSERT },
95   { "INSTEAD", TK_INSTEAD },
96   { "INTERSECT", TK_INTERSECT },
97   { "INTO", TK_INTO },
98   { "IS", TK_IS },
99   { "ISNULL", TK_ISNULL },
100   { "JOIN", TK_JOIN },
101   { "KEY", TK_KEY },
102   { "LEFT", TK_JOIN_KW },
103   { "LIKE", TK_LIKE },
104   { "LIMIT", TK_LIMIT },
105   { "MATCH", TK_MATCH },
106   { "NATURAL", TK_JOIN_KW },
107   { "NOT", TK_NOT },
108   { "NOTNULL", TK_NOTNULL },
109   { "NULL", TK_NULL },
110   { "OF", TK_OF },
111   { "OFFSET", TK_OFFSET },
112   { "ON", TK_ON },
113   { "OR", TK_OR },
114   { "ORDER", TK_ORDER },
115   { "OUTER", TK_JOIN_KW },
116   { "PRAGMA", TK_PRAGMA },
117   { "PRIMARY", TK_PRIMARY },
118   { "RAISE", TK_RAISE },
119   { "REFERENCES", TK_REFERENCES },
120   { "REPLACE", TK_REPLACE },
121   { "RESTRICT", TK_RESTRICT },
122   { "RIGHT", TK_JOIN_KW },
123   { "ROLLBACK", TK_ROLLBACK },
124   { "ROW", TK_ROW },
125   { "SELECT", TK_SELECT },
126   { "SET", TK_SET },
127   { "STATEMENT", TK_STATEMENT },
128   { "TABLE", TK_TABLE },
129   { "TEMP", TK_TEMP },
130   { "TEMPORARY", TK_TEMP },
131   { "THEN", TK_THEN },
132   { "TRANSACTION", TK_TRANSACTION },
133   { "TRIGGER", TK_TRIGGER },
134   { "UNION", TK_UNION },
135   { "UNIQUE", TK_UNIQUE },
136   { "UPDATE", TK_UPDATE },
137   { "USING", TK_USING },
138   { "VACUUM", TK_VACUUM },
139   { "VALUES", TK_VALUES },
140   { "VIEW", TK_VIEW },
141   { "WHEN", TK_WHEN },
142   { "WHERE", TK_WHERE },
143 };
144
145 #define KEYWORD_COUNT ( sizeof aKeywordTable/sizeof (Keyword) )
146
147 /*
148 ** This function looks up an identifier to determine if it is a
149 ** keyword.  If it is a keyword, the token code of that keyword is 
150 ** returned.  If the input is not a keyword, TK_ID is returned.
151 */
152 int sqliteKeywordCode(const WCHAR *z, int n){
153   int i, len;
154   char buffer[0x10];
155
156   len = WideCharToMultiByte( CP_ACP, 0, z, n, buffer, sizeof buffer, NULL, NULL );
157   for(i=0; i<KEYWORD_COUNT; i++)
158   {
159       if(memcmp(buffer, aKeywordTable[i].zName, len))
160           continue;
161       if(strlen(aKeywordTable[i].zName) == len )
162           return aKeywordTable[i].tokenType;
163   }
164   return TK_ID;
165 }
166
167
168 /*
169 ** If X is a character that can be used in an identifier then
170 ** isIdChar[X] will be 1.  Otherwise isIdChar[X] will be 0.
171 **
172 ** In this implementation, an identifier can be a string of
173 ** alphabetic characters, digits, and "_" plus any character
174 ** with the high-order bit set.  The latter rule means that
175 ** any sequence of UTF-8 characters or characters taken from
176 ** an extended ISO8859 character set can form an identifier.
177 */
178 static const char isIdChar[] = {
179 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
180     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
181     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
182     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
183     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
184     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
185     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
186     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
187     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
188     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 8x */
189     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 9x */
190     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ax */
191     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Bx */
192     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Cx */
193     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Dx */
194     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Ex */
195     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* Fx */
196 };
197
198
199 /*
200 ** Return the length of the token that begins at z[0].  Return
201 ** -1 if the token is (or might be) incomplete.  Store the token
202 ** type in *tokenType before returning.
203 */
204 int sqliteGetToken(const WCHAR *z, int *tokenType){
205   int i;
206   switch( *z ){
207     case ' ': case '\t': case '\n': case '\f': case '\r': {
208       for(i=1; isspace(z[i]); i++){}
209       *tokenType = TK_SPACE;
210       return i;
211     }
212     case '-': {
213       if( z[1]==0 ) return -1;
214       if( z[1]=='-' ){
215         for(i=2; z[i] && z[i]!='\n'; i++){}
216         *tokenType = TK_COMMENT;
217         return i;
218       }
219       *tokenType = TK_MINUS;
220       return 1;
221     }
222     case '(': {
223       if( z[1]=='+' && z[2]==')' ){
224         *tokenType = TK_ORACLE_OUTER_JOIN;
225         return 3;
226       }else{
227         *tokenType = TK_LP;
228         return 1;
229       }
230     }
231     case ')': {
232       *tokenType = TK_RP;
233       return 1;
234     }
235     case ';': {
236       *tokenType = TK_SEMI;
237       return 1;
238     }
239     case '+': {
240       *tokenType = TK_PLUS;
241       return 1;
242     }
243     case '*': {
244       *tokenType = TK_STAR;
245       return 1;
246     }
247     case '/': {
248       if( z[1]!='*' || z[2]==0 ){
249         *tokenType = TK_SLASH;
250         return 1;
251       }
252       for(i=3; z[i] && (z[i]!='/' || z[i-1]!='*'); i++){}
253       if( z[i] ) i++;
254       *tokenType = TK_COMMENT;
255       return i;
256     }
257     case '%': {
258       *tokenType = TK_REM;
259       return 1;
260     }
261     case '=': {
262       *tokenType = TK_EQ;
263       return 1 + (z[1]=='=');
264     }
265     case '<': {
266       if( z[1]=='=' ){
267         *tokenType = TK_LE;
268         return 2;
269       }else if( z[1]=='>' ){
270         *tokenType = TK_NE;
271         return 2;
272       }else if( z[1]=='<' ){
273         *tokenType = TK_LSHIFT;
274         return 2;
275       }else{
276         *tokenType = TK_LT;
277         return 1;
278       }
279     }
280     case '>': {
281       if( z[1]=='=' ){
282         *tokenType = TK_GE;
283         return 2;
284       }else if( z[1]=='>' ){
285         *tokenType = TK_RSHIFT;
286         return 2;
287       }else{
288         *tokenType = TK_GT;
289         return 1;
290       }
291     }
292     case '!': {
293       if( z[1]!='=' ){
294         *tokenType = TK_ILLEGAL;
295         return 2;
296       }else{
297         *tokenType = TK_NE;
298         return 2;
299       }
300     }
301     case '|': {
302       if( z[1]!='|' ){
303         *tokenType = TK_BITOR;
304         return 1;
305       }else{
306         *tokenType = TK_CONCAT;
307         return 2;
308       }
309     }
310     case ',': {
311       *tokenType = TK_COMMA;
312       return 1;
313     }
314     case '&': {
315       *tokenType = TK_BITAND;
316       return 1;
317     }
318     case '~': {
319       *tokenType = TK_BITNOT;
320       return 1;
321     }
322     case '`': case '\'': case '"': {
323       int delim = z[0];
324       for(i=1; z[i]; i++){
325         if( z[i]==delim ){
326           if( z[i+1]==delim ){
327             i++;
328           }else{
329             break;
330           }
331         }
332       }
333       if( z[i] ) i++;
334       *tokenType = TK_STRING;
335       return i;
336     }
337     case '.': {
338       if( !isdigit(z[1]) ){
339         *tokenType = TK_DOT;
340         return 1;
341       }
342       /* Fall thru into the next case */
343     }
344     case '0': case '1': case '2': case '3': case '4':
345     case '5': case '6': case '7': case '8': case '9': {
346       *tokenType = TK_INTEGER;
347       for(i=1; isdigit(z[i]); i++){}
348       if( z[i]=='.' ){
349         i++;
350         while( isdigit(z[i]) ){ i++; }
351         *tokenType = TK_FLOAT;
352       }
353       if( (z[i]=='e' || z[i]=='E') &&
354            ( isdigit(z[i+1]) 
355             || ((z[i+1]=='+' || z[i+1]=='-') && isdigit(z[i+2]))
356            )
357       ){
358         i += 2;
359         while( isdigit(z[i]) ){ i++; }
360         *tokenType = TK_FLOAT;
361       }else if( z[0]=='.' ){
362         *tokenType = TK_FLOAT;
363       }
364       return i;
365     }
366     case '[': {
367       for(i=1; z[i] && z[i-1]!=']'; i++){}
368       *tokenType = TK_ID;
369       return i;
370     }
371     default: {
372       if( !isIdChar[*z] ){
373         break;
374       }
375       for(i=1; isIdChar[z[i]]; i++){}
376       *tokenType = sqliteKeywordCode(z, i);
377       return i;
378     }
379   }
380   *tokenType = TK_ILLEGAL;
381   return 1;
382 }