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
26 #include "parser.tab.h"
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
33 static const WCHAR breakW
[] = {'b','r','e','a','k',0};
34 static const WCHAR caseW
[] = {'c','a','s','e',0};
35 static const WCHAR catchW
[] = {'c','a','t','c','h',0};
36 static const WCHAR continueW
[] = {'c','o','n','t','i','n','u','e',0};
37 static const WCHAR defaultW
[] = {'d','e','f','a','u','l','t',0};
38 static const WCHAR deleteW
[] = {'d','e','l','e','t','e',0};
39 static const WCHAR doW
[] = {'d','o',0};
40 static const WCHAR elseW
[] = {'e','l','s','e',0};
41 static const WCHAR falseW
[] = {'f','a','l','s','e',0};
42 static const WCHAR finallyW
[] = {'f','i','n','a','l','l','y',0};
43 static const WCHAR forW
[] = {'f','o','r',0};
44 static const WCHAR functionW
[] = {'f','u','n','c','t','i','o','n',0};
45 static const WCHAR ifW
[] = {'i','f',0};
46 static const WCHAR inW
[] = {'i','n',0};
47 static const WCHAR instanceofW
[] = {'i','n','s','t','a','n','c','e','o','f',0};
48 static const WCHAR newW
[] = {'n','e','w',0};
49 static const WCHAR nullW
[] = {'n','u','l','l',0};
50 static const WCHAR returnW
[] = {'r','e','t','u','r','n',0};
51 static const WCHAR switchW
[] = {'s','w','i','t','c','h',0};
52 static const WCHAR thisW
[] = {'t','h','i','s',0};
53 static const WCHAR throwW
[] = {'t','h','r','o','w',0};
54 static const WCHAR trueW
[] = {'t','r','u','e',0};
55 static const WCHAR tryW
[] = {'t','r','y',0};
56 static const WCHAR typeofW
[] = {'t','y','p','e','o','f',0};
57 static const WCHAR undefinedW
[] = {'u','n','d','e','f','i','n','e','d',0};
58 static const WCHAR varW
[] = {'v','a','r',0};
59 static const WCHAR voidW
[] = {'v','o','i','d',0};
60 static const WCHAR whileW
[] = {'w','h','i','l','e',0};
61 static const WCHAR withW
[] = {'w','i','t','h',0};
70 {continueW
, kCONTINUE
},
78 {functionW
, kFUNCTION
},
81 {instanceofW
, kINSTANCEOF
},
91 {undefinedW
, kUNDEFINED
},
98 static int lex_error(parser_ctx_t
*ctx
, HRESULT hres
)
104 static int check_keyword(parser_ctx_t
*ctx
, const WCHAR
*word
, const WCHAR
**lval
)
106 const WCHAR
*p1
= ctx
->ptr
;
107 const WCHAR
*p2
= word
;
109 while(p1
< ctx
->end
&& *p2
) {
116 if(*p2
|| (p1
< ctx
->end
&& isalnumW(*p1
)))
124 /* ECMA-262 3rd Edition 7.3 */
125 static BOOL
is_endline(WCHAR c
)
127 return c
== '\n' || c
== '\r' || c
== 0x2028 || c
== 0x2029;
130 static BOOL
is_identifier_char(WCHAR c
)
132 return isalnumW(c
) || c
== '$' || c
== '_' || c
== '\\';
135 static int hex_to_int(WCHAR c
)
137 if('0' <= c
&& c
<= '9')
140 if('a' <= c
&& c
<= 'f')
143 if('A' <= c
&& c
<= 'F')
149 static int check_keywords(parser_ctx_t
*ctx
, const WCHAR
**lval
)
151 int min
= 0, max
= sizeof(keywords
)/sizeof(keywords
[0])-1, r
, i
;
156 r
= check_keyword(ctx
, keywords
[i
].word
, lval
);
158 return keywords
[i
].token
;
169 static void skip_spaces(parser_ctx_t
*ctx
)
171 while(ctx
->ptr
< ctx
->end
&& isspaceW(*ctx
->ptr
)) {
172 if(is_endline(*ctx
->ptr
++))
177 static BOOL
skip_comment(parser_ctx_t
*ctx
)
179 if(ctx
->ptr
+1 >= ctx
->end
|| *ctx
->ptr
!= '/')
182 switch(ctx
->ptr
[1]) {
185 while(ctx
->ptr
+1 < ctx
->end
&& (ctx
->ptr
[0] != '*' || ctx
->ptr
[1] != '/'))
188 if(ctx
->ptr
[0] == '*' && ctx
->ptr
[1] == '/') {
191 WARN("unexpected end of file (missing end of comment)\n");
197 while(ctx
->ptr
< ctx
->end
&& !is_endline(*ctx
->ptr
))
207 static BOOL
unescape(WCHAR
*str
)
249 i
= hex_to_int(*++p
);
254 i
= hex_to_int(*++p
);
260 i
= hex_to_int(*++p
);
265 i
= hex_to_int(*++p
);
270 i
= hex_to_int(*++p
);
275 i
= hex_to_int(*++p
);
292 static int parse_identifier(parser_ctx_t
*ctx
, const WCHAR
**ret
)
294 const WCHAR
*ptr
= ctx
->ptr
++;
298 while(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
))
303 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
304 memcpy(wstr
, ptr
, (len
+1)*sizeof(WCHAR
));
307 /* FIXME: unescape */
311 static int parse_string_literal(parser_ctx_t
*ctx
, const WCHAR
**ret
, WCHAR endch
)
313 const WCHAR
*ptr
= ++ctx
->ptr
;
317 while(ctx
->ptr
< ctx
->end
&& *ctx
->ptr
!= endch
) {
318 if(*ctx
->ptr
++ == '\\')
322 if(ctx
->ptr
== ctx
->end
) {
323 WARN("unexpected end of file\n");
324 return lex_error(ctx
, E_FAIL
);
329 *ret
= wstr
= parser_alloc(ctx
, (len
+1)*sizeof(WCHAR
));
330 memcpy(wstr
, ptr
, (len
+1)*sizeof(WCHAR
));
335 if(!unescape(wstr
)) {
336 WARN("unescape failed\n");
337 return lex_error(ctx
, E_FAIL
);
340 return tStringLiteral
;
343 static literal_t
*alloc_int_literal(parser_ctx_t
*ctx
, LONG l
)
345 literal_t
*ret
= parser_alloc(ctx
, sizeof(literal_t
));
353 static int parse_double_literal(parser_ctx_t
*ctx
, LONG int_part
, literal_t
**literal
)
357 if(ctx
->ptr
== ctx
->end
|| !isdigitW(*ctx
->ptr
)) {
358 ERR("No digit after point\n");
363 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
364 d
+= (tmp
/= 10.0)*(*ctx
->ptr
++ - '0');
366 if(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
== 'e' || *ctx
->ptr
== 'E')) {
370 if(ctx
->ptr
< ctx
->end
) {
371 if(*ctx
->ptr
== '+') {
373 }else if(*ctx
->ptr
== '-') {
376 }else if(!isdigitW(*ctx
->ptr
)) {
377 WARN("Expected exponent part\n");
378 return lex_error(ctx
, E_FAIL
);
382 if(ctx
->ptr
== ctx
->end
) {
383 WARN("unexpected end of file\n");
384 return lex_error(ctx
, E_FAIL
);
387 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
388 e
= e
*10 + *ctx
->ptr
++ - '0';
394 *literal
= parser_alloc(ctx
, sizeof(literal_t
));
395 (*literal
)->vt
= VT_R8
;
396 (*literal
)->u
.dval
= d
;
398 return tNumericLiteral
;
401 static int parse_numeric_literal(parser_ctx_t
*ctx
, literal_t
**literal
)
405 l
= *ctx
->ptr
++ - '0';
406 if(ctx
->ptr
== ctx
->end
) {
407 *literal
= alloc_int_literal(ctx
, l
);
408 return tNumericLiteral
;
412 if(*ctx
->ptr
== 'x' || *ctx
->ptr
== 'X') {
413 if(++ctx
->ptr
== ctx
->end
) {
414 ERR("unexpexted end of file\n");
418 while(ctx
->ptr
< ctx
->end
&& (d
= hex_to_int(*ctx
->ptr
)) != -1) {
423 if(ctx
->ptr
< ctx
->end
&& is_identifier_char(*ctx
->ptr
)) {
424 WARN("unexpected identifier char\n");
425 return lex_error(ctx
, E_FAIL
);
428 *literal
= alloc_int_literal(ctx
, l
);
429 return tNumericLiteral
;
432 if(isdigitW(*ctx
->ptr
) || is_identifier_char(*ctx
->ptr
)) {
433 WARN("wrong char after zero\n");
434 return lex_error(ctx
, E_FAIL
);
437 *literal
= alloc_int_literal(ctx
, 0);
440 while(ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
441 l
= l
*10 + *(ctx
->ptr
++)-'0';
443 if(ctx
->ptr
< ctx
->end
) {
444 if(*ctx
->ptr
== '.') {
446 return parse_double_literal(ctx
, l
, literal
);
449 if(is_identifier_char(*ctx
->ptr
)) {
450 WARN("unexpected identifier char\n");
451 return lex_error(ctx
, E_FAIL
);
455 *literal
= alloc_int_literal(ctx
, l
);
456 return tNumericLiteral
;
459 int parser_lex(void *lval
, parser_ctx_t
*ctx
)
467 if(ctx
->ptr
== ctx
->end
)
469 }while(skip_comment(ctx
));
471 if(isalphaW(*ctx
->ptr
)) {
472 ret
= check_keywords(ctx
, lval
);
476 return parse_identifier(ctx
, (const WCHAR
**)lval
);
479 if(isdigitW(*ctx
->ptr
))
480 return parse_numeric_literal(ctx
, lval
);
496 *(const WCHAR
**)lval
= ctx
->ptr
++;
500 if(++ctx
->ptr
< ctx
->end
&& isdigitW(*ctx
->ptr
))
501 return parse_double_literal(ctx
, 0, lval
);
505 if(++ctx
->ptr
== ctx
->end
) {
506 *(int*)lval
= EXPR_LESS
;
513 *(int*)lval
= EXPR_LESSEQ
;
516 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* <<= */
518 *(int*)lval
= EXPR_ASSIGNLSHIFT
;
521 *(int*)lval
= EXPR_LSHIFT
;
524 *(int*)lval
= EXPR_LESS
;
529 if(++ctx
->ptr
== ctx
->end
) { /* > */
530 *(int*)lval
= EXPR_GREATER
;
537 *(int*)lval
= EXPR_GREATEREQ
;
540 if(++ctx
->ptr
< ctx
->end
) {
541 if(*ctx
->ptr
== '=') { /* >>= */
543 *(int*)lval
= EXPR_ASSIGNRSHIFT
;
546 if(*ctx
->ptr
== '>') { /* >>> */
547 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* >>>= */
549 *(int*)lval
= EXPR_ASSIGNRRSHIFT
;
552 *(int*)lval
= EXPR_RRSHIFT
;
556 *(int*)lval
= EXPR_RSHIFT
;
559 *(int*)lval
= EXPR_GREATER
;
565 if(ctx
->ptr
< ctx
->end
) {
572 *(int*)lval
= EXPR_ASSIGNADD
;
580 if(ctx
->ptr
< ctx
->end
) {
587 *(int*)lval
= EXPR_ASSIGNSUB
;
594 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* *= */
596 *(int*)lval
= EXPR_ASSIGNMUL
;
602 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* %= */
604 *(int*)lval
= EXPR_ASSIGNMOD
;
610 if(++ctx
->ptr
< ctx
->end
) {
614 *(int*)lval
= EXPR_ASSIGNAND
;
624 if(++ctx
->ptr
< ctx
->end
) {
628 *(int*)lval
= EXPR_ASSIGNOR
;
638 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* ^= */
640 *(int*)lval
= EXPR_ASSIGNXOR
;
646 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* != */
647 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* !== */
649 *(int*)lval
= EXPR_NOTEQEQ
;
652 *(int*)lval
= EXPR_NOTEQ
;
658 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* == */
659 if(++ctx
->ptr
< ctx
->end
&& *ctx
->ptr
== '=') { /* === */
661 *(int*)lval
= EXPR_EQEQ
;
664 *(int*)lval
= EXPR_EQ
;
670 if(++ctx
->ptr
< ctx
->end
) {
671 if(*ctx
->ptr
== '=') { /* /= */
673 *(int*)lval
= EXPR_ASSIGNDIV
;
681 return parse_string_literal(ctx
, (const WCHAR
**)lval
, *ctx
->ptr
);
685 return parse_identifier(ctx
, lval
);
688 WARN("unexpected char '%c' %d\n", *ctx
->ptr
, *ctx
->ptr
);
692 static void add_object_literal(parser_ctx_t
*ctx
, DispatchEx
*obj
)
694 obj_literal_t
*literal
= parser_alloc(ctx
, sizeof(obj_literal_t
));
697 literal
->next
= ctx
->obj_literals
;
698 ctx
->obj_literals
= literal
;
701 literal_t
*parse_regexp(parser_ctx_t
*ctx
)
703 const WCHAR
*re
, *flags
;
712 while(ctx
->ptr
< ctx
->end
&& (*ctx
->ptr
!= '/' || *(ctx
->ptr
-1) == '\\'))
715 if(ctx
->ptr
== ctx
->end
) {
716 WARN("unexpected end of file\n");
720 re_len
= ctx
->ptr
-re
;
723 while(ctx
->ptr
< ctx
->end
&& isalnumW(*ctx
->ptr
))
726 hres
= create_regexp_str(ctx
->script
, re
, re_len
, flags
, ctx
->ptr
-flags
, ®exp
);
730 add_object_literal(ctx
, regexp
);
732 ret
= parser_alloc(ctx
, sizeof(literal_t
));
733 ret
->vt
= VT_DISPATCH
;
734 ret
->u
.disp
= (IDispatch
*)_IDispatchEx_(regexp
);