msvcr120: Add _dpcomp implementation.
[wine.git] / dlls / jscript / lex.c
blob57ab0338f249e8976dffd993b419de5e5c9c4aee
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <limits.h>
24 #include "jscript.h"
25 #include "activscp.h"
26 #include "objsafe.h"
27 #include "engine.h"
28 #include "parser.h"
30 #include "parser.tab.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
37 static const WCHAR breakW[] = {'b','r','e','a','k',0};
38 static const WCHAR caseW[] = {'c','a','s','e',0};
39 static const WCHAR catchW[] = {'c','a','t','c','h',0};
40 static const WCHAR continueW[] = {'c','o','n','t','i','n','u','e',0};
41 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
42 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
43 static const WCHAR doW[] = {'d','o',0};
44 static const WCHAR elseW[] = {'e','l','s','e',0};
45 static const WCHAR falseW[] = {'f','a','l','s','e',0};
46 static const WCHAR finallyW[] = {'f','i','n','a','l','l','y',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 ifW[] = {'i','f',0};
50 static const WCHAR inW[] = {'i','n',0};
51 static const WCHAR instanceofW[] = {'i','n','s','t','a','n','c','e','o','f',0};
52 static const WCHAR newW[] = {'n','e','w',0};
53 static const WCHAR nullW[] = {'n','u','l','l',0};
54 static const WCHAR returnW[] = {'r','e','t','u','r','n',0};
55 static const WCHAR switchW[] = {'s','w','i','t','c','h',0};
56 static const WCHAR thisW[] = {'t','h','i','s',0};
57 static const WCHAR throwW[] = {'t','h','r','o','w',0};
58 static const WCHAR trueW[] = {'t','r','u','e',0};
59 static const WCHAR tryW[] = {'t','r','y',0};
60 static const WCHAR typeofW[] = {'t','y','p','e','o','f',0};
61 static const WCHAR varW[] = {'v','a','r',0};
62 static const WCHAR voidW[] = {'v','o','i','d',0};
63 static const WCHAR whileW[] = {'w','h','i','l','e',0};
64 static const WCHAR withW[] = {'w','i','t','h',0};
66 static const WCHAR elifW[] = {'e','l','i','f',0};
67 static const WCHAR endW[] = {'e','n','d',0};
69 static const struct {
70 const WCHAR *word;
71 int token;
72 BOOL no_nl;
73 } keywords[] = {
74 {breakW, kBREAK, TRUE},
75 {caseW, kCASE},
76 {catchW, kCATCH},
77 {continueW, kCONTINUE, TRUE},
78 {defaultW, kDEFAULT},
79 {deleteW, kDELETE},
80 {doW, kDO},
81 {elseW, kELSE},
82 {falseW, kFALSE},
83 {finallyW, kFINALLY},
84 {forW, kFOR},
85 {functionW, kFUNCTION},
86 {ifW, kIF},
87 {inW, kIN},
88 {instanceofW, kINSTANCEOF},
89 {newW, kNEW},
90 {nullW, kNULL},
91 {returnW, kRETURN, TRUE},
92 {switchW, kSWITCH},
93 {thisW, kTHIS},
94 {throwW, kTHROW},
95 {trueW, kTRUE},
96 {tryW, kTRY},
97 {typeofW, kTYPEOF},
98 {varW, kVAR},
99 {voidW, kVOID},
100 {whileW, kWHILE},
101 {withW, kWITH}
104 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
106 ctx->hres = hres;
107 ctx->lexer_error = TRUE;
108 return -1;
111 /* ECMA-262 3rd Edition 7.6 */
112 static BOOL is_identifier_char(WCHAR c)
114 return isalnumW(c) || c == '$' || c == '_' || c == '\\';
117 static BOOL is_identifier_first_char(WCHAR c)
119 return isalphaW(c) || c == '$' || c == '_' || c == '\\';
122 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
124 const WCHAR *p1 = ctx->ptr;
125 const WCHAR *p2 = word;
127 while(p1 < ctx->end && *p2) {
128 if(*p1 != *p2)
129 return *p1 - *p2;
130 p1++;
131 p2++;
134 if(*p2 || (p1 < ctx->end && is_identifier_char(*p1)))
135 return 1;
137 if(lval)
138 *lval = ctx->ptr;
139 ctx->ptr = p1;
140 return 0;
143 /* ECMA-262 3rd Edition 7.3 */
144 static BOOL is_endline(WCHAR c)
146 return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
149 static int hex_to_int(WCHAR c)
151 if('0' <= c && c <= '9')
152 return c-'0';
154 if('a' <= c && c <= 'f')
155 return c-'a'+10;
157 if('A' <= c && c <= 'F')
158 return c-'A'+10;
160 return -1;
163 static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
165 int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
167 while(min <= max) {
168 i = (min+max)/2;
170 r = check_keyword(ctx, keywords[i].word, lval);
171 if(!r) {
172 ctx->implicit_nl_semicolon = keywords[i].no_nl;
173 return keywords[i].token;
176 if(r > 0)
177 min = i+1;
178 else
179 max = i-1;
182 return 0;
185 static BOOL skip_html_comment(parser_ctx_t *ctx)
187 const WCHAR html_commentW[] = {'<','!','-','-',0};
189 if(!ctx->is_html || ctx->ptr+3 >= ctx->end ||
190 memcmp(ctx->ptr, html_commentW, sizeof(WCHAR)*4))
191 return FALSE;
193 ctx->nl = TRUE;
194 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
196 return TRUE;
199 static BOOL skip_comment(parser_ctx_t *ctx)
201 if(ctx->ptr+1 >= ctx->end)
202 return FALSE;
204 if(*ctx->ptr != '/') {
205 if(*ctx->ptr == '@' && ctx->ptr+2 < ctx->end && ctx->ptr[1] == '*' && ctx->ptr[2] == '/') {
206 ctx->ptr += 3;
207 return TRUE;
210 return FALSE;
213 switch(ctx->ptr[1]) {
214 case '*':
215 ctx->ptr += 2;
216 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
217 return FALSE;
218 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
219 ctx->ptr++;
221 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
222 ctx->ptr += 2;
223 }else {
224 WARN("unexpected end of file (missing end of comment)\n");
225 ctx->ptr = ctx->end;
227 break;
228 case '/':
229 ctx->ptr += 2;
230 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
231 return FALSE;
232 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
233 ctx->ptr++;
234 break;
235 default:
236 return FALSE;
239 return TRUE;
242 static BOOL skip_spaces(parser_ctx_t *ctx)
244 while(ctx->ptr < ctx->end && (isspaceW(*ctx->ptr) || *ctx->ptr == 0xFEFF /* UTF16 BOM */)) {
245 if(is_endline(*ctx->ptr++))
246 ctx->nl = TRUE;
249 return ctx->ptr != ctx->end;
252 static BOOL unescape(WCHAR *str)
254 WCHAR *pd, *p, c;
255 int i;
257 pd = p = str;
258 while(*p) {
259 if(*p != '\\') {
260 *pd++ = *p++;
261 continue;
264 p++;
266 switch(*p) {
267 case '\'':
268 case '\"':
269 case '\\':
270 c = *p;
271 break;
272 case 'b':
273 c = '\b';
274 break;
275 case 't':
276 c = '\t';
277 break;
278 case 'n':
279 c = '\n';
280 break;
281 case 'f':
282 c = '\f';
283 break;
284 case 'r':
285 c = '\r';
286 break;
287 case 'x':
288 i = hex_to_int(*++p);
289 if(i == -1)
290 return FALSE;
291 c = i << 4;
293 i = hex_to_int(*++p);
294 if(i == -1)
295 return FALSE;
296 c += i;
297 break;
298 case 'u':
299 i = hex_to_int(*++p);
300 if(i == -1)
301 return FALSE;
302 c = i << 12;
304 i = hex_to_int(*++p);
305 if(i == -1)
306 return FALSE;
307 c += i << 8;
309 i = hex_to_int(*++p);
310 if(i == -1)
311 return FALSE;
312 c += i << 4;
314 i = hex_to_int(*++p);
315 if(i == -1)
316 return FALSE;
317 c += i;
318 break;
319 default:
320 if(isdigitW(*p)) {
321 c = *p++ - '0';
322 if(isdigitW(*p)) {
323 c = c*8 + (*p++ - '0');
324 if(isdigitW(*p))
325 c = c*8 + (*p++ - '0');
327 p--;
329 else
330 c = *p;
333 *pd++ = c;
334 p++;
337 *pd = 0;
338 return TRUE;
341 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
343 const WCHAR *ptr = ctx->ptr++;
344 WCHAR *wstr;
345 int len;
347 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
348 ctx->ptr++;
350 len = ctx->ptr-ptr;
352 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
353 memcpy(wstr, ptr, len*sizeof(WCHAR));
354 wstr[len] = 0;
356 /* FIXME: unescape */
357 return tIdentifier;
360 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
362 const WCHAR *ptr = ++ctx->ptr;
363 WCHAR *wstr;
364 int len;
366 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
367 if(*ctx->ptr++ == '\\')
368 ctx->ptr++;
371 if(ctx->ptr == ctx->end)
372 return lex_error(ctx, JS_E_UNTERMINATED_STRING);
374 len = ctx->ptr-ptr;
376 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
377 memcpy(wstr, ptr, len*sizeof(WCHAR));
378 wstr[len] = 0;
380 ctx->ptr++;
382 if(!unescape(wstr)) {
383 WARN("unescape failed\n");
384 return lex_error(ctx, E_FAIL);
387 return tStringLiteral;
390 static literal_t *new_double_literal(parser_ctx_t *ctx, DOUBLE d)
392 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
394 ret->type = LT_DOUBLE;
395 ret->u.dval = d;
396 return ret;
399 literal_t *new_boolean_literal(parser_ctx_t *ctx, BOOL bval)
401 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
403 ret->type = LT_BOOL;
404 ret->u.bval = bval;
406 return ret;
409 static BOOL parse_double_literal(parser_ctx_t *ctx, LONG int_part, double *ret)
411 LONGLONG d, hlp;
412 int exp = 0;
414 d = int_part;
415 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
416 hlp = d*10 + *(ctx->ptr++) - '0';
417 if(d>MAXLONGLONG/10 || hlp<0) {
418 exp++;
419 break;
421 else
422 d = hlp;
424 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
425 exp++;
426 ctx->ptr++;
429 if(*ctx->ptr == '.') {
430 ctx->ptr++;
432 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
433 hlp = d*10 + *(ctx->ptr++) - '0';
434 if(d>MAXLONGLONG/10 || hlp<0)
435 break;
437 d = hlp;
438 exp--;
440 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
441 ctx->ptr++;
444 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
445 int sign = 1, e = 0;
447 ctx->ptr++;
448 if(ctx->ptr < ctx->end) {
449 if(*ctx->ptr == '+') {
450 ctx->ptr++;
451 }else if(*ctx->ptr == '-') {
452 sign = -1;
453 ctx->ptr++;
454 }else if(!isdigitW(*ctx->ptr)) {
455 WARN("Expected exponent part\n");
456 lex_error(ctx, E_FAIL);
457 return FALSE;
461 if(ctx->ptr == ctx->end) {
462 WARN("unexpected end of file\n");
463 lex_error(ctx, E_FAIL);
464 return FALSE;
467 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
468 if(e > INT_MAX/10 || (e = e*10 + *ctx->ptr++ - '0')<0)
469 e = INT_MAX;
471 e *= sign;
473 if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
474 else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
475 else exp += e;
478 if(is_identifier_char(*ctx->ptr)) {
479 WARN("wrong char after zero\n");
480 lex_error(ctx, JS_E_MISSING_SEMICOLON);
481 return FALSE;
484 *ret = exp>=0 ? d*pow(10, exp) : d/pow(10, -exp);
485 return TRUE;
488 static BOOL parse_numeric_literal(parser_ctx_t *ctx, double *ret)
490 LONG l, d;
492 l = *ctx->ptr++ - '0';
493 if(!l) {
494 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
495 if(++ctx->ptr == ctx->end) {
496 ERR("unexpected end of file\n");
497 return FALSE;
500 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
501 l = l*16 + d;
502 ctx->ptr++;
505 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
506 WARN("unexpected identifier char\n");
507 lex_error(ctx, JS_E_MISSING_SEMICOLON);
508 return FALSE;
511 *ret = l;
512 return TRUE;
515 if(isdigitW(*ctx->ptr)) {
516 unsigned base = 8;
517 const WCHAR *ptr;
518 double val = 0;
520 for(ptr = ctx->ptr; ptr < ctx->end && isdigitW(*ptr); ptr++) {
521 if(*ptr > '7') {
522 base = 10;
523 break;
527 do {
528 val = val*base + *ctx->ptr-'0';
529 }while(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr));
531 /* FIXME: Do we need it here? */
532 if(ctx->ptr < ctx->end && (is_identifier_char(*ctx->ptr) || *ctx->ptr == '.')) {
533 WARN("wrong char after octal literal: '%c'\n", *ctx->ptr);
534 lex_error(ctx, JS_E_MISSING_SEMICOLON);
535 return FALSE;
538 *ret = val;
539 return TRUE;
542 if(is_identifier_char(*ctx->ptr)) {
543 WARN("wrong char after zero\n");
544 lex_error(ctx, JS_E_MISSING_SEMICOLON);
545 return FALSE;
549 return parse_double_literal(ctx, l, ret);
552 static int next_token(parser_ctx_t *ctx, void *lval)
554 do {
555 if(!skip_spaces(ctx))
556 return tEOF;
557 }while(skip_comment(ctx) || skip_html_comment(ctx));
559 if(ctx->implicit_nl_semicolon) {
560 if(ctx->nl)
561 return ';';
562 ctx->implicit_nl_semicolon = FALSE;
565 if(isalphaW(*ctx->ptr)) {
566 int ret = check_keywords(ctx, lval);
567 if(ret)
568 return ret;
570 return parse_identifier(ctx, lval);
573 if(isdigitW(*ctx->ptr)) {
574 double n;
576 if(!parse_numeric_literal(ctx, &n))
577 return -1;
579 *(literal_t**)lval = new_double_literal(ctx, n);
580 return tNumericLiteral;
583 switch(*ctx->ptr) {
584 case '{':
585 case '(':
586 case ')':
587 case '[':
588 case ']':
589 case ';':
590 case ',':
591 case '~':
592 case '?':
593 return *ctx->ptr++;
595 case '}':
596 *(const WCHAR**)lval = ctx->ptr++;
597 return '}';
599 case '.':
600 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
601 double n;
602 if(!parse_double_literal(ctx, 0, &n))
603 return -1;
604 *(literal_t**)lval = new_double_literal(ctx, n);
605 return tNumericLiteral;
607 return '.';
609 case '<':
610 if(++ctx->ptr == ctx->end) {
611 *(int*)lval = EXPR_LESS;
612 return tRelOper;
615 switch(*ctx->ptr) {
616 case '=': /* <= */
617 ctx->ptr++;
618 *(int*)lval = EXPR_LESSEQ;
619 return tRelOper;
620 case '<': /* << */
621 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
622 ctx->ptr++;
623 *(int*)lval = EXPR_ASSIGNLSHIFT;
624 return tAssignOper;
626 *(int*)lval = EXPR_LSHIFT;
627 return tShiftOper;
628 default: /* < */
629 *(int*)lval = EXPR_LESS;
630 return tRelOper;
633 case '>':
634 if(++ctx->ptr == ctx->end) { /* > */
635 *(int*)lval = EXPR_GREATER;
636 return tRelOper;
639 switch(*ctx->ptr) {
640 case '=': /* >= */
641 ctx->ptr++;
642 *(int*)lval = EXPR_GREATEREQ;
643 return tRelOper;
644 case '>': /* >> */
645 if(++ctx->ptr < ctx->end) {
646 if(*ctx->ptr == '=') { /* >>= */
647 ctx->ptr++;
648 *(int*)lval = EXPR_ASSIGNRSHIFT;
649 return tAssignOper;
651 if(*ctx->ptr == '>') { /* >>> */
652 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
653 ctx->ptr++;
654 *(int*)lval = EXPR_ASSIGNRRSHIFT;
655 return tAssignOper;
657 *(int*)lval = EXPR_RRSHIFT;
658 return tRelOper;
661 *(int*)lval = EXPR_RSHIFT;
662 return tShiftOper;
663 default:
664 *(int*)lval = EXPR_GREATER;
665 return tRelOper;
668 case '+':
669 ctx->ptr++;
670 if(ctx->ptr < ctx->end) {
671 switch(*ctx->ptr) {
672 case '+': /* ++ */
673 ctx->ptr++;
674 return tINC;
675 case '=': /* += */
676 ctx->ptr++;
677 *(int*)lval = EXPR_ASSIGNADD;
678 return tAssignOper;
681 return '+';
683 case '-':
684 ctx->ptr++;
685 if(ctx->ptr < ctx->end) {
686 switch(*ctx->ptr) {
687 case '-': /* -- or --> */
688 ctx->ptr++;
689 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
690 ctx->ptr++;
691 return tHTMLCOMMENT;
693 return tDEC;
694 case '=': /* -= */
695 ctx->ptr++;
696 *(int*)lval = EXPR_ASSIGNSUB;
697 return tAssignOper;
700 return '-';
702 case '*':
703 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
704 ctx->ptr++;
705 *(int*)lval = EXPR_ASSIGNMUL;
706 return tAssignOper;
708 return '*';
710 case '%':
711 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
712 ctx->ptr++;
713 *(int*)lval = EXPR_ASSIGNMOD;
714 return tAssignOper;
716 return '%';
718 case '&':
719 if(++ctx->ptr < ctx->end) {
720 switch(*ctx->ptr) {
721 case '=': /* &= */
722 ctx->ptr++;
723 *(int*)lval = EXPR_ASSIGNAND;
724 return tAssignOper;
725 case '&': /* && */
726 ctx->ptr++;
727 return tANDAND;
730 return '&';
732 case '|':
733 if(++ctx->ptr < ctx->end) {
734 switch(*ctx->ptr) {
735 case '=': /* |= */
736 ctx->ptr++;
737 *(int*)lval = EXPR_ASSIGNOR;
738 return tAssignOper;
739 case '|': /* || */
740 ctx->ptr++;
741 return tOROR;
744 return '|';
746 case '^':
747 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
748 ctx->ptr++;
749 *(int*)lval = EXPR_ASSIGNXOR;
750 return tAssignOper;
752 return '^';
754 case '!':
755 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
756 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
757 ctx->ptr++;
758 *(int*)lval = EXPR_NOTEQEQ;
759 return tEqOper;
761 *(int*)lval = EXPR_NOTEQ;
762 return tEqOper;
764 return '!';
766 case '=':
767 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
768 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
769 ctx->ptr++;
770 *(int*)lval = EXPR_EQEQ;
771 return tEqOper;
773 *(int*)lval = EXPR_EQ;
774 return tEqOper;
776 return '=';
778 case '/':
779 if(++ctx->ptr < ctx->end) {
780 if(*ctx->ptr == '=') { /* /= */
781 ctx->ptr++;
782 *(int*)lval = EXPR_ASSIGNDIV;
783 return kDIVEQ;
786 return '/';
788 case ':':
789 if(++ctx->ptr < ctx->end && *ctx->ptr == ':') {
790 ctx->ptr++;
791 return kDCOL;
793 return ':';
795 case '\"':
796 case '\'':
797 return parse_string_literal(ctx, lval, *ctx->ptr);
799 case '_':
800 case '$':
801 return parse_identifier(ctx, lval);
803 case '@':
804 return '@';
807 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
808 return 0;
811 struct _cc_var_t {
812 ccval_t val;
813 struct _cc_var_t *next;
814 unsigned name_len;
815 WCHAR name[0];
818 void release_cc(cc_ctx_t *cc)
820 cc_var_t *iter, *next;
822 for(iter = cc->vars; iter; iter = next) {
823 next = iter->next;
824 heap_free(iter);
827 heap_free(cc);
830 static BOOL new_cc_var(cc_ctx_t *cc, const WCHAR *name, int len, ccval_t v)
832 cc_var_t *new_v;
834 if(len == -1)
835 len = strlenW(name);
837 new_v = heap_alloc(sizeof(cc_var_t) + (len+1)*sizeof(WCHAR));
838 if(!new_v)
839 return FALSE;
841 new_v->val = v;
842 memcpy(new_v->name, name, (len+1)*sizeof(WCHAR));
843 new_v->name_len = len;
844 new_v->next = cc->vars;
845 cc->vars = new_v;
846 return TRUE;
849 static cc_var_t *find_cc_var(cc_ctx_t *cc, const WCHAR *name, unsigned name_len)
851 cc_var_t *iter;
853 for(iter = cc->vars; iter; iter = iter->next) {
854 if(iter->name_len == name_len && !memcmp(iter->name, name, name_len*sizeof(WCHAR)))
855 return iter;
858 return NULL;
861 static BOOL init_cc(parser_ctx_t *ctx)
863 cc_ctx_t *cc;
865 static const WCHAR _win32W[] = {'_','w','i','n','3','2',0};
866 static const WCHAR _win64W[] = {'_','w','i','n','6','4',0};
867 static const WCHAR _x86W[] = {'_','x','8','6',0};
868 static const WCHAR _amd64W[] = {'_','a','m','d','6','4',0};
869 static const WCHAR _jscriptW[] = {'_','j','s','c','r','i','p','t',0};
870 static const WCHAR _jscript_buildW[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
871 static const WCHAR _jscript_versionW[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
873 if(ctx->script->cc)
874 return TRUE;
876 cc = heap_alloc(sizeof(cc_ctx_t));
877 if(!cc) {
878 lex_error(ctx, E_OUTOFMEMORY);
879 return FALSE;
882 cc->vars = NULL;
884 if(!new_cc_var(cc, _jscriptW, -1, ccval_bool(TRUE))
885 || !new_cc_var(cc, sizeof(void*) == 8 ? _win64W : _win32W, -1, ccval_bool(TRUE))
886 || !new_cc_var(cc, sizeof(void*) == 8 ? _amd64W : _x86W, -1, ccval_bool(TRUE))
887 || !new_cc_var(cc, _jscript_versionW, -1, ccval_num(JSCRIPT_MAJOR_VERSION + (DOUBLE)JSCRIPT_MINOR_VERSION/10.0))
888 || !new_cc_var(cc, _jscript_buildW, -1, ccval_num(JSCRIPT_BUILD_VERSION))) {
889 release_cc(cc);
890 lex_error(ctx, E_OUTOFMEMORY);
891 return FALSE;
894 ctx->script->cc = cc;
895 return TRUE;
898 static BOOL parse_cc_identifier(parser_ctx_t *ctx, const WCHAR **ret, unsigned *ret_len)
900 if(*ctx->ptr != '@') {
901 lex_error(ctx, JS_E_EXPECTED_AT);
902 return FALSE;
905 if(!is_identifier_first_char(*++ctx->ptr)) {
906 lex_error(ctx, JS_E_EXPECTED_IDENTIFIER);
907 return FALSE;
910 *ret = ctx->ptr;
911 while(++ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr));
912 *ret_len = ctx->ptr - *ret;
913 return TRUE;
916 int try_parse_ccval(parser_ctx_t *ctx, ccval_t *r)
918 if(!skip_spaces(ctx))
919 return -1;
921 if(isdigitW(*ctx->ptr)) {
922 double n;
924 if(!parse_numeric_literal(ctx, &n))
925 return -1;
927 *r = ccval_num(n);
928 return 1;
931 if(*ctx->ptr == '@') {
932 const WCHAR *ident;
933 unsigned ident_len;
934 cc_var_t *cc_var;
936 if(!parse_cc_identifier(ctx, &ident, &ident_len))
937 return -1;
939 cc_var = find_cc_var(ctx->script->cc, ident, ident_len);
940 *r = cc_var ? cc_var->val : ccval_num(NAN);
941 return 1;
944 if(!check_keyword(ctx, trueW, NULL)) {
945 *r = ccval_bool(TRUE);
946 return 1;
949 if(!check_keyword(ctx, falseW, NULL)) {
950 *r = ccval_bool(FALSE);
951 return 1;
954 return 0;
957 static int skip_code(parser_ctx_t *ctx, BOOL exec_else)
959 int if_depth = 1;
960 const WCHAR *ptr;
962 while(1) {
963 ptr = strchrW(ctx->ptr, '@');
964 if(!ptr) {
965 WARN("No @end\n");
966 return lex_error(ctx, JS_E_EXPECTED_CCEND);
968 ctx->ptr = ptr+1;
970 if(!check_keyword(ctx, endW, NULL)) {
971 if(--if_depth)
972 continue;
973 return 0;
976 if(exec_else && !check_keyword(ctx, elifW, NULL)) {
977 if(if_depth > 1)
978 continue;
980 if(!skip_spaces(ctx) || *ctx->ptr != '(')
981 return lex_error(ctx, JS_E_MISSING_LBRACKET);
983 if(!parse_cc_expr(ctx))
984 return -1;
986 if(!get_ccbool(ctx->ccval))
987 continue; /* skip block of code */
989 /* continue parsing */
990 ctx->cc_if_depth++;
991 return 0;
994 if(exec_else && !check_keyword(ctx, elseW, NULL)) {
995 if(if_depth > 1)
996 continue;
998 /* parse else block */
999 ctx->cc_if_depth++;
1000 return 0;
1003 if(!check_keyword(ctx, ifW, NULL)) {
1004 if_depth++;
1005 continue;
1008 ctx->ptr++;
1012 static int cc_token(parser_ctx_t *ctx, void *lval)
1014 unsigned id_len = 0;
1015 cc_var_t *var;
1017 static const WCHAR cc_onW[] = {'c','c','_','o','n',0};
1018 static const WCHAR setW[] = {'s','e','t',0};
1020 ctx->ptr++;
1022 if(!check_keyword(ctx, cc_onW, NULL))
1023 return init_cc(ctx) ? 0 : -1;
1025 if(!check_keyword(ctx, setW, NULL)) {
1026 const WCHAR *ident;
1027 unsigned ident_len;
1028 cc_var_t *var;
1030 if(!init_cc(ctx))
1031 return -1;
1033 if(!skip_spaces(ctx))
1034 return lex_error(ctx, JS_E_EXPECTED_AT);
1036 if(!parse_cc_identifier(ctx, &ident, &ident_len))
1037 return -1;
1039 if(!skip_spaces(ctx) || *ctx->ptr != '=')
1040 return lex_error(ctx, JS_E_EXPECTED_ASSIGN);
1041 ctx->ptr++;
1043 if(!parse_cc_expr(ctx)) {
1044 WARN("parsing CC expression failed\n");
1045 return -1;
1048 var = find_cc_var(ctx->script->cc, ident, ident_len);
1049 if(var) {
1050 var->val = ctx->ccval;
1051 }else {
1052 if(!new_cc_var(ctx->script->cc, ident, ident_len, ctx->ccval))
1053 return lex_error(ctx, E_OUTOFMEMORY);
1056 return 0;
1059 if(!check_keyword(ctx, ifW, NULL)) {
1060 if(!init_cc(ctx))
1061 return -1;
1063 if(!skip_spaces(ctx) || *ctx->ptr != '(')
1064 return lex_error(ctx, JS_E_MISSING_LBRACKET);
1066 if(!parse_cc_expr(ctx))
1067 return -1;
1069 if(get_ccbool(ctx->ccval)) {
1070 /* continue parsing block inside if */
1071 ctx->cc_if_depth++;
1072 return 0;
1075 return skip_code(ctx, TRUE);
1078 if(!check_keyword(ctx, elifW, NULL) || !check_keyword(ctx, elseW, NULL)) {
1079 if(!ctx->cc_if_depth)
1080 return lex_error(ctx, JS_E_SYNTAX);
1082 return skip_code(ctx, FALSE);
1085 if(!check_keyword(ctx, endW, NULL)) {
1086 if(!ctx->cc_if_depth)
1087 return lex_error(ctx, JS_E_SYNTAX);
1089 ctx->cc_if_depth--;
1090 return 0;
1093 if(!ctx->script->cc)
1094 return lex_error(ctx, JS_E_DISABLED_CC);
1096 while(ctx->ptr+id_len < ctx->end && is_identifier_char(ctx->ptr[id_len]))
1097 id_len++;
1098 if(!id_len)
1099 return '@';
1101 TRACE("var %s\n", debugstr_wn(ctx->ptr, id_len));
1103 var = find_cc_var(ctx->script->cc, ctx->ptr, id_len);
1104 ctx->ptr += id_len;
1105 if(!var || var->val.is_num) {
1106 *(literal_t**)lval = new_double_literal(ctx, var ? var->val.u.n : NAN);
1107 return tNumericLiteral;
1110 *(literal_t**)lval = new_boolean_literal(ctx, var->val.u.b);
1111 return tBooleanLiteral;
1114 int parser_lex(void *lval, parser_ctx_t *ctx)
1116 int ret;
1118 ctx->nl = ctx->ptr == ctx->begin;
1120 do {
1121 ret = next_token(ctx, lval);
1122 } while(ret == '@' && !(ret = cc_token(ctx, lval)));
1124 return ret;
1127 literal_t *parse_regexp(parser_ctx_t *ctx)
1129 const WCHAR *re, *flags_ptr;
1130 BOOL in_class = FALSE;
1131 DWORD re_len, flags;
1132 literal_t *ret;
1133 HRESULT hres;
1135 TRACE("\n");
1137 while(*--ctx->ptr != '/');
1139 /* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
1140 re = ++ctx->ptr;
1141 while(ctx->ptr < ctx->end) {
1142 if(*ctx->ptr == '\\') {
1143 if(++ctx->ptr == ctx->end)
1144 break;
1145 }else if(in_class) {
1146 if(*ctx->ptr == '\n')
1147 break;
1148 if(*ctx->ptr == ']')
1149 in_class = FALSE;
1150 }else {
1151 if(*ctx->ptr == '/')
1152 break;
1154 if(*ctx->ptr == '[')
1155 in_class = TRUE;
1157 ctx->ptr++;
1160 if(ctx->ptr == ctx->end || *ctx->ptr != '/') {
1161 WARN("pre-parsing failed\n");
1162 return NULL;
1165 re_len = ctx->ptr-re;
1167 flags_ptr = ++ctx->ptr;
1168 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
1169 ctx->ptr++;
1171 hres = parse_regexp_flags(flags_ptr, ctx->ptr-flags_ptr, &flags);
1172 if(FAILED(hres))
1173 return NULL;
1175 ret = parser_alloc(ctx, sizeof(literal_t));
1176 ret->type = LT_REGEXP;
1177 ret->u.regexp.str = re;
1178 ret->u.regexp.str_len = re_len;
1179 ret->u.regexp.flags = flags;
1180 return ret;