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 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 varW
[] = {'v','a','r',0};
61 static const WCHAR voidW
[] = {'v','o','i','d',0};
62 static const WCHAR whileW
[] = {'w','h','i','l','e',0};
63 static const WCHAR withW
[] = {'w','i','t','h',0};
70 {breakW
, kBREAK
, TRUE
},
73 {continueW
, kCONTINUE
, TRUE
},
81 {functionW
, kFUNCTION
},
84 {instanceofW
, kINSTANCEOF
},
87 {returnW
, kRETURN
, TRUE
},
100 static int lex_error(parser_ctx_t
*ctx
, HRESULT 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
)))
134 /* ECMA-262 3rd Edition 7.3 */
135 static BOOL
is_endline(WCHAR c
)
137 return c
== '\n' || c
== '\r' || c
== 0x2028 || c
== 0x2029;
140 static int hex_to_int(WCHAR c
)
142 if('0' <= c
&& c
<= '9')
145 if('a' <= c
&& c
<= 'f')
148 if('A' <= c
&& c
<= 'F')
154 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
156 int min
= 0, max
= sizeof(keywords
)/sizeof(keywords
[0])-1, r
, i
;
161 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
163 ctx
->implicit_nl_semicolon
= keywords
[i
].no_nl
;
164 return keywords
[i
].token
;
176 static BOOL
skip_html_comment(parser_ctx_t
*ctx
)
178 const WCHAR html_commentW
[] = {'<','!','-','-',0};
180 if(!ctx
->is_html
|| ctx
->ptr
+3 >= ctx
->end
||
181 memcmp(ctx
->ptr
, html_commentW
, sizeof(WCHAR
)*4))
185 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
++));
190 static BOOL
skip_comment(parser_ctx_t
*ctx
)
192 if(ctx
->ptr
+1 >= ctx
->end
)
195 if(*ctx
->ptr
!= '/') {
196 if(*ctx
->ptr
== '@' && ctx
->ptr
+2 < ctx
->end
&& ctx
->ptr
[1] == '*' && ctx
->ptr
[2] == '/') {
204 switch(ctx
->ptr
[1]) {
207 if(ctx
->ptr
+2 < ctx
->end
&& *ctx
->ptr
== '@' && is_identifier_char(ctx
->ptr
[1]))
209 while(ctx
->ptr
+1 < ctx
->end
&& (ctx
->ptr
[0] != '*' || ctx
->ptr
[1] != '/'))
212 if(ctx
->ptr
[0] == '*' && ctx
->ptr
[1] == '/') {
215 WARN("unexpected end of file (missing end of comment)\n");
221 if(ctx
->ptr
+2 < ctx
->end
&& *ctx
->ptr
== '@' && is_identifier_char(ctx
->ptr
[1]))
223 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
))
233 static BOOL
unescape(WCHAR
*str
)
269 i
= hex_to_int(*++p
);
274 i
= hex_to_int(*++p
);
280 i
= hex_to_int(*++p
);
285 i
= hex_to_int(*++p
);
290 i
= hex_to_int(*++p
);
295 i
= hex_to_int(*++p
);
304 c
= c
*8 + (*p
++ - '0');
306 c
= c
*8 + (*p
++ - '0');
322 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
324 const WCHAR
*ptr
= ctx
->ptr
++;
328 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
333 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
334 memcpy(wstr
, ptr
, len
*sizeof(WCHAR
));
337 /* FIXME: unescape */
341 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
, WCHAR endch
)
343 const WCHAR
*ptr
= ++ctx
->ptr
;
347 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= endch
) {
348 if(*ctx
->ptr
++ == '\\')
352 if(ctx
->ptr
== ctx
->end
)
353 return lex_error(ctx
, JS_E_UNTERMINATED_STRING
);
357 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
358 memcpy(wstr
, ptr
, len
*sizeof(WCHAR
));
363 if(!unescape(wstr
)) {
364 WARN("unescape failed\n");
365 return lex_error(ctx
, E_FAIL
);
368 return tStringLiteral
;
371 static literal_t
*new_double_literal(parser_ctx_t
*ctx
, DOUBLE d
)
373 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
375 ret
->type
= LT_DOUBLE
;
380 literal_t
*new_boolean_literal(parser_ctx_t
*ctx
, BOOL bval
)
382 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
390 static int parse_double_literal(parser_ctx_t
*ctx
, LONG int_part
, literal_t
**literal
)
396 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
397 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
398 if(d
>MAXLONGLONG
/10 || hlp
<0) {
405 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
410 if(*ctx
->ptr
== '.') {
413 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
414 hlp
= d
*10 + *(ctx
->ptr
++) - '0';
415 if(d
>MAXLONGLONG
/10 || hlp
<0)
421 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
425 if(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')) {
429 if(ctx
->ptr
< ctx
->end
) {
430 if(*ctx
->ptr
== '+') {
432 }else if(*ctx
->ptr
== '-') {
435 }else if(!isdigitW(*ctx
->ptr
)) {
436 WARN("Expected exponent part\n");
437 return lex_error(ctx
, E_FAIL
);
441 if(ctx
->ptr
== ctx
->end
) {
442 WARN("unexpected end of file\n");
443 return lex_error(ctx
, E_FAIL
);
446 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
)) {
447 if(e
> INT_MAX
/10 || (e
= e
*10 + *ctx
->ptr
++ - '0')<0)
452 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
453 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
457 if(is_identifier_char(*ctx
->ptr
)) {
458 WARN("wrong char after zero\n");
459 return lex_error(ctx
, JS_E_MISSING_SEMICOLON
);
462 *literal
= new_double_literal(ctx
, exp
>=0 ? d
*pow(10, exp
) : d
/pow(10, -exp
));
463 return tNumericLiteral
;
466 static int parse_numeric_literal(parser_ctx_t
*ctx
, literal_t
**literal
)
470 l
= *ctx
->ptr
++ - '0';
472 if(*ctx
->ptr
== 'x' || *ctx
->ptr
== 'X') {
473 if(++ctx
->ptr
== ctx
->end
) {
474 ERR("unexpected end of file\n");
478 while(ctx
->ptr
< ctx
->end
&& (d
= hex_to_int(*ctx
->ptr
)) != -1) {
483 if(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
)) {
484 WARN("unexpected identifier char\n");
485 return lex_error(ctx
, JS_E_MISSING_SEMICOLON
);
488 *literal
= new_double_literal(ctx
, l
);
489 return tNumericLiteral
;
492 if(isdigitW(*ctx
->ptr
)) {
497 for(ptr
= ctx
->ptr
; ptr
< ctx
->end
&& isdigitW(*ptr
); ptr
++) {
505 val
= val
*base
+ *ctx
->ptr
-'0';
506 }while(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
));
508 /* FIXME: Do we need it here? */
509 if(ctx
->ptr
< ctx
->end
&& (is_identifier_char(*ctx
->ptr
) || *ctx
->ptr
== '.')) {
510 WARN("wrong char after octal literal: '%c'\n", *ctx
->ptr
);
511 return lex_error(ctx
, JS_E_MISSING_SEMICOLON
);
514 *literal
= new_double_literal(ctx
, val
);
515 return tNumericLiteral
;
518 if(is_identifier_char(*ctx
->ptr
)) {
519 WARN("wrong char after zero\n");
520 return lex_error(ctx
, JS_E_MISSING_SEMICOLON
);
524 return parse_double_literal(ctx
, l
, literal
);
527 static int next_token(parser_ctx_t
*ctx
, void *lval
)
530 while(ctx
->ptr
< ctx
->end
&& isspaceW(*ctx
->ptr
)) {
531 if(is_endline(*ctx
->ptr
++))
534 if(ctx
->ptr
== ctx
->end
)
536 }while(skip_comment(ctx
) || skip_html_comment(ctx
));
538 if(ctx
->implicit_nl_semicolon
) {
541 ctx
->implicit_nl_semicolon
= FALSE
;
544 if(isalphaW(*ctx
->ptr
)) {
545 int ret
= check_keywords(ctx
, lval
);
549 return parse_identifier(ctx
, lval
);
552 if(isdigitW(*ctx
->ptr
))
553 return parse_numeric_literal(ctx
, lval
);
569 *(const WCHAR
**)lval
= ctx
->ptr
++;
573 if(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
574 return parse_double_literal(ctx
, 0, lval
);
578 if(++ctx
->ptr
== ctx
->end
) {
579 *(int*)lval
= EXPR_LESS
;
586 *(int*)lval
= EXPR_LESSEQ
;
589 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* <<= */
591 *(int*)lval
= EXPR_ASSIGNLSHIFT
;
594 *(int*)lval
= EXPR_LSHIFT
;
597 *(int*)lval
= EXPR_LESS
;
602 if(++ctx
->ptr
== ctx
->end
) { /* > */
603 *(int*)lval
= EXPR_GREATER
;
610 *(int*)lval
= EXPR_GREATEREQ
;
613 if(++ctx
->ptr
< ctx
->end
) {
614 if(*ctx
->ptr
== '=') { /* >>= */
616 *(int*)lval
= EXPR_ASSIGNRSHIFT
;
619 if(*ctx
->ptr
== '>') { /* >>> */
620 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* >>>= */
622 *(int*)lval
= EXPR_ASSIGNRRSHIFT
;
625 *(int*)lval
= EXPR_RRSHIFT
;
629 *(int*)lval
= EXPR_RSHIFT
;
632 *(int*)lval
= EXPR_GREATER
;
638 if(ctx
->ptr
< ctx
->end
) {
645 *(int*)lval
= EXPR_ASSIGNADD
;
653 if(ctx
->ptr
< ctx
->end
) {
655 case '-': /* -- or --> */
657 if(ctx
->is_html
&& ctx
->nl
&& ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '>') {
664 *(int*)lval
= EXPR_ASSIGNSUB
;
671 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* *= */
673 *(int*)lval
= EXPR_ASSIGNMUL
;
679 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* %= */
681 *(int*)lval
= EXPR_ASSIGNMOD
;
687 if(++ctx
->ptr
< ctx
->end
) {
691 *(int*)lval
= EXPR_ASSIGNAND
;
701 if(++ctx
->ptr
< ctx
->end
) {
705 *(int*)lval
= EXPR_ASSIGNOR
;
715 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* ^= */
717 *(int*)lval
= EXPR_ASSIGNXOR
;
723 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* != */
724 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* !== */
726 *(int*)lval
= EXPR_NOTEQEQ
;
729 *(int*)lval
= EXPR_NOTEQ
;
735 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* == */
736 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* === */
738 *(int*)lval
= EXPR_EQEQ
;
741 *(int*)lval
= EXPR_EQ
;
747 if(++ctx
->ptr
< ctx
->end
) {
748 if(*ctx
->ptr
== '=') { /* /= */
750 *(int*)lval
= EXPR_ASSIGNDIV
;
758 return parse_string_literal(ctx
, lval
, *ctx
->ptr
);
762 return parse_identifier(ctx
, lval
);
768 WARN("unexpected char '%c' %d\n", *ctx
->ptr
, *ctx
->ptr
);
778 struct _cc_var_t
*next
;
783 void release_cc(cc_ctx_t
*cc
)
785 cc_var_t
*iter
, *next
;
787 for(iter
= cc
->vars
; iter
; iter
= next
) {
795 static BOOL
add_cc_var(cc_ctx_t
*cc
, const WCHAR
*name
, cc_var_t
*v
)
802 new_v
= heap_alloc(sizeof(cc_var_t
) + (len
+1)*sizeof(WCHAR
));
806 memcpy(new_v
, v
, sizeof(*v
));
807 memcpy(new_v
->name
, name
, (len
+1)*sizeof(WCHAR
));
808 new_v
->name_len
= len
;
809 new_v
->next
= cc
->vars
;
814 static cc_var_t
*find_cc_var(cc_ctx_t
*cc
, const WCHAR
*name
, unsigned name_len
)
818 for(iter
= cc
->vars
; iter
; iter
= iter
->next
) {
819 if(iter
->name_len
== name_len
&& !memcmp(iter
->name
, name
, name_len
*sizeof(WCHAR
)))
826 static int init_cc(parser_ctx_t
*ctx
)
831 static const WCHAR _win32W
[] = {'_','w','i','n','3','2',0};
832 static const WCHAR _win64W
[] = {'_','w','i','n','6','4',0};
833 static const WCHAR _x86W
[] = {'_','x','8','6',0};
834 static const WCHAR _amd64W
[] = {'_','a','m','d','6','4',0};
835 static const WCHAR _jscriptW
[] = {'_','j','s','c','r','i','p','t',0};
836 static const WCHAR _jscript_buildW
[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
837 static const WCHAR _jscript_versionW
[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
842 cc
= heap_alloc(sizeof(cc_ctx_t
));
844 return lex_error(ctx
, E_OUTOFMEMORY
);
849 if(!add_cc_var(cc
, _jscriptW
, &v
)
850 || !add_cc_var(cc
, sizeof(void*) == 8 ? _win64W
: _win32W
, &v
)
851 || !add_cc_var(cc
, sizeof(void*) == 8 ? _amd64W
: _x86W
, &v
)) {
853 return lex_error(ctx
, E_OUTOFMEMORY
);
857 v
.u
.n
= JSCRIPT_BUILD_VERSION
;
858 if(!add_cc_var(cc
, _jscript_buildW
, &v
)) {
860 return lex_error(ctx
, E_OUTOFMEMORY
);
863 v
.u
.n
= JSCRIPT_MAJOR_VERSION
+ (DOUBLE
)JSCRIPT_MINOR_VERSION
/10.0;
864 if(!add_cc_var(cc
, _jscript_versionW
, &v
)) {
866 return lex_error(ctx
, E_OUTOFMEMORY
);
869 ctx
->script
->cc
= cc
;
873 static int cc_token(parser_ctx_t
*ctx
, void *lval
)
878 static const WCHAR cc_onW
[] = {'c','c','_','o','n',0};
879 static const WCHAR setW
[] = {'s','e','t',0};
880 static const WCHAR elifW
[] = {'e','l','i','f',0};
881 static const WCHAR endW
[] = {'e','n','d',0};
885 if(!check_keyword(ctx
, cc_onW
, NULL
))
888 if(!check_keyword(ctx
, setW
, NULL
)) {
889 FIXME("@set not implemented\n");
890 return lex_error(ctx
, E_NOTIMPL
);
893 if(!check_keyword(ctx
, ifW
, NULL
)) {
894 FIXME("@if not implemented\n");
895 return lex_error(ctx
, E_NOTIMPL
);
898 if(!check_keyword(ctx
, elifW
, NULL
)) {
899 FIXME("@elif not implemented\n");
900 return lex_error(ctx
, E_NOTIMPL
);
903 if(!check_keyword(ctx
, elseW
, NULL
)) {
904 FIXME("@else not implemented\n");
905 return lex_error(ctx
, E_NOTIMPL
);
908 if(!check_keyword(ctx
, endW
, NULL
)) {
909 FIXME("@end not implemented\n");
910 return lex_error(ctx
, E_NOTIMPL
);
914 return lex_error(ctx
, JS_E_DISABLED_CC
);
916 while(ctx
->ptr
+id_len
< ctx
->end
&& is_identifier_char(ctx
->ptr
[id_len
]))
921 TRACE("var %s\n", debugstr_wn(ctx
->ptr
, id_len
));
923 var
= find_cc_var(ctx
->script
->cc
, ctx
->ptr
, id_len
);
925 if(!var
|| var
->is_num
) {
926 *(literal_t
**)lval
= new_double_literal(ctx
, var
? var
->u
.n
: NAN
);
927 return tNumericLiteral
;
930 *(literal_t
**)lval
= new_boolean_literal(ctx
, var
->u
.b
);
931 return tBooleanLiteral
;
934 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
938 ctx
->nl
= ctx
->ptr
== ctx
->begin
;
941 ret
= next_token(ctx
, lval
);
942 } while(ret
== '@' && !(ret
= cc_token(ctx
, lval
)));
947 literal_t
*parse_regexp(parser_ctx_t
*ctx
)
949 const WCHAR
*re
, *flags_ptr
;
950 BOOL in_class
= FALSE
;
957 while(*--ctx
->ptr
!= '/');
959 /* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
961 while(ctx
->ptr
< ctx
->end
) {
962 if(*ctx
->ptr
== '\\') {
963 if(++ctx
->ptr
== ctx
->end
)
966 if(*ctx
->ptr
== '\n')
980 if(ctx
->ptr
== ctx
->end
|| *ctx
->ptr
!= '/') {
981 WARN("pre-parsing failed\n");
985 re_len
= ctx
->ptr
-re
;
987 flags_ptr
= ++ctx
->ptr
;
988 while(ctx
->ptr
< ctx
->end
&& isalnumW(*ctx
->ptr
))
991 hres
= parse_regexp_flags(flags_ptr
, ctx
->ptr
-flags_ptr
, &flags
);
995 ret
= parser_alloc(ctx
, sizeof(literal_t
));
996 ret
->type
= LT_REGEXP
;
997 ret
->u
.regexp
.str
= re
;
998 ret
->u
.regexp
.str_len
= re_len
;
999 ret
->u
.regexp
.flags
= flags
;