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
20 #include "wine/port.h"
29 #include "parser.tab.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
36 #define LONGLONG_MAX (((LONGLONG)0x7fffffff<<32)|0xffffffff)
38 static const WCHAR breakW
[] = {'b','r','e','a','k',0};
39 static const WCHAR caseW
[] = {'c','a','s','e',0};
40 static const WCHAR catchW
[] = {'c','a','t','c','h',0};
41 static const WCHAR continueW
[] = {'c','o','n','t','i','n','u','e',0};
42 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
43 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
44 static const WCHAR doW
[] = {'d','o',0};
45 static const WCHAR elseW
[] = {'e','l','s','e',0};
46 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
47 static const WCHAR finallyW
[] = {'f','i','n','a','l','l','y',0};
48 static const WCHAR forW
[] = {'f','o','r',0};
49 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
50 static const WCHAR ifW
[] = {'i','f',0};
51 static const WCHAR inW
[] = {'i','n',0};
52 static const WCHAR instanceofW
[] = {'i','n','s','t','a','n','c','e','o','f',0};
53 static const WCHAR newW
[] = {'n','e','w',0};
54 static const WCHAR nullW
[] = {'n','u','l','l',0};
55 static const WCHAR returnW
[] = {'r','e','t','u','r','n',0};
56 static const WCHAR switchW
[] = {'s','w','i','t','c','h',0};
57 static const WCHAR thisW
[] = {'t','h','i','s',0};
58 static const WCHAR throwW
[] = {'t','h','r','o','w',0};
59 static const WCHAR trueW
[] = {'t','r','u','e',0};
60 static const WCHAR tryW
[] = {'t','r','y',0};
61 static const WCHAR typeofW
[] = {'t','y','p','e','o','f',0};
62 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
63 static const WCHAR varW
[] = {'v','a','r',0};
64 static const WCHAR voidW
[] = {'v','o','i','d',0};
65 static const WCHAR whileW
[] = {'w','h','i','l','e',0};
66 static const WCHAR withW
[] = {'w','i','t','h',0};
75 {continueW
, kCONTINUE
},
83 {functionW
, kFUNCTION
},
86 {instanceofW
, kINSTANCEOF
},
102 static int lex_error(parser_ctx_t
*ctx
, HRESULT hres
)
105 ctx
->lexer_error
= TRUE
;
109 /* ECMA-262 3rd Edition 7.6 */
110 static BOOL
is_identifier_char(WCHAR c
)
112 return isalnumW(c
) || c
== '$' || c
== '_' || c
== '\\';
115 static int check_keyword(parser_ctx_t
*ctx
, const WCHAR
*word
, const WCHAR
**lval
)
117 const WCHAR
*p1
= ctx
->ptr
;
118 const WCHAR
*p2
= word
;
120 while(p1
< ctx
->end
&& *p2
) {
127 if(*p2
|| (p1
< ctx
->end
&& is_identifier_char(*p1
)))
136 /* ECMA-262 3rd Edition 7.3 */
137 static BOOL
is_endline(WCHAR c
)
139 return c
== '\n' || c
== '\r' || c
== 0x2028 || c
== 0x2029;
142 static int hex_to_int(WCHAR c
)
144 if('0' <= c
&& c
<= '9')
147 if('a' <= c
&& c
<= 'f')
150 if('A' <= c
&& c
<= 'F')
156 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
158 int min
= 0, max
= sizeof(keywords
)/sizeof(keywords
[0])-1, r
, i
;
163 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
165 return keywords
[i
].token
;
176 static void skip_spaces(parser_ctx_t
*ctx
)
178 while(ctx
->ptr
< ctx
->end
&& isspaceW(*ctx
->ptr
)) {
179 if(is_endline(*ctx
->ptr
++))
184 static BOOL
skip_html_comment(parser_ctx_t
*ctx
)
186 const WCHAR html_commentW
[] = {'<','!','-','-',0};
188 if(!ctx
->is_html
|| ctx
->ptr
+3 >= ctx
->end
||
189 memcmp(ctx
->ptr
, html_commentW
, sizeof(WCHAR
)*4))
193 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
++));
198 static BOOL
skip_comment(parser_ctx_t
*ctx
)
200 if(ctx
->ptr
+1 >= ctx
->end
)
203 if(*ctx
->ptr
!= '/') {
204 if(*ctx
->ptr
== '@' && ctx
->ptr
+2 < ctx
->end
&& ctx
->ptr
[1] == '*' && ctx
->ptr
[2] == '/') {
212 switch(ctx
->ptr
[1]) {
215 if(ctx
->ptr
+2 < ctx
->end
&& *ctx
->ptr
== '@' && is_identifier_char(ctx
->ptr
[1]))
217 while(ctx
->ptr
+1 < ctx
->end
&& (ctx
->ptr
[0] != '*' || ctx
->ptr
[1] != '/'))
220 if(ctx
->ptr
[0] == '*' && ctx
->ptr
[1] == '/') {
223 WARN("unexpected end of file (missing end of comment)\n");
229 if(ctx
->ptr
+2 < ctx
->end
&& *ctx
->ptr
== '@' && is_identifier_char(ctx
->ptr
[1]))
231 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
))
241 static BOOL
unescape(WCHAR
*str
)
277 i
= hex_to_int(*++p
);
282 i
= hex_to_int(*++p
);
288 i
= hex_to_int(*++p
);
293 i
= hex_to_int(*++p
);
298 i
= hex_to_int(*++p
);
303 i
= hex_to_int(*++p
);
312 c
= c
*8 + (*p
++ - '0');
314 c
= c
*8 + (*p
++ - '0');
330 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
332 const WCHAR
*ptr
= ctx
->ptr
++;
336 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
341 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
342 memcpy(wstr
, ptr
, len
*sizeof(WCHAR
));
345 /* FIXME: unescape */
349 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
, WCHAR endch
)
351 const WCHAR
*ptr
= ++ctx
->ptr
;
355 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= endch
) {
356 if(*ctx
->ptr
++ == '\\')
360 if(ctx
->ptr
== ctx
->end
)
361 return lex_error(ctx
, JS_E_UNTERMINATED_STRING
);
365 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
366 memcpy(wstr
, ptr
, len
*sizeof(WCHAR
));
371 if(!unescape(wstr
)) {
372 WARN("unescape failed\n");
373 return lex_error(ctx
, E_FAIL
);
376 return tStringLiteral
;
379 static literal_t
*new_double_literal(parser_ctx_t
*ctx
, DOUBLE d
)
381 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
383 ret
->type
= LT_DOUBLE
;
388 literal_t
*new_boolean_literal(parser_ctx_t
*ctx
, VARIANT_BOOL bval
)
390 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
398 static int parse_double_literal(parser_ctx_t
*ctx
, LONG int_part
, literal_t
**literal
)
404 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
405 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
406 if(d
>LONGLONG_MAX
/10 || hlp
<0) {
413 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
418 if(*ctx
->ptr
== '.') {
421 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
422 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
423 if(d
>LONGLONG_MAX
/10 || hlp
<0)
429 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
433 if(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')) {
437 if(ctx
->ptr
< ctx
->end
) {
438 if(*ctx
->ptr
== '+') {
440 }else if(*ctx
->ptr
== '-') {
443 }else if(!isdigitW(*ctx
->ptr
)) {
444 WARN("Expected exponent part\n");
445 return lex_error(ctx
, E_FAIL
);
449 if(ctx
->ptr
== ctx
->end
) {
450 WARN("unexpected end of file\n");
451 return lex_error(ctx
, E_FAIL
);
454 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
455 if(e
> INT_MAX
/10 || (e
= e
*10 + *ctx
->ptr
++ - '0')<0)
460 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
461 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
465 *literal
= new_double_literal(ctx
, exp
>=0 ? d
*pow(10, exp
) : d
/pow(10, -exp
));
466 return tNumericLiteral
;
469 static int parse_numeric_literal(parser_ctx_t
*ctx
, literal_t
**literal
)
473 l
= *ctx
->ptr
++ - '0';
475 if(*ctx
->ptr
== 'x' || *ctx
->ptr
== 'X') {
476 if(++ctx
->ptr
== ctx
->end
) {
477 ERR("unexpected end of file\n");
481 while(ctx
->ptr
< ctx
->end
&& (d
= hex_to_int(*ctx
->ptr
)) != -1) {
486 if(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
)) {
487 WARN("unexpected identifier char\n");
488 return lex_error(ctx
, E_FAIL
);
491 *literal
= new_double_literal(ctx
, l
);
492 return tNumericLiteral
;
495 if(is_identifier_char(*ctx
->ptr
)) {
496 WARN("wrong char after zero\n");
497 return lex_error(ctx
, E_FAIL
);
500 if(isdigitW(*ctx
->ptr
)) {
501 FIXME("octal literals not implemented\n");
502 return lex_error(ctx
, E_NOTIMPL
);
506 return parse_double_literal(ctx
, l
, literal
);
509 static int next_token(parser_ctx_t
*ctx
, void *lval
)
513 if(ctx
->ptr
== ctx
->end
)
515 }while(skip_comment(ctx
) || skip_html_comment(ctx
));
517 if(isalphaW(*ctx
->ptr
)) {
518 int ret
= check_keywords(ctx
, lval
);
522 return parse_identifier(ctx
, lval
);
525 if(isdigitW(*ctx
->ptr
))
526 return parse_numeric_literal(ctx
, lval
);
542 *(const WCHAR
**)lval
= ctx
->ptr
++;
546 if(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
547 return parse_double_literal(ctx
, 0, lval
);
551 if(++ctx
->ptr
== ctx
->end
) {
552 *(int*)lval
= EXPR_LESS
;
559 *(int*)lval
= EXPR_LESSEQ
;
562 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* <<= */
564 *(int*)lval
= EXPR_ASSIGNLSHIFT
;
567 *(int*)lval
= EXPR_LSHIFT
;
570 *(int*)lval
= EXPR_LESS
;
575 if(++ctx
->ptr
== ctx
->end
) { /* > */
576 *(int*)lval
= EXPR_GREATER
;
583 *(int*)lval
= EXPR_GREATEREQ
;
586 if(++ctx
->ptr
< ctx
->end
) {
587 if(*ctx
->ptr
== '=') { /* >>= */
589 *(int*)lval
= EXPR_ASSIGNRSHIFT
;
592 if(*ctx
->ptr
== '>') { /* >>> */
593 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* >>>= */
595 *(int*)lval
= EXPR_ASSIGNRRSHIFT
;
598 *(int*)lval
= EXPR_RRSHIFT
;
602 *(int*)lval
= EXPR_RSHIFT
;
605 *(int*)lval
= EXPR_GREATER
;
611 if(ctx
->ptr
< ctx
->end
) {
618 *(int*)lval
= EXPR_ASSIGNADD
;
626 if(ctx
->ptr
< ctx
->end
) {
628 case '-': /* -- or --> */
630 if(ctx
->is_html
&& ctx
->nl
&& ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '>') {
637 *(int*)lval
= EXPR_ASSIGNSUB
;
644 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* *= */
646 *(int*)lval
= EXPR_ASSIGNMUL
;
652 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* %= */
654 *(int*)lval
= EXPR_ASSIGNMOD
;
660 if(++ctx
->ptr
< ctx
->end
) {
664 *(int*)lval
= EXPR_ASSIGNAND
;
674 if(++ctx
->ptr
< ctx
->end
) {
678 *(int*)lval
= EXPR_ASSIGNOR
;
688 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* ^= */
690 *(int*)lval
= EXPR_ASSIGNXOR
;
696 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* != */
697 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* !== */
699 *(int*)lval
= EXPR_NOTEQEQ
;
702 *(int*)lval
= EXPR_NOTEQ
;
708 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* == */
709 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* === */
711 *(int*)lval
= EXPR_EQEQ
;
714 *(int*)lval
= EXPR_EQ
;
720 if(++ctx
->ptr
< ctx
->end
) {
721 if(*ctx
->ptr
== '=') { /* /= */
723 *(int*)lval
= EXPR_ASSIGNDIV
;
731 return parse_string_literal(ctx
, lval
, *ctx
->ptr
);
735 return parse_identifier(ctx
, lval
);
741 WARN("unexpected char '%c' %d\n", *ctx
->ptr
, *ctx
->ptr
);
751 struct _cc_var_t
*next
;
756 void release_cc(cc_ctx_t
*cc
)
758 cc_var_t
*iter
, *next
;
760 for(iter
= cc
->vars
; iter
; iter
= next
) {
768 static BOOL
add_cc_var(cc_ctx_t
*cc
, const WCHAR
*name
, cc_var_t
*v
)
775 new_v
= heap_alloc(sizeof(cc_var_t
) + (len
+1)*sizeof(WCHAR
));
779 memcpy(new_v
, v
, sizeof(*v
));
780 memcpy(new_v
->name
, name
, (len
+1)*sizeof(WCHAR
));
781 new_v
->name_len
= len
;
782 new_v
->next
= cc
->vars
;
787 static cc_var_t
*find_cc_var(cc_ctx_t
*cc
, const WCHAR
*name
, unsigned name_len
)
791 for(iter
= cc
->vars
; iter
; iter
= iter
->next
) {
792 if(iter
->name_len
== name_len
&& !memcmp(iter
->name
, name
, name_len
*sizeof(WCHAR
)))
799 static int init_cc(parser_ctx_t
*ctx
)
804 static const WCHAR _win32W
[] = {'_','w','i','n','3','2',0};
805 static const WCHAR _win64W
[] = {'_','w','i','n','6','4',0};
806 static const WCHAR _x86W
[] = {'_','x','8','6',0};
807 static const WCHAR _amd64W
[] = {'_','a','m','d','6','4',0};
808 static const WCHAR _jscriptW
[] = {'_','j','s','c','r','i','p','t',0};
809 static const WCHAR _jscript_buildW
[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
810 static const WCHAR _jscript_versionW
[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
815 cc
= heap_alloc(sizeof(cc_ctx_t
));
817 return lex_error(ctx
, E_OUTOFMEMORY
);
821 v
.u
.b
= VARIANT_TRUE
;
822 if(!add_cc_var(cc
, _jscriptW
, &v
)
823 || !add_cc_var(cc
, sizeof(void*) == 8 ? _win64W
: _win32W
, &v
)
824 || !add_cc_var(cc
, sizeof(void*) == 8 ? _amd64W
: _x86W
, &v
)) {
826 return lex_error(ctx
, E_OUTOFMEMORY
);
830 v
.u
.n
= JSCRIPT_BUILD_VERSION
;
831 if(!add_cc_var(cc
, _jscript_buildW
, &v
)) {
833 return lex_error(ctx
, E_OUTOFMEMORY
);
836 v
.u
.n
= JSCRIPT_MAJOR_VERSION
+ (DOUBLE
)JSCRIPT_MINOR_VERSION
/10.0;
837 if(!add_cc_var(cc
, _jscript_versionW
, &v
)) {
839 return lex_error(ctx
, E_OUTOFMEMORY
);
842 ctx
->script
->cc
= cc
;
846 static int cc_token(parser_ctx_t
*ctx
, void *lval
)
851 static const WCHAR cc_onW
[] = {'c','c','_','o','n',0};
852 static const WCHAR setW
[] = {'s','e','t',0};
853 static const WCHAR elifW
[] = {'e','l','i','f',0};
854 static const WCHAR endW
[] = {'e','n','d',0};
858 if(!check_keyword(ctx
, cc_onW
, NULL
))
861 if(!check_keyword(ctx
, setW
, NULL
)) {
862 FIXME("@set not implemented\n");
863 return lex_error(ctx
, E_NOTIMPL
);
866 if(!check_keyword(ctx
, ifW
, NULL
)) {
867 FIXME("@if not implemented\n");
868 return lex_error(ctx
, E_NOTIMPL
);
871 if(!check_keyword(ctx
, elifW
, NULL
)) {
872 FIXME("@elif not implemented\n");
873 return lex_error(ctx
, E_NOTIMPL
);
876 if(!check_keyword(ctx
, elseW
, NULL
)) {
877 FIXME("@else not implemented\n");
878 return lex_error(ctx
, E_NOTIMPL
);
881 if(!check_keyword(ctx
, endW
, NULL
)) {
882 FIXME("@end not implemented\n");
883 return lex_error(ctx
, E_NOTIMPL
);
887 return lex_error(ctx
, JS_E_DISABLED_CC
);
889 while(ctx
->ptr
+id_len
< ctx
->end
&& is_identifier_char(ctx
->ptr
[id_len
]))
894 TRACE("var %s\n", debugstr_wn(ctx
->ptr
, id_len
));
896 var
= find_cc_var(ctx
->script
->cc
, ctx
->ptr
, id_len
);
898 if(!var
|| var
->is_num
) {
899 *(literal_t
**)lval
= new_double_literal(ctx
, var
? var
->u
.n
: NAN
);
900 return tNumericLiteral
;
903 *(literal_t
**)lval
= new_boolean_literal(ctx
, var
->u
.b
);
904 return tBooleanLiteral
;
907 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
911 ctx
->nl
= ctx
->ptr
== ctx
->begin
;
914 ret
= next_token(ctx
, lval
);
915 } while(ret
== '@' && !(ret
= cc_token(ctx
, lval
)));
920 literal_t
*parse_regexp(parser_ctx_t
*ctx
)
922 const WCHAR
*re
, *flags_ptr
;
923 BOOL in_class
= FALSE
;
930 while(*--ctx
->ptr
!= '/');
932 /* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
934 while(ctx
->ptr
< ctx
->end
) {
935 if(*ctx
->ptr
== '\\') {
936 if(++ctx
->ptr
== ctx
->end
)
939 if(*ctx
->ptr
== '\n')
953 if(ctx
->ptr
== ctx
->end
|| *ctx
->ptr
!= '/') {
954 WARN("pre-parsing failed\n");
958 re_len
= ctx
->ptr
-re
;
960 flags_ptr
= ++ctx
->ptr
;
961 while(ctx
->ptr
< ctx
->end
&& isalnumW(*ctx
->ptr
))
964 hres
= parse_regexp_flags(flags_ptr
, ctx
->ptr
-flags_ptr
, &flags
);
968 ret
= parser_alloc(ctx
, sizeof(literal_t
));
969 ret
->type
= LT_REGEXP
;
970 ret
->u
.regexp
.str
= re
;
971 ret
->u
.regexp
.str_len
= re_len
;
972 ret
->u
.regexp
.flags
= flags
;