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 varW[] = {'v','a','r',0};
63 static const WCHAR voidW[] = {'v','o','i','d',0};
64 static const WCHAR whileW[] = {'w','h','i','l','e',0};
65 static const WCHAR withW[] = {'w','i','t','h',0};
72 {breakW, kBREAK, TRUE},
75 {continueW, kCONTINUE, TRUE},
83 {functionW, kFUNCTION},
86 {instanceofW, kINSTANCEOF},
89 {returnW, kRETURN, TRUE},
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 ctx->implicit_nl_semicolon = keywords[i].no_nl;
166 return keywords[i].token;
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)
197 if(*ctx->ptr != '/') {
198 if(*ctx->ptr == '@' && ctx->ptr+2 < ctx->end && ctx->ptr[1] == '*' && ctx->ptr[2] == '/') {
206 switch(ctx->ptr[1]) {
209 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
211 while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
214 if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
217 WARN("unexpected end of file (missing end of comment)\n");
223 if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
225 while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
235 static BOOL unescape(WCHAR *str)
271 i = hex_to_int(*++p);
276 i = hex_to_int(*++p);
282 i = hex_to_int(*++p);
287 i = hex_to_int(*++p);
292 i = hex_to_int(*++p);
297 i = hex_to_int(*++p);
306 c = c*8 + (*p++ - '0');
308 c = c*8 + (*p++ - '0');
324 static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
326 const WCHAR *ptr = ctx->ptr++;
330 while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
335 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
336 memcpy(wstr, ptr, len*sizeof(WCHAR));
339 /* FIXME: unescape */
343 static int parse_string_literal(parser_ctx_t *ctx, const WCHAR **ret, WCHAR endch)
345 const WCHAR *ptr = ++ctx->ptr;
349 while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
350 if(*ctx->ptr++ == '\\')
354 if(ctx->ptr == ctx->end)
355 return lex_error(ctx, JS_E_UNTERMINATED_STRING);
359 *ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
360 memcpy(wstr, ptr, len*sizeof(WCHAR));
365 if(!unescape(wstr)) {
366 WARN("unescape failed\n");
367 return lex_error(ctx, E_FAIL);
370 return tStringLiteral;
373 static literal_t *new_double_literal(parser_ctx_t *ctx, DOUBLE d)
375 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
377 ret->type = LT_DOUBLE;
382 literal_t *new_boolean_literal(parser_ctx_t *ctx, BOOL bval)
384 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
392 static int parse_double_literal(parser_ctx_t *ctx, LONG int_part, literal_t **literal)
398 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
399 hlp = d*10 + *(ctx->ptr++) - '0';
400 if(d>LONGLONG_MAX/10 || hlp<0) {
407 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
412 if(*ctx->ptr == '.') {
415 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
416 hlp = d*10 + *(ctx->ptr++) - '0';
417 if(d>LONGLONG_MAX/10 || hlp<0)
423 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
427 if(ctx->ptr < ctx->end && (*ctx->ptr == 'e' || *ctx->ptr == 'E')) {
431 if(ctx->ptr < ctx->end) {
432 if(*ctx->ptr == '+') {
434 }else if(*ctx->ptr == '-') {
437 }else if(!isdigitW(*ctx->ptr)) {
438 WARN("Expected exponent part\n");
439 return lex_error(ctx, E_FAIL);
443 if(ctx->ptr == ctx->end) {
444 WARN("unexpected end of file\n");
445 return lex_error(ctx, E_FAIL);
448 while(ctx->ptr < ctx->end && isdigitW(*ctx->ptr)) {
449 if(e > INT_MAX/10 || (e = e*10 + *ctx->ptr++ - '0')<0)
454 if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
455 else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
459 *literal = new_double_literal(ctx, exp>=0 ? d*pow(10, exp) : d/pow(10, -exp));
460 return tNumericLiteral;
463 static int parse_numeric_literal(parser_ctx_t *ctx, literal_t **literal)
467 l = *ctx->ptr++ - '0';
469 if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
470 if(++ctx->ptr == ctx->end) {
471 ERR("unexpected end of file\n");
475 while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
480 if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
481 WARN("unexpected identifier char\n");
482 return lex_error(ctx, E_FAIL);
485 *literal = new_double_literal(ctx, l);
486 return tNumericLiteral;
489 if(is_identifier_char(*ctx->ptr)) {
490 WARN("wrong char after zero\n");
491 return lex_error(ctx, E_FAIL);
494 if(isdigitW(*ctx->ptr)) {
495 FIXME("octal literals not implemented\n");
496 return lex_error(ctx, E_NOTIMPL);
500 return parse_double_literal(ctx, l, literal);
503 static int next_token(parser_ctx_t *ctx, void *lval)
506 while(ctx->ptr < ctx->end && isspaceW(*ctx->ptr)) {
507 if(is_endline(*ctx->ptr++))
510 if(ctx->ptr == ctx->end)
512 }while(skip_comment(ctx) || skip_html_comment(ctx));
514 if(ctx->implicit_nl_semicolon) {
517 ctx->implicit_nl_semicolon = FALSE;
520 if(isalphaW(*ctx->ptr)) {
521 int ret = check_keywords(ctx, lval);
525 return parse_identifier(ctx, lval);
528 if(isdigitW(*ctx->ptr))
529 return parse_numeric_literal(ctx, lval);
545 *(const WCHAR**)lval = ctx->ptr++;
549 if(++ctx->ptr < ctx->end && isdigitW(*ctx->ptr))
550 return parse_double_literal(ctx, 0, lval);
554 if(++ctx->ptr == ctx->end) {
555 *(int*)lval = EXPR_LESS;
562 *(int*)lval = EXPR_LESSEQ;
565 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
567 *(int*)lval = EXPR_ASSIGNLSHIFT;
570 *(int*)lval = EXPR_LSHIFT;
573 *(int*)lval = EXPR_LESS;
578 if(++ctx->ptr == ctx->end) { /* > */
579 *(int*)lval = EXPR_GREATER;
586 *(int*)lval = EXPR_GREATEREQ;
589 if(++ctx->ptr < ctx->end) {
590 if(*ctx->ptr == '=') { /* >>= */
592 *(int*)lval = EXPR_ASSIGNRSHIFT;
595 if(*ctx->ptr == '>') { /* >>> */
596 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
598 *(int*)lval = EXPR_ASSIGNRRSHIFT;
601 *(int*)lval = EXPR_RRSHIFT;
605 *(int*)lval = EXPR_RSHIFT;
608 *(int*)lval = EXPR_GREATER;
614 if(ctx->ptr < ctx->end) {
621 *(int*)lval = EXPR_ASSIGNADD;
629 if(ctx->ptr < ctx->end) {
631 case '-': /* -- or --> */
633 if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
640 *(int*)lval = EXPR_ASSIGNSUB;
647 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
649 *(int*)lval = EXPR_ASSIGNMUL;
655 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
657 *(int*)lval = EXPR_ASSIGNMOD;
663 if(++ctx->ptr < ctx->end) {
667 *(int*)lval = EXPR_ASSIGNAND;
677 if(++ctx->ptr < ctx->end) {
681 *(int*)lval = EXPR_ASSIGNOR;
691 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
693 *(int*)lval = EXPR_ASSIGNXOR;
699 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
700 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
702 *(int*)lval = EXPR_NOTEQEQ;
705 *(int*)lval = EXPR_NOTEQ;
711 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
712 if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
714 *(int*)lval = EXPR_EQEQ;
717 *(int*)lval = EXPR_EQ;
723 if(++ctx->ptr < ctx->end) {
724 if(*ctx->ptr == '=') { /* /= */
726 *(int*)lval = EXPR_ASSIGNDIV;
734 return parse_string_literal(ctx, lval, *ctx->ptr);
738 return parse_identifier(ctx, lval);
744 WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
754 struct _cc_var_t *next;
759 void release_cc(cc_ctx_t *cc)
761 cc_var_t *iter, *next;
763 for(iter = cc->vars; iter; iter = next) {
771 static BOOL add_cc_var(cc_ctx_t *cc, const WCHAR *name, cc_var_t *v)
778 new_v = heap_alloc(sizeof(cc_var_t) + (len+1)*sizeof(WCHAR));
782 memcpy(new_v, v, sizeof(*v));
783 memcpy(new_v->name, name, (len+1)*sizeof(WCHAR));
784 new_v->name_len = len;
785 new_v->next = cc->vars;
790 static cc_var_t *find_cc_var(cc_ctx_t *cc, const WCHAR *name, unsigned name_len)
794 for(iter = cc->vars; iter; iter = iter->next) {
795 if(iter->name_len == name_len && !memcmp(iter->name, name, name_len*sizeof(WCHAR)))
802 static int init_cc(parser_ctx_t *ctx)
807 static const WCHAR _win32W[] = {'_','w','i','n','3','2',0};
808 static const WCHAR _win64W[] = {'_','w','i','n','6','4',0};
809 static const WCHAR _x86W[] = {'_','x','8','6',0};
810 static const WCHAR _amd64W[] = {'_','a','m','d','6','4',0};
811 static const WCHAR _jscriptW[] = {'_','j','s','c','r','i','p','t',0};
812 static const WCHAR _jscript_buildW[] = {'_','j','s','c','r','i','p','t','_','b','u','i','l','d',0};
813 static const WCHAR _jscript_versionW[] = {'_','j','s','c','r','i','p','t','_','v','e','r','s','i','o','n',0};
818 cc = heap_alloc(sizeof(cc_ctx_t));
820 return lex_error(ctx, E_OUTOFMEMORY);
825 if(!add_cc_var(cc, _jscriptW, &v)
826 || !add_cc_var(cc, sizeof(void*) == 8 ? _win64W : _win32W, &v)
827 || !add_cc_var(cc, sizeof(void*) == 8 ? _amd64W : _x86W, &v)) {
829 return lex_error(ctx, E_OUTOFMEMORY);
833 v.u.n = JSCRIPT_BUILD_VERSION;
834 if(!add_cc_var(cc, _jscript_buildW, &v)) {
836 return lex_error(ctx, E_OUTOFMEMORY);
839 v.u.n = JSCRIPT_MAJOR_VERSION + (DOUBLE)JSCRIPT_MINOR_VERSION/10.0;
840 if(!add_cc_var(cc, _jscript_versionW, &v)) {
842 return lex_error(ctx, E_OUTOFMEMORY);
845 ctx->script->cc = cc;
849 static int cc_token(parser_ctx_t *ctx, void *lval)
854 static const WCHAR cc_onW[] = {'c','c','_','o','n',0};
855 static const WCHAR setW[] = {'s','e','t',0};
856 static const WCHAR elifW[] = {'e','l','i','f',0};
857 static const WCHAR endW[] = {'e','n','d',0};
861 if(!check_keyword(ctx, cc_onW, NULL))
864 if(!check_keyword(ctx, setW, NULL)) {
865 FIXME("@set not implemented\n");
866 return lex_error(ctx, E_NOTIMPL);
869 if(!check_keyword(ctx, ifW, NULL)) {
870 FIXME("@if not implemented\n");
871 return lex_error(ctx, E_NOTIMPL);
874 if(!check_keyword(ctx, elifW, NULL)) {
875 FIXME("@elif not implemented\n");
876 return lex_error(ctx, E_NOTIMPL);
879 if(!check_keyword(ctx, elseW, NULL)) {
880 FIXME("@else not implemented\n");
881 return lex_error(ctx, E_NOTIMPL);
884 if(!check_keyword(ctx, endW, NULL)) {
885 FIXME("@end not implemented\n");
886 return lex_error(ctx, E_NOTIMPL);
890 return lex_error(ctx, JS_E_DISABLED_CC);
892 while(ctx->ptr+id_len < ctx->end && is_identifier_char(ctx->ptr[id_len]))
897 TRACE("var %s\n", debugstr_wn(ctx->ptr, id_len));
899 var = find_cc_var(ctx->script->cc, ctx->ptr, id_len);
901 if(!var || var->is_num) {
902 *(literal_t**)lval = new_double_literal(ctx, var ? var->u.n : NAN);
903 return tNumericLiteral;
906 *(literal_t**)lval = new_boolean_literal(ctx, var->u.b);
907 return tBooleanLiteral;
910 int parser_lex(void *lval, parser_ctx_t *ctx)
914 ctx->nl = ctx->ptr == ctx->begin;
917 ret = next_token(ctx, lval);
918 } while(ret == '@' && !(ret = cc_token(ctx, lval)));
923 literal_t *parse_regexp(parser_ctx_t *ctx)
925 const WCHAR *re, *flags_ptr;
926 BOOL in_class = FALSE;
933 while(*--ctx->ptr != '/');
935 /* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
937 while(ctx->ptr < ctx->end) {
938 if(*ctx->ptr == '\\') {
939 if(++ctx->ptr == ctx->end)
942 if(*ctx->ptr == '\n')
956 if(ctx->ptr == ctx->end || *ctx->ptr != '/') {
957 WARN("pre-parsing failed\n");
961 re_len = ctx->ptr-re;
963 flags_ptr = ++ctx->ptr;
964 while(ctx->ptr < ctx->end && isalnumW(*ctx->ptr))
967 hres = parse_regexp_flags(flags_ptr, ctx->ptr-flags_ptr, &flags);
971 ret = parser_alloc(ctx, sizeof(literal_t));
972 ret->type = LT_REGEXP;
973 ret->u.regexp.str = re;
974 ret->u.regexp.str_len = re_len;
975 ret->u.regexp.flags = flags;