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
},
100 static int lex_error(parser_ctx_t
*ctx
, HRESULT hres
)
102 ctx
->hres
= JSCRIPT_ERROR
|hres
;
103 ctx
->lexer_error
= TRUE
;
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
) {
125 if(*p2
|| (p1
< ctx
->end
&& is_identifier_char(*p1
)))
133 /* ECMA-262 3rd Edition 7.3 */
134 static BOOL
is_endline(WCHAR c
)
136 return c
== '\n' || c
== '\r' || c
== 0x2028 || c
== 0x2029;
139 static int hex_to_int(WCHAR c
)
141 if('0' <= c
&& c
<= '9')
144 if('a' <= c
&& c
<= 'f')
147 if('A' <= c
&& c
<= 'F')
153 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
155 int min
= 0, max
= sizeof(keywords
)/sizeof(keywords
[0])-1, r
, i
;
160 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
162 return keywords
[i
].token
;
173 static void skip_spaces(parser_ctx_t
*ctx
)
175 while(ctx
->ptr
< ctx
->end
&& isspaceW(*ctx
->ptr
)) {
176 if(is_endline(*ctx
->ptr
++))
181 static BOOL
skip_html_comment(parser_ctx_t
*ctx
)
183 const WCHAR html_commentW
[] = {'<','!','-','-',0};
185 if(!ctx
->is_html
|| ctx
->ptr
+3 >= ctx
->end
||
186 memcmp(ctx
->ptr
, html_commentW
, sizeof(WCHAR
)*4))
190 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
++));
195 static BOOL
skip_comment(parser_ctx_t
*ctx
)
197 if(ctx
->ptr
+1 >= ctx
->end
|| *ctx
->ptr
!= '/')
200 switch(ctx
->ptr
[1]) {
203 while(ctx
->ptr
+1 < ctx
->end
&& (ctx
->ptr
[0] != '*' || ctx
->ptr
[1] != '/'))
206 if(ctx
->ptr
[0] == '*' && ctx
->ptr
[1] == '/') {
209 WARN("unexpected end of file (missing end of comment)\n");
215 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
))
225 static BOOL
unescape(WCHAR
*str
)
265 i
= hex_to_int(*++p
);
270 i
= hex_to_int(*++p
);
276 i
= hex_to_int(*++p
);
281 i
= hex_to_int(*++p
);
286 i
= hex_to_int(*++p
);
291 i
= hex_to_int(*++p
);
300 c
= c
*8 + (*p
++ - '0');
302 c
= c
*8 + (*p
++ - '0');
318 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
320 const WCHAR
*ptr
= ctx
->ptr
++;
324 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
329 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
330 memcpy(wstr
, ptr
, (len
+1)*sizeof(WCHAR
));
333 /* FIXME: unescape */
337 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
, WCHAR endch
)
339 const WCHAR
*ptr
= ++ctx
->ptr
;
343 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= endch
) {
344 if(*ctx
->ptr
++ == '\\')
348 if(ctx
->ptr
== ctx
->end
)
349 return lex_error(ctx
, IDS_UNTERMINATED_STR
);
353 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
354 memcpy(wstr
, ptr
, (len
+1)*sizeof(WCHAR
));
359 if(!unescape(wstr
)) {
360 WARN("unescape failed\n");
361 return lex_error(ctx
, E_FAIL
);
364 return tStringLiteral
;
367 static literal_t
*alloc_int_literal(parser_ctx_t
*ctx
, LONG l
)
369 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
377 static int parse_double_literal(parser_ctx_t
*ctx
, LONG int_part
, literal_t
**literal
)
382 if(ctx
->ptr
== ctx
->end
|| (!isdigitW(*ctx
->ptr
) &&
383 *ctx
->ptr
!='.' && *ctx
->ptr
!='e' && *ctx
->ptr
!='E')) {
384 ERR("Illegal character\n");
389 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
390 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
391 if(d
>LONGLONG_MAX
/10 || hlp
<0) {
398 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
403 if(*ctx
->ptr
== '.') ctx
->ptr
++;
405 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
406 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
407 if(d
>LONGLONG_MAX
/10 || hlp
<0)
413 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
416 if(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')) {
420 if(ctx
->ptr
< ctx
->end
) {
421 if(*ctx
->ptr
== '+') {
423 }else if(*ctx
->ptr
== '-') {
426 }else if(!isdigitW(*ctx
->ptr
)) {
427 WARN("Expected exponent part\n");
428 return lex_error(ctx
, E_FAIL
);
432 if(ctx
->ptr
== ctx
->end
) {
433 WARN("unexpected end of file\n");
434 return lex_error(ctx
, E_FAIL
);
437 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
438 if(e
> INT_MAX
/10 || (e
= e
*10 + *ctx
->ptr
++ - '0')<0)
443 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
444 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
448 *literal
= parser_alloc(ctx
, sizeof(literal_t
));
449 (*literal
)->type
= LT_DOUBLE
;
450 (*literal
)->u
.dval
= (double)d
*pow(10, exp
);
452 return tNumericLiteral
;
455 static int parse_numeric_literal(parser_ctx_t
*ctx
, literal_t
**literal
)
459 l
= *ctx
->ptr
++ - '0';
460 if(ctx
->ptr
== ctx
->end
) {
461 *literal
= alloc_int_literal(ctx
, l
);
462 return tNumericLiteral
;
466 if(*ctx
->ptr
== 'x' || *ctx
->ptr
== 'X') {
467 if(++ctx
->ptr
== ctx
->end
) {
468 ERR("unexpexted end of file\n");
472 while(ctx
->ptr
< ctx
->end
&& (d
= hex_to_int(*ctx
->ptr
)) != -1) {
477 if(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
)) {
478 WARN("unexpected identifier char\n");
479 return lex_error(ctx
, E_FAIL
);
482 *literal
= alloc_int_literal(ctx
, l
);
483 return tNumericLiteral
;
486 if(isdigitW(*ctx
->ptr
) || is_identifier_char(*ctx
->ptr
)) {
487 WARN("wrong char after zero\n");
488 return lex_error(ctx
, E_FAIL
);
491 *literal
= alloc_int_literal(ctx
, 0);
494 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
496 d
= l
*10 + *(ctx
->ptr
)-'0';
498 /* Check for integer overflow */
499 if (l
> INT_MAX
/10 || d
< 0)
500 return parse_double_literal(ctx
, l
, literal
);
506 if(ctx
->ptr
< ctx
->end
) {
507 if(*ctx
->ptr
== '.' || *ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')
508 return parse_double_literal(ctx
, l
, literal
);
510 if(is_identifier_char(*ctx
->ptr
)) {
511 WARN("unexpected identifier char\n");
512 return lex_error(ctx
, E_FAIL
);
516 *literal
= alloc_int_literal(ctx
, l
);
517 return tNumericLiteral
;
520 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
524 ctx
->nl
= ctx
->ptr
== ctx
->begin
;
528 if(ctx
->ptr
== ctx
->end
)
530 }while(skip_comment(ctx
) || skip_html_comment(ctx
));
532 if(isalphaW(*ctx
->ptr
)) {
533 ret
= check_keywords(ctx
, lval
);
537 return parse_identifier(ctx
, lval
);
540 if(isdigitW(*ctx
->ptr
))
541 return parse_numeric_literal(ctx
, lval
);
557 *(const WCHAR
**)lval
= ctx
->ptr
++;
561 if(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
562 return parse_double_literal(ctx
, 0, lval
);
566 if(++ctx
->ptr
== ctx
->end
) {
567 *(int*)lval
= EXPR_LESS
;
574 *(int*)lval
= EXPR_LESSEQ
;
577 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* <<= */
579 *(int*)lval
= EXPR_ASSIGNLSHIFT
;
582 *(int*)lval
= EXPR_LSHIFT
;
585 *(int*)lval
= EXPR_LESS
;
590 if(++ctx
->ptr
== ctx
->end
) { /* > */
591 *(int*)lval
= EXPR_GREATER
;
598 *(int*)lval
= EXPR_GREATEREQ
;
601 if(++ctx
->ptr
< ctx
->end
) {
602 if(*ctx
->ptr
== '=') { /* >>= */
604 *(int*)lval
= EXPR_ASSIGNRSHIFT
;
607 if(*ctx
->ptr
== '>') { /* >>> */
608 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* >>>= */
610 *(int*)lval
= EXPR_ASSIGNRRSHIFT
;
613 *(int*)lval
= EXPR_RRSHIFT
;
617 *(int*)lval
= EXPR_RSHIFT
;
620 *(int*)lval
= EXPR_GREATER
;
626 if(ctx
->ptr
< ctx
->end
) {
633 *(int*)lval
= EXPR_ASSIGNADD
;
641 if(ctx
->ptr
< ctx
->end
) {
643 case '-': /* -- or --> */
645 if(ctx
->is_html
&& ctx
->nl
&& ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '>') {
652 *(int*)lval
= EXPR_ASSIGNSUB
;
659 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* *= */
661 *(int*)lval
= EXPR_ASSIGNMUL
;
667 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* %= */
669 *(int*)lval
= EXPR_ASSIGNMOD
;
675 if(++ctx
->ptr
< ctx
->end
) {
679 *(int*)lval
= EXPR_ASSIGNAND
;
689 if(++ctx
->ptr
< ctx
->end
) {
693 *(int*)lval
= EXPR_ASSIGNOR
;
703 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* ^= */
705 *(int*)lval
= EXPR_ASSIGNXOR
;
711 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* != */
712 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* !== */
714 *(int*)lval
= EXPR_NOTEQEQ
;
717 *(int*)lval
= EXPR_NOTEQ
;
723 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* == */
724 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* === */
726 *(int*)lval
= EXPR_EQEQ
;
729 *(int*)lval
= EXPR_EQ
;
735 if(++ctx
->ptr
< ctx
->end
) {
736 if(*ctx
->ptr
== '=') { /* /= */
738 *(int*)lval
= EXPR_ASSIGNDIV
;
746 return parse_string_literal(ctx
, lval
, *ctx
->ptr
);
750 return parse_identifier(ctx
, lval
);
753 WARN("unexpected char '%c' %d\n", *ctx
->ptr
, *ctx
->ptr
);
757 literal_t
*parse_regexp(parser_ctx_t
*ctx
)
759 const WCHAR
*re
, *flags_ptr
;
766 while(*ctx
->ptr
!= '/')
770 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= '/') {
771 if(*ctx
->ptr
++ == '\\' && ctx
->ptr
< ctx
->end
)
775 if(ctx
->ptr
== ctx
->end
) {
776 WARN("unexpected end of file\n");
780 re_len
= ctx
->ptr
-re
;
782 flags_ptr
= ++ctx
->ptr
;
783 while(ctx
->ptr
< ctx
->end
&& isalnumW(*ctx
->ptr
))
786 hres
= parse_regexp_flags(flags_ptr
, ctx
->ptr
-flags_ptr
, &flags
);
790 ret
= parser_alloc(ctx
, sizeof(literal_t
));
791 ret
->type
= LT_REGEXP
;
792 ret
->u
.regexp
.str
= re
;
793 ret
->u
.regexp
.str_len
= re_len
;
794 ret
->u
.regexp
.flags
= flags
;