jscript: Don't unescape '\v'.
[wine.git] / dlls / jscript / lex.c
blob723f0ef7ed9e9d5e87d4e21f74b9518452ac6e82
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 <math.h>
20 #include <limits.h>
22 #include "jscript.h"
23 #include "activscp.h"
24 #include "objsafe.h"
25 #include "engine.h"
27 #include "parser.tab.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
34 #define LONGLONG_MAX (((LONGLONG)0x7fffffff<<32)|0xffffffff)
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 undefinedW[] = {'u','n','d','e','f','i','n','e','d',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 struct {
67 const WCHAR *word;
68 int token;
69 } keywords[] = {
70 {breakW, kBREAK},
71 {caseW, kCASE},
72 {catchW, kCATCH},
73 {continueW, kCONTINUE},
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},
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 return keywords[i].token;
165 if(r > 0)
166 min = i+1;
167 else
168 max = i-1;
171 return 0;
174 static void skip_spaces(parser_ctx_t *ctx)
176 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
177 if(is_endline(*ctx->ptr++))
178 ctx->nl = TRUE;
182 static BOOL skip_html_comment(parser_ctx_t *ctx)
184 const WCHAR html_commentW[] = {'<','!','-','-',0};
186 if(!ctx->is_html || ctx->ptr+3 >= ctx->end ||
187 memcmp(ctx->ptr, html_commentW, sizeof(WCHAR)*4))
188 return FALSE;
190 ctx->nl = TRUE;
191 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
193 return TRUE;
196 static BOOL skip_comment(parser_ctx_t *ctx)
198 if(ctx->ptr+1 >= ctx->end)
199 return FALSE;
201 if(*ctx->ptr != '/') {
202 if(*ctx->ptr == '@' && ctx->ptr+2 < ctx->end && ctx->ptr[1] == '*' && ctx->ptr[2] == '/') {
203 ctx->ptr += 3;
204 return TRUE;
207 return FALSE;
210 switch(ctx->ptr[1]) {
211 case '*':
212 ctx->ptr += 2;
213 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
214 return FALSE;
215 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
216 ctx->ptr++;
218 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
219 ctx->ptr += 2;
220 }else {
221 WARN("unexpected end of file (missing end of comment)\n");
222 ctx->ptr = ctx->end;
224 break;
225 case '/':
226 ctx->ptr += 2;
227 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
228 return FALSE;
229 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
230 ctx->ptr++;
231 break;
232 default:
233 return FALSE;
236 return TRUE;
239 static BOOL unescape(WCHAR *str)
241 WCHAR *pd, *p, c;
242 int i;
244 pd = p = str;
245 while(*p) {
246 if(*p != '\\') {
247 *pd++ = *p++;
248 continue;
251 p++;
252 c = 0;
254 switch(*p) {
255 case '\'':
256 case '\"':
257 case '\\':
258 c = *p;
259 break;
260 case 'b':
261 c = '\b';
262 break;
263 case 't':
264 c = '\t';
265 break;
266 case 'n':
267 c = '\n';
268 break;
269 case 'f':
270 c = '\f';
271 break;
272 case 'r':
273 c = '\r';
274 break;
275 case 'x':
276 i = hex_to_int(*++p);
277 if(i == -1)
278 return FALSE;
279 c = i << 4;
281 i = hex_to_int(*++p);
282 if(i == -1)
283 return FALSE;
284 c += i;
285 break;
286 case 'u':
287 i = hex_to_int(*++p);
288 if(i == -1)
289 return FALSE;
290 c = i << 12;
292 i = hex_to_int(*++p);
293 if(i == -1)
294 return FALSE;
295 c += i << 8;
297 i = hex_to_int(*++p);
298 if(i == -1)
299 return FALSE;
300 c += i << 4;
302 i = hex_to_int(*++p);
303 if(i == -1)
304 return FALSE;
305 c += i;
306 break;
307 default:
308 if(isdigitW(*p)) {
309 c = *p++ - '0';
310 if(isdigitW(*p)) {
311 c = c*8 + (*p++ - '0');
312 if(isdigitW(*p))
313 c = c*8 + (*p++ - '0');
315 p--;
317 else
318 c = *p;
321 *pd++ = c;
322 p++;
325 *pd = 0;
326 return TRUE;
329 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
331 const WCHAR *ptr = ctx->ptr++;
332 WCHAR *wstr;
333 int len;
335 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
336 ctx->ptr++;
338 len = ctx->ptr-ptr;
340 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
341 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
342 wstr[len] = 0;
344 /* FIXME: unescape */
345 return tIdentifier;
348 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
350 const WCHAR *ptr = ++ctx->ptr;
351 WCHAR *wstr;
352 int len;
354 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
355 if(*ctx->ptr++ == '\\')
356 ctx->ptr++;
359 if(ctx->ptr == ctx->end)
360 return lex_error(ctx, JS_E_UNTERMINATED_STRING);
362 len = ctx->ptr-ptr;
364 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
365 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
366 wstr[len] = 0;
368 ctx->ptr++;
370 if(!unescape(wstr)) {
371 WARN("unescape failed\n");
372 return lex_error(ctx, E_FAIL);
375 return tStringLiteral;
378 static literal_t *new_int_literal(parser_ctx_t *ctx, LONG l)
380 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
382 ret->type = LT_INT;
383 ret->u.lval = l;
385 return ret;
388 static literal_t *new_double_literal(parser_ctx_t *ctx, DOUBLE d)
390 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
392 ret->type = LT_DOUBLE;
393 ret->u.dval = d;
394 return ret;
397 literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
399 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
401 ret->type = LT_BOOL;
402 ret->u.bval = bval;
404 return ret;
407 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
409 LONGLONG d, hlp;
410 int exp = 0;
412 if(ctx->ptr == ctx->end || (!isdigitW(*ctx->ptr) &&
413 *ctx->ptr!='.' && *ctx->ptr!='e' && *ctx->ptr!='E')) {
414 ERR("Illegal character\n");
415 return 0;
418 d = int_part;
419 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
420 hlp = d*10 + *(ctx->ptr++) - '0';
421 if(d>LONGLONG_MAX/10 || hlp<0) {
422 exp++;
423 break;
425 else
426 d = hlp;
428 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
429 exp++;
430 ctx->ptr++;
433 if(*ctx->ptr == '.') ctx->ptr++;
435 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
436 hlp = d*10 + *(ctx->ptr++) - '0';
437 if(d>LONGLONG_MAX/10 || hlp<0)
438 break;
440 d = hlp;
441 exp--;
443 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
444 ctx->ptr++;
446 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
447 int sign = 1, e = 0;
449 ctx->ptr++;
450 if(ctx->ptr < ctx->end) {
451 if(*ctx->ptr == '+') {
452 ctx->ptr++;
453 }else if(*ctx->ptr == '-') {
454 sign = -1;
455 ctx->ptr++;
456 }else if(!isdigitW(*ctx->ptr)) {
457 WARN("Expected exponent part\n");
458 return lex_error(ctx, E_FAIL);
462 if(ctx->ptr == ctx->end) {
463 WARN("unexpected end of file\n");
464 return lex_error(ctx, E_FAIL);
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 *literal = new_double_literal(ctx, (DOUBLE)d*pow(10, exp));
479 return tNumericLiteral;
482 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
484 LONG l, d;
486 l = *ctx->ptr++ - '0';
487 if(ctx->ptr == ctx->end) {
488 *literal = new_int_literal(ctx, l);
489 return tNumericLiteral;
492 if(!l) {
493 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
494 if(++ctx->ptr == ctx->end) {
495 ERR("unexpexted end of file\n");
496 return 0;
499 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
500 l = l*16 + d;
501 ctx->ptr++;
504 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
505 WARN("unexpected identifier char\n");
506 return lex_error(ctx, E_FAIL);
509 *literal = new_int_literal(ctx, l);
510 return tNumericLiteral;
513 if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
514 WARN("wrong char after zero\n");
515 return lex_error(ctx, E_FAIL);
518 *literal = new_int_literal(ctx, 0);
521 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
523 d = l*10 + *(ctx->ptr)-'0';
525 /* Check for integer overflow */
526 if (l > INT_MAX/10 || d < 0)
527 return parse_double_literal(ctx, l, literal);
529 l = d;
530 ctx->ptr++;
533 if(ctx->ptr < ctx->end) {
534 if(*ctx->ptr == '.' || *ctx->ptr == 'e' || *ctx->ptr == 'E')
535 return parse_double_literal(ctx, l, literal);
537 if(is_identifier_char(*ctx->ptr)) {
538 WARN("unexpected identifier char\n");
539 return lex_error(ctx, E_FAIL);
543 *literal = new_int_literal(ctx, l);
544 return tNumericLiteral;
547 static int next_token(parser_ctx_t *ctx, void *lval)
549 do {
550 skip_spaces(ctx);
551 if(ctx->ptr == ctx->end)
552 return tEOF;
553 }while(skip_comment(ctx) || skip_html_comment(ctx));
555 if(isalphaW(*ctx->ptr)) {
556 int ret = check_keywords(ctx, lval);
557 if(ret)
558 return ret;
560 return parse_identifier(ctx, lval);
563 if(isdigitW(*ctx->ptr))
564 return parse_numeric_literal(ctx, lval);
566 switch(*ctx->ptr) {
567 case '{':
568 case '(':
569 case ')':
570 case '[':
571 case ']':
572 case ';':
573 case ',':
574 case '~':
575 case '?':
576 case ':':
577 return *ctx->ptr++;
579 case '}':
580 *(const WCHAR**)lval = ctx->ptr++;
581 return '}';
583 case '.':
584 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
585 return parse_double_literal(ctx, 0, lval);
586 return '.';
588 case '<':
589 if(++ctx->ptr == ctx->end) {
590 *(int*)lval = EXPR_LESS;
591 return tRelOper;
594 switch(*ctx->ptr) {
595 case '=': /* <= */
596 ctx->ptr++;
597 *(int*)lval = EXPR_LESSEQ;
598 return tRelOper;
599 case '<': /* << */
600 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
601 ctx->ptr++;
602 *(int*)lval = EXPR_ASSIGNLSHIFT;
603 return tAssignOper;
605 *(int*)lval = EXPR_LSHIFT;
606 return tShiftOper;
607 default: /* < */
608 *(int*)lval = EXPR_LESS;
609 return tRelOper;
612 case '>':
613 if(++ctx->ptr == ctx->end) { /* > */
614 *(int*)lval = EXPR_GREATER;
615 return tRelOper;
618 switch(*ctx->ptr) {
619 case '=': /* >= */
620 ctx->ptr++;
621 *(int*)lval = EXPR_GREATEREQ;
622 return tRelOper;
623 case '>': /* >> */
624 if(++ctx->ptr < ctx->end) {
625 if(*ctx->ptr == '=') { /* >>= */
626 ctx->ptr++;
627 *(int*)lval = EXPR_ASSIGNRSHIFT;
628 return tAssignOper;
630 if(*ctx->ptr == '>') { /* >>> */
631 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
632 ctx->ptr++;
633 *(int*)lval = EXPR_ASSIGNRRSHIFT;
634 return tAssignOper;
636 *(int*)lval = EXPR_RRSHIFT;
637 return tRelOper;
640 *(int*)lval = EXPR_RSHIFT;
641 return tShiftOper;
642 default:
643 *(int*)lval = EXPR_GREATER;
644 return tRelOper;
647 case '+':
648 ctx->ptr++;
649 if(ctx->ptr < ctx->end) {
650 switch(*ctx->ptr) {
651 case '+': /* ++ */
652 ctx->ptr++;
653 return tINC;
654 case '=': /* += */
655 ctx->ptr++;
656 *(int*)lval = EXPR_ASSIGNADD;
657 return tAssignOper;
660 return '+';
662 case '-':
663 ctx->ptr++;
664 if(ctx->ptr < ctx->end) {
665 switch(*ctx->ptr) {
666 case '-': /* -- or --> */
667 ctx->ptr++;
668 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
669 ctx->ptr++;
670 return tHTMLCOMMENT;
672 return tDEC;
673 case '=': /* -= */
674 ctx->ptr++;
675 *(int*)lval = EXPR_ASSIGNSUB;
676 return tAssignOper;
679 return '-';
681 case '*':
682 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
683 ctx->ptr++;
684 *(int*)lval = EXPR_ASSIGNMUL;
685 return tAssignOper;
687 return '*';
689 case '%':
690 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
691 ctx->ptr++;
692 *(int*)lval = EXPR_ASSIGNMOD;
693 return tAssignOper;
695 return '%';
697 case '&':
698 if(++ctx->ptr < ctx->end) {
699 switch(*ctx->ptr) {
700 case '=': /* &= */
701 ctx->ptr++;
702 *(int*)lval = EXPR_ASSIGNAND;
703 return tAssignOper;
704 case '&': /* && */
705 ctx->ptr++;
706 return tANDAND;
709 return '&';
711 case '|':
712 if(++ctx->ptr < ctx->end) {
713 switch(*ctx->ptr) {
714 case '=': /* |= */
715 ctx->ptr++;
716 *(int*)lval = EXPR_ASSIGNOR;
717 return tAssignOper;
718 case '|': /* || */
719 ctx->ptr++;
720 return tOROR;
723 return '|';
725 case '^':
726 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
727 ctx->ptr++;
728 *(int*)lval = EXPR_ASSIGNXOR;
729 return tAssignOper;
731 return '^';
733 case '!':
734 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
735 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
736 ctx->ptr++;
737 *(int*)lval = EXPR_NOTEQEQ;
738 return tEqOper;
740 *(int*)lval = EXPR_NOTEQ;
741 return tEqOper;
743 return '!';
745 case '=':
746 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
747 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
748 ctx->ptr++;
749 *(int*)lval = EXPR_EQEQ;
750 return tEqOper;
752 *(int*)lval = EXPR_EQ;
753 return tEqOper;
755 return '=';
757 case '/':
758 if(++ctx->ptr < ctx->end) {
759 if(*ctx->ptr == '=') { /* /= */
760 ctx->ptr++;
761 *(int*)lval = EXPR_ASSIGNDIV;
762 return kDIVEQ;
765 return '/';
767 case '\"':
768 case '\'':
769 return parse_string_literal(ctx, lval, *ctx->ptr);
771 case '_':
772 case '$':
773 return parse_identifier(ctx, lval);
775 case '@':
776 return '@';
779 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
780 return 0;
783 struct _cc_var_t {
784 BOOL is_num;
785 union {
786 VARIANT_BOOL b;
787 DOUBLE n;
788 } u;
789 struct _cc_var_t *next;
790 unsigned name_len;
791 WCHAR name[0];
794 void release_cc(cc_ctx_t *cc)
796 cc_var_t *iter, *next;
798 for(iter = cc->vars; iter; iter = next) {
799 next = iter->next;
800 heap_free(iter);
803 heap_free(cc);
806 static BOOL add_cc_var(cc_ctx_t *cc, const WCHAR *name, cc_var_t *v)
808 cc_var_t *new_v;
809 unsigned len;
811 len = strlenW(name);
813 new_v = heap_alloc(sizeof(cc_var_t) + (len+1)*sizeof(WCHAR));
814 if(!new_v)
815 return FALSE;
817 memcpy(new_v, v, sizeof(*v));
818 memcpy(new_v->name, name, (len+1)*sizeof(WCHAR));
819 new_v->name_len = len;
820 new_v->next = cc->vars;
821 cc->vars = new_v;
822 return TRUE;
825 static cc_var_t *find_cc_var(cc_ctx_t *cc, const WCHAR *name, unsigned name_len)
827 cc_var_t *iter;
829 for(iter = cc->vars; iter; iter = iter->next) {
830 if(iter->name_len == name_len && !memcmp(iter->name, name, name_len*sizeof(WCHAR)))
831 return iter;
834 return NULL;
837 static int init_cc(parser_ctx_t *ctx)
839 cc_ctx_t *cc;
840 cc_var_t v;
842 static const WCHAR _win32W[] = {'_','w','i','n','3','2',0};
843 static const WCHAR _win64W[] = {'_','w','i','n','6','4',0};
844 static const WCHAR _x86W[] = {'_','x','8','6',0};
845 static const WCHAR _amd64W[] = {'_','a','m','d','6','4',0};
846 static const WCHAR _jscriptW[] = {'_','j','s','c','r','i','p','t',0};
847 static const WCHAR _jscript_buildW[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
848 static const WCHAR _jscript_versionW[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
850 if(ctx->script->cc)
851 return 0;
853 cc = heap_alloc(sizeof(cc_ctx_t));
854 if(!cc)
855 return lex_error(ctx, E_OUTOFMEMORY);
857 cc->vars = NULL;
858 v.is_num = FALSE;
859 v.u.b = VARIANT_TRUE;
860 if(!add_cc_var(cc, _jscriptW, &v)
861 || !add_cc_var(cc, sizeof(void*) == 8 ? _win64W : _win32W, &v)
862 || !add_cc_var(cc, sizeof(void*) == 8 ? _amd64W : _x86W, &v)) {
863 release_cc(cc);
864 return lex_error(ctx, E_OUTOFMEMORY);
867 v.is_num = TRUE;
868 v.u.n = JSCRIPT_BUILD_VERSION;
869 if(!add_cc_var(cc, _jscript_buildW, &v)) {
870 release_cc(cc);
871 return lex_error(ctx, E_OUTOFMEMORY);
874 v.u.n = JSCRIPT_MAJOR_VERSION + (DOUBLE)JSCRIPT_MINOR_VERSION/10.0;
875 if(!add_cc_var(cc, _jscript_versionW, &v)) {
876 release_cc(cc);
877 return lex_error(ctx, E_OUTOFMEMORY);
880 ctx->script->cc = cc;
881 return 0;
884 static int cc_token(parser_ctx_t *ctx, void *lval)
886 unsigned id_len = 0;
887 cc_var_t *var;
889 static const WCHAR cc_onW[] = {'c','c','_','o','n',0};
890 static const WCHAR setW[] = {'s','e','t',0};
891 static const WCHAR elifW[] = {'e','l','i','f',0};
892 static const WCHAR endW[] = {'e','n','d',0};
894 ctx->ptr++;
896 if(!check_keyword(ctx, cc_onW, NULL))
897 return init_cc(ctx);
899 if(!check_keyword(ctx, setW, NULL)) {
900 FIXME("@set not implemented\n");
901 return lex_error(ctx, E_NOTIMPL);
904 if(!check_keyword(ctx, ifW, NULL)) {
905 FIXME("@if not implemented\n");
906 return lex_error(ctx, E_NOTIMPL);
909 if(!check_keyword(ctx, elifW, NULL)) {
910 FIXME("@elif not implemented\n");
911 return lex_error(ctx, E_NOTIMPL);
914 if(!check_keyword(ctx, elseW, NULL)) {
915 FIXME("@else not implemented\n");
916 return lex_error(ctx, E_NOTIMPL);
919 if(!check_keyword(ctx, endW, NULL)) {
920 FIXME("@end not implemented\n");
921 return lex_error(ctx, E_NOTIMPL);
924 if(!ctx->script->cc)
925 return lex_error(ctx, JS_E_DISABLED_CC);
927 while(ctx->ptr+id_len < ctx->end && is_identifier_char(ctx->ptr[id_len]))
928 id_len++;
929 if(!id_len)
930 return '@';
932 TRACE("var %s\n", debugstr_wn(ctx->ptr, id_len));
934 var = find_cc_var(ctx->script->cc, ctx->ptr, id_len);
935 ctx->ptr += id_len;
936 if(!var || var->is_num) {
937 *(literal_t**)lval = new_double_literal(ctx, var ? var->u.n : ret_nan());
938 return tNumericLiteral;
941 *(literal_t**)lval = new_boolean_literal(ctx, var->u.b);
942 return tBooleanLiteral;
945 int parser_lex(void *lval, parser_ctx_t *ctx)
947 int ret;
949 ctx->nl = ctx->ptr == ctx->begin;
951 do {
952 ret = next_token(ctx, lval);
953 } while(ret == '@' && !(ret = cc_token(ctx, lval)));
955 return ret;
958 literal_t *parse_regexp(parser_ctx_t *ctx)
960 const WCHAR *re, *flags_ptr;
961 DWORD re_len, flags;
962 literal_t *ret;
963 HRESULT hres;
965 TRACE("\n");
967 while(*ctx->ptr != '/')
968 ctx->ptr--;
970 re = ++ctx->ptr;
971 while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
972 if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
973 ctx->ptr++;
976 if(ctx->ptr == ctx->end) {
977 WARN("unexpected end of file\n");
978 return NULL;
981 re_len = ctx->ptr-re;
983 flags_ptr = ++ctx->ptr;
984 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
985 ctx->ptr++;
987 hres = parse_regexp_flags(flags_ptr, ctx->ptr-flags_ptr, &flags);
988 if(FAILED(hres))
989 return NULL;
991 ret = parser_alloc(ctx, sizeof(literal_t));
992 ret->type = LT_REGEXP;
993 ret->u.regexp.str = re;
994 ret->u.regexp.str_len = re_len;
995 ret->u.regexp.flags = flags;
996 return ret;