user32: ToUnicodeEx should return 0 for an unknown key.
[wine/dibdrv.git] / dlls / msi / tokenize.c
blob1404c34b625aefc742ce3e0715accb63fd25a21f
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.
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.
19 #include <ctype.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wine/unicode.h"
26 #include "query.h"
27 #include "sql.tab.h"
30 ** All the keywords of the SQL language are stored as in a hash
31 ** table composed of instances of the following structure.
33 typedef struct Keyword Keyword;
34 struct Keyword {
35 const WCHAR *zName; /* The keyword name */
36 int tokenType; /* The token value for this keyword */
39 #define MAX_TOKEN_LEN 11
41 static const WCHAR ALTER_W[] = { 'A','L','T','E','R',0 };
42 static const WCHAR AND_W[] = { 'A','N','D',0 };
43 static const WCHAR BY_W[] = { 'B','Y',0 };
44 static const WCHAR CHAR_W[] = { 'C','H','A','R',0 };
45 static const WCHAR CHARACTER_W[] = { 'C','H','A','R','A','C','T','E','R',0 };
46 static const WCHAR CREATE_W[] = { 'C','R','E','A','T','E',0 };
47 static const WCHAR CROSS_W[] = { 'C','R','O','S','S',0 };
48 static const WCHAR DELETE_W[] = { 'D','E','L','E','T','E',0 };
49 static const WCHAR DISTINCT_W[] = { 'D','I','S','T','I','N','C','T',0 };
50 static const WCHAR FREE_W[] = { 'F','R','E','E',0 };
51 static const WCHAR FROM_W[] = { 'F','R','O','M',0 };
52 static const WCHAR FULL_W[] = { 'F','U','L','L',0 };
53 static const WCHAR HOLD_W[] = { 'H','O','L','D',0 };
54 static const WCHAR INNER_W[] = { 'I','N','N','E','R',0 };
55 static const WCHAR INSERT_W[] = { 'I','N','S','E','R','T',0 };
56 static const WCHAR INT_W[] = { 'I','N','T',0 };
57 static const WCHAR INTEGER_W[] = { 'I','N','T','E','G','E','R',0 };
58 static const WCHAR INTO_W[] = { 'I','N','T','O',0 };
59 static const WCHAR IS_W[] = { 'I','S',0 };
60 static const WCHAR JOIN_W[] = { 'J','O','I','N',0 };
61 static const WCHAR KEY_W[] = { 'K','E','Y',0 };
62 static const WCHAR LEFT_W[] = { 'L','E','F','T',0 };
63 static const WCHAR LIKE_W[] = { 'L','I','K','E',0 };
64 static const WCHAR LOCALIZABLE_W[] = { 'L','O','C','A','L','I','Z','A','B','L','E',0 };
65 static const WCHAR LONG_W[] = { 'L','O','N','G',0 };
66 static const WCHAR LONGCHAR_W[] = { 'L','O','N','G','C','H','A','R',0 };
67 static const WCHAR NATURAL_W[] = { 'N','A','T','U','R','A','L',0 };
68 static const WCHAR NOT_W[] = { 'N','O','T',0 };
69 static const WCHAR NULL_W[] = { 'N','U','L','L',0 };
70 static const WCHAR OBJECT_W[] = { 'O','B','J','E','C','T',0 };
71 static const WCHAR OR_W[] = { 'O','R',0 };
72 static const WCHAR ORDER_W[] = { 'O','R','D','E','R',0 };
73 static const WCHAR OUTER_W[] = { 'O','U','T','E','R',0 };
74 static const WCHAR PRIMARY_W[] = { 'P','R','I','M','A','R','Y',0 };
75 static const WCHAR RIGHT_W[] = { 'R','I','G','H','T',0 };
76 static const WCHAR SELECT_W[] = { 'S','E','L','E','C','T',0 };
77 static const WCHAR SET_W[] = { 'S','E','T',0 };
78 static const WCHAR SHORT_W[] = { 'S','H','O','R','T',0 };
79 static const WCHAR TABLE_W[] = { 'T','A','B','L','E',0 };
80 static const WCHAR TEMP_W[] = { 'T','E','M','P',0 };
81 static const WCHAR TEMPORARY_W[] = { 'T','E','M','P','O','R','A','R','Y',0 };
82 static const WCHAR UPDATE_W[] = { 'U','P','D','A','T','E',0 };
83 static const WCHAR VALUES_W[] = { 'V','A','L','U','E','S',0 };
84 static const WCHAR WHERE_W[] = { 'W','H','E','R','E',0 };
87 ** These are the keywords
89 static const Keyword aKeywordTable[] = {
90 { ALTER_W, TK_ALTER },
91 { AND_W, TK_AND },
92 { BY_W, TK_BY },
93 { CHAR_W, TK_CHAR },
94 { CHARACTER_W, TK_CHAR },
95 { CREATE_W, TK_CREATE },
96 { DELETE_W, TK_DELETE },
97 { DISTINCT_W, TK_DISTINCT },
98 { FREE_W, TK_FREE },
99 { FROM_W, TK_FROM },
100 { HOLD_W, TK_HOLD },
101 { INSERT_W, TK_INSERT },
102 { INT_W, TK_INT },
103 { INTEGER_W, TK_INT },
104 { INTO_W, TK_INTO },
105 { IS_W, TK_IS },
106 { KEY_W, TK_KEY },
107 { LIKE_W, TK_LIKE },
108 { LOCALIZABLE_W, TK_LOCALIZABLE },
109 { LONG_W, TK_LONG },
110 { LONGCHAR_W, TK_LONGCHAR },
111 { NOT_W, TK_NOT },
112 { NULL_W, TK_NULL },
113 { OBJECT_W, TK_OBJECT },
114 { OR_W, TK_OR },
115 { ORDER_W, TK_ORDER },
116 { PRIMARY_W, TK_PRIMARY },
117 { SELECT_W, TK_SELECT },
118 { SET_W, TK_SET },
119 { SHORT_W, TK_SHORT },
120 { TABLE_W, TK_TABLE },
121 { TEMPORARY_W, TK_TEMPORARY },
122 { UPDATE_W, TK_UPDATE },
123 { VALUES_W, TK_VALUES },
124 { WHERE_W, TK_WHERE },
127 #define KEYWORD_COUNT ( sizeof aKeywordTable/sizeof (Keyword) )
130 ** Comparison function for binary search.
132 static int compKeyword(const void *m1, const void *m2){
133 const Keyword *k1 = m1, *k2 = m2;
135 return strcmpiW( k1->zName, k2->zName );
139 ** This function looks up an identifier to determine if it is a
140 ** keyword. If it is a keyword, the token code of that keyword is
141 ** returned. If the input is not a keyword, TK_ID is returned.
143 static int sqliteKeywordCode(const WCHAR *z, int n){
144 WCHAR str[MAX_TOKEN_LEN+1];
145 Keyword key, *r;
147 if( n>MAX_TOKEN_LEN )
148 return TK_ID;
150 memcpy( str, z, n*sizeof (WCHAR) );
151 str[n] = 0;
152 key.tokenType = 0;
153 key.zName = str;
154 r = bsearch( &key, aKeywordTable, KEYWORD_COUNT, sizeof (Keyword), compKeyword );
155 if( r )
156 return r->tokenType;
157 return TK_ID;
162 ** If X is a character that can be used in an identifier then
163 ** isIdChar[X] will be 1. Otherwise isIdChar[X] will be 0.
165 ** In this implementation, an identifier can be a string of
166 ** alphabetic characters, digits, and "_" plus any character
167 ** with the high-order bit set. The latter rule means that
168 ** any sequence of UTF-8 characters or characters taken from
169 ** an extended ISO8859 character set can form an identifier.
171 static const char isIdChar[] = {
172 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */
174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */
175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
176 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
177 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
178 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
179 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
180 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
181 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 8x */
182 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 9x */
183 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ax */
184 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Bx */
185 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Cx */
186 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Dx */
187 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Ex */
188 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* Fx */
193 ** Return the length of the token that begins at z[0]. Return
194 ** -1 if the token is (or might be) incomplete. Store the token
195 ** type in *tokenType before returning.
197 int sqliteGetToken(const WCHAR *z, int *tokenType){
198 int i;
199 switch( *z ){
200 case ' ': case '\t': case '\n': case '\f': case '\r': {
201 for(i=1; isspace(z[i]); i++){}
202 *tokenType = TK_SPACE;
203 return i;
205 case '-': {
206 if( z[1]==0 ) return -1;
207 *tokenType = TK_MINUS;
208 return 1;
210 case '(':
211 *tokenType = TK_LP;
212 return 1;
213 case ')':
214 *tokenType = TK_RP;
215 return 1;
216 case '*':
217 *tokenType = TK_STAR;
218 return 1;
219 case '=':
220 *tokenType = TK_EQ;
221 return 1;
222 case '<': {
223 if( z[1]=='=' ){
224 *tokenType = TK_LE;
225 return 2;
226 }else if( z[1]=='>' ){
227 *tokenType = TK_NE;
228 return 2;
229 }else{
230 *tokenType = TK_LT;
231 return 1;
234 case '>': {
235 if( z[1]=='=' ){
236 *tokenType = TK_GE;
237 return 2;
238 }else{
239 *tokenType = TK_GT;
240 return 1;
243 case '!': {
244 if( z[1]!='=' ){
245 *tokenType = TK_ILLEGAL;
246 return 2;
247 }else{
248 *tokenType = TK_NE;
249 return 2;
252 case '?':
253 *tokenType = TK_WILDCARD;
254 return 1;
255 case ',':
256 *tokenType = TK_COMMA;
257 return 1;
258 case '`': case '\'': case '"': {
259 int delim = z[0];
260 for(i=1; z[i]; i++){
261 if( z[i]==delim ){
262 if( z[i+1]==delim ){
263 i++;
264 }else{
265 break;
269 if( z[i] ) i++;
270 if( delim == '`' )
271 *tokenType = TK_ID;
272 else
273 *tokenType = TK_STRING;
274 return i;
276 case '.': {
277 if( !isdigit(z[1]) ){
278 *tokenType = TK_DOT;
279 return 1;
281 /* Fall thru into the next case */
283 case '0': case '1': case '2': case '3': case '4':
284 case '5': case '6': case '7': case '8': case '9': {
285 *tokenType = TK_INTEGER;
286 for(i=1; isdigit(z[i]); i++){}
287 return i;
289 case '[': {
290 for(i=1; z[i] && z[i-1]!=']'; i++){}
291 *tokenType = TK_ID;
292 return i;
294 default: {
295 if( !isIdChar[*z] ){
296 break;
298 for(i=1; isIdChar[z[i]]; i++){}
299 *tokenType = sqliteKeywordCode(z, i);
300 return i;
303 *tokenType = TK_ILLEGAL;
304 return 1;