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 static const WCHAR breakW
[] = {'b','r','e','a','k',0};
35 static const WCHAR caseW
[] = {'c','a','s','e',0};
36 static const WCHAR catchW
[] = {'c','a','t','c','h',0};
37 static const WCHAR continueW
[] = {'c','o','n','t','i','n','u','e',0};
38 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
39 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
40 static const WCHAR doW
[] = {'d','o',0};
41 static const WCHAR elseW
[] = {'e','l','s','e',0};
42 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
43 static const WCHAR finallyW
[] = {'f','i','n','a','l','l','y',0};
44 static const WCHAR forW
[] = {'f','o','r',0};
45 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
46 static const WCHAR ifW
[] = {'i','f',0};
47 static const WCHAR inW
[] = {'i','n',0};
48 static const WCHAR instanceofW
[] = {'i','n','s','t','a','n','c','e','o','f',0};
49 static const WCHAR newW
[] = {'n','e','w',0};
50 static const WCHAR nullW
[] = {'n','u','l','l',0};
51 static const WCHAR returnW
[] = {'r','e','t','u','r','n',0};
52 static const WCHAR switchW
[] = {'s','w','i','t','c','h',0};
53 static const WCHAR thisW
[] = {'t','h','i','s',0};
54 static const WCHAR throwW
[] = {'t','h','r','o','w',0};
55 static const WCHAR trueW
[] = {'t','r','u','e',0};
56 static const WCHAR tryW
[] = {'t','r','y',0};
57 static const WCHAR typeofW
[] = {'t','y','p','e','o','f',0};
58 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
59 static const WCHAR varW
[] = {'v','a','r',0};
60 static const WCHAR voidW
[] = {'v','o','i','d',0};
61 static const WCHAR whileW
[] = {'w','h','i','l','e',0};
62 static const WCHAR withW
[] = {'w','i','t','h',0};
71 {continueW
, kCONTINUE
},
79 {functionW
, kFUNCTION
},
82 {instanceofW
, kINSTANCEOF
},
92 {undefinedW
, kUNDEFINED
},
99 static int lex_error(parser_ctx_t
*ctx
, HRESULT hres
)
105 static int check_keyword(parser_ctx_t
*ctx
, const WCHAR
*word
, const WCHAR
**lval
)
107 const WCHAR
*p1
= ctx
->ptr
;
108 const WCHAR
*p2
= word
;
110 while(p1
< ctx
->end
&& *p2
) {
117 if(*p2
|| (p1
< ctx
->end
&& isalnumW(*p1
)))
125 /* ECMA-262 3rd Edition 7.3 */
126 static BOOL
is_endline(WCHAR c
)
128 return c
== '\n' || c
== '\r' || c
== 0x2028 || c
== 0x2029;
131 static BOOL
is_identifier_char(WCHAR c
)
133 return isalnumW(c
) || c
== '$' || c
== '_' || c
== '\\';
136 static int hex_to_int(WCHAR c
)
138 if('0' <= c
&& c
<= '9')
141 if('a' <= c
&& c
<= 'f')
144 if('A' <= c
&& c
<= 'F')
150 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
152 int min
= 0, max
= sizeof(keywords
)/sizeof(keywords
[0])-1, r
, i
;
157 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
159 return keywords
[i
].token
;
170 static void skip_spaces(parser_ctx_t
*ctx
)
172 while(ctx
->ptr
< ctx
->end
&& isspaceW(*ctx
->ptr
)) {
173 if(is_endline(*ctx
->ptr
++))
178 static BOOL
skip_html_comment(parser_ctx_t
*ctx
)
180 const WCHAR html_commentW
[] = {'<','!','-','-',0};
182 if(!ctx
->is_html
|| ctx
->ptr
+3 >= ctx
->end
||
183 memcmp(ctx
->ptr
, html_commentW
, sizeof(WCHAR
)*4))
187 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
++));
192 static BOOL
skip_comment(parser_ctx_t
*ctx
)
194 if(ctx
->ptr
+1 >= ctx
->end
|| *ctx
->ptr
!= '/')
197 switch(ctx
->ptr
[1]) {
200 while(ctx
->ptr
+1 < ctx
->end
&& (ctx
->ptr
[0] != '*' || ctx
->ptr
[1] != '/'))
203 if(ctx
->ptr
[0] == '*' && ctx
->ptr
[1] == '/') {
206 WARN("unexpected end of file (missing end of comment)\n");
212 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
))
222 static BOOL
unescape(WCHAR
*str
)
262 i
= hex_to_int(*++p
);
267 i
= hex_to_int(*++p
);
273 i
= hex_to_int(*++p
);
278 i
= hex_to_int(*++p
);
283 i
= hex_to_int(*++p
);
288 i
= hex_to_int(*++p
);
297 c
= c
*10 + (*p
++ - '0');
313 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
315 const WCHAR
*ptr
= ctx
->ptr
++;
319 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
324 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
325 memcpy(wstr
, ptr
, (len
+1)*sizeof(WCHAR
));
328 /* FIXME: unescape */
332 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
, WCHAR endch
)
334 const WCHAR
*ptr
= ++ctx
->ptr
;
338 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= endch
) {
339 if(*ctx
->ptr
++ == '\\')
343 if(ctx
->ptr
== ctx
->end
) {
344 WARN("unexpected end of file\n");
345 return lex_error(ctx
, E_FAIL
);
350 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
351 memcpy(wstr
, ptr
, (len
+1)*sizeof(WCHAR
));
356 if(!unescape(wstr
)) {
357 WARN("unescape failed\n");
358 return lex_error(ctx
, E_FAIL
);
361 return tStringLiteral
;
364 static literal_t
*alloc_int_literal(parser_ctx_t
*ctx
, LONG l
)
366 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
374 static int parse_double_literal(parser_ctx_t
*ctx
, LONG int_part
, literal_t
**literal
)
378 if(ctx
->ptr
== ctx
->end
|| (!isdigitW(*ctx
->ptr
) &&
379 *ctx
->ptr
!='.' && *ctx
->ptr
!='e' && *ctx
->ptr
!='E')) {
380 ERR("Illegal character\n");
385 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
386 d
= d
*10 + *(ctx
->ptr
++) - '0';
388 if(*ctx
->ptr
== '.') ctx
->ptr
++;
390 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
391 d
+= (tmp
/= 10.0)*(*ctx
->ptr
++ - '0');
393 if(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')) {
397 if(ctx
->ptr
< ctx
->end
) {
398 if(*ctx
->ptr
== '+') {
400 }else if(*ctx
->ptr
== '-') {
403 }else if(!isdigitW(*ctx
->ptr
)) {
404 WARN("Expected exponent part\n");
405 return lex_error(ctx
, E_FAIL
);
409 if(ctx
->ptr
== ctx
->end
) {
410 WARN("unexpected end of file\n");
411 return lex_error(ctx
, E_FAIL
);
414 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
415 e
= e
*10 + *ctx
->ptr
++ - '0';
421 *literal
= parser_alloc(ctx
, sizeof(literal_t
));
422 (*literal
)->vt
= VT_R8
;
423 (*literal
)->u
.dval
= d
;
425 return tNumericLiteral
;
428 static int parse_numeric_literal(parser_ctx_t
*ctx
, literal_t
**literal
)
432 l
= *ctx
->ptr
++ - '0';
433 if(ctx
->ptr
== ctx
->end
) {
434 *literal
= alloc_int_literal(ctx
, l
);
435 return tNumericLiteral
;
439 if(*ctx
->ptr
== 'x' || *ctx
->ptr
== 'X') {
440 if(++ctx
->ptr
== ctx
->end
) {
441 ERR("unexpexted end of file\n");
445 while(ctx
->ptr
< ctx
->end
&& (d
= hex_to_int(*ctx
->ptr
)) != -1) {
450 if(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
)) {
451 WARN("unexpected identifier char\n");
452 return lex_error(ctx
, E_FAIL
);
455 *literal
= alloc_int_literal(ctx
, l
);
456 return tNumericLiteral
;
459 if(isdigitW(*ctx
->ptr
) || is_identifier_char(*ctx
->ptr
)) {
460 WARN("wrong char after zero\n");
461 return lex_error(ctx
, E_FAIL
);
464 *literal
= alloc_int_literal(ctx
, 0);
467 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
469 d
= l
*10 + *(ctx
->ptr
)-'0';
471 /* Check for integer overflow */
472 if (l
> INT_MAX
/10 || d
< 0)
473 return parse_double_literal(ctx
, l
, literal
);
479 if(ctx
->ptr
< ctx
->end
) {
480 if(*ctx
->ptr
== '.' || *ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')
481 return parse_double_literal(ctx
, l
, literal
);
483 if(is_identifier_char(*ctx
->ptr
)) {
484 WARN("unexpected identifier char\n");
485 return lex_error(ctx
, E_FAIL
);
489 *literal
= alloc_int_literal(ctx
, l
);
490 return tNumericLiteral
;
493 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
497 ctx
->nl
= ctx
->ptr
== ctx
->begin
;
501 if(ctx
->ptr
== ctx
->end
)
503 }while(skip_comment(ctx
) || skip_html_comment(ctx
));
505 if(isalphaW(*ctx
->ptr
)) {
506 ret
= check_keywords(ctx
, lval
);
510 return parse_identifier(ctx
, lval
);
513 if(isdigitW(*ctx
->ptr
))
514 return parse_numeric_literal(ctx
, lval
);
530 *(const WCHAR
**)lval
= ctx
->ptr
++;
534 if(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
535 return parse_double_literal(ctx
, 0, lval
);
539 if(++ctx
->ptr
== ctx
->end
) {
540 *(int*)lval
= EXPR_LESS
;
547 *(int*)lval
= EXPR_LESSEQ
;
550 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* <<= */
552 *(int*)lval
= EXPR_ASSIGNLSHIFT
;
555 *(int*)lval
= EXPR_LSHIFT
;
558 *(int*)lval
= EXPR_LESS
;
563 if(++ctx
->ptr
== ctx
->end
) { /* > */
564 *(int*)lval
= EXPR_GREATER
;
571 *(int*)lval
= EXPR_GREATEREQ
;
574 if(++ctx
->ptr
< ctx
->end
) {
575 if(*ctx
->ptr
== '=') { /* >>= */
577 *(int*)lval
= EXPR_ASSIGNRSHIFT
;
580 if(*ctx
->ptr
== '>') { /* >>> */
581 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* >>>= */
583 *(int*)lval
= EXPR_ASSIGNRRSHIFT
;
586 *(int*)lval
= EXPR_RRSHIFT
;
590 *(int*)lval
= EXPR_RSHIFT
;
593 *(int*)lval
= EXPR_GREATER
;
599 if(ctx
->ptr
< ctx
->end
) {
606 *(int*)lval
= EXPR_ASSIGNADD
;
614 if(ctx
->ptr
< ctx
->end
) {
616 case '-': /* -- or --> */
618 if(ctx
->is_html
&& ctx
->nl
&& ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '>') {
625 *(int*)lval
= EXPR_ASSIGNSUB
;
632 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* *= */
634 *(int*)lval
= EXPR_ASSIGNMUL
;
640 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* %= */
642 *(int*)lval
= EXPR_ASSIGNMOD
;
648 if(++ctx
->ptr
< ctx
->end
) {
652 *(int*)lval
= EXPR_ASSIGNAND
;
662 if(++ctx
->ptr
< ctx
->end
) {
666 *(int*)lval
= EXPR_ASSIGNOR
;
676 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* ^= */
678 *(int*)lval
= EXPR_ASSIGNXOR
;
684 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* != */
685 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* !== */
687 *(int*)lval
= EXPR_NOTEQEQ
;
690 *(int*)lval
= EXPR_NOTEQ
;
696 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* == */
697 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* === */
699 *(int*)lval
= EXPR_EQEQ
;
702 *(int*)lval
= EXPR_EQ
;
708 if(++ctx
->ptr
< ctx
->end
) {
709 if(*ctx
->ptr
== '=') { /* /= */
711 *(int*)lval
= EXPR_ASSIGNDIV
;
719 return parse_string_literal(ctx
, lval
, *ctx
->ptr
);
723 return parse_identifier(ctx
, lval
);
726 WARN("unexpected char '%c' %d\n", *ctx
->ptr
, *ctx
->ptr
);
730 static void add_object_literal(parser_ctx_t
*ctx
, DispatchEx
*obj
)
732 obj_literal_t
*literal
= parser_alloc(ctx
, sizeof(obj_literal_t
));
735 literal
->next
= ctx
->obj_literals
;
736 ctx
->obj_literals
= literal
;
739 literal_t
*parse_regexp(parser_ctx_t
*ctx
)
741 const WCHAR
*re
, *flags
;
750 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= '/') {
751 if(*ctx
->ptr
++ == '\\' && ctx
->ptr
< ctx
->end
)
755 if(ctx
->ptr
== ctx
->end
) {
756 WARN("unexpected end of file\n");
760 re_len
= ctx
->ptr
-re
;
763 while(ctx
->ptr
< ctx
->end
&& isalnumW(*ctx
->ptr
))
766 hres
= create_regexp_str(ctx
->script
, re
, re_len
, flags
, ctx
->ptr
-flags
, ®exp
);
770 add_object_literal(ctx
, regexp
);
772 ret
= parser_alloc(ctx
, sizeof(literal_t
));
773 ret
->vt
= VT_DISPATCH
;
774 ret
->u
.disp
= (IDispatch
*)_IDispatchEx_(regexp
);