dmstyle: None of the COM classes support aggregation.
[wine.git] / dlls / jscript / lex.c
blob363e18da3eb66158dc255fa60f946050454555b8
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"
29 #include "parser.tab.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
36 static const WCHAR breakW[] = {'b','r','e','a','k',0};
37 static const WCHAR caseW[] = {'c','a','s','e',0};
38 static const WCHAR catchW[] = {'c','a','t','c','h',0};
39 static const WCHAR continueW[] = {'c','o','n','t','i','n','u','e',0};
40 static const WCHAR defaultW[] = {'d','e','f','a','u','l','t',0};
41 static const WCHAR deleteW[] = {'d','e','l','e','t','e',0};
42 static const WCHAR doW[] = {'d','o',0};
43 static const WCHAR elseW[] = {'e','l','s','e',0};
44 static const WCHAR falseW[] = {'f','a','l','s','e',0};
45 static const WCHAR finallyW[] = {'f','i','n','a','l','l','y',0};
46 static const WCHAR forW[] = {'f','o','r',0};
47 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
48 static const WCHAR ifW[] = {'i','f',0};
49 static const WCHAR inW[] = {'i','n',0};
50 static const WCHAR instanceofW[] = {'i','n','s','t','a','n','c','e','o','f',0};
51 static const WCHAR newW[] = {'n','e','w',0};
52 static const WCHAR nullW[] = {'n','u','l','l',0};
53 static const WCHAR returnW[] = {'r','e','t','u','r','n',0};
54 static const WCHAR switchW[] = {'s','w','i','t','c','h',0};
55 static const WCHAR thisW[] = {'t','h','i','s',0};
56 static const WCHAR throwW[] = {'t','h','r','o','w',0};
57 static const WCHAR trueW[] = {'t','r','u','e',0};
58 static const WCHAR tryW[] = {'t','r','y',0};
59 static const WCHAR typeofW[] = {'t','y','p','e','o','f',0};
60 static const WCHAR varW[] = {'v','a','r',0};
61 static const WCHAR voidW[] = {'v','o','i','d',0};
62 static const WCHAR whileW[] = {'w','h','i','l','e',0};
63 static const WCHAR withW[] = {'w','i','t','h',0};
65 static const struct {
66 const WCHAR *word;
67 int token;
68 BOOL no_nl;
69 } keywords[] = {
70 {breakW, kBREAK, TRUE},
71 {caseW, kCASE},
72 {catchW, kCATCH},
73 {continueW, kCONTINUE, TRUE},
74 {defaultW, kDEFAULT},
75 {deleteW, kDELETE},
76 {doW, kDO},
77 {elseW, kELSE},
78 {falseW, kFALSE},
79 {finallyW, kFINALLY},
80 {forW, kFOR},
81 {functionW, kFUNCTION},
82 {ifW, kIF},
83 {inW, kIN},
84 {instanceofW, kINSTANCEOF},
85 {newW, kNEW},
86 {nullW, kNULL},
87 {returnW, kRETURN, TRUE},
88 {switchW, kSWITCH},
89 {thisW, kTHIS},
90 {throwW, kTHROW},
91 {trueW, kTRUE},
92 {tryW, kTRY},
93 {typeofW, kTYPEOF},
94 {varW, kVAR},
95 {voidW, kVOID},
96 {whileW, kWHILE},
97 {withW, kWITH}
100 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
102 ctx->hres = hres;
103 ctx->lexer_error = TRUE;
104 return -1;
107 /* ECMA-262 3rd Edition 7.6 */
108 static BOOL is_identifier_char(WCHAR c)
110 return isalnumW(c) || c == '$' || c == '_' || c == '\\';
113 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
115 const WCHAR *p1 = ctx->ptr;
116 const WCHAR *p2 = word;
118 while(p1 < ctx->end && *p2) {
119 if(*p1 != *p2)
120 return *p1 - *p2;
121 p1++;
122 p2++;
125 if(*p2 || (p1 < ctx->end && is_identifier_char(*p1)))
126 return 1;
128 if(lval)
129 *lval = ctx->ptr;
130 ctx->ptr = p1;
131 return 0;
134 /* ECMA-262 3rd Edition 7.3 */
135 static BOOL is_endline(WCHAR c)
137 return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
140 static int hex_to_int(WCHAR c)
142 if('0' <= c && c <= '9')
143 return c-'0';
145 if('a' <= c && c <= 'f')
146 return c-'a'+10;
148 if('A' <= c && c <= 'F')
149 return c-'A'+10;
151 return -1;
154 static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
156 int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
158 while(min <= max) {
159 i = (min+max)/2;
161 r = check_keyword(ctx, keywords[i].word, lval);
162 if(!r) {
163 ctx->implicit_nl_semicolon = keywords[i].no_nl;
164 return keywords[i].token;
167 if(r > 0)
168 min = i+1;
169 else
170 max = i-1;
173 return 0;
176 static BOOL skip_html_comment(parser_ctx_t *ctx)
178 const WCHAR html_commentW[] = {'<','!','-','-',0};
180 if(!ctx->is_html || ctx->ptr+3 >= ctx->end ||
181 memcmp(ctx->ptr, html_commentW, sizeof(WCHAR)*4))
182 return FALSE;
184 ctx->nl = TRUE;
185 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
187 return TRUE;
190 static BOOL skip_comment(parser_ctx_t *ctx)
192 if(ctx->ptr+1 >= ctx->end)
193 return FALSE;
195 if(*ctx->ptr != '/') {
196 if(*ctx->ptr == '@' && ctx->ptr+2 < ctx->end && ctx->ptr[1] == '*' && ctx->ptr[2] == '/') {
197 ctx->ptr += 3;
198 return TRUE;
201 return FALSE;
204 switch(ctx->ptr[1]) {
205 case '*':
206 ctx->ptr += 2;
207 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
208 return FALSE;
209 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
210 ctx->ptr++;
212 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
213 ctx->ptr += 2;
214 }else {
215 WARN("unexpected end of file (missing end of comment)\n");
216 ctx->ptr = ctx->end;
218 break;
219 case '/':
220 ctx->ptr += 2;
221 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
222 return FALSE;
223 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
224 ctx->ptr++;
225 break;
226 default:
227 return FALSE;
230 return TRUE;
233 static BOOL unescape(WCHAR *str)
235 WCHAR *pd, *p, c;
236 int i;
238 pd = p = str;
239 while(*p) {
240 if(*p != '\\') {
241 *pd++ = *p++;
242 continue;
245 p++;
247 switch(*p) {
248 case '\'':
249 case '\"':
250 case '\\':
251 c = *p;
252 break;
253 case 'b':
254 c = '\b';
255 break;
256 case 't':
257 c = '\t';
258 break;
259 case 'n':
260 c = '\n';
261 break;
262 case 'f':
263 c = '\f';
264 break;
265 case 'r':
266 c = '\r';
267 break;
268 case 'x':
269 i = hex_to_int(*++p);
270 if(i == -1)
271 return FALSE;
272 c = i << 4;
274 i = hex_to_int(*++p);
275 if(i == -1)
276 return FALSE;
277 c += i;
278 break;
279 case 'u':
280 i = hex_to_int(*++p);
281 if(i == -1)
282 return FALSE;
283 c = i << 12;
285 i = hex_to_int(*++p);
286 if(i == -1)
287 return FALSE;
288 c += i << 8;
290 i = hex_to_int(*++p);
291 if(i == -1)
292 return FALSE;
293 c += i << 4;
295 i = hex_to_int(*++p);
296 if(i == -1)
297 return FALSE;
298 c += i;
299 break;
300 default:
301 if(isdigitW(*p)) {
302 c = *p++ - '0';
303 if(isdigitW(*p)) {
304 c = c*8 + (*p++ - '0');
305 if(isdigitW(*p))
306 c = c*8 + (*p++ - '0');
308 p--;
310 else
311 c = *p;
314 *pd++ = c;
315 p++;
318 *pd = 0;
319 return TRUE;
322 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
324 const WCHAR *ptr = ctx->ptr++;
325 WCHAR *wstr;
326 int len;
328 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
329 ctx->ptr++;
331 len = ctx->ptr-ptr;
333 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
334 memcpy(wstr, ptr, len*sizeof(WCHAR));
335 wstr[len] = 0;
337 /* FIXME: unescape */
338 return tIdentifier;
341 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
343 const WCHAR *ptr = ++ctx->ptr;
344 WCHAR *wstr;
345 int len;
347 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
348 if(*ctx->ptr++ == '\\')
349 ctx->ptr++;
352 if(ctx->ptr == ctx->end)
353 return lex_error(ctx, JS_E_UNTERMINATED_STRING);
355 len = ctx->ptr-ptr;
357 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
358 memcpy(wstr, ptr, len*sizeof(WCHAR));
359 wstr[len] = 0;
361 ctx->ptr++;
363 if(!unescape(wstr)) {
364 WARN("unescape failed\n");
365 return lex_error(ctx, E_FAIL);
368 return tStringLiteral;
371 static literal_t *new_double_literal(parser_ctx_t *ctx, DOUBLE d)
373 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
375 ret->type = LT_DOUBLE;
376 ret->u.dval = d;
377 return ret;
380 literal_t *new_boolean_literal(parser_ctx_t *ctx, BOOL bval)
382 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
384 ret->type = LT_BOOL;
385 ret->u.bval = bval;
387 return ret;
390 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
392 LONGLONG d, hlp;
393 int exp = 0;
395 d = int_part;
396 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
397 hlp = d*10 + *(ctx->ptr++) - '0';
398 if(d>MAXLONGLONG/10 || hlp<0) {
399 exp++;
400 break;
402 else
403 d = hlp;
405 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
406 exp++;
407 ctx->ptr++;
410 if(*ctx->ptr == '.') {
411 ctx->ptr++;
413 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
414 hlp = d*10 + *(ctx->ptr++) - '0';
415 if(d>MAXLONGLONG/10 || hlp<0)
416 break;
418 d = hlp;
419 exp--;
421 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
422 ctx->ptr++;
425 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
426 int sign = 1, e = 0;
428 ctx->ptr++;
429 if(ctx->ptr < ctx->end) {
430 if(*ctx->ptr == '+') {
431 ctx->ptr++;
432 }else if(*ctx->ptr == '-') {
433 sign = -1;
434 ctx->ptr++;
435 }else if(!isdigitW(*ctx->ptr)) {
436 WARN("Expected exponent part\n");
437 return lex_error(ctx, E_FAIL);
441 if(ctx->ptr == ctx->end) {
442 WARN("unexpected end of file\n");
443 return lex_error(ctx, E_FAIL);
446 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
447 if(e > INT_MAX/10 || (e = e*10 + *ctx->ptr++ - '0')<0)
448 e = INT_MAX;
450 e *= sign;
452 if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
453 else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
454 else exp += e;
457 if(is_identifier_char(*ctx->ptr)) {
458 WARN("wrong char after zero\n");
459 return lex_error(ctx, JS_E_MISSING_SEMICOLON);
462 *literal = new_double_literal(ctx, exp>=0 ? d*pow(10, exp) : d/pow(10, -exp));
463 return tNumericLiteral;
466 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
468 LONG l, d;
470 l = *ctx->ptr++ - '0';
471 if(!l) {
472 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
473 if(++ctx->ptr == ctx->end) {
474 ERR("unexpected end of file\n");
475 return 0;
478 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
479 l = l*16 + d;
480 ctx->ptr++;
483 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
484 WARN("unexpected identifier char\n");
485 return lex_error(ctx, JS_E_MISSING_SEMICOLON);
488 *literal = new_double_literal(ctx, l);
489 return tNumericLiteral;
492 if(isdigitW(*ctx->ptr)) {
493 unsigned base = 8;
494 const WCHAR *ptr;
495 double val = 0;
497 for(ptr = ctx->ptr; ptr < ctx->end && isdigitW(*ptr); ptr++) {
498 if(*ptr > '7') {
499 base = 10;
500 break;
504 do {
505 val = val*base + *ctx->ptr-'0';
506 }while(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr));
508 /* FIXME: Do we need it here? */
509 if(ctx->ptr < ctx->end && (is_identifier_char(*ctx->ptr) || *ctx->ptr == '.')) {
510 WARN("wrong char after octal literal: '%c'\n", *ctx->ptr);
511 return lex_error(ctx, JS_E_MISSING_SEMICOLON);
514 *literal = new_double_literal(ctx, val);
515 return tNumericLiteral;
518 if(is_identifier_char(*ctx->ptr)) {
519 WARN("wrong char after zero\n");
520 return lex_error(ctx, JS_E_MISSING_SEMICOLON);
524 return parse_double_literal(ctx, l, literal);
527 static int next_token(parser_ctx_t *ctx, void *lval)
529 do {
530 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
531 if(is_endline(*ctx->ptr++))
532 ctx->nl = TRUE;
534 if(ctx->ptr == ctx->end)
535 return tEOF;
536 }while(skip_comment(ctx) || skip_html_comment(ctx));
538 if(ctx->implicit_nl_semicolon) {
539 if(ctx->nl)
540 return ';';
541 ctx->implicit_nl_semicolon = FALSE;
544 if(isalphaW(*ctx->ptr)) {
545 int ret = check_keywords(ctx, lval);
546 if(ret)
547 return ret;
549 return parse_identifier(ctx, lval);
552 if(isdigitW(*ctx->ptr))
553 return parse_numeric_literal(ctx, lval);
555 switch(*ctx->ptr) {
556 case '{':
557 case '(':
558 case ')':
559 case '[':
560 case ']':
561 case ';':
562 case ',':
563 case '~':
564 case '?':
565 case ':':
566 return *ctx->ptr++;
568 case '}':
569 *(const WCHAR**)lval = ctx->ptr++;
570 return '}';
572 case '.':
573 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
574 return parse_double_literal(ctx, 0, lval);
575 return '.';
577 case '<':
578 if(++ctx->ptr == ctx->end) {
579 *(int*)lval = EXPR_LESS;
580 return tRelOper;
583 switch(*ctx->ptr) {
584 case '=': /* <= */
585 ctx->ptr++;
586 *(int*)lval = EXPR_LESSEQ;
587 return tRelOper;
588 case '<': /* << */
589 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
590 ctx->ptr++;
591 *(int*)lval = EXPR_ASSIGNLSHIFT;
592 return tAssignOper;
594 *(int*)lval = EXPR_LSHIFT;
595 return tShiftOper;
596 default: /* < */
597 *(int*)lval = EXPR_LESS;
598 return tRelOper;
601 case '>':
602 if(++ctx->ptr == ctx->end) { /* > */
603 *(int*)lval = EXPR_GREATER;
604 return tRelOper;
607 switch(*ctx->ptr) {
608 case '=': /* >= */
609 ctx->ptr++;
610 *(int*)lval = EXPR_GREATEREQ;
611 return tRelOper;
612 case '>': /* >> */
613 if(++ctx->ptr < ctx->end) {
614 if(*ctx->ptr == '=') { /* >>= */
615 ctx->ptr++;
616 *(int*)lval = EXPR_ASSIGNRSHIFT;
617 return tAssignOper;
619 if(*ctx->ptr == '>') { /* >>> */
620 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
621 ctx->ptr++;
622 *(int*)lval = EXPR_ASSIGNRRSHIFT;
623 return tAssignOper;
625 *(int*)lval = EXPR_RRSHIFT;
626 return tRelOper;
629 *(int*)lval = EXPR_RSHIFT;
630 return tShiftOper;
631 default:
632 *(int*)lval = EXPR_GREATER;
633 return tRelOper;
636 case '+':
637 ctx->ptr++;
638 if(ctx->ptr < ctx->end) {
639 switch(*ctx->ptr) {
640 case '+': /* ++ */
641 ctx->ptr++;
642 return tINC;
643 case '=': /* += */
644 ctx->ptr++;
645 *(int*)lval = EXPR_ASSIGNADD;
646 return tAssignOper;
649 return '+';
651 case '-':
652 ctx->ptr++;
653 if(ctx->ptr < ctx->end) {
654 switch(*ctx->ptr) {
655 case '-': /* -- or --> */
656 ctx->ptr++;
657 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
658 ctx->ptr++;
659 return tHTMLCOMMENT;
661 return tDEC;
662 case '=': /* -= */
663 ctx->ptr++;
664 *(int*)lval = EXPR_ASSIGNSUB;
665 return tAssignOper;
668 return '-';
670 case '*':
671 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
672 ctx->ptr++;
673 *(int*)lval = EXPR_ASSIGNMUL;
674 return tAssignOper;
676 return '*';
678 case '%':
679 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
680 ctx->ptr++;
681 *(int*)lval = EXPR_ASSIGNMOD;
682 return tAssignOper;
684 return '%';
686 case '&':
687 if(++ctx->ptr < ctx->end) {
688 switch(*ctx->ptr) {
689 case '=': /* &= */
690 ctx->ptr++;
691 *(int*)lval = EXPR_ASSIGNAND;
692 return tAssignOper;
693 case '&': /* && */
694 ctx->ptr++;
695 return tANDAND;
698 return '&';
700 case '|':
701 if(++ctx->ptr < ctx->end) {
702 switch(*ctx->ptr) {
703 case '=': /* |= */
704 ctx->ptr++;
705 *(int*)lval = EXPR_ASSIGNOR;
706 return tAssignOper;
707 case '|': /* || */
708 ctx->ptr++;
709 return tOROR;
712 return '|';
714 case '^':
715 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
716 ctx->ptr++;
717 *(int*)lval = EXPR_ASSIGNXOR;
718 return tAssignOper;
720 return '^';
722 case '!':
723 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
724 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
725 ctx->ptr++;
726 *(int*)lval = EXPR_NOTEQEQ;
727 return tEqOper;
729 *(int*)lval = EXPR_NOTEQ;
730 return tEqOper;
732 return '!';
734 case '=':
735 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
736 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
737 ctx->ptr++;
738 *(int*)lval = EXPR_EQEQ;
739 return tEqOper;
741 *(int*)lval = EXPR_EQ;
742 return tEqOper;
744 return '=';
746 case '/':
747 if(++ctx->ptr < ctx->end) {
748 if(*ctx->ptr == '=') { /* /= */
749 ctx->ptr++;
750 *(int*)lval = EXPR_ASSIGNDIV;
751 return kDIVEQ;
754 return '/';
756 case '\"':
757 case '\'':
758 return parse_string_literal(ctx, lval, *ctx->ptr);
760 case '_':
761 case '$':
762 return parse_identifier(ctx, lval);
764 case '@':
765 return '@';
768 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
769 return 0;
772 struct _cc_var_t {
773 BOOL is_num;
774 union {
775 BOOL b;
776 DOUBLE n;
777 } u;
778 struct _cc_var_t *next;
779 unsigned name_len;
780 WCHAR name[0];
783 void release_cc(cc_ctx_t *cc)
785 cc_var_t *iter, *next;
787 for(iter = cc->vars; iter; iter = next) {
788 next = iter->next;
789 heap_free(iter);
792 heap_free(cc);
795 static BOOL add_cc_var(cc_ctx_t *cc, const WCHAR *name, cc_var_t *v)
797 cc_var_t *new_v;
798 unsigned len;
800 len = strlenW(name);
802 new_v = heap_alloc(sizeof(cc_var_t) + (len+1)*sizeof(WCHAR));
803 if(!new_v)
804 return FALSE;
806 memcpy(new_v, v, sizeof(*v));
807 memcpy(new_v->name, name, (len+1)*sizeof(WCHAR));
808 new_v->name_len = len;
809 new_v->next = cc->vars;
810 cc->vars = new_v;
811 return TRUE;
814 static cc_var_t *find_cc_var(cc_ctx_t *cc, const WCHAR *name, unsigned name_len)
816 cc_var_t *iter;
818 for(iter = cc->vars; iter; iter = iter->next) {
819 if(iter->name_len == name_len && !memcmp(iter->name, name, name_len*sizeof(WCHAR)))
820 return iter;
823 return NULL;
826 static int init_cc(parser_ctx_t *ctx)
828 cc_ctx_t *cc;
829 cc_var_t v;
831 static const WCHAR _win32W[] = {'_','w','i','n','3','2',0};
832 static const WCHAR _win64W[] = {'_','w','i','n','6','4',0};
833 static const WCHAR _x86W[] = {'_','x','8','6',0};
834 static const WCHAR _amd64W[] = {'_','a','m','d','6','4',0};
835 static const WCHAR _jscriptW[] = {'_','j','s','c','r','i','p','t',0};
836 static const WCHAR _jscript_buildW[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
837 static const WCHAR _jscript_versionW[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
839 if(ctx->script->cc)
840 return 0;
842 cc = heap_alloc(sizeof(cc_ctx_t));
843 if(!cc)
844 return lex_error(ctx, E_OUTOFMEMORY);
846 cc->vars = NULL;
847 v.is_num = FALSE;
848 v.u.b = TRUE;
849 if(!add_cc_var(cc, _jscriptW, &v)
850 || !add_cc_var(cc, sizeof(void*) == 8 ? _win64W : _win32W, &v)
851 || !add_cc_var(cc, sizeof(void*) == 8 ? _amd64W : _x86W, &v)) {
852 release_cc(cc);
853 return lex_error(ctx, E_OUTOFMEMORY);
856 v.is_num = TRUE;
857 v.u.n = JSCRIPT_BUILD_VERSION;
858 if(!add_cc_var(cc, _jscript_buildW, &v)) {
859 release_cc(cc);
860 return lex_error(ctx, E_OUTOFMEMORY);
863 v.u.n = JSCRIPT_MAJOR_VERSION + (DOUBLE)JSCRIPT_MINOR_VERSION/10.0;
864 if(!add_cc_var(cc, _jscript_versionW, &v)) {
865 release_cc(cc);
866 return lex_error(ctx, E_OUTOFMEMORY);
869 ctx->script->cc = cc;
870 return 0;
873 static int cc_token(parser_ctx_t *ctx, void *lval)
875 unsigned id_len = 0;
876 cc_var_t *var;
878 static const WCHAR cc_onW[] = {'c','c','_','o','n',0};
879 static const WCHAR setW[] = {'s','e','t',0};
880 static const WCHAR elifW[] = {'e','l','i','f',0};
881 static const WCHAR endW[] = {'e','n','d',0};
883 ctx->ptr++;
885 if(!check_keyword(ctx, cc_onW, NULL))
886 return init_cc(ctx);
888 if(!check_keyword(ctx, setW, NULL)) {
889 FIXME("@set not implemented\n");
890 return lex_error(ctx, E_NOTIMPL);
893 if(!check_keyword(ctx, ifW, NULL)) {
894 FIXME("@if not implemented\n");
895 return lex_error(ctx, E_NOTIMPL);
898 if(!check_keyword(ctx, elifW, NULL)) {
899 FIXME("@elif not implemented\n");
900 return lex_error(ctx, E_NOTIMPL);
903 if(!check_keyword(ctx, elseW, NULL)) {
904 FIXME("@else not implemented\n");
905 return lex_error(ctx, E_NOTIMPL);
908 if(!check_keyword(ctx, endW, NULL)) {
909 FIXME("@end not implemented\n");
910 return lex_error(ctx, E_NOTIMPL);
913 if(!ctx->script->cc)
914 return lex_error(ctx, JS_E_DISABLED_CC);
916 while(ctx->ptr+id_len < ctx->end && is_identifier_char(ctx->ptr[id_len]))
917 id_len++;
918 if(!id_len)
919 return '@';
921 TRACE("var %s\n", debugstr_wn(ctx->ptr, id_len));
923 var = find_cc_var(ctx->script->cc, ctx->ptr, id_len);
924 ctx->ptr += id_len;
925 if(!var || var->is_num) {
926 *(literal_t**)lval = new_double_literal(ctx, var ? var->u.n : NAN);
927 return tNumericLiteral;
930 *(literal_t**)lval = new_boolean_literal(ctx, var->u.b);
931 return tBooleanLiteral;
934 int parser_lex(void *lval, parser_ctx_t *ctx)
936 int ret;
938 ctx->nl = ctx->ptr == ctx->begin;
940 do {
941 ret = next_token(ctx, lval);
942 } while(ret == '@' && !(ret = cc_token(ctx, lval)));
944 return ret;
947 literal_t *parse_regexp(parser_ctx_t *ctx)
949 const WCHAR *re, *flags_ptr;
950 BOOL in_class = FALSE;
951 DWORD re_len, flags;
952 literal_t *ret;
953 HRESULT hres;
955 TRACE("\n");
957 while(*--ctx->ptr != '/');
959 /* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
960 re = ++ctx->ptr;
961 while(ctx->ptr < ctx->end) {
962 if(*ctx->ptr == '\\') {
963 if(++ctx->ptr == ctx->end)
964 break;
965 }else if(in_class) {
966 if(*ctx->ptr == '\n')
967 break;
968 if(*ctx->ptr == ']')
969 in_class = FALSE;
970 }else {
971 if(*ctx->ptr == '/')
972 break;
974 if(*ctx->ptr == '[')
975 in_class = TRUE;
977 ctx->ptr++;
980 if(ctx->ptr == ctx->end || *ctx->ptr != '/') {
981 WARN("pre-parsing failed\n");
982 return NULL;
985 re_len = ctx->ptr-re;
987 flags_ptr = ++ctx->ptr;
988 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
989 ctx->ptr++;
991 hres = parse_regexp_flags(flags_ptr, ctx->ptr-flags_ptr, &flags);
992 if(FAILED(hres))
993 return NULL;
995 ret = parser_alloc(ctx, sizeof(literal_t));
996 ret->type = LT_REGEXP;
997 ret->u.regexp.str = re;
998 ret->u.regexp.str_len = re_len;
999 ret->u.regexp.flags = flags;
1000 return ret;