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
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};
73 {continueW, kCONTINUE},
81 {functionW, kFUNCTION},
84 {instanceofW, kINSTANCEOF},
94 {undefinedW, kUNDEFINED},
101 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
103 ctx->hres = JSCRIPT_ERROR|hres;
104 ctx->lexer_error = TRUE;
108 /* ECMA-262 3rd Edition 7.6 */
109 static BOOL is_identifier_char(WCHAR c)
111 return isalnumW(c) || c == '$' || c == '_' || c == '\\';
114 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
116 const WCHAR *p1 = ctx->ptr;
117 const WCHAR *p2 = word;
119 while(p1 < ctx->end && *p2) {
126 if(*p2 || (p1 < ctx->end && is_identifier_char(*p1)))
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')
145 if('a' <= c && c <= 'f')
148 if('A' <= c && c <= 'F')
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;
161 r = check_keyword(ctx, keywords[i].word, lval);
163 return keywords[i].token;
174 static void skip_spaces(parser_ctx_t *ctx)
176 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
177 if(is_endline(*ctx->ptr++))
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))
191 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
196 static BOOL skip_comment(parser_ctx_t *ctx)
198 if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
201 switch(ctx->ptr[1]) {
204 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
207 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
210 WARN("unexpected end of file (missing end of comment)\n");
216 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
226 static BOOL unescape(WCHAR *str)
266 i = hex_to_int(*++p);
271 i = hex_to_int(*++p);
277 i = hex_to_int(*++p);
282 i = hex_to_int(*++p);
287 i = hex_to_int(*++p);
292 i = hex_to_int(*++p);
301 c = c*8 + (*p++ - '0');
303 c = c*8 + (*p++ - '0');
319 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
321 const WCHAR *ptr = ctx->ptr++;
325 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
330 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
331 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
334 /* FIXME: unescape */
338 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
340 const WCHAR *ptr = ++ctx->ptr;
344 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
345 if(*ctx->ptr++ == '\\')
349 if(ctx->ptr == ctx->end)
350 return lex_error(ctx, IDS_UNTERMINATED_STR);
354 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
355 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
360 if(!unescape(wstr)) {
361 WARN("unescape failed\n");
362 return lex_error(ctx, E_FAIL);
365 return tStringLiteral;
368 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
370 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
378 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
383 if(ctx->ptr == ctx->end || (!isdigitW(*ctx->ptr) &&
384 *ctx->ptr!='.' && *ctx->ptr!='e' && *ctx->ptr!='E')) {
385 ERR("Illegal character\n");
390 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
391 hlp = d*10 + *(ctx->ptr++) - '0';
392 if(d>LONGLONG_MAX/10 || hlp<0) {
399 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
404 if(*ctx->ptr == '.') ctx->ptr++;
406 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
407 hlp = d*10 + *(ctx->ptr++) - '0';
408 if(d>LONGLONG_MAX/10 || hlp<0)
414 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
417 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
421 if(ctx->ptr < ctx->end) {
422 if(*ctx->ptr == '+') {
424 }else if(*ctx->ptr == '-') {
427 }else if(!isdigitW(*ctx->ptr)) {
428 WARN("Expected exponent part\n");
429 return lex_error(ctx, E_FAIL);
433 if(ctx->ptr == ctx->end) {
434 WARN("unexpected end of file\n");
435 return lex_error(ctx, E_FAIL);
438 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
439 if(e > INT_MAX/10 || (e = e*10 + *ctx->ptr++ - '0')<0)
444 if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
445 else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
449 *literal = parser_alloc(ctx, sizeof(literal_t));
450 (*literal)->type = LT_DOUBLE;
451 (*literal)->u.dval = (double)d*pow(10, exp);
453 return tNumericLiteral;
456 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
460 l = *ctx->ptr++ - '0';
461 if(ctx->ptr == ctx->end) {
462 *literal = alloc_int_literal(ctx, l);
463 return tNumericLiteral;
467 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
468 if(++ctx->ptr == ctx->end) {
469 ERR("unexpexted end of file\n");
473 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
478 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
479 WARN("unexpected identifier char\n");
480 return lex_error(ctx, E_FAIL);
483 *literal = alloc_int_literal(ctx, l);
484 return tNumericLiteral;
487 if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
488 WARN("wrong char after zero\n");
489 return lex_error(ctx, E_FAIL);
492 *literal = alloc_int_literal(ctx, 0);
495 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
497 d = l*10 + *(ctx->ptr)-'0';
499 /* Check for integer overflow */
500 if (l > INT_MAX/10 || d < 0)
501 return parse_double_literal(ctx, l, literal);
507 if(ctx->ptr < ctx->end) {
508 if(*ctx->ptr == '.' || *ctx->ptr == 'e' || *ctx->ptr == 'E')
509 return parse_double_literal(ctx, l, literal);
511 if(is_identifier_char(*ctx->ptr)) {
512 WARN("unexpected identifier char\n");
513 return lex_error(ctx, E_FAIL);
517 *literal = alloc_int_literal(ctx, l);
518 return tNumericLiteral;
521 int parser_lex(void *lval, parser_ctx_t *ctx)
525 ctx->nl = ctx->ptr == ctx->begin;
529 if(ctx->ptr == ctx->end)
531 }while(skip_comment(ctx) || skip_html_comment(ctx));
533 if(isalphaW(*ctx->ptr)) {
534 ret = check_keywords(ctx, lval);
538 return parse_identifier(ctx, lval);
541 if(isdigitW(*ctx->ptr))
542 return parse_numeric_literal(ctx, lval);
558 *(const WCHAR**)lval = ctx->ptr++;
562 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
563 return parse_double_literal(ctx, 0, lval);
567 if(++ctx->ptr == ctx->end) {
568 *(int*)lval = EXPR_LESS;
575 *(int*)lval = EXPR_LESSEQ;
578 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
580 *(int*)lval = EXPR_ASSIGNLSHIFT;
583 *(int*)lval = EXPR_LSHIFT;
586 *(int*)lval = EXPR_LESS;
591 if(++ctx->ptr == ctx->end) { /* > */
592 *(int*)lval = EXPR_GREATER;
599 *(int*)lval = EXPR_GREATEREQ;
602 if(++ctx->ptr < ctx->end) {
603 if(*ctx->ptr == '=') { /* >>= */
605 *(int*)lval = EXPR_ASSIGNRSHIFT;
608 if(*ctx->ptr == '>') { /* >>> */
609 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
611 *(int*)lval = EXPR_ASSIGNRRSHIFT;
614 *(int*)lval = EXPR_RRSHIFT;
618 *(int*)lval = EXPR_RSHIFT;
621 *(int*)lval = EXPR_GREATER;
627 if(ctx->ptr < ctx->end) {
634 *(int*)lval = EXPR_ASSIGNADD;
642 if(ctx->ptr < ctx->end) {
644 case '-': /* -- or --> */
646 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
653 *(int*)lval = EXPR_ASSIGNSUB;
660 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
662 *(int*)lval = EXPR_ASSIGNMUL;
668 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
670 *(int*)lval = EXPR_ASSIGNMOD;
676 if(++ctx->ptr < ctx->end) {
680 *(int*)lval = EXPR_ASSIGNAND;
690 if(++ctx->ptr < ctx->end) {
694 *(int*)lval = EXPR_ASSIGNOR;
704 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
706 *(int*)lval = EXPR_ASSIGNXOR;
712 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
713 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
715 *(int*)lval = EXPR_NOTEQEQ;
718 *(int*)lval = EXPR_NOTEQ;
724 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
725 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
727 *(int*)lval = EXPR_EQEQ;
730 *(int*)lval = EXPR_EQ;
736 if(++ctx->ptr < ctx->end) {
737 if(*ctx->ptr == '=') { /* /= */
739 *(int*)lval = EXPR_ASSIGNDIV;
747 return parse_string_literal(ctx, lval, *ctx->ptr);
751 return parse_identifier(ctx, lval);
754 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
758 literal_t *parse_regexp(parser_ctx_t *ctx)
760 const WCHAR *re, *flags_ptr;
767 while(*ctx->ptr != '/')
771 while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
772 if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
776 if(ctx->ptr == ctx->end) {
777 WARN("unexpected end of file\n");
781 re_len = ctx->ptr-re;
783 flags_ptr = ++ctx->ptr;
784 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
787 hres = parse_regexp_flags(flags_ptr, ctx->ptr-flags_ptr, &flags);
791 ret = parser_alloc(ctx, sizeof(literal_t));
792 ret->type = LT_REGEXP;
793 ret->u.regexp.str = re;
794 ret->u.regexp.str_len = re_len;
795 ret->u.regexp.flags = flags;