[MSI]
[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 #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 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
74
75 /*
76 ** These are the keywords
77 ** They MUST be in alphabetical order
78 */
79 static const Keyword aKeywordTable[] = {
80 { addW, ARRAY_SIZE(addW), TK_ADD },
81 { alterW, ARRAY_SIZE(alterW), TK_ALTER },
82 { andW, ARRAY_SIZE(andW), TK_AND },
83 { byW, ARRAY_SIZE(byW), TK_BY },
84 { charW, ARRAY_SIZE(charW), TK_CHAR },
85 { characterW, ARRAY_SIZE(characterW), TK_CHAR },
86 { createW, ARRAY_SIZE(createW), TK_CREATE },
87 { deleteW, ARRAY_SIZE(deleteW), TK_DELETE },
88 { distinctW, ARRAY_SIZE(distinctW), TK_DISTINCT },
89 { dropW, ARRAY_SIZE(dropW), TK_DROP },
90 { freeW, ARRAY_SIZE(freeW), TK_FREE },
91 { fromW, ARRAY_SIZE(fromW), TK_FROM },
92 { holdW, ARRAY_SIZE(holdW), TK_HOLD },
93 { insertW, ARRAY_SIZE(insertW), TK_INSERT },
94 { intW, ARRAY_SIZE(intW), TK_INT },
95 { integerW, ARRAY_SIZE(integerW), TK_INT },
96 { intoW, ARRAY_SIZE(intoW), TK_INTO },
97 { isW, ARRAY_SIZE(isW), TK_IS },
98 { keyW, ARRAY_SIZE(keyW), TK_KEY },
99 { likeW, ARRAY_SIZE(likeW), TK_LIKE },
100 { localizableW, ARRAY_SIZE(localizableW), TK_LOCALIZABLE },
101 { longW, ARRAY_SIZE(longW), TK_LONG },
102 { longcharW, ARRAY_SIZE(longcharW), TK_LONGCHAR },
103 { notW, ARRAY_SIZE(notW), TK_NOT },
104 { nullW, ARRAY_SIZE(nullW), TK_NULL },
105 { objectW, ARRAY_SIZE(objectW), TK_OBJECT },
106 { orW, ARRAY_SIZE(orW), TK_OR },
107 { orderW, ARRAY_SIZE(orderW), TK_ORDER },
108 { primaryW, ARRAY_SIZE(primaryW), TK_PRIMARY },
109 { selectW, ARRAY_SIZE(selectW), TK_SELECT },
110 { setW, ARRAY_SIZE(setW), TK_SET },
111 { shortW, ARRAY_SIZE(shortW), TK_SHORT },
112 { tableW, ARRAY_SIZE(tableW), TK_TABLE },
113 { temporaryW, ARRAY_SIZE(temporaryW), TK_TEMPORARY },
114 { updateW, ARRAY_SIZE(updateW), TK_UPDATE },
115 { valuesW, ARRAY_SIZE(valuesW), TK_VALUES },
116 { whereW, ARRAY_SIZE(whereW), TK_WHERE },
117 };
118
119 /*
120 ** Comparison function for binary search.
121 */
122 static int compKeyword(const void *m1, const void *m2){
123 const Keyword *k1 = m1, *k2 = m2;
124 int ret, len = min( k1->len, k2->len );
125
126 if ((ret = memicmpW( k1->name, k2->name, len ))) return ret;
127 if (k1->len < k2->len) return -1;
128 else if (k1->len > k2->len) return 1;
129 return 0;
130 }
131
132 /*
133 ** This function looks up an identifier to determine if it is a
134 ** keyword. If it is a keyword, the token code of that keyword is
135 ** returned. If the input is not a keyword, TK_ID is returned.
136 */
137 static int sqliteKeywordCode(const WCHAR *z, int n){
138 Keyword key, *r;
139
140 if( n>MAX_TOKEN_LEN )
141 return TK_ID;
142
143 key.tokenType = 0;
144 key.name = z;
145 key.len = n;
146 r = bsearch( &key, aKeywordTable, ARRAY_SIZE(aKeywordTable), sizeof(Keyword), compKeyword );
147 if( r )
148 return r->tokenType;
149 return TK_ID;
150 }
151
152
153 /*
154 ** If X is a character that can be used in an identifier then
155 ** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
156 **
157 ** In this implementation, an identifier can be a string of
158 ** alphabetic characters, digits, and "_" plus any character
159 ** with the high-order bit set. The latter rule means that
160 ** any sequence of UTF-8 characters or characters taken from
161 ** an extended ISO8859 character set can form an identifier.
162 */
163 static const char isIdChar[] = {
164 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
165 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, /* 2x */
168 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
169 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
170 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
171 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
172 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
173 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
174 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
175 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
176 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
177 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
178 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
179 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
180 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
181 };
182
183
184 /*
185 ** Return the length of the token that begins at z[0]. Return
186 ** -1 if the token is (or might be) incomplete. Store the token
187 ** type in *tokenType before returning.
188 */
189 int sqliteGetToken(const WCHAR *z, int *tokenType, int *skip){
190 int i;
191
192 *skip = 0;
193 switch( *z ){
194 case ' ': case '\t': case '\n': case '\f':
195 for(i=1; isspace(z[i]) && z[i] != '\r'; i++){}
196 *tokenType = TK_SPACE;
197 return i;
198 case '-':
199 if( z[1]==0 ) return -1;
200 *tokenType = TK_MINUS;
201 return 1;
202 case '(':
203 *tokenType = TK_LP;
204 return 1;
205 case ')':
206 *tokenType = TK_RP;
207 return 1;
208 case '*':
209 *tokenType = TK_STAR;
210 return 1;
211 case '=':
212 *tokenType = TK_EQ;
213 return 1;
214 case '<':
215 if( z[1]=='=' ){
216 *tokenType = TK_LE;
217 return 2;
218 }else if( z[1]=='>' ){
219 *tokenType = TK_NE;
220 return 2;
221 }else{
222 *tokenType = TK_LT;
223 return 1;
224 }
225 case '>':
226 if( z[1]=='=' ){
227 *tokenType = TK_GE;
228 return 2;
229 }else{
230 *tokenType = TK_GT;
231 return 1;
232 }
233 case '!':
234 if( z[1]!='=' ){
235 *tokenType = TK_ILLEGAL;
236 return 2;
237 }else{
238 *tokenType = TK_NE;
239 return 2;
240 }
241 case '?':
242 *tokenType = TK_WILDCARD;
243 return 1;
244 case ',':
245 *tokenType = TK_COMMA;
246 return 1;
247 case '`': case '\'': {
248 int delim = z[0];
249 for(i=1; z[i]; i++){
250 if( z[i]==delim )
251 break;
252 }
253 if( z[i] ) i++;
254 if( delim == '`' )
255 *tokenType = TK_ID;
256 else
257 *tokenType = TK_STRING;
258 return i;
259 }
260 case '.':
261 if( !isdigit(z[1]) ){
262 *tokenType = TK_DOT;
263 return 1;
264 }
265 /* Fall through */
266 case '0': case '1': case '2': case '3': case '4':
267 case '5': case '6': case '7': case '8': case '9':
268 *tokenType = TK_INTEGER;
269 for(i=1; isdigit(z[i]); i++){}
270 return i;
271 case '[':
272 for(i=1; z[i] && z[i-1]!=']'; i++){}
273 *tokenType = TK_ID;
274 return i;
275 default:
276 if( !isIdChar[*z] ){
277 break;
278 }
279 for(i=1; isIdChar[z[i]]; i++){}
280 *tokenType = sqliteKeywordCode(z, i);
281 if( *tokenType == TK_ID && z[i] == '`' ) *skip = 1;
282 return i;
283 }
284 *tokenType = TK_ILLEGAL;
285 return 1;
286 }