28bdde309a5ce548a9b391e9409caa2b658a472a
[reactos.git] / reactos / dll / win32 / 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 ** A 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 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define COM_NO_WINDOWS_H
22
23 //#include <ctype.h>
24 //#include <stdarg.h>
25 //#include <stdlib.h>
26
27 //#include "windef.h"
28 //#include "winbase.h"
29 #include <wine/unicode.h>
30 #include "query.h"
31 #include "sql.tab.h"
32
33 /*
34 ** All the keywords of the SQL language are stored as in a hash
35 ** table composed of instances of the following structure.
36 */
37 typedef struct Keyword Keyword;
38 struct Keyword {
39 const WCHAR *name; /* The keyword name */
40 unsigned int len;
41 int tokenType; /* The token value for this keyword */
42 };
43
44 #define MAX_TOKEN_LEN 11
45
46 static const WCHAR addW[] = {'A','D','D'};
47 static const WCHAR alterW[] = {'A','L','T','E','R'};
48 static const WCHAR andW[] = {'A','N','D'};
49 static const WCHAR byW[] = {'B','Y'};
50 static const WCHAR charW[] = {'C','H','A','R'};
51 static const WCHAR characterW[] = {'C','H','A','R','A','C','T','E','R'};
52 static const WCHAR createW[] = {'C','R','E','A','T','E'};
53 static const WCHAR deleteW[] = {'D','E','L','E','T','E'};
54 static const WCHAR distinctW[] = {'D','I','S','T','I','N','C','T'};
55 static const WCHAR dropW[] = {'D','R','O','P'};
56 static const WCHAR freeW[] = {'F','R','E','E'};
57 static const WCHAR fromW[] = {'F','R','O','M'};
58 static const WCHAR holdW[] = {'H','O','L','D'};
59 static const WCHAR insertW[] = {'I','N','S','E','R','T'};
60 static const WCHAR intW[] = {'I','N','T'};
61 static const WCHAR integerW[] = {'I','N','T','E','G','E','R'};
62 static const WCHAR intoW[] = {'I','N','T','O'};
63 static const WCHAR isW[] = {'I','S'};
64 static const WCHAR keyW[] = {'K','E','Y'};
65 static const WCHAR likeW[] = {'L','I','K','E'};
66 static const WCHAR localizableW[] = {'L','O','C','A','L','I','Z','A','B','L','E'};
67 static const WCHAR longW[] = {'L','O','N','G'};
68 static const WCHAR longcharW[] = {'L','O','N','G','C','H','A','R'};
69 static const WCHAR notW[] = {'N','O','T'};
70 static const WCHAR nullW[] = {'N','U','L','L'};
71 static const WCHAR objectW[] = {'O','B','J','E','C','T'};
72 static const WCHAR orW[] = {'O','R'};
73 static const WCHAR orderW[] = {'O','R','D','E','R'};
74 static const WCHAR primaryW[] = {'P','R','I','M','A','R','Y'};
75 static const WCHAR selectW[] = {'S','E','L','E','C','T'};
76 static const WCHAR setW[] = {'S','E','T'};
77 static const WCHAR shortW[] = {'S','H','O','R','T'};
78 static const WCHAR tableW[] = {'T','A','B','L','E'};
79 static const WCHAR temporaryW[] = {'T','E','M','P','O','R','A','R','Y'};
80 static const WCHAR updateW[] = {'U','P','D','A','T','E'};
81 static const WCHAR valuesW[] = {'V','A','L','U','E','S'};
82 static const WCHAR whereW[] = {'W','H','E','R','E'};
83
84 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
85
86 /*
87 ** These are the keywords
88 ** They MUST be in alphabetical order
89 */
90 static const Keyword aKeywordTable[] = {
91 { addW, ARRAY_SIZE(addW), TK_ADD },
92 { alterW, ARRAY_SIZE(alterW), TK_ALTER },
93 { andW, ARRAY_SIZE(andW), TK_AND },
94 { byW, ARRAY_SIZE(byW), TK_BY },
95 { charW, ARRAY_SIZE(charW), TK_CHAR },
96 { characterW, ARRAY_SIZE(characterW), TK_CHAR },
97 { createW, ARRAY_SIZE(createW), TK_CREATE },
98 { deleteW, ARRAY_SIZE(deleteW), TK_DELETE },
99 { distinctW, ARRAY_SIZE(distinctW), TK_DISTINCT },
100 { dropW, ARRAY_SIZE(dropW), TK_DROP },
101 { freeW, ARRAY_SIZE(freeW), TK_FREE },
102 { fromW, ARRAY_SIZE(fromW), TK_FROM },
103 { holdW, ARRAY_SIZE(holdW), TK_HOLD },
104 { insertW, ARRAY_SIZE(insertW), TK_INSERT },
105 { intW, ARRAY_SIZE(intW), TK_INT },
106 { integerW, ARRAY_SIZE(integerW), TK_INT },
107 { intoW, ARRAY_SIZE(intoW), TK_INTO },
108 { isW, ARRAY_SIZE(isW), TK_IS },
109 { keyW, ARRAY_SIZE(keyW), TK_KEY },
110 { likeW, ARRAY_SIZE(likeW), TK_LIKE },
111 { localizableW, ARRAY_SIZE(localizableW), TK_LOCALIZABLE },
112 { longW, ARRAY_SIZE(longW), TK_LONG },
113 { longcharW, ARRAY_SIZE(longcharW), TK_LONGCHAR },
114 { notW, ARRAY_SIZE(notW), TK_NOT },
115 { nullW, ARRAY_SIZE(nullW), TK_NULL },
116 { objectW, ARRAY_SIZE(objectW), TK_OBJECT },
117 { orW, ARRAY_SIZE(orW), TK_OR },
118 { orderW, ARRAY_SIZE(orderW), TK_ORDER },
119 { primaryW, ARRAY_SIZE(primaryW), TK_PRIMARY },
120 { selectW, ARRAY_SIZE(selectW), TK_SELECT },
121 { setW, ARRAY_SIZE(setW), TK_SET },
122 { shortW, ARRAY_SIZE(shortW), TK_SHORT },
123 { tableW, ARRAY_SIZE(tableW), TK_TABLE },
124 { temporaryW, ARRAY_SIZE(temporaryW), TK_TEMPORARY },
125 { updateW, ARRAY_SIZE(updateW), TK_UPDATE },
126 { valuesW, ARRAY_SIZE(valuesW), TK_VALUES },
127 { whereW, ARRAY_SIZE(whereW), TK_WHERE },
128 };
129
130 /*
131 ** Comparison function for binary search.
132 */
133 static int compKeyword(const void *m1, const void *m2){
134 const Keyword *k1 = m1, *k2 = m2;
135 int ret, len = min( k1->len, k2->len );
136
137 if ((ret = memicmpW( k1->name, k2->name, len ))) return ret;
138 if (k1->len < k2->len) return -1;
139 else if (k1->len > k2->len) return 1;
140 return 0;
141 }
142
143 /*
144 ** This function looks up an identifier to determine if it is a
145 ** keyword. If it is a keyword, the token code of that keyword is
146 ** returned. If the input is not a keyword, TK_ID is returned.
147 */
148 static int sqliteKeywordCode(const WCHAR *z, int n){
149 Keyword key, *r;
150
151 if( n>MAX_TOKEN_LEN )
152 return TK_ID;
153
154 key.tokenType = 0;
155 key.name = z;
156 key.len = n;
157 r = bsearch( &key, aKeywordTable, ARRAY_SIZE(aKeywordTable), sizeof(Keyword), compKeyword );
158 if( r )
159 return r->tokenType;
160 return TK_ID;
161 }
162
163
164 /*
165 ** If X is a character that can be used in an identifier then
166 ** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
167 **
168 ** In this implementation, an identifier can be a string of
169 ** alphabetic characters, digits, and "_" plus any character
170 ** with the high-order bit set. The latter rule means that
171 ** any sequence of UTF-8 characters or characters taken from
172 ** an extended ISO8859 character set can form an identifier.
173 */
174 static const char isIdChar[] = {
175 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
176 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 2x */
179 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
180 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
181 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
182 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
183 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
184 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
185 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
186 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
187 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
188 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
189 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
190 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
191 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
192 };
193
194
195 /*
196 ** Return the length of the token that begins at z[0]. Return
197 ** -1 if the token is (or might be) incomplete. Store the token
198 ** type in *tokenType before returning.
199 */
200 int sqliteGetToken(const WCHAR *z, int *tokenType, int *skip){
201 int i;
202
203 *skip = 0;
204 switch( *z ){
205 case ' ': case '\t': case '\n': case '\f':
206 for(i=1; isspace(z[i]) && z[i] != '\r'; i++){}
207 *tokenType = TK_SPACE;
208 return i;
209 case '-':
210 if( z[1]==0 ) return -1;
211 *tokenType = TK_MINUS;
212 return 1;
213 case '(':
214 *tokenType = TK_LP;
215 return 1;
216 case ')':
217 *tokenType = TK_RP;
218 return 1;
219 case '*':
220 *tokenType = TK_STAR;
221 return 1;
222 case '=':
223 *tokenType = TK_EQ;
224 return 1;
225 case '<':
226 if( z[1]=='=' ){
227 *tokenType = TK_LE;
228 return 2;
229 }else if( z[1]=='>' ){
230 *tokenType = TK_NE;
231 return 2;
232 }else{
233 *tokenType = TK_LT;
234 return 1;
235 }
236 case '>':
237 if( z[1]=='=' ){
238 *tokenType = TK_GE;
239 return 2;
240 }else{
241 *tokenType = TK_GT;
242 return 1;
243 }
244 case '!':
245 if( z[1]!='=' ){
246 *tokenType = TK_ILLEGAL;
247 return 2;
248 }else{
249 *tokenType = TK_NE;
250 return 2;
251 }
252 case '?':
253 *tokenType = TK_WILDCARD;
254 return 1;
255 case ',':
256 *tokenType = TK_COMMA;
257 return 1;
258 case '`': case '\'': {
259 int delim = z[0];
260 for(i=1; z[i]; i++){
261 if( z[i]==delim )
262 break;
263 }
264 if( z[i] ) i++;
265 if( delim == '`' )
266 *tokenType = TK_ID;
267 else
268 *tokenType = TK_STRING;
269 return i;
270 }
271 case '.':
272 if( !isdigit(z[1]) ){
273 *tokenType = TK_DOT;
274 return 1;
275 }
276 /* Fall through */
277 case '0': case '1': case '2': case '3': case '4':
278 case '5': case '6': case '7': case '8': case '9':
279 *tokenType = TK_INTEGER;
280 for(i=1; isdigit(z[i]); i++){}
281 return i;
282 case '[':
283 for(i=1; z[i] && z[i-1]!=']'; i++){}
284 *tokenType = TK_ID;
285 return i;
286 default:
287 if( !isIdChar[*z] ){
288 break;
289 }
290 for(i=1; isIdChar[z[i]]; i++){}
291 *tokenType = sqliteKeywordCode(z, i);
292 if( *tokenType == TK_ID && z[i] == '`' ) *skip = 1;
293 return i;
294 }
295 *tokenType = TK_ILLEGAL;
296 return 1;
297 }