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)
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)))
123 /* ECMA-262 3rd Edition 7.3 */
124 static BOOL is_endline(WCHAR c)
126 return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
129 static BOOL is_identifier_char(WCHAR c)
131 return isalnumW(c) || c == '$' || c == '_' || c == '\\';
134 static int hex_to_int(WCHAR c)
136 if('0' <= c && c <= '9')
139 if('a' <= c && c <= 'f')
142 if('A' <= c && c <= 'F')
148 static int check_keywords(parser_ctx_t *ctx)
150 int min = 0, max = sizeof(keywords)/sizeof(keywords[0])-1, r, i;
155 r = check_keyword(ctx, keywords[i].word);
157 return keywords[i].token;
168 static void skip_spaces(parser_ctx_t *ctx)
170 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
171 if(is_endline(*ctx->ptr++))
176 static BOOL skip_comment(parser_ctx_t *ctx)
178 if(ctx->ptr+1 >= ctx->end || *ctx->ptr != '/')
181 switch(ctx->ptr[1]) {
184 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
187 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
190 WARN("unexpected end of file (missing end of comment)\n");
196 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
206 static BOOL unescape(WCHAR *str)
248 i = hex_to_int(*++p);
253 i = hex_to_int(*++p);
259 i = hex_to_int(*++p);
264 i = hex_to_int(*++p);
269 i = hex_to_int(*++p);
274 i = hex_to_int(*++p);
291 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
293 const WCHAR *ptr = ctx->ptr++;
297 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
302 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
303 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
306 /* FIXME: unescape */
310 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
312 const WCHAR *ptr = ++ctx->ptr;
316 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
317 if(*ctx->ptr++ == '\\')
321 if(ctx->ptr == ctx->end) {
322 WARN("unexpected end of file\n");
323 return lex_error(ctx, E_FAIL);
328 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
329 memcpy(wstr, ptr, (len+1)*sizeof(WCHAR));
334 if(!unescape(wstr)) {
335 WARN("unescape failed\n");
336 return lex_error(ctx, E_FAIL);
339 return tStringLiteral;
342 static literal_t *alloc_int_literal(parser_ctx_t *ctx, LONG l)
344 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
352 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
356 if(ctx->ptr == ctx->end || !isdigitW(*ctx->ptr)) {
357 ERR("No digit after point\n");
362 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
363 d += (tmp /= 10.0)*(*ctx->ptr++ - '0');
365 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
369 if(ctx->ptr < ctx->end) {
370 if(*ctx->ptr == '+') {
372 }else if(*ctx->ptr == '-') {
375 }else if(!isdigitW(*ctx->ptr)) {
376 WARN("Expected exponent part\n");
377 return lex_error(ctx, E_FAIL);
381 if(ctx->ptr == ctx->end) {
382 WARN("unexpected end of file\n");
383 return lex_error(ctx, E_FAIL);
386 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
387 e = e*10 + *ctx->ptr++ - '0';
393 *literal = parser_alloc(ctx, sizeof(literal_t));
394 (*literal)->vt = VT_R8;
395 (*literal)->u.dval = d;
397 return tNumericLiteral;
400 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
404 l = *ctx->ptr++ - '0';
405 if(ctx->ptr == ctx->end) {
406 *literal = alloc_int_literal(ctx, l);
407 return tNumericLiteral;
411 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
412 if(++ctx->ptr == ctx->end) {
413 ERR("unexpexted end of file\n");
417 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
422 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
423 WARN("unexpected identifier char\n");
424 return lex_error(ctx, E_FAIL);
427 *literal = alloc_int_literal(ctx, l);
428 return tNumericLiteral;
431 if(isdigitW(*ctx->ptr) || is_identifier_char(*ctx->ptr)) {
432 WARN("wrong char after zero\n");
433 return lex_error(ctx, E_FAIL);
436 *literal = alloc_int_literal(ctx, 0);
439 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
440 l = l*10 + *(ctx->ptr++)-'0';
442 if(ctx->ptr < ctx->end) {
443 if(*ctx->ptr == '.') {
445 return parse_double_literal(ctx, l, literal);
448 if(is_identifier_char(*ctx->ptr)) {
449 WARN("unexpected identifier char\n");
450 return lex_error(ctx, E_FAIL);
454 *literal = alloc_int_literal(ctx, l);
455 return tNumericLiteral;
458 int parser_lex(void *lval, parser_ctx_t *ctx)
466 if(ctx->ptr == ctx->end)
468 }while(skip_comment(ctx));
470 if(isalphaW(*ctx->ptr)) {
471 ret = check_keywords(ctx);
475 return parse_identifier(ctx, (const WCHAR**)lval);
478 if(isdigitW(*ctx->ptr))
479 return parse_numeric_literal(ctx, lval);
496 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
497 return parse_double_literal(ctx, 0, lval);
501 if(++ctx->ptr == ctx->end) {
502 *(int*)lval = EXPR_LESS;
509 *(int*)lval = EXPR_LESSEQ;
512 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
514 *(int*)lval = EXPR_ASSIGNLSHIFT;
517 *(int*)lval = EXPR_LSHIFT;
520 *(int*)lval = EXPR_LESS;
525 if(++ctx->ptr == ctx->end) { /* > */
526 *(int*)lval = EXPR_GREATER;
533 *(int*)lval = EXPR_GREATEREQ;
536 if(++ctx->ptr < ctx->end) {
537 if(*ctx->ptr == '=') { /* >>= */
539 *(int*)lval = EXPR_ASSIGNRSHIFT;
542 if(*ctx->ptr == '>') { /* >>> */
543 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
545 *(int*)lval = EXPR_ASSIGNRRSHIFT;
548 *(int*)lval = EXPR_RRSHIFT;
552 *(int*)lval = EXPR_RSHIFT;
555 *(int*)lval = EXPR_GREATER;
561 if(ctx->ptr < ctx->end) {
568 *(int*)lval = EXPR_ASSIGNADD;
576 if(ctx->ptr < ctx->end) {
583 *(int*)lval = EXPR_ASSIGNSUB;
590 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
592 *(int*)lval = EXPR_ASSIGNMUL;
598 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
600 *(int*)lval = EXPR_ASSIGNMOD;
606 if(++ctx->ptr < ctx->end) {
610 *(int*)lval = EXPR_ASSIGNAND;
620 if(++ctx->ptr < ctx->end) {
624 *(int*)lval = EXPR_ASSIGNOR;
634 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
636 *(int*)lval = EXPR_ASSIGNXOR;
642 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
643 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
645 *(int*)lval = EXPR_NOTEQEQ;
648 *(int*)lval = EXPR_NOTEQ;
654 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
655 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
657 *(int*)lval = EXPR_EQEQ;
660 *(int*)lval = EXPR_EQ;
666 if(++ctx->ptr < ctx->end) {
667 if(*ctx->ptr == '=') { /* /= */
669 *(int*)lval = EXPR_ASSIGNMUL;
677 return parse_string_literal(ctx, (const WCHAR**)lval, *ctx->ptr);
681 return parse_identifier(ctx, lval);
684 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);