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_comment(parser_ctx_t *ctx)
179 if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
182 switch(ctx->ptr[1]) {
185 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
188 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
191 WARN("unexpected end of file (missing end of comment)\n");
197 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
207 static BOOL unescape(WCHAR *str)
247 i = hex_to_int(*++p);
252 i = hex_to_int(*++p);
258 i = hex_to_int(*++p);
263 i = hex_to_int(*++p);
268 i = hex_to_int(*++p);
273 i = hex_to_int(*++p);
282 c = c*10 + (*p++ - '0');
298 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
300 const WCHAR *ptr = ctx->ptr++;
304 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
309 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
310 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
313 /* FIXME: unescape */
317 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
319 const WCHAR *ptr = ++ctx->ptr;
323 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
324 if(*ctx->ptr++ == '\\')
328 if(ctx->ptr == ctx->end) {
329 WARN("unexpected end of file\n");
330 return lex_error(ctx, E_FAIL);
335 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
336 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
341 if(!unescape(wstr)) {
342 WARN("unescape failed\n");
343 return lex_error(ctx, E_FAIL);
346 return tStringLiteral;
349 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
351 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
359 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
363 if(ctx->ptr == ctx->end || !isdigitW(*ctx->ptr)) {
364 ERR("No digit after point\n");
369 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
370 d += (tmp /= 10.0)*(*ctx->ptr++ - '0');
372 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
376 if(ctx->ptr < ctx->end) {
377 if(*ctx->ptr == '+') {
379 }else if(*ctx->ptr == '-') {
382 }else if(!isdigitW(*ctx->ptr)) {
383 WARN("Expected exponent part\n");
384 return lex_error(ctx, E_FAIL);
388 if(ctx->ptr == ctx->end) {
389 WARN("unexpected end of file\n");
390 return lex_error(ctx, E_FAIL);
393 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
394 e = e*10 + *ctx->ptr++ - '0';
400 *literal = parser_alloc(ctx, sizeof(literal_t));
401 (*literal)->vt = VT_R8;
402 (*literal)->u.dval = d;
404 return tNumericLiteral;
407 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
411 l = *ctx->ptr++ - '0';
412 if(ctx->ptr == ctx->end) {
413 *literal = alloc_int_literal(ctx, l);
414 return tNumericLiteral;
418 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
419 if(++ctx->ptr == ctx->end) {
420 ERR("unexpexted end of file\n");
424 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
429 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
430 WARN("unexpected identifier char\n");
431 return lex_error(ctx, E_FAIL);
434 *literal = alloc_int_literal(ctx, l);
435 return tNumericLiteral;
438 if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
439 WARN("wrong char after zero\n");
440 return lex_error(ctx, E_FAIL);
443 *literal = alloc_int_literal(ctx, 0);
446 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
447 l = l*10 + *(ctx->ptr++)-'0';
449 if(ctx->ptr < ctx->end) {
450 if(*ctx->ptr == '.') {
452 return parse_double_literal(ctx, l, literal);
455 if(is_identifier_char(*ctx->ptr)) {
456 WARN("unexpected identifier char\n");
457 return lex_error(ctx, E_FAIL);
461 *literal = alloc_int_literal(ctx, l);
462 return tNumericLiteral;
465 int parser_lex(void *lval, parser_ctx_t *ctx)
473 if(ctx->ptr == ctx->end)
475 }while(skip_comment(ctx));
477 if(isalphaW(*ctx->ptr)) {
478 ret = check_keywords(ctx, lval);
482 return parse_identifier(ctx, (const WCHAR**)lval);
485 if(isdigitW(*ctx->ptr))
486 return parse_numeric_literal(ctx, lval);
502 *(const WCHAR**)lval = ctx->ptr++;
506 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
507 return parse_double_literal(ctx, 0, lval);
511 if(++ctx->ptr == ctx->end) {
512 *(int*)lval = EXPR_LESS;
519 *(int*)lval = EXPR_LESSEQ;
522 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
524 *(int*)lval = EXPR_ASSIGNLSHIFT;
527 *(int*)lval = EXPR_LSHIFT;
530 *(int*)lval = EXPR_LESS;
535 if(++ctx->ptr == ctx->end) { /* > */
536 *(int*)lval = EXPR_GREATER;
543 *(int*)lval = EXPR_GREATEREQ;
546 if(++ctx->ptr < ctx->end) {
547 if(*ctx->ptr == '=') { /* >>= */
549 *(int*)lval = EXPR_ASSIGNRSHIFT;
552 if(*ctx->ptr == '>') { /* >>> */
553 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
555 *(int*)lval = EXPR_ASSIGNRRSHIFT;
558 *(int*)lval = EXPR_RRSHIFT;
562 *(int*)lval = EXPR_RSHIFT;
565 *(int*)lval = EXPR_GREATER;
571 if(ctx->ptr < ctx->end) {
578 *(int*)lval = EXPR_ASSIGNADD;
586 if(ctx->ptr < ctx->end) {
593 *(int*)lval = EXPR_ASSIGNSUB;
600 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
602 *(int*)lval = EXPR_ASSIGNMUL;
608 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
610 *(int*)lval = EXPR_ASSIGNMOD;
616 if(++ctx->ptr < ctx->end) {
620 *(int*)lval = EXPR_ASSIGNAND;
630 if(++ctx->ptr < ctx->end) {
634 *(int*)lval = EXPR_ASSIGNOR;
644 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
646 *(int*)lval = EXPR_ASSIGNXOR;
652 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
653 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
655 *(int*)lval = EXPR_NOTEQEQ;
658 *(int*)lval = EXPR_NOTEQ;
664 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
665 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
667 *(int*)lval = EXPR_EQEQ;
670 *(int*)lval = EXPR_EQ;
676 if(++ctx->ptr < ctx->end) {
677 if(*ctx->ptr == '=') { /* /= */
679 *(int*)lval = EXPR_ASSIGNDIV;
687 return parse_string_literal(ctx, (const WCHAR**)lval, *ctx->ptr);
691 return parse_identifier(ctx, lval);
694 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
698 static void add_object_literal(parser_ctx_t *ctx, DispatchEx *obj)
700 obj_literal_t *literal = parser_alloc(ctx, sizeof(obj_literal_t));
703 literal->next = ctx->obj_literals;
704 ctx->obj_literals = literal;
707 literal_t *parse_regexp(parser_ctx_t *ctx)
709 const WCHAR *re, *flags;
718 while(ctx->ptr < ctx->end && *ctx->ptr != '/') {
719 if(*ctx->ptr++ == '\\' && ctx->ptr < ctx->end)
723 if(ctx->ptr == ctx->end) {
724 WARN("unexpected end of file\n");
728 re_len = ctx->ptr-re;
731 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
734 hres = create_regexp_str(ctx->script, re, re_len, flags, ctx->ptr-flags, ®exp);
738 add_object_literal(ctx, regexp);
740 ret = parser_alloc(ctx, sizeof(literal_t));
741 ret->vt = VT_DISPATCH;
742 ret->u.disp = (IDispatch*)_IDispatchEx_(regexp);