ws2_32: Use better types for some variables.
[wine] / dlls / jscript / lex.c
1 /*
2  * Copyright 2008 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 <math.h>
20
21 #include "jscript.h"
22 #include "activscp.h"
23 #include "objsafe.h"
24 #include "engine.h"
25
26 #include "parser.tab.h"
27
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32
33 static const WCHAR breakW[] = {'b','r','e','a','k',0};
34 static const WCHAR caseW[] = {'c','a','s','e',0};
35 static const WCHAR catchW[] = {'c','a','t','c','h',0};
36 static const WCHAR continueW[] = {'c','o','n','t','i','n','u','e',0};
37 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
38 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
39 static const WCHAR doW[] = {'d','o',0};
40 static const WCHAR elseW[] = {'e','l','s','e',0};
41 static const WCHAR falseW[] = {'f','a','l','s','e',0};
42 static const WCHAR finallyW[] = {'f','i','n','a','l','l','y',0};
43 static const WCHAR forW[] = {'f','o','r',0};
44 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
45 static const WCHAR ifW[] = {'i','f',0};
46 static const WCHAR inW[] = {'i','n',0};
47 static const WCHAR instanceofW[] = {'i','n','s','t','a','n','c','e','o','f',0};
48 static const WCHAR newW[] = {'n','e','w',0};
49 static const WCHAR nullW[] = {'n','u','l','l',0};
50 static const WCHAR returnW[] = {'r','e','t','u','r','n',0};
51 static const WCHAR switchW[] = {'s','w','i','t','c','h',0};
52 static const WCHAR thisW[] = {'t','h','i','s',0};
53 static const WCHAR throwW[] = {'t','h','r','o','w',0};
54 static const WCHAR trueW[] = {'t','r','u','e',0};
55 static const WCHAR tryW[] = {'t','r','y',0};
56 static const WCHAR typeofW[] = {'t','y','p','e','o','f',0};
57 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
58 static const WCHAR varW[] = {'v','a','r',0};
59 static const WCHAR voidW[] = {'v','o','i','d',0};
60 static const WCHAR whileW[] = {'w','h','i','l','e',0};
61 static const WCHAR withW[] = {'w','i','t','h',0};
62
63 static const struct {
64     const WCHAR *word;
65     int token;
66 } keywords[] = {
67     {breakW,       kBREAK},
68     {caseW,        kCASE},
69     {catchW,       kCATCH},
70     {continueW,    kCONTINUE},
71     {defaultW,     kDEFAULT},
72     {deleteW,      kDELETE},
73     {doW,          kDO},
74     {elseW,        kELSE},
75     {falseW,       kFALSE},
76     {finallyW,     kFINALLY},
77     {forW,         kFOR},
78     {functionW,    kFUNCTION},
79     {ifW,          kIF},
80     {inW,          kIN},
81     {instanceofW,  kINSTANCEOF},
82     {newW,         kNEW},
83     {nullW,        kNULL},
84     {returnW,      kRETURN},
85     {switchW,      kSWITCH},
86     {thisW,        kTHIS},
87     {throwW,       kTHROW},
88     {trueW,        kTRUE},
89     {tryW,         kTRY},
90     {typeofW,      kTYPEOF},
91     {undefinedW,   kUNDEFINED},
92     {varW,         kVAR},
93     {voidW,        kVOID},
94     {whileW,       kWHILE},
95     {withW,        kWITH}
96 };
97
98 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
99 {
100     ctx->hres = hres;
101     return -1;
102 }
103
104 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
105 {
106     const WCHAR *p1 = ctx->ptr;
107     const WCHAR *p2 = word;
108
109     while(p1 < ctx->end && *p2) {
110         if(*p1 != *p2)
111             return *p1 - *p2;
112         p1++;
113         p2++;
114     }
115
116     if(*p2 || (p1 < ctx->end && isalnumW(*p1)))
117         return 1;
118
119     *lval = ctx->ptr;
120     ctx->ptr = p1;
121     return 0;
122 }
123
124 /* ECMA-262 3rd Edition    7.3 */
125 static BOOL is_endline(WCHAR c)
126 {
127     return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
128 }
129
130 static BOOL is_identifier_char(WCHAR c)
131 {
132     return isalnumW(c) || c == '$' || c == '_' || c == '\\';
133 }
134
135 static int hex_to_int(WCHAR c)
136 {
137     if('0' <= c && c <= '9')
138         return c-'0';
139
140     if('a' <= c && c <= 'f')
141         return c-'a'+10;
142
143     if('A' <= c && c <= 'F')
144         return c-'A'+10;
145
146     return -1;
147 }
148
149 static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
150 {
151     int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
152
153     while(min <= max) {
154         i = (min+max)/2;
155
156         r = check_keyword(ctx, keywords[i].word, lval);
157         if(!r)
158             return keywords[i].token;
159
160         if(r > 0)
161             min = i+1;
162         else
163             max = i-1;
164     }
165
166     return 0;
167 }
168
169 static void skip_spaces(parser_ctx_t *ctx)
170 {
171     while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
172         if(is_endline(*ctx->ptr++))
173             ctx->nl = TRUE;
174     }
175 }
176
177 static BOOL skip_html_comment(parser_ctx_t *ctx)
178 {
179     const WCHAR html_commentW[] = {'<','!','-','-',0};
180
181     if(!ctx->is_html || ctx->ptr+3 >= ctx->end ||
182         memcmp(ctx->ptr, html_commentW, sizeof(WCHAR)*4))
183         return FALSE;
184
185     ctx->nl = TRUE;
186     while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
187
188     return TRUE;
189 }
190
191 static BOOL skip_comment(parser_ctx_t *ctx)
192 {
193     if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
194         return FALSE;
195
196     switch(ctx->ptr[1]) {
197     case '*':
198         ctx->ptr += 2;
199         while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
200             ctx->ptr++;
201
202         if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
203             ctx->ptr += 2;
204         }else {
205             WARN("unexpected end of file (missing end of comment)\n");
206             ctx->ptr = ctx->end;
207         }
208         break;
209     case '/':
210         ctx->ptr += 2;
211         while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
212             ctx->ptr++;
213         break;
214     default:
215         return FALSE;
216     }
217
218     return TRUE;
219 }
220
221 static BOOL unescape(WCHAR *str)
222 {
223     WCHAR *pd, *p, c;
224     int i;
225
226     pd = p = str;
227     while(*p) {
228         if(*p != '\\') {
229             *pd++ = *p++;
230             continue;
231         }
232
233         p++;
234         c = 0;
235
236         switch(*p) {
237         case '\'':
238         case '\"':
239         case '\\':
240             c = *p;
241             break;
242         case 'b':
243             c = '\b';
244             break;
245         case 't':
246             c = '\t';
247             break;
248         case 'n':
249             c = '\n';
250             break;
251         case 'v':
252             c = '\v';
253             break;
254         case 'f':
255             c = '\f';
256             break;
257         case 'r':
258             c = '\r';
259             break;
260         case 'x':
261             i = hex_to_int(*++p);
262             if(i == -1)
263                 return FALSE;
264             c = i << 4;
265
266             i = hex_to_int(*++p);
267             if(i == -1)
268                 return FALSE;
269             c += i;
270             break;
271         case 'u':
272             i = hex_to_int(*++p);
273             if(i == -1)
274                 return FALSE;
275             c = i << 12;
276
277             i = hex_to_int(*++p);
278             if(i == -1)
279                 return FALSE;
280             c += i << 8;
281
282             i = hex_to_int(*++p);
283             if(i == -1)
284                 return FALSE;
285             c += 1 << 4;
286
287             i = hex_to_int(*++p);
288             if(i == -1)
289                 return FALSE;
290             c += i;
291             break;
292         default:
293             if(isdigitW(*p)) {
294                 c = *p++ - '0';
295                 while(isdigitW(*p))
296                     c = c*10 + (*p++ - '0');
297                 *pd++ = c;
298                 continue;
299             }
300
301             c = *p;
302         }
303
304         *pd++ = c;
305         p++;
306     }
307
308     *pd = 0;
309     return TRUE;
310 }
311
312 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
313 {
314     const WCHAR *ptr = ctx->ptr++;
315     WCHAR *wstr;
316     int len;
317
318     while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
319         ctx->ptr++;
320
321     len = ctx->ptr-ptr;
322
323     *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
324     memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
325     wstr[len] = 0;
326
327     /* FIXME: unescape */
328     return tIdentifier;
329 }
330
331 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
332 {
333     const WCHAR *ptr = ++ctx->ptr;
334     WCHAR *wstr;
335     int len;
336
337     while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
338         if(*ctx->ptr++ == '\\')
339             ctx->ptr++;
340     }
341
342     if(ctx->ptr == ctx->end) {
343         WARN("unexpected end of file\n");
344         return lex_error(ctx, E_FAIL);
345     }
346
347     len = ctx->ptr-ptr;
348
349     *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
350     memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
351     wstr[len] = 0;
352
353     ctx->ptr++;
354
355     if(!unescape(wstr)) {
356         WARN("unescape failed\n");
357         return lex_error(ctx, E_FAIL);
358     }
359
360     return tStringLiteral;
361 }
362
363 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
364 {
365     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
366
367     ret->vt = VT_I4;
368     ret->u.lval = l;
369
370     return ret;
371 }
372
373 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
374 {
375     double d, tmp = 1.0;
376
377     if(ctx->ptr == ctx->end || !isdigitW(*ctx->ptr)) {
378         ERR("No digit after point\n");
379         return 0;
380     }
381
382     d = int_part;
383     while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
384         d += (tmp /= 10.0)*(*ctx->ptr++ - '0');
385
386     if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
387         int sign = 1, e = 0;
388
389         ctx->ptr++;
390         if(ctx->ptr < ctx->end) {
391             if(*ctx->ptr == '+') {
392                 ctx->ptr++;
393             }else if(*ctx->ptr == '-') {
394                 sign = -1;
395                 ctx->ptr++;
396             }else if(!isdigitW(*ctx->ptr)) {
397                 WARN("Expected exponent part\n");
398                 return lex_error(ctx, E_FAIL);
399             }
400         }
401
402         if(ctx->ptr == ctx->end) {
403             WARN("unexpected end of file\n");
404             return lex_error(ctx, E_FAIL);
405         }
406
407         while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
408             e = e*10 + *ctx->ptr++ - '0';
409         e *= sign;
410
411         d *= pow(10, e);
412     }
413
414     *literal = parser_alloc(ctx, sizeof(literal_t));
415     (*literal)->vt = VT_R8;
416     (*literal)->u.dval = d;
417
418     return tNumericLiteral;
419 }
420
421 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
422 {
423     LONG l, d;
424
425     l = *ctx->ptr++ - '0';
426     if(ctx->ptr == ctx->end) {
427         *literal = alloc_int_literal(ctx, l);
428         return tNumericLiteral;
429     }
430
431     if(!l) {
432         if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
433             if(++ctx->ptr == ctx->end) {
434                 ERR("unexpexted end of file\n");
435                 return 0;
436             }
437
438             while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
439                 l = l*16 + d;
440                 ctx->ptr++;
441             }
442
443             if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
444                 WARN("unexpected identifier char\n");
445                 return lex_error(ctx, E_FAIL);
446             }
447
448             *literal = alloc_int_literal(ctx, l);
449             return tNumericLiteral;
450         }
451
452         if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
453             WARN("wrong char after zero\n");
454             return lex_error(ctx, E_FAIL);
455         }
456
457         *literal = alloc_int_literal(ctx, 0);
458     }
459
460     while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
461         l = l*10 + *(ctx->ptr++)-'0';
462
463     if(ctx->ptr < ctx->end) {
464         if(*ctx->ptr == '.') {
465             ctx->ptr++;
466             return parse_double_literal(ctx, l, literal);
467         }
468
469         if(is_identifier_char(*ctx->ptr)) {
470             WARN("unexpected identifier char\n");
471             return lex_error(ctx, E_FAIL);
472         }
473     }
474
475     *literal = alloc_int_literal(ctx, l);
476     return tNumericLiteral;
477 }
478
479 int parser_lex(void *lval, parser_ctx_t *ctx)
480 {
481     int ret;
482
483     ctx->nl = ctx->ptr == ctx->begin;
484
485     do {
486         skip_spaces(ctx);
487         if(ctx->ptr == ctx->end)
488             return 0;
489     }while(skip_comment(ctx) || skip_html_comment(ctx));
490
491     if(isalphaW(*ctx->ptr)) {
492         ret = check_keywords(ctx, lval);
493         if(ret)
494             return ret;
495
496         return parse_identifier(ctx, lval);
497     }
498
499     if(isdigitW(*ctx->ptr))
500         return parse_numeric_literal(ctx, lval);
501
502     switch(*ctx->ptr) {
503     case '{':
504     case '(':
505     case ')':
506     case '[':
507     case ']':
508     case ';':
509     case ',':
510     case '~':
511     case '?':
512     case ':':
513         return *ctx->ptr++;
514
515     case '}':
516         *(const WCHAR**)lval = ctx->ptr++;
517         return '}';
518
519     case '.':
520         if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
521             return parse_double_literal(ctx, 0, lval);
522         return '.';
523
524     case '<':
525         if(++ctx->ptr == ctx->end) {
526             *(int*)lval = EXPR_LESS;
527             return tRelOper;
528         }
529
530         switch(*ctx->ptr) {
531         case '=':  /* <= */
532             ctx->ptr++;
533             *(int*)lval = EXPR_LESSEQ;
534             return tRelOper;
535         case '<':  /* << */
536             if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
537                 ctx->ptr++;
538                 *(int*)lval = EXPR_ASSIGNLSHIFT;
539                 return tAssignOper;
540             }
541             *(int*)lval = EXPR_LSHIFT;
542             return tShiftOper;
543         default: /* < */
544             *(int*)lval = EXPR_LESS;
545             return tRelOper;
546         }
547
548     case '>':
549         if(++ctx->ptr == ctx->end) { /* > */
550             *(int*)lval = EXPR_GREATER;
551             return tRelOper;
552         }
553
554         switch(*ctx->ptr) {
555         case '=':  /* >= */
556             ctx->ptr++;
557             *(int*)lval = EXPR_GREATEREQ;
558             return tRelOper;
559         case '>':  /* >> */
560             if(++ctx->ptr < ctx->end) {
561                 if(*ctx->ptr == '=') {  /* >>= */
562                     ctx->ptr++;
563                     *(int*)lval = EXPR_ASSIGNRSHIFT;
564                     return tAssignOper;
565                 }
566                 if(*ctx->ptr == '>') {  /* >>> */
567                     if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* >>>= */
568                         ctx->ptr++;
569                         *(int*)lval = EXPR_ASSIGNRRSHIFT;
570                         return tAssignOper;
571                     }
572                     *(int*)lval = EXPR_RRSHIFT;
573                     return tRelOper;
574                 }
575             }
576             *(int*)lval = EXPR_RSHIFT;
577             return tShiftOper;
578         default:
579             *(int*)lval = EXPR_GREATER;
580             return tRelOper;
581         }
582
583     case '+':
584         ctx->ptr++;
585         if(ctx->ptr < ctx->end) {
586             switch(*ctx->ptr) {
587             case '+':  /* ++ */
588                 ctx->ptr++;
589                 return tINC;
590             case '=':  /* += */
591                 ctx->ptr++;
592                 *(int*)lval = EXPR_ASSIGNADD;
593                 return tAssignOper;
594             }
595         }
596         return '+';
597
598     case '-':
599         ctx->ptr++;
600         if(ctx->ptr < ctx->end) {
601             switch(*ctx->ptr) {
602             case '-':  /* -- or --> */
603                 ctx->ptr++;
604                 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
605                     ctx->ptr++;
606                     return tHTMLCOMMENT;
607                 }
608                 return tDEC;
609             case '=':  /* -= */
610                 ctx->ptr++;
611                 *(int*)lval = EXPR_ASSIGNSUB;
612                 return tAssignOper;
613             }
614         }
615         return '-';
616
617     case '*':
618         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
619             ctx->ptr++;
620             *(int*)lval = EXPR_ASSIGNMUL;
621             return tAssignOper;
622         }
623         return '*';
624
625     case '%':
626         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
627             ctx->ptr++;
628             *(int*)lval = EXPR_ASSIGNMOD;
629             return tAssignOper;
630         }
631         return '%';
632
633     case '&':
634         if(++ctx->ptr < ctx->end) {
635             switch(*ctx->ptr) {
636             case '=':  /* &= */
637                 ctx->ptr++;
638                 *(int*)lval = EXPR_ASSIGNAND;
639                 return tAssignOper;
640             case '&':  /* && */
641                 ctx->ptr++;
642                 return tANDAND;
643             }
644         }
645         return '&';
646
647     case '|':
648         if(++ctx->ptr < ctx->end) {
649             switch(*ctx->ptr) {
650             case '=':  /* |= */
651                 ctx->ptr++;
652                 *(int*)lval = EXPR_ASSIGNOR;
653                 return tAssignOper;
654             case '|':  /* || */
655                 ctx->ptr++;
656                 return tOROR;
657             }
658         }
659         return '|';
660
661     case '^':
662         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* ^= */
663             ctx->ptr++;
664             *(int*)lval = EXPR_ASSIGNXOR;
665             return tAssignOper;
666         }
667         return '^';
668
669     case '!':
670         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* != */
671             if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* !== */
672                 ctx->ptr++;
673                 *(int*)lval = EXPR_NOTEQEQ;
674                 return tEqOper;
675             }
676             *(int*)lval = EXPR_NOTEQ;
677             return tEqOper;
678         }
679         return '!';
680
681     case '=':
682         if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* == */
683             if(++ctx->ptr < ctx->end && *ctx->ptr == '=') {  /* === */
684                 ctx->ptr++;
685                 *(int*)lval = EXPR_EQEQ;
686                 return tEqOper;
687             }
688             *(int*)lval = EXPR_EQ;
689             return tEqOper;
690         }
691         return '=';
692
693     case '/':
694         if(++ctx->ptr < ctx->end) {
695             if(*ctx->ptr == '=') {  /* /= */
696                 ctx->ptr++;
697                 *(int*)lval = EXPR_ASSIGNDIV;
698                 return tAssignOper;
699             }
700         }
701         return '/';
702
703     case '\"':
704     case '\'':
705         return parse_string_literal(ctx, lval, *ctx->ptr);
706
707     case '_':
708     case '$':
709         return parse_identifier(ctx, lval);
710     }
711
712     WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
713     return 0;
714 }
715
716 static void add_object_literal(parser_ctx_t *ctx, DispatchEx *obj)
717 {
718     obj_literal_t *literal = parser_alloc(ctx, sizeof(obj_literal_t));
719
720     literal->obj = obj;
721     literal->next = ctx->obj_literals;
722     ctx->obj_literals = literal;
723 }
724
725 literal_t *parse_regexp(parser_ctx_t *ctx)
726 {
727     const WCHAR *re, *flags;
728     DispatchEx *regexp;
729     literal_t *ret;
730     DWORD re_len;
731     HRESULT hres;
732
733     TRACE("\n");
734
735     re = ctx->ptr;
736     while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
737         if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
738             ctx->ptr++;
739     }
740
741     if(ctx->ptr == ctx->end) {
742         WARN("unexpected end of file\n");
743         return NULL;
744     }
745
746     re_len = ctx->ptr-re;
747
748     flags = ++ctx->ptr;
749     while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
750         ctx->ptr++;
751
752     hres = create_regexp_str(ctx->script, re, re_len, flags, ctx->ptr-flags, &regexp);
753     if(FAILED(hres))
754         return NULL;
755
756     add_object_literal(ctx, regexp);
757
758     ret = parser_alloc(ctx, sizeof(literal_t));
759     ret->vt = VT_DISPATCH;
760     ret->u.disp = (IDispatch*)_IDispatchEx_(regexp);
761     return ret;
762 }