2008-09-03 00:25:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Jacek Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-05-30 10:17:18 +02:00
|
|
|
|
2009-05-27 01:10:49 +02:00
|
|
|
#include <limits.h>
|
2019-06-06 09:17:49 +02:00
|
|
|
#include <math.h>
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
#include "jscript.h"
|
|
|
|
#include "activscp.h"
|
|
|
|
#include "objsafe.h"
|
|
|
|
#include "engine.h"
|
2014-09-30 16:51:14 +02:00
|
|
|
#include "parser.h"
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
#include "parser.tab.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(jscript);
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const WCHAR *word;
|
|
|
|
int token;
|
2012-09-10 14:44:51 +02:00
|
|
|
BOOL no_nl;
|
2018-11-30 22:44:32 +01:00
|
|
|
unsigned min_version;
|
2008-09-03 00:25:46 +02:00
|
|
|
} keywords[] = {
|
2019-09-16 15:53:34 +02:00
|
|
|
{L"break", kBREAK, TRUE},
|
|
|
|
{L"case", kCASE},
|
|
|
|
{L"catch", kCATCH},
|
|
|
|
{L"continue", kCONTINUE, TRUE},
|
|
|
|
{L"default", kDEFAULT},
|
|
|
|
{L"delete", kDELETE},
|
|
|
|
{L"do", kDO},
|
|
|
|
{L"else", kELSE},
|
|
|
|
{L"false", kFALSE},
|
|
|
|
{L"finally", kFINALLY},
|
|
|
|
{L"for", kFOR},
|
|
|
|
{L"function", kFUNCTION},
|
|
|
|
{L"get", kGET, FALSE, SCRIPTLANGUAGEVERSION_ES5},
|
|
|
|
{L"if", kIF},
|
|
|
|
{L"in", kIN},
|
|
|
|
{L"instanceof", kINSTANCEOF},
|
|
|
|
{L"new", kNEW},
|
|
|
|
{L"null", kNULL},
|
|
|
|
{L"return", kRETURN, TRUE},
|
|
|
|
{L"set", kSET, FALSE, SCRIPTLANGUAGEVERSION_ES5},
|
|
|
|
{L"switch", kSWITCH},
|
|
|
|
{L"this", kTHIS},
|
|
|
|
{L"throw", kTHROW},
|
|
|
|
{L"true", kTRUE},
|
|
|
|
{L"try", kTRY},
|
|
|
|
{L"typeof", kTYPEOF},
|
|
|
|
{L"var", kVAR},
|
|
|
|
{L"void", kVOID},
|
|
|
|
{L"while", kWHILE},
|
|
|
|
{L"with", kWITH}
|
2008-09-03 00:25:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int lex_error(parser_ctx_t *ctx, HRESULT hres)
|
|
|
|
{
|
2010-12-28 15:05:13 +01:00
|
|
|
ctx->hres = hres;
|
2009-08-05 23:27:09 +02:00
|
|
|
ctx->lexer_error = TRUE;
|
2008-09-03 00:25:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-08-23 23:38:06 +02:00
|
|
|
/* ECMA-262 3rd Edition 7.6 */
|
2016-01-27 20:43:41 +01:00
|
|
|
BOOL is_identifier_char(WCHAR c)
|
2009-08-23 23:38:06 +02:00
|
|
|
{
|
2019-06-06 09:17:49 +02:00
|
|
|
return iswalnum(c) || c == '$' || c == '_' || c == '\\';
|
2009-08-23 23:38:06 +02:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:57 +02:00
|
|
|
static BOOL is_identifier_first_char(WCHAR c)
|
|
|
|
{
|
2019-06-06 09:17:49 +02:00
|
|
|
return iswalpha(c) || c == '$' || c == '_' || c == '\\';
|
2014-09-30 16:51:57 +02:00
|
|
|
}
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
static int check_keyword(parser_ctx_t *ctx, const WCHAR *word, const WCHAR **lval)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
|
|
|
const WCHAR *p1 = ctx->ptr;
|
|
|
|
const WCHAR *p2 = word;
|
|
|
|
|
|
|
|
while(p1 < ctx->end && *p2) {
|
|
|
|
if(*p1 != *p2)
|
|
|
|
return *p1 - *p2;
|
|
|
|
p1++;
|
|
|
|
p2++;
|
|
|
|
}
|
|
|
|
|
2009-08-23 23:38:06 +02:00
|
|
|
if(*p2 || (p1 < ctx->end && is_identifier_char(*p1)))
|
2008-09-03 00:25:46 +02:00
|
|
|
return 1;
|
|
|
|
|
2010-12-28 15:40:00 +01:00
|
|
|
if(lval)
|
2018-04-04 21:43:19 +02:00
|
|
|
*lval = word;
|
2008-09-03 00:25:46 +02:00
|
|
|
ctx->ptr = p1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECMA-262 3rd Edition 7.3 */
|
|
|
|
static BOOL is_endline(WCHAR c)
|
|
|
|
{
|
|
|
|
return c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hex_to_int(WCHAR c)
|
|
|
|
{
|
|
|
|
if('0' <= c && c <= '9')
|
|
|
|
return c-'0';
|
|
|
|
|
|
|
|
if('a' <= c && c <= 'f')
|
|
|
|
return c-'a'+10;
|
|
|
|
|
|
|
|
if('A' <= c && c <= 'F')
|
|
|
|
return c-'A'+10;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
static int check_keywords(parser_ctx_t *ctx, const WCHAR **lval)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
2018-07-09 20:59:12 +02:00
|
|
|
int min = 0, max = ARRAY_SIZE(keywords)-1, r, i;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
while(min <= max) {
|
|
|
|
i = (min+max)/2;
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
r = check_keyword(ctx, keywords[i].word, lval);
|
2012-09-10 14:44:51 +02:00
|
|
|
if(!r) {
|
2018-11-30 22:44:32 +01:00
|
|
|
if(ctx->script->version < keywords[i].min_version) {
|
|
|
|
TRACE("ignoring keyword %s in incompatible mode\n",
|
|
|
|
debugstr_w(keywords[i].word));
|
2019-06-06 09:17:49 +02:00
|
|
|
ctx->ptr -= lstrlenW(keywords[i].word);
|
2018-11-30 22:44:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-10 14:44:51 +02:00
|
|
|
ctx->implicit_nl_semicolon = keywords[i].no_nl;
|
2008-09-03 00:25:46 +02:00
|
|
|
return keywords[i].token;
|
2012-09-10 14:44:51 +02:00
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
if(r > 0)
|
|
|
|
min = i+1;
|
|
|
|
else
|
|
|
|
max = i-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-09 23:34:26 +02:00
|
|
|
static BOOL skip_html_comment(parser_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
const WCHAR html_commentW[] = {'<','!','-','-',0};
|
|
|
|
|
|
|
|
if(!ctx->is_html || ctx->ptr+3 >= ctx->end ||
|
|
|
|
memcmp(ctx->ptr, html_commentW, sizeof(WCHAR)*4))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ctx->nl = TRUE;
|
|
|
|
while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr++));
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-09-03 00:25:46 +02:00
|
|
|
static BOOL skip_comment(parser_ctx_t *ctx)
|
|
|
|
{
|
2010-12-28 15:40:00 +01:00
|
|
|
if(ctx->ptr+1 >= ctx->end)
|
2008-09-03 00:25:46 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2010-12-28 15:40:00 +01:00
|
|
|
if(*ctx->ptr != '/') {
|
|
|
|
if(*ctx->ptr == '@' && ctx->ptr+2 < ctx->end && ctx->ptr[1] == '*' && ctx->ptr[2] == '/') {
|
|
|
|
ctx->ptr += 3;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-09-03 00:25:46 +02:00
|
|
|
switch(ctx->ptr[1]) {
|
|
|
|
case '*':
|
|
|
|
ctx->ptr += 2;
|
2010-12-28 15:40:00 +01:00
|
|
|
if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
while(ctx->ptr+1 < ctx->end && (ctx->ptr[0] != '*' || ctx->ptr[1] != '/'))
|
|
|
|
ctx->ptr++;
|
|
|
|
|
|
|
|
if(ctx->ptr[0] == '*' && ctx->ptr[1] == '/') {
|
|
|
|
ctx->ptr += 2;
|
|
|
|
}else {
|
|
|
|
WARN("unexpected end of file (missing end of comment)\n");
|
|
|
|
ctx->ptr = ctx->end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
ctx->ptr += 2;
|
2010-12-28 15:40:00 +01:00
|
|
|
if(ctx->ptr+2 < ctx->end && *ctx->ptr == '@' && is_identifier_char(ctx->ptr[1]))
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
while(ctx->ptr < ctx->end && !is_endline(*ctx->ptr))
|
|
|
|
ctx->ptr++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:44 +02:00
|
|
|
static BOOL skip_spaces(parser_ctx_t *ctx)
|
|
|
|
{
|
2019-06-06 09:17:49 +02:00
|
|
|
while(ctx->ptr < ctx->end && (iswspace(*ctx->ptr) || *ctx->ptr == 0xFEFF /* UTF16 BOM */)) {
|
2014-09-30 16:51:44 +02:00
|
|
|
if(is_endline(*ctx->ptr++))
|
|
|
|
ctx->nl = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->ptr != ctx->end;
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:44:07 +01:00
|
|
|
BOOL unescape(WCHAR *str, size_t *len)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
2019-03-01 17:44:07 +01:00
|
|
|
WCHAR *pd, *p, c, *end = str + *len;
|
2008-09-03 00:25:46 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
pd = p = str;
|
2019-03-01 17:44:07 +01:00
|
|
|
while(p < end) {
|
2008-09-03 00:25:46 +02:00
|
|
|
if(*p != '\\') {
|
|
|
|
*pd++ = *p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:44:07 +01:00
|
|
|
if(++p == end)
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
switch(*p) {
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
|
|
|
case '\\':
|
|
|
|
c = *p;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
c = '\b';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
c = '\f';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 'x':
|
2019-03-01 17:44:07 +01:00
|
|
|
if(p + 2 >= end)
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
i = hex_to_int(*++p);
|
|
|
|
if(i == -1)
|
|
|
|
return FALSE;
|
2008-10-07 19:02:08 +02:00
|
|
|
c = i << 4;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
i = hex_to_int(*++p);
|
|
|
|
if(i == -1)
|
|
|
|
return FALSE;
|
|
|
|
c += i;
|
|
|
|
break;
|
|
|
|
case 'u':
|
2019-03-01 17:44:07 +01:00
|
|
|
if(p + 4 >= end)
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
i = hex_to_int(*++p);
|
|
|
|
if(i == -1)
|
|
|
|
return FALSE;
|
2008-10-07 19:02:08 +02:00
|
|
|
c = i << 12;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
i = hex_to_int(*++p);
|
|
|
|
if(i == -1)
|
|
|
|
return FALSE;
|
2008-10-07 19:02:08 +02:00
|
|
|
c += i << 8;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
i = hex_to_int(*++p);
|
|
|
|
if(i == -1)
|
|
|
|
return FALSE;
|
2009-10-07 22:11:48 +02:00
|
|
|
c += i << 4;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
i = hex_to_int(*++p);
|
|
|
|
if(i == -1)
|
|
|
|
return FALSE;
|
|
|
|
c += i;
|
|
|
|
break;
|
|
|
|
default:
|
2019-11-08 21:26:08 +01:00
|
|
|
if(is_digit(*p)) {
|
2008-10-07 19:02:08 +02:00
|
|
|
c = *p++ - '0';
|
2019-11-08 21:26:08 +01:00
|
|
|
if(p < end && is_digit(*p)) {
|
2009-10-07 22:11:48 +02:00
|
|
|
c = c*8 + (*p++ - '0');
|
2019-11-08 21:26:08 +01:00
|
|
|
if(p < end && is_digit(*p))
|
2009-10-07 22:11:48 +02:00
|
|
|
c = c*8 + (*p++ - '0');
|
|
|
|
}
|
|
|
|
p--;
|
2008-10-07 19:02:08 +02:00
|
|
|
}
|
2009-10-07 22:11:48 +02:00
|
|
|
else
|
|
|
|
c = *p;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*pd++ = c;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:44:07 +01:00
|
|
|
*len = pd - str;
|
2008-09-03 00:25:46 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_identifier(parser_ctx_t *ctx, const WCHAR **ret)
|
|
|
|
{
|
|
|
|
const WCHAR *ptr = ctx->ptr++;
|
|
|
|
WCHAR *wstr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
while(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr))
|
|
|
|
ctx->ptr++;
|
|
|
|
|
|
|
|
len = ctx->ptr-ptr;
|
|
|
|
|
|
|
|
*ret = wstr = parser_alloc(ctx, (len+1)*sizeof(WCHAR));
|
2011-08-05 17:23:25 +02:00
|
|
|
memcpy(wstr, ptr, len*sizeof(WCHAR));
|
2008-09-03 00:25:46 +02:00
|
|
|
wstr[len] = 0;
|
|
|
|
|
|
|
|
/* FIXME: unescape */
|
|
|
|
return tIdentifier;
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:44:18 +01:00
|
|
|
static int parse_string_literal(parser_ctx_t *ctx, jsstr_t **ret, WCHAR endch)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
2019-03-01 17:44:18 +01:00
|
|
|
const WCHAR *ptr = ++ctx->ptr, *ret_str = ptr;
|
|
|
|
BOOL needs_unescape = FALSE;
|
|
|
|
WCHAR *unescape_str;
|
2019-03-01 17:44:07 +01:00
|
|
|
size_t len;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
while(ctx->ptr < ctx->end && *ctx->ptr != endch) {
|
2019-03-01 17:44:18 +01:00
|
|
|
if(*ctx->ptr++ == '\\') {
|
2008-09-03 00:25:46 +02:00
|
|
|
ctx->ptr++;
|
2019-03-01 17:44:18 +01:00
|
|
|
needs_unescape = TRUE;
|
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2009-08-05 23:27:09 +02:00
|
|
|
if(ctx->ptr == ctx->end)
|
2010-12-28 15:05:13 +01:00
|
|
|
return lex_error(ctx, JS_E_UNTERMINATED_STRING);
|
2008-09-03 00:25:46 +02:00
|
|
|
|
2019-03-01 17:44:18 +01:00
|
|
|
len = ctx->ptr - ptr;
|
2008-09-03 00:25:46 +02:00
|
|
|
ctx->ptr++;
|
|
|
|
|
2019-03-01 17:44:18 +01:00
|
|
|
if(needs_unescape) {
|
|
|
|
ret_str = unescape_str = parser_alloc(ctx, len * sizeof(WCHAR));
|
|
|
|
if(!unescape_str)
|
|
|
|
return lex_error(ctx, E_OUTOFMEMORY);
|
|
|
|
memcpy(unescape_str, ptr, len * sizeof(WCHAR));
|
|
|
|
if(!unescape(unescape_str, &len)) {
|
|
|
|
WARN("unescape failed\n");
|
|
|
|
return lex_error(ctx, E_FAIL);
|
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 17:44:18 +01:00
|
|
|
if(!(*ret = compiler_alloc_string_len(ctx->compiler, ret_str, len)))
|
|
|
|
return lex_error(ctx, E_OUTOFMEMORY);
|
|
|
|
|
|
|
|
/* FIXME: leaking string */
|
2008-09-03 00:25:46 +02:00
|
|
|
return tStringLiteral;
|
|
|
|
}
|
|
|
|
|
2010-12-28 15:05:50 +01:00
|
|
|
static literal_t *new_double_literal(parser_ctx_t *ctx, DOUBLE d)
|
|
|
|
{
|
|
|
|
literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
|
|
|
|
|
|
|
|
ret->type = LT_DOUBLE;
|
|
|
|
ret->u.dval = d;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:20:37 +02:00
|
|
|
literal_t *new_boolean_literal(parser_ctx_t *ctx, BOOL bval)
|
2010-12-28 15:05:36 +01:00
|
|
|
{
|
|
|
|
literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
|
|
|
|
|
|
|
|
ret->type = LT_BOOL;
|
|
|
|
ret->u.bval = bval;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:43:41 +01:00
|
|
|
HRESULT parse_decimal(const WCHAR **iter, const WCHAR *end, double *ret)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
2016-01-27 20:42:52 +01:00
|
|
|
const WCHAR *ptr = *iter;
|
|
|
|
LONGLONG d = 0, hlp;
|
2009-06-03 23:17:47 +02:00
|
|
|
int exp = 0;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
while(ptr < end && is_digit(*ptr)) {
|
2016-01-27 20:42:52 +01:00
|
|
|
hlp = d*10 + *(ptr++) - '0';
|
2013-10-14 15:14:16 +02:00
|
|
|
if(d>MAXLONGLONG/10 || hlp<0) {
|
2009-06-03 23:17:47 +02:00
|
|
|
exp++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
d = hlp;
|
|
|
|
}
|
2019-11-08 21:26:08 +01:00
|
|
|
while(ptr < end && is_digit(*ptr)) {
|
2009-06-03 23:17:47 +02:00
|
|
|
exp++;
|
2016-01-27 20:42:52 +01:00
|
|
|
ptr++;
|
2009-06-03 23:17:47 +02:00
|
|
|
}
|
2009-05-27 01:10:49 +02:00
|
|
|
|
2016-01-27 20:42:52 +01:00
|
|
|
if(*ptr == '.') {
|
|
|
|
ptr++;
|
2009-05-27 01:10:49 +02:00
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
while(ptr < end && is_digit(*ptr)) {
|
2016-01-27 20:42:52 +01:00
|
|
|
hlp = d*10 + *(ptr++) - '0';
|
2013-10-14 15:14:16 +02:00
|
|
|
if(d>MAXLONGLONG/10 || hlp<0)
|
2012-06-20 11:18:22 +02:00
|
|
|
break;
|
2009-06-03 23:17:47 +02:00
|
|
|
|
2012-06-20 11:18:22 +02:00
|
|
|
d = hlp;
|
|
|
|
exp--;
|
|
|
|
}
|
2019-11-08 21:26:08 +01:00
|
|
|
while(ptr < end && is_digit(*ptr))
|
2016-01-27 20:42:52 +01:00
|
|
|
ptr++;
|
2009-06-03 23:17:47 +02:00
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
|
2016-01-27 20:42:52 +01:00
|
|
|
if(ptr < end && (*ptr == 'e' || *ptr == 'E')) {
|
2008-09-03 00:25:46 +02:00
|
|
|
int sign = 1, e = 0;
|
|
|
|
|
2016-01-27 20:42:52 +01:00
|
|
|
if(++ptr < end) {
|
|
|
|
if(*ptr == '+') {
|
|
|
|
ptr++;
|
|
|
|
}else if(*ptr == '-') {
|
2008-09-03 00:25:46 +02:00
|
|
|
sign = -1;
|
2016-01-27 20:42:52 +01:00
|
|
|
ptr++;
|
2019-11-08 21:26:08 +01:00
|
|
|
}else if(!is_digit(*ptr)) {
|
2008-09-03 00:25:46 +02:00
|
|
|
WARN("Expected exponent part\n");
|
2016-01-27 20:42:52 +01:00
|
|
|
return E_FAIL;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:42:52 +01:00
|
|
|
if(ptr == end) {
|
2008-09-03 00:25:46 +02:00
|
|
|
WARN("unexpected end of file\n");
|
2016-01-27 20:42:52 +01:00
|
|
|
return E_FAIL;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
while(ptr < end && is_digit(*ptr)) {
|
2016-01-27 20:42:52 +01:00
|
|
|
if(e > INT_MAX/10 || (e = e*10 + *ptr++ - '0')<0)
|
2009-06-03 23:17:47 +02:00
|
|
|
e = INT_MAX;
|
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
e *= sign;
|
|
|
|
|
2009-06-03 23:17:47 +02:00
|
|
|
if(exp<0 && e<0 && e+exp>0) exp = INT_MIN;
|
|
|
|
else if(exp>0 && e>0 && e+exp<0) exp = INT_MAX;
|
|
|
|
else exp += e;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2016-01-27 20:42:52 +01:00
|
|
|
if(is_identifier_char(*ptr)) {
|
2013-11-06 17:18:38 +01:00
|
|
|
WARN("wrong char after zero\n");
|
2016-01-27 20:42:52 +01:00
|
|
|
return JS_E_MISSING_SEMICOLON;
|
2013-11-06 17:18:38 +01:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:24 +02:00
|
|
|
*ret = exp>=0 ? d*pow(10, exp) : d/pow(10, -exp);
|
2016-01-27 20:42:52 +01:00
|
|
|
*iter = ptr;
|
|
|
|
return S_OK;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:24 +02:00
|
|
|
static BOOL parse_numeric_literal(parser_ctx_t *ctx, double *ret)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
2016-01-27 20:42:52 +01:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
if(*ctx->ptr == '0') {
|
|
|
|
ctx->ptr++;
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
if(*ctx->ptr == 'x' || *ctx->ptr == 'X') {
|
2018-04-11 19:36:11 +02:00
|
|
|
double r = 0;
|
|
|
|
int d;
|
2008-09-03 00:25:46 +02:00
|
|
|
if(++ctx->ptr == ctx->end) {
|
2012-04-17 22:09:00 +02:00
|
|
|
ERR("unexpected end of file\n");
|
2014-09-30 16:51:24 +02:00
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while(ctx->ptr < ctx->end && (d = hex_to_int(*ctx->ptr)) != -1) {
|
2018-04-11 19:36:11 +02:00
|
|
|
r = r*16 + d;
|
2008-09-03 00:25:46 +02:00
|
|
|
ctx->ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr)) {
|
|
|
|
WARN("unexpected identifier char\n");
|
2014-09-30 16:51:24 +02:00
|
|
|
lex_error(ctx, JS_E_MISSING_SEMICOLON);
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 19:36:11 +02:00
|
|
|
*ret = r;
|
2014-09-30 16:51:24 +02:00
|
|
|
return TRUE;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
if(is_digit(*ctx->ptr)) {
|
2013-11-06 17:18:28 +01:00
|
|
|
unsigned base = 8;
|
|
|
|
const WCHAR *ptr;
|
|
|
|
double val = 0;
|
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
for(ptr = ctx->ptr; ptr < ctx->end && is_digit(*ptr); ptr++) {
|
2013-11-06 17:18:28 +01:00
|
|
|
if(*ptr > '7') {
|
|
|
|
base = 10;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
val = val*base + *ctx->ptr-'0';
|
2019-11-08 21:26:08 +01:00
|
|
|
}while(++ctx->ptr < ctx->end && is_digit(*ctx->ptr));
|
2013-11-06 17:18:28 +01:00
|
|
|
|
|
|
|
/* FIXME: Do we need it here? */
|
|
|
|
if(ctx->ptr < ctx->end && (is_identifier_char(*ctx->ptr) || *ctx->ptr == '.')) {
|
|
|
|
WARN("wrong char after octal literal: '%c'\n", *ctx->ptr);
|
2014-09-30 16:51:24 +02:00
|
|
|
lex_error(ctx, JS_E_MISSING_SEMICOLON);
|
|
|
|
return FALSE;
|
2013-11-06 17:18:28 +01:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:24 +02:00
|
|
|
*ret = val;
|
|
|
|
return TRUE;
|
2013-11-06 17:18:28 +01:00
|
|
|
}
|
|
|
|
|
2012-06-20 11:18:22 +02:00
|
|
|
if(is_identifier_char(*ctx->ptr)) {
|
2008-09-03 00:25:46 +02:00
|
|
|
WARN("wrong char after zero\n");
|
2014-09-30 16:51:24 +02:00
|
|
|
lex_error(ctx, JS_E_MISSING_SEMICOLON);
|
|
|
|
return FALSE;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:42:52 +01:00
|
|
|
hres = parse_decimal(&ctx->ptr, ctx->end, ret);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
lex_error(ctx, hres);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2010-12-28 15:40:00 +01:00
|
|
|
static int next_token(parser_ctx_t *ctx, void *lval)
|
2008-09-03 00:25:46 +02:00
|
|
|
{
|
|
|
|
do {
|
2014-09-30 16:51:44 +02:00
|
|
|
if(!skip_spaces(ctx))
|
2010-12-28 15:05:25 +01:00
|
|
|
return tEOF;
|
2009-04-09 23:34:26 +02:00
|
|
|
}while(skip_comment(ctx) || skip_html_comment(ctx));
|
2008-09-03 00:25:46 +02:00
|
|
|
|
2012-09-10 14:44:51 +02:00
|
|
|
if(ctx->implicit_nl_semicolon) {
|
|
|
|
if(ctx->nl)
|
|
|
|
return ';';
|
|
|
|
ctx->implicit_nl_semicolon = FALSE;
|
|
|
|
}
|
|
|
|
|
2019-06-06 09:17:49 +02:00
|
|
|
if(iswalpha(*ctx->ptr)) {
|
2010-12-28 15:40:00 +01:00
|
|
|
int ret = check_keywords(ctx, lval);
|
2008-09-03 00:25:46 +02:00
|
|
|
if(ret)
|
|
|
|
return ret;
|
|
|
|
|
2009-02-02 11:01:02 +01:00
|
|
|
return parse_identifier(ctx, lval);
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
if(is_digit(*ctx->ptr)) {
|
2014-09-30 16:51:24 +02:00
|
|
|
double n;
|
|
|
|
|
|
|
|
if(!parse_numeric_literal(ctx, &n))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*(literal_t**)lval = new_double_literal(ctx, n);
|
|
|
|
return tNumericLiteral;
|
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
switch(*ctx->ptr) {
|
|
|
|
case '{':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '[':
|
|
|
|
case ']':
|
|
|
|
case ';':
|
|
|
|
case ',':
|
|
|
|
case '~':
|
|
|
|
case '?':
|
|
|
|
return *ctx->ptr++;
|
|
|
|
|
2008-09-21 15:47:47 +02:00
|
|
|
case '}':
|
|
|
|
*(const WCHAR**)lval = ctx->ptr++;
|
|
|
|
return '}';
|
|
|
|
|
2008-09-03 00:25:46 +02:00
|
|
|
case '.':
|
2019-11-08 21:26:08 +01:00
|
|
|
if(ctx->ptr+1 < ctx->end && is_digit(ctx->ptr[1])) {
|
2014-09-30 16:51:24 +02:00
|
|
|
double n;
|
2016-01-27 20:42:52 +01:00
|
|
|
HRESULT hres;
|
|
|
|
hres = parse_decimal(&ctx->ptr, ctx->end, &n);
|
|
|
|
if(FAILED(hres)) {
|
|
|
|
lex_error(ctx, hres);
|
2014-09-30 16:51:24 +02:00
|
|
|
return -1;
|
2016-01-27 20:42:52 +01:00
|
|
|
}
|
2014-09-30 16:51:24 +02:00
|
|
|
*(literal_t**)lval = new_double_literal(ctx, n);
|
|
|
|
return tNumericLiteral;
|
|
|
|
}
|
2016-04-28 11:12:35 +02:00
|
|
|
ctx->ptr++;
|
2008-09-03 00:25:46 +02:00
|
|
|
return '.';
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
if(++ctx->ptr == ctx->end) {
|
|
|
|
*(int*)lval = EXPR_LESS;
|
|
|
|
return tRelOper;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(*ctx->ptr) {
|
|
|
|
case '=': /* <= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_LESSEQ;
|
|
|
|
return tRelOper;
|
|
|
|
case '<': /* << */
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* <<= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNLSHIFT;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
*(int*)lval = EXPR_LSHIFT;
|
|
|
|
return tShiftOper;
|
|
|
|
default: /* < */
|
|
|
|
*(int*)lval = EXPR_LESS;
|
|
|
|
return tRelOper;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
if(++ctx->ptr == ctx->end) { /* > */
|
|
|
|
*(int*)lval = EXPR_GREATER;
|
|
|
|
return tRelOper;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(*ctx->ptr) {
|
|
|
|
case '=': /* >= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_GREATEREQ;
|
|
|
|
return tRelOper;
|
|
|
|
case '>': /* >> */
|
|
|
|
if(++ctx->ptr < ctx->end) {
|
|
|
|
if(*ctx->ptr == '=') { /* >>= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNRSHIFT;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
if(*ctx->ptr == '>') { /* >>> */
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* >>>= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNRRSHIFT;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
*(int*)lval = EXPR_RRSHIFT;
|
|
|
|
return tRelOper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*(int*)lval = EXPR_RSHIFT;
|
|
|
|
return tShiftOper;
|
|
|
|
default:
|
|
|
|
*(int*)lval = EXPR_GREATER;
|
|
|
|
return tRelOper;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
ctx->ptr++;
|
|
|
|
if(ctx->ptr < ctx->end) {
|
|
|
|
switch(*ctx->ptr) {
|
|
|
|
case '+': /* ++ */
|
|
|
|
ctx->ptr++;
|
|
|
|
return tINC;
|
|
|
|
case '=': /* += */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNADD;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '+';
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
ctx->ptr++;
|
|
|
|
if(ctx->ptr < ctx->end) {
|
|
|
|
switch(*ctx->ptr) {
|
2009-04-09 23:34:26 +02:00
|
|
|
case '-': /* -- or --> */
|
2008-09-03 00:25:46 +02:00
|
|
|
ctx->ptr++;
|
2009-04-09 23:34:26 +02:00
|
|
|
if(ctx->is_html && ctx->nl && ctx->ptr < ctx->end && *ctx->ptr == '>') {
|
|
|
|
ctx->ptr++;
|
|
|
|
return tHTMLCOMMENT;
|
|
|
|
}
|
2008-09-03 00:25:46 +02:00
|
|
|
return tDEC;
|
|
|
|
case '=': /* -= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNSUB;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '-';
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* *= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNMUL;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
return '*';
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* %= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNMOD;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
return '%';
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
if(++ctx->ptr < ctx->end) {
|
|
|
|
switch(*ctx->ptr) {
|
|
|
|
case '=': /* &= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNAND;
|
|
|
|
return tAssignOper;
|
|
|
|
case '&': /* && */
|
|
|
|
ctx->ptr++;
|
|
|
|
return tANDAND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '&';
|
|
|
|
|
|
|
|
case '|':
|
|
|
|
if(++ctx->ptr < ctx->end) {
|
|
|
|
switch(*ctx->ptr) {
|
|
|
|
case '=': /* |= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNOR;
|
|
|
|
return tAssignOper;
|
|
|
|
case '|': /* || */
|
|
|
|
ctx->ptr++;
|
|
|
|
return tOROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '|';
|
|
|
|
|
|
|
|
case '^':
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* ^= */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_ASSIGNXOR;
|
|
|
|
return tAssignOper;
|
|
|
|
}
|
|
|
|
return '^';
|
|
|
|
|
|
|
|
case '!':
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* != */
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* !== */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_NOTEQEQ;
|
|
|
|
return tEqOper;
|
|
|
|
}
|
|
|
|
*(int*)lval = EXPR_NOTEQ;
|
|
|
|
return tEqOper;
|
|
|
|
}
|
|
|
|
return '!';
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* == */
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == '=') { /* === */
|
|
|
|
ctx->ptr++;
|
|
|
|
*(int*)lval = EXPR_EQEQ;
|
|
|
|
return tEqOper;
|
|
|
|
}
|
|
|
|
*(int*)lval = EXPR_EQ;
|
|
|
|
return tEqOper;
|
|
|
|
}
|
|
|
|
return '=';
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
if(++ctx->ptr < ctx->end) {
|
|
|
|
if(*ctx->ptr == '=') { /* /= */
|
|
|
|
ctx->ptr++;
|
2008-09-16 20:47:27 +02:00
|
|
|
*(int*)lval = EXPR_ASSIGNDIV;
|
2009-08-27 01:20:37 +02:00
|
|
|
return kDIVEQ;
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return '/';
|
|
|
|
|
2015-06-02 14:45:10 +02:00
|
|
|
case ':':
|
|
|
|
if(++ctx->ptr < ctx->end && *ctx->ptr == ':') {
|
|
|
|
ctx->ptr++;
|
|
|
|
return kDCOL;
|
|
|
|
}
|
|
|
|
return ':';
|
|
|
|
|
2008-09-03 00:25:46 +02:00
|
|
|
case '\"':
|
|
|
|
case '\'':
|
2009-02-02 11:01:02 +01:00
|
|
|
return parse_string_literal(ctx, lval, *ctx->ptr);
|
2008-09-03 00:25:46 +02:00
|
|
|
|
|
|
|
case '_':
|
|
|
|
case '$':
|
|
|
|
return parse_identifier(ctx, lval);
|
2010-12-28 15:40:00 +01:00
|
|
|
|
|
|
|
case '@':
|
|
|
|
return '@';
|
2008-09-03 00:25:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WARN("unexpected char '%c' %d\n", *ctx->ptr, *ctx->ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-15 20:38:18 +02:00
|
|
|
|
2010-12-28 15:40:00 +01:00
|
|
|
struct _cc_var_t {
|
2014-09-30 16:51:36 +02:00
|
|
|
ccval_t val;
|
2010-12-28 15:40:00 +01:00
|
|
|
struct _cc_var_t *next;
|
|
|
|
unsigned name_len;
|
|
|
|
WCHAR name[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
void release_cc(cc_ctx_t *cc)
|
|
|
|
{
|
|
|
|
cc_var_t *iter, *next;
|
|
|
|
|
|
|
|
for(iter = cc->vars; iter; iter = next) {
|
|
|
|
next = iter->next;
|
|
|
|
heap_free(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
heap_free(cc);
|
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:36 +02:00
|
|
|
static BOOL new_cc_var(cc_ctx_t *cc, const WCHAR *name, int len, ccval_t v)
|
2010-12-28 15:40:00 +01:00
|
|
|
{
|
|
|
|
cc_var_t *new_v;
|
|
|
|
|
2014-09-30 16:51:36 +02:00
|
|
|
if(len == -1)
|
2019-06-06 09:17:49 +02:00
|
|
|
len = lstrlenW(name);
|
2010-12-28 15:40:00 +01:00
|
|
|
|
|
|
|
new_v = heap_alloc(sizeof(cc_var_t) + (len+1)*sizeof(WCHAR));
|
|
|
|
if(!new_v)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-09-30 16:51:36 +02:00
|
|
|
new_v->val = v;
|
2010-12-28 15:40:00 +01:00
|
|
|
memcpy(new_v->name, name, (len+1)*sizeof(WCHAR));
|
|
|
|
new_v->name_len = len;
|
|
|
|
new_v->next = cc->vars;
|
|
|
|
cc->vars = new_v;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cc_var_t *find_cc_var(cc_ctx_t *cc, const WCHAR *name, unsigned name_len)
|
|
|
|
{
|
|
|
|
cc_var_t *iter;
|
|
|
|
|
|
|
|
for(iter = cc->vars; iter; iter = iter->next) {
|
|
|
|
if(iter->name_len == name_len && !memcmp(iter->name, name, name_len*sizeof(WCHAR)))
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-01 10:45:30 +02:00
|
|
|
static BOOL init_cc(parser_ctx_t *ctx)
|
2010-12-28 15:40:00 +01:00
|
|
|
{
|
|
|
|
cc_ctx_t *cc;
|
|
|
|
|
|
|
|
if(ctx->script->cc)
|
2014-10-01 10:45:30 +02:00
|
|
|
return TRUE;
|
2010-12-28 15:40:00 +01:00
|
|
|
|
|
|
|
cc = heap_alloc(sizeof(cc_ctx_t));
|
2014-10-01 10:45:30 +02:00
|
|
|
if(!cc) {
|
|
|
|
lex_error(ctx, E_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-12-28 15:40:00 +01:00
|
|
|
|
|
|
|
cc->vars = NULL;
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!new_cc_var(cc, L"_jscript", -1, ccval_bool(TRUE))
|
|
|
|
|| !new_cc_var(cc, sizeof(void*) == 8 ? L"_win64" : L"_win32", -1, ccval_bool(TRUE))
|
|
|
|
|| !new_cc_var(cc, sizeof(void*) == 8 ? L"_amd64" : L"_x86", -1, ccval_bool(TRUE))
|
|
|
|
|| !new_cc_var(cc, L"_jscript_version", -1, ccval_num(JSCRIPT_MAJOR_VERSION + (DOUBLE)JSCRIPT_MINOR_VERSION/10.0))
|
|
|
|
|| !new_cc_var(cc, L"_jscript_build", -1, ccval_num(JSCRIPT_BUILD_VERSION))) {
|
2010-12-28 15:40:00 +01:00
|
|
|
release_cc(cc);
|
2014-10-01 10:45:30 +02:00
|
|
|
lex_error(ctx, E_OUTOFMEMORY);
|
|
|
|
return FALSE;
|
2010-12-28 15:40:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->script->cc = cc;
|
2014-10-01 10:45:30 +02:00
|
|
|
return TRUE;
|
2010-12-28 15:40:00 +01:00
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:57 +02:00
|
|
|
static BOOL parse_cc_identifier(parser_ctx_t *ctx, const WCHAR **ret, unsigned *ret_len)
|
|
|
|
{
|
|
|
|
if(*ctx->ptr != '@') {
|
|
|
|
lex_error(ctx, JS_E_EXPECTED_AT);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_identifier_first_char(*++ctx->ptr)) {
|
|
|
|
lex_error(ctx, JS_E_EXPECTED_IDENTIFIER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = ctx->ptr;
|
|
|
|
while(++ctx->ptr < ctx->end && is_identifier_char(*ctx->ptr));
|
|
|
|
*ret_len = ctx->ptr - *ret;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int try_parse_ccval(parser_ctx_t *ctx, ccval_t *r)
|
|
|
|
{
|
|
|
|
if(!skip_spaces(ctx))
|
|
|
|
return -1;
|
|
|
|
|
2019-11-08 21:26:08 +01:00
|
|
|
if(is_digit(*ctx->ptr)) {
|
2014-09-30 16:51:57 +02:00
|
|
|
double n;
|
|
|
|
|
|
|
|
if(!parse_numeric_literal(ctx, &n))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*r = ccval_num(n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*ctx->ptr == '@') {
|
|
|
|
const WCHAR *ident;
|
|
|
|
unsigned ident_len;
|
|
|
|
cc_var_t *cc_var;
|
|
|
|
|
|
|
|
if(!parse_cc_identifier(ctx, &ident, &ident_len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
cc_var = find_cc_var(ctx->script->cc, ident, ident_len);
|
|
|
|
*r = cc_var ? cc_var->val : ccval_num(NAN);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"true", NULL)) {
|
2014-09-30 16:51:57 +02:00
|
|
|
*r = ccval_bool(TRUE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"false", NULL)) {
|
2014-09-30 16:51:57 +02:00
|
|
|
*r = ccval_bool(FALSE);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-01 10:45:12 +02:00
|
|
|
static int skip_code(parser_ctx_t *ctx, BOOL exec_else)
|
|
|
|
{
|
|
|
|
int if_depth = 1;
|
|
|
|
const WCHAR *ptr;
|
|
|
|
|
|
|
|
while(1) {
|
2019-06-06 09:17:49 +02:00
|
|
|
ptr = wcschr(ctx->ptr, '@');
|
2014-10-01 10:45:12 +02:00
|
|
|
if(!ptr) {
|
|
|
|
WARN("No @end\n");
|
|
|
|
return lex_error(ctx, JS_E_EXPECTED_CCEND);
|
|
|
|
}
|
|
|
|
ctx->ptr = ptr+1;
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"end", NULL)) {
|
2014-10-01 10:45:12 +02:00
|
|
|
if(--if_depth)
|
|
|
|
continue;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(exec_else && !check_keyword(ctx, L"elif", NULL)) {
|
2014-10-01 10:45:12 +02:00
|
|
|
if(if_depth > 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!skip_spaces(ctx) || *ctx->ptr != '(')
|
|
|
|
return lex_error(ctx, JS_E_MISSING_LBRACKET);
|
|
|
|
|
|
|
|
if(!parse_cc_expr(ctx))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(!get_ccbool(ctx->ccval))
|
|
|
|
continue; /* skip block of code */
|
|
|
|
|
|
|
|
/* continue parsing */
|
|
|
|
ctx->cc_if_depth++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(exec_else && !check_keyword(ctx, L"else", NULL)) {
|
2014-10-01 10:45:12 +02:00
|
|
|
if(if_depth > 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* parse else block */
|
|
|
|
ctx->cc_if_depth++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"if", NULL)) {
|
2014-10-01 10:45:12 +02:00
|
|
|
if_depth++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-28 15:40:00 +01:00
|
|
|
static int cc_token(parser_ctx_t *ctx, void *lval)
|
|
|
|
{
|
|
|
|
unsigned id_len = 0;
|
|
|
|
cc_var_t *var;
|
|
|
|
|
|
|
|
ctx->ptr++;
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"cc_on", NULL))
|
2014-10-01 10:45:30 +02:00
|
|
|
return init_cc(ctx) ? 0 : -1;
|
2010-12-28 15:40:00 +01:00
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"set", NULL)) {
|
2014-09-30 16:51:57 +02:00
|
|
|
const WCHAR *ident;
|
|
|
|
unsigned ident_len;
|
|
|
|
cc_var_t *var;
|
|
|
|
|
2014-10-01 10:45:30 +02:00
|
|
|
if(!init_cc(ctx))
|
|
|
|
return -1;
|
|
|
|
|
2014-09-30 16:51:57 +02:00
|
|
|
if(!skip_spaces(ctx))
|
|
|
|
return lex_error(ctx, JS_E_EXPECTED_AT);
|
|
|
|
|
|
|
|
if(!parse_cc_identifier(ctx, &ident, &ident_len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(!skip_spaces(ctx) || *ctx->ptr != '=')
|
|
|
|
return lex_error(ctx, JS_E_EXPECTED_ASSIGN);
|
|
|
|
ctx->ptr++;
|
|
|
|
|
|
|
|
if(!parse_cc_expr(ctx)) {
|
|
|
|
WARN("parsing CC expression failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
var = find_cc_var(ctx->script->cc, ident, ident_len);
|
|
|
|
if(var) {
|
|
|
|
var->val = ctx->ccval;
|
|
|
|
}else {
|
|
|
|
if(!new_cc_var(ctx->script->cc, ident, ident_len, ctx->ccval))
|
|
|
|
return lex_error(ctx, E_OUTOFMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-12-28 15:40:00 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"if", NULL)) {
|
2014-10-01 10:45:37 +02:00
|
|
|
if(!init_cc(ctx))
|
|
|
|
return -1;
|
|
|
|
|
2014-10-01 10:45:12 +02:00
|
|
|
if(!skip_spaces(ctx) || *ctx->ptr != '(')
|
|
|
|
return lex_error(ctx, JS_E_MISSING_LBRACKET);
|
2010-12-28 15:40:00 +01:00
|
|
|
|
2014-10-01 10:45:12 +02:00
|
|
|
if(!parse_cc_expr(ctx))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(get_ccbool(ctx->ccval)) {
|
|
|
|
/* continue parsing block inside if */
|
|
|
|
ctx->cc_if_depth++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return skip_code(ctx, TRUE);
|
2010-12-28 15:40:00 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"elif", NULL) || !check_keyword(ctx, L"else", NULL)) {
|
2014-10-01 10:45:12 +02:00
|
|
|
if(!ctx->cc_if_depth)
|
|
|
|
return lex_error(ctx, JS_E_SYNTAX);
|
|
|
|
|
|
|
|
return skip_code(ctx, FALSE);
|
2010-12-28 15:40:00 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 15:53:34 +02:00
|
|
|
if(!check_keyword(ctx, L"end", NULL)) {
|
2014-10-01 10:45:12 +02:00
|
|
|
if(!ctx->cc_if_depth)
|
|
|
|
return lex_error(ctx, JS_E_SYNTAX);
|
|
|
|
|
|
|
|
ctx->cc_if_depth--;
|
|
|
|
return 0;
|
2010-12-28 15:40:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!ctx->script->cc)
|
|
|
|
return lex_error(ctx, JS_E_DISABLED_CC);
|
|
|
|
|
|
|
|
while(ctx->ptr+id_len < ctx->end && is_identifier_char(ctx->ptr[id_len]))
|
|
|
|
id_len++;
|
|
|
|
if(!id_len)
|
|
|
|
return '@';
|
|
|
|
|
|
|
|
TRACE("var %s\n", debugstr_wn(ctx->ptr, id_len));
|
|
|
|
|
|
|
|
var = find_cc_var(ctx->script->cc, ctx->ptr, id_len);
|
|
|
|
ctx->ptr += id_len;
|
2014-09-30 16:51:36 +02:00
|
|
|
if(!var || var->val.is_num) {
|
|
|
|
*(literal_t**)lval = new_double_literal(ctx, var ? var->val.u.n : NAN);
|
2010-12-28 15:40:00 +01:00
|
|
|
return tNumericLiteral;
|
|
|
|
}
|
|
|
|
|
2014-09-30 16:51:36 +02:00
|
|
|
*(literal_t**)lval = new_boolean_literal(ctx, var->val.u.b);
|
2010-12-28 15:40:00 +01:00
|
|
|
return tBooleanLiteral;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parser_lex(void *lval, parser_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ctx->nl = ctx->ptr == ctx->begin;
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = next_token(ctx, lval);
|
|
|
|
} while(ret == '@' && !(ret = cc_token(ctx, lval)));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:38:18 +02:00
|
|
|
literal_t *parse_regexp(parser_ctx_t *ctx)
|
|
|
|
{
|
2009-10-14 14:42:30 +02:00
|
|
|
const WCHAR *re, *flags_ptr;
|
2012-04-20 11:08:08 +02:00
|
|
|
BOOL in_class = FALSE;
|
2009-10-14 14:42:30 +02:00
|
|
|
DWORD re_len, flags;
|
2008-09-15 20:38:18 +02:00
|
|
|
literal_t *ret;
|
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
2012-04-02 13:46:11 +02:00
|
|
|
while(*--ctx->ptr != '/');
|
2009-08-27 01:20:37 +02:00
|
|
|
|
2012-04-20 11:08:08 +02:00
|
|
|
/* Simple regexp pre-parser; '/' if used in char class does not terminate regexp literal */
|
2009-08-27 01:20:37 +02:00
|
|
|
re = ++ctx->ptr;
|
2012-04-20 11:08:08 +02:00
|
|
|
while(ctx->ptr < ctx->end) {
|
|
|
|
if(*ctx->ptr == '\\') {
|
|
|
|
if(++ctx->ptr == ctx->end)
|
|
|
|
break;
|
|
|
|
}else if(in_class) {
|
|
|
|
if(*ctx->ptr == '\n')
|
|
|
|
break;
|
|
|
|
if(*ctx->ptr == ']')
|
|
|
|
in_class = FALSE;
|
|
|
|
}else {
|
|
|
|
if(*ctx->ptr == '/')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(*ctx->ptr == '[')
|
|
|
|
in_class = TRUE;
|
|
|
|
}
|
|
|
|
ctx->ptr++;
|
2008-10-02 16:23:15 +02:00
|
|
|
}
|
2008-09-15 20:38:18 +02:00
|
|
|
|
2012-04-20 11:08:08 +02:00
|
|
|
if(ctx->ptr == ctx->end || *ctx->ptr != '/') {
|
|
|
|
WARN("pre-parsing failed\n");
|
2008-09-15 20:38:18 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
re_len = ctx->ptr-re;
|
|
|
|
|
2009-10-14 14:42:30 +02:00
|
|
|
flags_ptr = ++ctx->ptr;
|
2019-06-06 09:17:49 +02:00
|
|
|
while(ctx->ptr < ctx->end && iswalnum(*ctx->ptr))
|
2008-09-15 20:38:18 +02:00
|
|
|
ctx->ptr++;
|
|
|
|
|
2009-10-14 14:42:30 +02:00
|
|
|
hres = parse_regexp_flags(flags_ptr, ctx->ptr-flags_ptr, &flags);
|
|
|
|
if(FAILED(hres))
|
|
|
|
return NULL;
|
|
|
|
|
2008-09-15 20:38:18 +02:00
|
|
|
ret = parser_alloc(ctx, sizeof(literal_t));
|
2009-10-14 14:43:01 +02:00
|
|
|
ret->type = LT_REGEXP;
|
2019-03-01 17:44:31 +01:00
|
|
|
ret->u.regexp.str = compiler_alloc_string_len(ctx->compiler, re, re_len);
|
2009-10-14 14:43:01 +02:00
|
|
|
ret->u.regexp.flags = flags;
|
2008-09-15 20:38:18 +02:00
|
|
|
return ret;
|
|
|
|
}
|