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