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
26 #include "parser.tab.h"
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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};
70 {continueW, kCONTINUE},
78 {functionW, kFUNCTION},
81 {instanceofW, kINSTANCEOF},
91 {undefinedW, kUNDEFINED},
98 static int lex_error(parser_ctx_t *ctx, HRESULT hres)
104 static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
106 const WCHAR *p1 = ctx->ptr;
107 const WCHAR *p2 = word;
109 while(p1 < ctx->end && *p2) {
116 if(*p2 || (p1 < ctx->end && isalnumW(*p1)))
124 /* ECMA-262 3rd Edition 7.3 */
125 static BOOL is_endline(WCHAR c)
127 return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
130 static BOOL is_identifier_char(WCHAR c)
132 return isalnumW(c) || c == '$' || c == '_' || c == '\\';
135 static int hex_to_int(WCHAR c)
137 if('0' <= c && c <= '9')
140 if('a' <= c && c <= 'f')
143 if('A' <= c && c <= 'F')
149 static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
151 int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
156 r = check_keyword(ctx, keywords[i].word, lval);
158 return keywords[i].token;
169 static void skip_spaces(parser_ctx_t *ctx)
171 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
172 if(is_endline(*ctx->ptr++))
177 static BOOL skip_html_comment(parser_ctx_t *ctx)
179 const WCHAR html_commentW[] = {'<','!','-','-',0};
181 if(!ctx->is_html || ctx->ptr+3 >= ctx->end ||
182 memcmp(ctx->ptr, html_commentW, sizeof(WCHAR)*4))
186 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
191 static BOOL skip_comment(parser_ctx_t *ctx)
193 if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
196 switch(ctx->ptr[1]) {
199 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
202 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
205 WARN("unexpected end of file (missing end of comment)\n");
211 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
221 static BOOL unescape(WCHAR *str)
261 i = hex_to_int(*++p);
266 i = hex_to_int(*++p);
272 i = hex_to_int(*++p);
277 i = hex_to_int(*++p);
282 i = hex_to_int(*++p);
287 i = hex_to_int(*++p);
296 c = c*10 + (*p++ - '0');
312 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
314 const WCHAR *ptr = ctx->ptr++;
318 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
323 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
324 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
327 /* FIXME: unescape */
331 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
333 const WCHAR *ptr = ++ctx->ptr;
337 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
338 if(*ctx->ptr++ == '\\')
342 if(ctx->ptr == ctx->end) {
343 WARN("unexpected end of file\n");
344 return lex_error(ctx, E_FAIL);
349 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
350 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
355 if(!unescape(wstr)) {
356 WARN("unescape failed\n");
357 return lex_error(ctx, E_FAIL);
360 return tStringLiteral;
363 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
365 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
373 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
377 if(ctx->ptr == ctx->end || !isdigitW(*ctx->ptr)) {
378 ERR("No digit after point\n");
383 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
384 d += (tmp /= 10.0)*(*ctx->ptr++ - '0');
386 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
390 if(ctx->ptr < ctx->end) {
391 if(*ctx->ptr == '+') {
393 }else if(*ctx->ptr == '-') {
396 }else if(!isdigitW(*ctx->ptr)) {
397 WARN("Expected exponent part\n");
398 return lex_error(ctx, E_FAIL);
402 if(ctx->ptr == ctx->end) {
403 WARN("unexpected end of file\n");
404 return lex_error(ctx, E_FAIL);
407 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
408 e = e*10 + *ctx->ptr++ - '0';
414 *literal = parser_alloc(ctx, sizeof(literal_t));
415 (*literal)->vt = VT_R8;
416 (*literal)->u.dval = d;
418 return tNumericLiteral;
421 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
425 l = *ctx->ptr++ - '0';
426 if(ctx->ptr == ctx->end) {
427 *literal = alloc_int_literal(ctx, l);
428 return tNumericLiteral;
432 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
433 if(++ctx->ptr == ctx->end) {
434 ERR("unexpexted end of file\n");
438 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
443 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
444 WARN("unexpected identifier char\n");
445 return lex_error(ctx, E_FAIL);
448 *literal = alloc_int_literal(ctx, l);
449 return tNumericLiteral;
452 if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
453 WARN("wrong char after zero\n");
454 return lex_error(ctx, E_FAIL);
457 *literal = alloc_int_literal(ctx, 0);
460 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
461 l = l*10 + *(ctx->ptr++)-'0';
463 if(ctx->ptr < ctx->end) {
464 if(*ctx->ptr == '.') {
466 return parse_double_literal(ctx, l, literal);
469 if(is_identifier_char(*ctx->ptr)) {
470 WARN("unexpected identifier char\n");
471 return lex_error(ctx, E_FAIL);
475 *literal = alloc_int_literal(ctx, l);
476 return tNumericLiteral;
479 int parser_lex(void *lval, parser_ctx_t *ctx)
483 ctx->nl = ctx->ptr == ctx->begin;
487 if(ctx->ptr == ctx->end)
489 }while(skip_comment(ctx) || skip_html_comment(ctx));
491 if(isalphaW(*ctx->ptr)) {
492 ret = check_keywords(ctx, lval);
496 return parse_identifier(ctx, lval);
499 if(isdigitW(*ctx->ptr))
500 return parse_numeric_literal(ctx, lval);
516 *(const WCHAR**)lval = ctx->ptr++;
520 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
521 return parse_double_literal(ctx, 0, lval);
525 if(++ctx->ptr == ctx->end) {
526 *(int*)lval = EXPR_LESS;
533 *(int*)lval = EXPR_LESSEQ;
536 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
538 *(int*)lval = EXPR_ASSIGNLSHIFT;
541 *(int*)lval = EXPR_LSHIFT;
544 *(int*)lval = EXPR_LESS;
549 if(++ctx->ptr == ctx->end) { /* > */
550 *(int*)lval = EXPR_GREATER;
557 *(int*)lval = EXPR_GREATEREQ;
560 if(++ctx->ptr < ctx->end) {
561 if(*ctx->ptr == '=') { /* >>= */
563 *(int*)lval = EXPR_ASSIGNRSHIFT;
566 if(*ctx->ptr == '>') { /* >>> */
567 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
569 *(int*)lval = EXPR_ASSIGNRRSHIFT;
572 *(int*)lval = EXPR_RRSHIFT;
576 *(int*)lval = EXPR_RSHIFT;
579 *(int*)lval = EXPR_GREATER;
585 if(ctx->ptr < ctx->end) {
592 *(int*)lval = EXPR_ASSIGNADD;
600 if(ctx->ptr < ctx->end) {
602 case '-': /* -- or --> */
604 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
611 *(int*)lval = EXPR_ASSIGNSUB;
618 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
620 *(int*)lval = EXPR_ASSIGNMUL;
626 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
628 *(int*)lval = EXPR_ASSIGNMOD;
634 if(++ctx->ptr < ctx->end) {
638 *(int*)lval = EXPR_ASSIGNAND;
648 if(++ctx->ptr < ctx->end) {
652 *(int*)lval = EXPR_ASSIGNOR;
662 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
664 *(int*)lval = EXPR_ASSIGNXOR;
670 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
671 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
673 *(int*)lval = EXPR_NOTEQEQ;
676 *(int*)lval = EXPR_NOTEQ;
682 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
683 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
685 *(int*)lval = EXPR_EQEQ;
688 *(int*)lval = EXPR_EQ;
694 if(++ctx->ptr < ctx->end) {
695 if(*ctx->ptr == '=') { /* /= */
697 *(int*)lval = EXPR_ASSIGNDIV;
705 return parse_string_literal(ctx, lval, *ctx->ptr);
709 return parse_identifier(ctx, lval);
712 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
716 static void add_object_literal(parser_ctx_t *ctx, DispatchEx *obj)
718 obj_literal_t *literal = parser_alloc(ctx, sizeof(obj_literal_t));
721 literal->next = ctx->obj_literals;
722 ctx->obj_literals = literal;
725 literal_t *parse_regexp(parser_ctx_t *ctx)
727 const WCHAR *re, *flags;
736 while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
737 if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
741 if(ctx->ptr == ctx->end) {
742 WARN("unexpected end of file\n");
746 re_len = ctx->ptr-re;
749 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
752 hres = create_regexp_str(ctx->script, re, re_len, flags, ctx->ptr-flags, ®exp);
756 add_object_literal(ctx, regexp);
758 ret = parser_alloc(ctx, sizeof(literal_t));
759 ret->vt = VT_DISPATCH;
760 ret->u.disp = (IDispatch*)_IDispatchEx_(regexp);