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