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
21 #include "parser.tab.h"
23 #define LONGLONG_MAX (((LONGLONG)0x7fffffff<<32)|0xffffffff)
25 static const WCHAR breakW
[] = {'b','r','e','a','k',0};
26 static const WCHAR caseW
[] = {'c','a','s','e',0};
27 static const WCHAR catchW
[] = {'c','a','t','c','h',0};
28 static const WCHAR continueW
[] = {'c','o','n','t','i','n','u','e',0};
29 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
30 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
31 static const WCHAR doW
[] = {'d','o',0};
32 static const WCHAR elseW
[] = {'e','l','s','e',0};
33 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
34 static const WCHAR finallyW
[] = {'f','i','n','a','l','l','y',0};
35 static const WCHAR forW
[] = {'f','o','r',0};
36 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
37 static const WCHAR ifW
[] = {'i','f',0};
38 static const WCHAR inW
[] = {'i','n',0};
39 static const WCHAR instanceofW
[] = {'i','n','s','t','a','n','c','e','o','f',0};
40 static const WCHAR newW
[] = {'n','e','w',0};
41 static const WCHAR nullW
[] = {'n','u','l','l',0};
42 static const WCHAR returnW
[] = {'r','e','t','u','r','n',0};
43 static const WCHAR switchW
[] = {'s','w','i','t','c','h',0};
44 static const WCHAR thisW
[] = {'t','h','i','s',0};
45 static const WCHAR throwW
[] = {'t','h','r','o','w',0};
46 static const WCHAR trueW
[] = {'t','r','u','e',0};
47 static const WCHAR tryW
[] = {'t','r','y',0};
48 static const WCHAR typeofW
[] = {'t','y','p','e','o','f',0};
49 static const WCHAR varW
[] = {'v','a','r',0};
50 static const WCHAR voidW
[] = {'v','o','i','d',0};
51 static const WCHAR whileW
[] = {'w','h','i','l','e',0};
52 static const WCHAR withW
[] = {'w','i','t','h',0};
59 {breakW
, kBREAK
, TRUE
},
62 {continueW
, kCONTINUE
, TRUE
},
70 {functionW
, kFUNCTION
},
73 {instanceofW
, kINSTANCEOF
},
76 {returnW
, kRETURN
, TRUE
},
89 static int lex_error(parser_ctx_t
*ctx
, HRESULT hres
)
92 ctx
->lexer_error
= TRUE
;
96 /* ECMA-262 3rd Edition 7.6 */
97 static BOOL
is_identifier_char(WCHAR c
)
99 return isalnumW(c
) || c
== '$' || c
== '_' || c
== '\\';
102 static int check_keyword(parser_ctx_t
*ctx
, const WCHAR
*word
, const WCHAR
**lval
)
104 const WCHAR
*p1
= ctx
->ptr
;
105 const WCHAR
*p2
= word
;
107 while(p1
< ctx
->end
&& *p2
) {
114 if(*p2
|| (p1
< ctx
->end
&& is_identifier_char(*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 int hex_to_int(WCHAR c
)
131 if('0' <= c
&& c
<= '9')
134 if('a' <= c
&& c
<= 'f')
137 if('A' <= c
&& c
<= 'F')
143 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
145 int min
= 0, max
= sizeof(keywords
)/sizeof(keywords
[0])-1, r
, i
;
150 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
152 ctx
->implicit_nl_semicolon
= keywords
[i
].no_nl
;
153 return keywords
[i
].token
;
165 static BOOL
skip_html_comment(parser_ctx_t
*ctx
)
167 const WCHAR html_commentW
[] = {'<','!','-','-',0};
169 if(!ctx
->is_html
|| ctx
->ptr
+3 >= ctx
->end
||
170 memcmp(ctx
->ptr
, html_commentW
, sizeof(WCHAR
)*4))
174 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
++));
179 static BOOL
skip_comment(parser_ctx_t
*ctx
)
181 if(ctx
->ptr
+1 >= ctx
->end
)
184 if(*ctx
->ptr
!= '/') {
185 if(*ctx
->ptr
== '@' && ctx
->ptr
+2 < ctx
->end
&& ctx
->ptr
[1] == '*' && ctx
->ptr
[2] == '/') {
193 switch(ctx
->ptr
[1]) {
196 if(ctx
->ptr
+2 < ctx
->end
&& *ctx
->ptr
== '@' && is_identifier_char(ctx
->ptr
[1]))
198 while(ctx
->ptr
+1 < ctx
->end
&& (ctx
->ptr
[0] != '*' || ctx
->ptr
[1] != '/'))
201 if(ctx
->ptr
[0] == '*' && ctx
->ptr
[1] == '/') {
204 WARN("unexpected end of file (missing end of comment)\n");
210 if(ctx
->ptr
+2 < ctx
->end
&& *ctx
->ptr
== '@' && is_identifier_char(ctx
->ptr
[1]))
212 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
))
222 static BOOL
unescape(WCHAR
*str
)
258 i
= hex_to_int(*++p
);
263 i
= hex_to_int(*++p
);
269 i
= hex_to_int(*++p
);
274 i
= hex_to_int(*++p
);
279 i
= hex_to_int(*++p
);
284 i
= hex_to_int(*++p
);
293 c
= c
*8 + (*p
++ - '0');
295 c
= c
*8 + (*p
++ - '0');
311 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
313 const WCHAR
*ptr
= ctx
->ptr
++;
317 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
322 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
323 memcpy(wstr
, ptr
, len
*sizeof(WCHAR
));
326 /* FIXME: unescape */
330 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
, WCHAR endch
)
332 const WCHAR
*ptr
= ++ctx
->ptr
;
336 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= endch
) {
337 if(*ctx
->ptr
++ == '\\')
341 if(ctx
->ptr
== ctx
->end
)
342 return lex_error(ctx
, JS_E_UNTERMINATED_STRING
);
346 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
347 memcpy(wstr
, ptr
, len
*sizeof(WCHAR
));
352 if(!unescape(wstr
)) {
353 WARN("unescape failed\n");
354 return lex_error(ctx
, E_FAIL
);
357 return tStringLiteral
;
360 static literal_t
*new_double_literal(parser_ctx_t
*ctx
, DOUBLE d
)
362 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
364 ret
->type
= LT_DOUBLE
;
369 literal_t
*new_boolean_literal(parser_ctx_t
*ctx
, BOOL bval
)
371 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
379 static int parse_double_literal(parser_ctx_t
*ctx
, LONG int_part
, literal_t
**literal
)
385 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
386 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
387 if(d
>LONGLONG_MAX
/10 || hlp
<0) {
394 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
399 if(*ctx
->ptr
== '.') {
402 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
403 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
404 if(d
>LONGLONG_MAX
/10 || hlp
<0)
410 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
414 if(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')) {
418 if(ctx
->ptr
< ctx
->end
) {
419 if(*ctx
->ptr
== '+') {
421 }else if(*ctx
->ptr
== '-') {
424 }else if(!isdigitW(*ctx
->ptr
)) {
425 WARN("Expected exponent part\n");
426 return lex_error(ctx
, E_FAIL
);
430 if(ctx
->ptr
== ctx
->end
) {
431 WARN("unexpected end of file\n");
432 return lex_error(ctx
, E_FAIL
);
435 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
436 if(e
> INT_MAX
/10 || (e
= e
*10 + *ctx
->ptr
++ - '0')<0)
441 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
442 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
446 *literal
= new_double_literal(ctx
, exp
>=0 ? d
*pow(10, exp
) : d
/pow(10, -exp
));
447 return tNumericLiteral
;
450 static int parse_numeric_literal(parser_ctx_t
*ctx
, literal_t
**literal
)
454 l
= *ctx
->ptr
++ - '0';
456 if(*ctx
->ptr
== 'x' || *ctx
->ptr
== 'X') {
457 if(++ctx
->ptr
== ctx
->end
) {
458 ERR("unexpected end of file\n");
462 while(ctx
->ptr
< ctx
->end
&& (d
= hex_to_int(*ctx
->ptr
)) != -1) {
467 if(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
)) {
468 WARN("unexpected identifier char\n");
469 return lex_error(ctx
, E_FAIL
);
472 *literal
= new_double_literal(ctx
, l
);
473 return tNumericLiteral
;
476 if(is_identifier_char(*ctx
->ptr
)) {
477 WARN("wrong char after zero\n");
478 return lex_error(ctx
, E_FAIL
);
481 if(isdigitW(*ctx
->ptr
)) {
482 FIXME("octal literals not implemented\n");
483 return lex_error(ctx
, E_NOTIMPL
);
487 return parse_double_literal(ctx
, l
, literal
);
490 static int next_token(parser_ctx_t
*ctx
, void *lval
)
493 while(ctx
->ptr
< ctx
->end
&& isspaceW(*ctx
->ptr
)) {
494 if(is_endline(*ctx
->ptr
++))
497 if(ctx
->ptr
== ctx
->end
)
499 }while(skip_comment(ctx
) || skip_html_comment(ctx
));
501 if(ctx
->implicit_nl_semicolon
) {
504 ctx
->implicit_nl_semicolon
= FALSE
;
507 if(isalphaW(*ctx
->ptr
)) {
508 int ret
= check_keywords(ctx
, lval
);
512 return parse_identifier(ctx
, lval
);
515 if(isdigitW(*ctx
->ptr
))
516 return parse_numeric_literal(ctx
, lval
);
532 *(const WCHAR
**)lval
= ctx
->ptr
++;
536 if(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
537 return parse_double_literal(ctx
, 0, lval
);
541 if(++ctx
->ptr
== ctx
->end
) {
542 *(int*)lval
= EXPR_LESS
;
549 *(int*)lval
= EXPR_LESSEQ
;
552 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* <<= */
554 *(int*)lval
= EXPR_ASSIGNLSHIFT
;
557 *(int*)lval
= EXPR_LSHIFT
;
560 *(int*)lval
= EXPR_LESS
;
565 if(++ctx
->ptr
== ctx
->end
) { /* > */
566 *(int*)lval
= EXPR_GREATER
;
573 *(int*)lval
= EXPR_GREATEREQ
;
576 if(++ctx
->ptr
< ctx
->end
) {
577 if(*ctx
->ptr
== '=') { /* >>= */
579 *(int*)lval
= EXPR_ASSIGNRSHIFT
;
582 if(*ctx
->ptr
== '>') { /* >>> */
583 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* >>>= */
585 *(int*)lval
= EXPR_ASSIGNRRSHIFT
;
588 *(int*)lval
= EXPR_RRSHIFT
;
592 *(int*)lval
= EXPR_RSHIFT
;
595 *(int*)lval
= EXPR_GREATER
;
601 if(ctx
->ptr
< ctx
->end
) {
608 *(int*)lval
= EXPR_ASSIGNADD
;
616 if(ctx
->ptr
< ctx
->end
) {
618 case '-': /* -- or --> */
620 if(ctx
->is_html
&& ctx
->nl
&& ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '>') {
627 *(int*)lval
= EXPR_ASSIGNSUB
;
634 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* *= */
636 *(int*)lval
= EXPR_ASSIGNMUL
;
642 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* %= */
644 *(int*)lval
= EXPR_ASSIGNMOD
;
650 if(++ctx
->ptr
< ctx
->end
) {
654 *(int*)lval
= EXPR_ASSIGNAND
;
664 if(++ctx
->ptr
< ctx
->end
) {
668 *(int*)lval
= EXPR_ASSIGNOR
;
678 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* ^= */
680 *(int*)lval
= EXPR_ASSIGNXOR
;
686 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* != */
687 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* !== */
689 *(int*)lval
= EXPR_NOTEQEQ
;
692 *(int*)lval
= EXPR_NOTEQ
;
698 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* == */
699 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* === */
701 *(int*)lval
= EXPR_EQEQ
;
704 *(int*)lval
= EXPR_EQ
;
710 if(++ctx
->ptr
< ctx
->end
) {
711 if(*ctx
->ptr
== '=') { /* /= */
713 *(int*)lval
= EXPR_ASSIGNDIV
;
721 return parse_string_literal(ctx
, lval
, *ctx
->ptr
);
725 return parse_identifier(ctx
, lval
);
731 WARN("unexpected char '%c' %d\n", *ctx
->ptr
, *ctx
->ptr
);
741 struct _cc_var_t
*next
;
746 void release_cc(cc_ctx_t
*cc
)
748 cc_var_t
*iter
, *next
;
750 for(iter
= cc
->vars
; iter
; iter
= next
) {
758 static BOOL
add_cc_var(cc_ctx_t
*cc
, const WCHAR
*name
, cc_var_t
*v
)
765 new_v
= heap_alloc(sizeof(cc_var_t
) + (len
+1)*sizeof(WCHAR
));
769 memcpy(new_v
, v
, sizeof(*v
));
770 memcpy(new_v
->name
, name
, (len
+1)*sizeof(WCHAR
));
771 new_v
->name_len
= len
;
772 new_v
->next
= cc
->vars
;
777 static cc_var_t
*find_cc_var(cc_ctx_t
*cc
, const WCHAR
*name
, unsigned name_len
)
781 for(iter
= cc
->vars
; iter
; iter
= iter
->next
) {
782 if(iter
->name_len
== name_len
&& !memcmp(iter
->name
, name
, name_len
*sizeof(WCHAR
)))
789 static int init_cc(parser_ctx_t
*ctx
)
794 static const WCHAR _win32W
[] = {'_','w','i','n','3','2',0};
795 static const WCHAR _win64W
[] = {'_','w','i','n','6','4',0};
796 static const WCHAR _x86W
[] = {'_','x','8','6',0};
797 static const WCHAR _amd64W
[] = {'_','a','m','d','6','4',0};
798 static const WCHAR _jscriptW
[] = {'_','j','s','c','r','i','p','t',0};
799 static const WCHAR _jscript_buildW
[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
800 static const WCHAR _jscript_versionW
[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
805 cc
= heap_alloc(sizeof(cc_ctx_t
));
807 return lex_error(ctx
, E_OUTOFMEMORY
);
812 if(!add_cc_var(cc
, _jscriptW
, &v
)
813 || !add_cc_var(cc
, sizeof(void*) == 8 ? _win64W
: _win32W
, &v
)
814 || !add_cc_var(cc
, sizeof(void*) == 8 ? _amd64W
: _x86W
, &v
)) {
816 return lex_error(ctx
, E_OUTOFMEMORY
);
820 v
.u
.n
= JSCRIPT_BUILD_VERSION
;
821 if(!add_cc_var(cc
, _jscript_buildW
, &v
)) {
823 return lex_error(ctx
, E_OUTOFMEMORY
);
826 v
.u
.n
= JSCRIPT_MAJOR_VERSION
+ (DOUBLE
)JSCRIPT_MINOR_VERSION
/10.0;
827 if(!add_cc_var(cc
, _jscript_versionW
, &v
)) {
829 return lex_error(ctx
, E_OUTOFMEMORY
);
832 ctx
->script
->cc
= cc
;
836 static int cc_token(parser_ctx_t
*ctx
, void *lval
)
841 static const WCHAR cc_onW
[] = {'c','c','_','o','n',0};
842 static const WCHAR setW
[] = {'s','e','t',0};
843 static const WCHAR elifW
[] = {'e','l','i','f',0};
844 static const WCHAR endW
[] = {'e','n','d',0};
848 if(!check_keyword(ctx
, cc_onW
, NULL
))
851 if(!check_keyword(ctx
, setW
, NULL
)) {
852 FIXME("@set not implemented\n");
853 return lex_error(ctx
, E_NOTIMPL
);
856 if(!check_keyword(ctx
, ifW
, NULL
)) {
857 FIXME("@if not implemented\n");
858 return lex_error(ctx
, E_NOTIMPL
);
861 if(!check_keyword(ctx
, elifW
, NULL
)) {
862 FIXME("@elif not implemented\n");
863 return lex_error(ctx
, E_NOTIMPL
);
866 if(!check_keyword(ctx
, elseW
, NULL
)) {
867 FIXME("@else not implemented\n");
868 return lex_error(ctx
, E_NOTIMPL
);
871 if(!check_keyword(ctx
, endW
, NULL
)) {
872 FIXME("@end not implemented\n");
873 return lex_error(ctx
, E_NOTIMPL
);
877 return lex_error(ctx
, JS_E_DISABLED_CC
);
879 while(ctx
->ptr
+id_len
< ctx
->end
&& is_identifier_char(ctx
->ptr
[id_len
]))
884 TRACE("var %s\n", debugstr_wn(ctx
->ptr
, id_len
));
886 var
= find_cc_var(ctx
->script
->cc
, ctx
->ptr
, id_len
);
888 if(!var
|| var
->is_num
) {
889 *(literal_t
**)lval
= new_double_literal(ctx
, var
? var
->u
.n
: NAN
);
890 return tNumericLiteral
;
893 *(literal_t
**)lval
= new_boolean_literal(ctx
, var
->u
.b
);
894 return tBooleanLiteral
;
897 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
901 ctx
->nl
= ctx
->ptr
== ctx
->begin
;
904 ret
= next_token(ctx
, lval
);
905 } while(ret
== '@' && !(ret
= cc_token(ctx
, lval
)));
910 literal_t
*parse_regexp(parser_ctx_t
*ctx
)
912 const WCHAR
*re
, *flags_ptr
;
913 BOOL in_class
= FALSE
;
920 while(*--ctx
->ptr
!= '/');
922 /* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
924 while(ctx
->ptr
< ctx
->end
) {
925 if(*ctx
->ptr
== '\\') {
926 if(++ctx
->ptr
== ctx
->end
)
929 if(*ctx
->ptr
== '\n')
943 if(ctx
->ptr
== ctx
->end
|| *ctx
->ptr
!= '/') {
944 WARN("pre-parsing failed\n");
948 re_len
= ctx
->ptr
-re
;
950 flags_ptr
= ++ctx
->ptr
;
951 while(ctx
->ptr
< ctx
->end
&& isalnumW(*ctx
->ptr
))
954 hres
= parse_regexp_flags(flags_ptr
, ctx
->ptr
-flags_ptr
, &flags
);
958 ret
= parser_alloc(ctx
, sizeof(literal_t
));
959 ret
->type
= LT_REGEXP
;
960 ret
->u
.regexp
.str
= re
;
961 ret
->u
.regexp
.str_len
= re_len
;
962 ret
->u
.regexp
.flags
= flags
;