vbscript: Added CreateObject tests (based on jscript ActiveXObject tests).
[wine] / dlls / vbscript / lex.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <assert.h>
20
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28
29 static const WCHAR andW[] = {'a','n','d',0};
30 static const WCHAR byrefW[] = {'b','y','r','e','f',0};
31 static const WCHAR byvalW[] = {'b','y','v','a','l',0};
32 static const WCHAR callW[] = {'c','a','l','l',0};
33 static const WCHAR classW[] = {'c','l','a','s','s',0};
34 static const WCHAR constW[] = {'c','o','n','s','t',0};
35 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
36 static const WCHAR dimW[] = {'d','i','m',0};
37 static const WCHAR doW[] = {'d','o',0};
38 static const WCHAR elseW[] = {'e','l','s','e',0};
39 static const WCHAR elseifW[] = {'e','l','s','e','i','f',0};
40 static const WCHAR emptyW[] = {'e','m','p','t','y',0};
41 static const WCHAR endW[] = {'e','n','d',0};
42 static const WCHAR eqvW[] = {'e','q','v',0};
43 static const WCHAR errorW[] = {'e','r','r','o','r',0};
44 static const WCHAR exitW[] = {'e','x','i','t',0};
45 static const WCHAR explicitW[] = {'e','x','p','l','i','c','i','t',0};
46 static const WCHAR falseW[] = {'f','a','l','s','e',0};
47 static const WCHAR forW[] = {'f','o','r',0};
48 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
49 static const WCHAR getW[] = {'g','e','t',0};
50 static const WCHAR gotoW[] = {'g','o','t','o',0};
51 static const WCHAR ifW[] = {'i','f',0};
52 static const WCHAR impW[] = {'i','m','p',0};
53 static const WCHAR isW[] = {'i','s',0};
54 static const WCHAR letW[] = {'l','e','t',0};
55 static const WCHAR loopW[] = {'l','o','o','p',0};
56 static const WCHAR meW[] = {'m','e',0};
57 static const WCHAR modW[] = {'m','o','d',0};
58 static const WCHAR newW[] = {'n','e','w',0};
59 static const WCHAR nextW[] = {'n','e','x','t',0};
60 static const WCHAR notW[] = {'n','o','t',0};
61 static const WCHAR nothingW[] = {'n','o','t','h','i','n','g',0};
62 static const WCHAR nullW[] = {'n','u','l','l',0};
63 static const WCHAR onW[] = {'o','n',0};
64 static const WCHAR optionW[] = {'o','p','t','i','o','n',0};
65 static const WCHAR orW[] = {'o','r',0};
66 static const WCHAR privateW[] = {'p','r','i','v','a','t','e',0};
67 static const WCHAR propertyW[] = {'p','r','o','p','e','r','t','y',0};
68 static const WCHAR publicW[] = {'p','u','b','l','i','c',0};
69 static const WCHAR remW[] = {'r','e','m',0};
70 static const WCHAR resumeW[] = {'r','e','s','u','m','e',0};
71 static const WCHAR setW[] = {'s','e','t',0};
72 static const WCHAR stepW[] = {'s','t','e','p',0};
73 static const WCHAR stopW[] = {'s','t','o','p',0};
74 static const WCHAR subW[] = {'s','u','b',0};
75 static const WCHAR thenW[] = {'t','h','e','n',0};
76 static const WCHAR toW[] = {'t','o',0};
77 static const WCHAR trueW[] = {'t','r','u','e',0};
78 static const WCHAR untilW[] = {'u','n','t','i','l',0};
79 static const WCHAR wendW[] = {'w','e','n','d',0};
80 static const WCHAR whileW[] = {'w','h','i','l','e',0};
81 static const WCHAR xorW[] = {'x','o','r',0};
82
83 static const struct {
84     const WCHAR *word;
85     int token;
86 } keywords[] = {
87     {andW,       tAND},
88     {byrefW,     tBYREF},
89     {byvalW,     tBYVAL},
90     {callW,      tCALL},
91     {classW,     tCLASS},
92     {constW,     tCONST},
93     {defaultW,   tDEFAULT},
94     {dimW,       tDIM},
95     {doW,        tDO},
96     {elseW,      tELSE},
97     {elseifW,    tELSEIF},
98     {emptyW,     tEMPTY},
99     {endW,       tEND},
100     {eqvW,       tEQV},
101     {errorW,     tERROR},
102     {exitW,      tEXIT},
103     {explicitW,  tEXPLICIT},
104     {falseW,     tFALSE},
105     {forW,       tFOR},
106     {functionW,  tFUNCTION},
107     {getW,       tGET},
108     {gotoW,      tGOTO},
109     {ifW,        tIF},
110     {impW,       tIMP},
111     {isW,        tIS},
112     {letW,       tLET},
113     {loopW,      tLOOP},
114     {meW,        tME},
115     {modW,       tMOD},
116     {newW,       tNEW},
117     {nextW,      tNEXT},
118     {notW,       tNOT},
119     {nothingW,   tNOTHING},
120     {nullW,      tNULL},
121     {onW,        tON},
122     {optionW,    tOPTION},
123     {orW,        tOR},
124     {privateW,   tPRIVATE},
125     {propertyW,  tPROPERTY},
126     {publicW,    tPUBLIC},
127     {remW,       tREM},
128     {resumeW,    tRESUME},
129     {setW,       tSET},
130     {stepW,      tSTEP},
131     {stopW,      tSTOP},
132     {subW,       tSUB},
133     {thenW,      tTHEN},
134     {toW,        tTO},
135     {trueW,      tTRUE},
136     {untilW,     tUNTIL},
137     {wendW,      tWEND},
138     {whileW,     tWHILE},
139     {xorW,       tXOR}
140 };
141
142 static inline BOOL is_identifier_char(WCHAR c)
143 {
144     return isalnumW(c) || c == '_';
145 }
146
147 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word)
148 {
149     const WCHAR *p1 = ctx->ptr;
150     const WCHAR *p2 = word;
151     WCHAR c;
152
153     while(p1 < ctx->end && *p2) {
154         c = tolowerW(*p1);
155         if(c != *p2)
156             return c - *p2;
157         p1++;
158         p2++;
159     }
160
161     if(*p2 || (p1 < ctx->end && is_identifier_char(*p1)))
162         return 1;
163
164     ctx->ptr = p1;
165     return 0;
166 }
167
168 static int check_keywords(parser_ctx_t *ctx)
169 {
170     int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
171
172     while(min <= max) {
173         i = (min+max)/2;
174
175         r = check_keyword(ctx, keywords[i].word);
176         if(!r)
177             return keywords[i].token;
178
179         if(r > 0)
180             min = i+1;
181         else
182             max = i-1;
183     }
184
185     return 0;
186 }
187
188 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
189 {
190     const WCHAR *ptr = ctx->ptr++;
191     WCHAR *str;
192     int len;
193
194     while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
195         ctx->ptr++;
196     len = ctx->ptr-ptr;
197
198     str = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
199     if(!str)
200         return 0;
201
202     memcpy(str, ptr, (len+1)*sizeof(WCHAR));
203     str[len] = 0;
204     *ret = str;
205     return tIdentifier;
206 }
207
208 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret)
209 {
210     const WCHAR *ptr = ++ctx->ptr;
211     WCHAR *rptr;
212     int len = 0;
213
214     while(ctx->ptr < ctx->end) {
215         if(*ctx->ptr == '\n') {
216             FIXME("newline inside string literal\n");
217             return 0;
218         }
219
220        if(*ctx->ptr == '"') {
221             if(ctx->ptr[1] != '"')
222                 break;
223             len--;
224             ctx->ptr++;
225         }
226         ctx->ptr++;
227     }
228
229     if(ctx->ptr == ctx->end) {
230         FIXME("unterminated string literal\n");
231         return 0;
232     }
233
234     len += ctx->ptr-ptr;
235
236     *ret = rptr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
237     if(!rptr)
238         return 0;
239
240     while(ptr < ctx->ptr) {
241         if(*ptr == '"')
242             ptr++;
243         *rptr++ = *ptr++;
244     }
245
246     *rptr = 0;
247     ctx->ptr++;
248     return tString;
249 }
250
251 static int parse_numeric_literal(parser_ctx_t *ctx, void **ret)
252 {
253     double n = 0;
254
255     if(*ctx->ptr == '0' && !('0' <= ctx->ptr[1] && ctx->ptr[1] <= '9') && ctx->ptr[1] != '.')
256         return *ctx->ptr++;
257
258     do {
259         n = n*10 + *ctx->ptr++ - '0';
260     }while('0' <= *ctx->ptr && *ctx->ptr <= '9');
261
262     if(*ctx->ptr != '.') {
263         if((LONG)n == n) {
264             LONG l = n;
265             *(LONG*)ret = l;
266             return (short)l == l ? tShort : tLong;
267         }
268     }else {
269         double e = 1.0;
270         while('0' <= *++ctx->ptr && *ctx->ptr <= '9')
271             n += (e /= 10.0)*(*ctx->ptr-'0');
272     }
273
274     *(double*)ret = n;
275     return tDouble;
276 }
277
278 static int hex_to_int(WCHAR c)
279 {
280     if('0' <= c && c <= '9')
281         return c-'0';
282     if('a' <= c && c <= 'f')
283         return c+10-'a';
284     if('A' <= c && c <= 'F')
285         return c+10-'A';
286     return -1;
287 }
288
289 static int parse_hex_literal(parser_ctx_t *ctx, LONG *ret)
290 {
291     const WCHAR *begin = ctx->ptr;
292     LONG l = 0, d;
293
294     while((d = hex_to_int(*++ctx->ptr)) != -1)
295         l = l*16 + d;
296
297     if(begin + 9 /* max digits+1 */ < ctx->ptr || *ctx->ptr != '&') {
298         FIXME("invalid literal\n");
299         return 0;
300     }
301
302     ctx->ptr++;
303     *ret = l;
304     return (short)l == l ? tShort : tLong;
305 }
306
307 static void skip_spaces(parser_ctx_t *ctx)
308 {
309     while(*ctx->ptr == ' ' || *ctx->ptr == '\t' || *ctx->ptr == '\r')
310         ctx->ptr++;
311 }
312
313 static int parse_next_token(void *lval, parser_ctx_t *ctx)
314 {
315     WCHAR c;
316
317     skip_spaces(ctx);
318     if(ctx->ptr == ctx->end)
319         return ctx->last_token == tNL ? tEOF : tNL;
320
321     c = *ctx->ptr;
322
323     if('0' <= c && c <= '9')
324         return parse_numeric_literal(ctx, lval);
325
326     if(isalphaW(c)) {
327         int ret = check_keywords(ctx);
328         if(!ret)
329             return parse_identifier(ctx, lval);
330         if(ret != tREM)
331             return ret;
332         c = '\'';
333     }
334
335     switch(c) {
336     case '\n':
337         ctx->ptr++;
338         return tNL;
339     case '\'':
340         ctx->ptr = strchrW(ctx->ptr, '\n');
341         if(ctx->ptr)
342             ctx->ptr++;
343         else
344             ctx->ptr = ctx->end;
345         return tNL;
346     case ':':
347     case ')':
348     case ',':
349     case '=':
350     case '+':
351     case '-':
352     case '*':
353     case '/':
354     case '^':
355     case '\\':
356     case '.':
357         return *ctx->ptr++;
358     case '(':
359         /* NOTE:
360          * We resolve empty brackets in lexer instead of parser to avoid complex conflicts
361          * in call statement special case |f()| without 'call' keyword
362          */
363         ctx->ptr++;
364         skip_spaces(ctx);
365         if(*ctx->ptr == ')') {
366             ctx->ptr++;
367             return tEMPTYBRACKETS;
368         }
369         return '(';
370     case '"':
371         return parse_string_literal(ctx, lval);
372     case '&':
373         if(*++ctx->ptr == 'h' || *ctx->ptr == 'H')
374             return parse_hex_literal(ctx, lval);
375         return '&';
376     case '<':
377         switch(*++ctx->ptr) {
378         case '>':
379             ctx->ptr++;
380             return tNEQ;
381         case '=':
382             ctx->ptr++;
383             return tLTEQ;
384         }
385         return '<';
386     case '>':
387         if(*++ctx->ptr == '=') {
388             ctx->ptr++;
389             return tGTEQ;
390         }
391         return '>';
392     default:
393         FIXME("Unhandled char %c in %s\n", *ctx->ptr, debugstr_w(ctx->ptr));
394     }
395
396     return 0;
397 }
398
399 int parser_lex(void *lval, parser_ctx_t *ctx)
400 {
401     int ret;
402
403     while(1) {
404         ret = parse_next_token(lval, ctx);
405         if(ret != tNL || ctx->last_token != tNL)
406             break;
407
408         ctx->last_nl = ctx->ptr-ctx->code;
409     }
410
411     return (ctx->last_token = ret);
412 }