parser.y 51.5 KB
Newer Older
Jacek Caban's avatar
Jacek Caban committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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
 */

%{

#include "jscript.h"
#include "engine.h"

#define YYLEX_PARAM ctx
#define YYPARSE_PARAM ctx

static int parser_error(const char*);
28
static void set_error(parser_ctx_t*,UINT);
29
static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
Jacek Caban's avatar
Jacek Caban committed
30 31
static BOOL allow_auto_semicolon(parser_ctx_t*);
static void program_parsed(parser_ctx_t*,source_elements_t*);
32
static source_elements_t *function_body_parsed(parser_ctx_t*,source_elements_t*);
Jacek Caban's avatar
Jacek Caban committed
33

34
typedef struct _statement_list_t {
Jacek Caban's avatar
Jacek Caban committed
35 36 37 38 39 40 41
    statement_t *head;
    statement_t *tail;
} statement_list_t;

static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
static literal_t *new_null_literal(parser_ctx_t*);

42
typedef struct _property_list_t {
Jacek Caban's avatar
Jacek Caban committed
43 44 45 46 47 48 49
    prop_val_t *head;
    prop_val_t *tail;
} property_list_t;

static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);

50
typedef struct _element_list_t {
Jacek Caban's avatar
Jacek Caban committed
51 52 53 54 55 56 57
    array_element_t *head;
    array_element_t *tail;
} element_list_t;

static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);

58
typedef struct _argument_list_t {
Jacek Caban's avatar
Jacek Caban committed
59 60 61 62 63 64 65
    argument_t *head;
    argument_t *tail;
} argument_list_t;

static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);

66
typedef struct _case_list_t {
Jacek Caban's avatar
Jacek Caban committed
67 68 69 70 71 72 73 74 75 76
    case_clausule_t *head;
    case_clausule_t *tail;
} case_list_t;

static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);

77
typedef struct _variable_list_t {
Jacek Caban's avatar
Jacek Caban committed
78 79 80 81 82 83 84 85
    variable_declaration_t *head;
    variable_declaration_t *tail;
} variable_list_t;

static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);

86
static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
Jacek Caban's avatar
Jacek Caban committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
        expression_t*,statement_t*);
static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);

struct statement_list_t {
   statement_t *head;
   statement_t *tail;
};

109 110
static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
Jacek Caban's avatar
Jacek Caban committed
111

112
typedef struct _parameter_list_t {
Jacek Caban's avatar
Jacek Caban committed
113 114 115 116 117 118 119
    parameter_t *head;
    parameter_t *tail;
} parameter_list_t;

static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);

120 121 122 123 124 125
static void push_func(parser_ctx_t*);
static inline void pop_func(parser_ctx_t *ctx)
{
    ctx->func_stack = ctx->func_stack->next;
}

126
static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
127 128
static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
        source_elements_t*,const WCHAR*,DWORD);
Jacek Caban's avatar
Jacek Caban committed
129 130 131 132
static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
133
static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
Jacek Caban's avatar
Jacek Caban committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);

static source_elements_t *new_source_elements(parser_ctx_t*);
static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);

%}

%pure_parser
%start Program

%union {
    int                     ival;
150
    const WCHAR             *srcptr;
Jacek Caban's avatar
Jacek Caban committed
151 152
    LPCWSTR                 wstr;
    literal_t               *literal;
153
    struct _argument_list_t *argument_list;
Jacek Caban's avatar
Jacek Caban committed
154
    case_clausule_t         *case_clausule;
155
    struct _case_list_t     *case_list;
Jacek Caban's avatar
Jacek Caban committed
156
    catch_block_t           *catch_block;
157
    struct _element_list_t  *element_list;
Jacek Caban's avatar
Jacek Caban committed
158 159
    expression_t            *expr;
    const WCHAR            *identifier;
160 161
    struct _parameter_list_t *parameter_list;
    struct _property_list_t *property_list;
Jacek Caban's avatar
Jacek Caban committed
162 163
    source_elements_t       *source_elements;
    statement_t             *statement;
164 165
    struct _statement_list_t *statement_list;
    struct _variable_list_t *variable_list;
Jacek Caban's avatar
Jacek Caban committed
166 167 168 169
    variable_declaration_t  *variable_declaration;
}

/* keywords */
170
%token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
171
%token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
172
%token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
Jacek Caban's avatar
Jacek Caban committed
173

174 175
%token <srcptr> kFUNCTION '}'

Jacek Caban's avatar
Jacek Caban committed
176 177 178
/* tokens */
%token <identifier> tIdentifier
%token <ival> tAssignOper tEqOper tShiftOper tRelOper
179
%token <literal> tNumericLiteral tBooleanLiteral
Jacek Caban's avatar
Jacek Caban committed
180
%token <wstr> tStringLiteral
181
%token tEOF
Jacek Caban's avatar
Jacek Caban committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

%type <source_elements> SourceElements
%type <source_elements> FunctionBody
%type <statement> Statement
%type <statement> Block
%type <statement> VariableStatement
%type <statement> EmptyStatement
%type <statement> ExpressionStatement
%type <statement> IfStatement
%type <statement> IterationStatement
%type <statement> ContinueStatement
%type <statement> BreakStatement
%type <statement> ReturnStatement
%type <statement> WithStatement
%type <statement> LabelledStatement
%type <statement> SwitchStatement
%type <statement> ThrowStatement
%type <statement> TryStatement
%type <statement> Finally
%type <statement_list> StatementList StatementList_opt
%type <parameter_list> FormalParameterList FormalParameterList_opt
203
%type <expr> Expression Expression_opt Expression_err
Jacek Caban's avatar
Jacek Caban committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
%type <expr> ExpressionNoIn ExpressionNoIn_opt
%type <expr> FunctionExpression
%type <expr> AssignmentExpression AssignmentExpressionNoIn
%type <expr> ConditionalExpression ConditionalExpressionNoIn
%type <expr> LeftHandSideExpression
%type <expr> LogicalORExpression LogicalORExpressionNoIn
%type <expr> LogicalANDExpression LogicalANDExpressionNoIn
%type <expr> BitwiseORExpression BitwiseORExpressionNoIn
%type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
%type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
%type <expr> EqualityExpression EqualityExpressionNoIn
%type <expr> RelationalExpression RelationalExpressionNoIn
%type <expr> ShiftExpression
%type <expr> AdditiveExpression
%type <expr> MultiplicativeExpression
%type <expr> Initialiser_opt Initialiser
%type <expr> InitialiserNoIn_opt InitialiserNoIn
%type <expr> UnaryExpression
%type <expr> PostfixExpression
%type <expr> NewExpression
%type <expr> CallExpression
%type <expr> MemberExpression
%type <expr> PrimaryExpression
%type <identifier> Identifier_opt
%type <variable_list> VariableDeclarationList
%type <variable_list> VariableDeclarationListNoIn
%type <variable_declaration> VariableDeclaration
%type <variable_declaration> VariableDeclarationNoIn
%type <case_list> CaseClausules CaseClausules_opt
%type <case_clausule> CaseClausule DefaultClausule CaseBlock
%type <catch_block> Catch
%type <argument_list> Arguments
%type <argument_list> ArgumentList
%type <literal> Literal
%type <expr> ArrayLiteral
%type <expr> ObjectLiteral
%type <ival> Elision Elision_opt
%type <element_list> ElementList
%type <property_list> PropertyNameAndValueList
%type <literal> PropertyName
%type <literal> BooleanLiteral
245
%type <srcptr> KFunction
246
%type <ival> AssignOper
Jacek Caban's avatar
Jacek Caban committed
247

248 249 250
%nonassoc LOWER_THAN_ELSE
%nonassoc kELSE

Jacek Caban's avatar
Jacek Caban committed
251 252 253 254
%%

/* ECMA-262 3rd Edition    14 */
Program
255
       : SourceElements HtmlComment tEOF
256 257 258 259 260
                                { program_parsed(ctx, $1); }

HtmlComment
        : tHTMLCOMMENT          {}
        | /* empty */           {}
Jacek Caban's avatar
Jacek Caban committed
261 262 263 264 265 266 267 268 269

/* ECMA-262 3rd Edition    14 */
SourceElements
        : /* empty */           { $$ = new_source_elements(ctx); }
        | SourceElements Statement
                                { $$ = source_elements_add_statement($1, $2); }

/* ECMA-262 3rd Edition    13 */
FunctionExpression
270
        : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
271
                                { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
Jacek Caban's avatar
Jacek Caban committed
272

273 274 275
KFunction
        : kFUNCTION             { push_func(ctx); $$ = $1; }

Jacek Caban's avatar
Jacek Caban committed
276 277
/* ECMA-262 3rd Edition    13 */
FunctionBody
278
        : SourceElements        { $$ = function_body_parsed(ctx, $1); }
Jacek Caban's avatar
Jacek Caban committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

/* ECMA-262 3rd Edition    13 */
FormalParameterList
        : tIdentifier           { $$ = new_parameter_list(ctx, $1); }
        | FormalParameterList ',' tIdentifier
                                { $$ = parameter_list_add(ctx, $1, $3); }

/* ECMA-262 3rd Edition    13 */
FormalParameterList_opt
        : /* empty */           { $$ = NULL; }
        | FormalParameterList   { $$ = $1; }

/* ECMA-262 3rd Edition    12 */
Statement
        : Block                 { $$ = $1; }
        | VariableStatement     { $$ = $1; }
        | EmptyStatement        { $$ = $1; }
296
        | FunctionExpression    { $$ = new_statement(ctx, STAT_EMPTY, 0); }
Jacek Caban's avatar
Jacek Caban committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        | ExpressionStatement   { $$ = $1; }
        | IfStatement           { $$ = $1; }
        | IterationStatement    { $$ = $1; }
        | ContinueStatement     { $$ = $1; }
        | BreakStatement        { $$ = $1; }
        | ReturnStatement       { $$ = $1; }
        | WithStatement         { $$ = $1; }
        | LabelledStatement     { $$ = $1; }
        | SwitchStatement       { $$ = $1; }
        | ThrowStatement        { $$ = $1; }
        | TryStatement          { $$ = $1; }

/* ECMA-262 3rd Edition    12.2 */
StatementList
        : Statement             { $$ = new_statement_list(ctx, $1); }
        | StatementList Statement
                                { $$ = statement_list_add($1, $2); }

/* ECMA-262 3rd Edition    12.2 */
StatementList_opt
        : /* empty */           { $$ = NULL; }
        | StatementList         { $$ = $1; }

/* ECMA-262 3rd Edition    12.1 */
Block
322
        : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
323
        | '{' '}'               { $$ = new_block_statement(ctx, NULL); }
Jacek Caban's avatar
Jacek Caban committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

/* ECMA-262 3rd Edition    12.2 */
VariableStatement
        : kVAR VariableDeclarationList semicolon_opt
                                { $$ = new_var_statement(ctx, $2); }

/* ECMA-262 3rd Edition    12.2 */
VariableDeclarationList
        : VariableDeclaration   { $$ = new_variable_list(ctx, $1); }
        | VariableDeclarationList ',' VariableDeclaration
                                { $$ = variable_list_add(ctx, $1, $3); }

/* ECMA-262 3rd Edition    12.2 */
VariableDeclarationListNoIn
        : VariableDeclarationNoIn
                                { $$ = new_variable_list(ctx, $1); }
        | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
                                { $$ = variable_list_add(ctx, $1, $3); }

/* ECMA-262 3rd Edition    12.2 */
VariableDeclaration
        : tIdentifier Initialiser_opt
                                { $$ = new_variable_declaration(ctx, $1, $2); }

/* ECMA-262 3rd Edition    12.2 */
VariableDeclarationNoIn
        : tIdentifier InitialiserNoIn_opt
                                { $$ = new_variable_declaration(ctx, $1, $2); }

/* ECMA-262 3rd Edition    12.2 */
Initialiser_opt
        : /* empty */           { $$ = NULL; }
        | Initialiser           { $$ = $1; }

/* ECMA-262 3rd Edition    12.2 */
Initialiser
        : '=' AssignmentExpression
                                { $$ = $2; }

/* ECMA-262 3rd Edition    12.2 */
InitialiserNoIn_opt
        : /* empty */           { $$ = NULL; }
        | InitialiserNoIn       { $$ = $1; }

/* ECMA-262 3rd Edition    12.2 */
InitialiserNoIn
        : '=' AssignmentExpressionNoIn
                                { $$ = $2; }

/* ECMA-262 3rd Edition    12.3 */
EmptyStatement
375
        : ';'                   { $$ = new_statement(ctx, STAT_EMPTY, 0); }
Jacek Caban's avatar
Jacek Caban committed
376 377 378 379 380 381 382 383

/* ECMA-262 3rd Edition    12.4 */
ExpressionStatement
        : Expression semicolon_opt
                                { $$ = new_expression_statement(ctx, $1); }

/* ECMA-262 3rd Edition    12.5 */
IfStatement
384
        : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
Jacek Caban's avatar
Jacek Caban committed
385
                                { $$ = new_if_statement(ctx, $3, $5, $7); }
386
        | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
Jacek Caban's avatar
Jacek Caban committed
387 388 389 390
                                { $$ = new_if_statement(ctx, $3, $5, NULL); }

/* ECMA-262 3rd Edition    12.6 */
IterationStatement
391
        : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
Jacek Caban's avatar
Jacek Caban committed
392
                                { $$ = new_while_statement(ctx, TRUE, $5, $2); }
393
        | kWHILE left_bracket Expression_err right_bracket Statement
Jacek Caban's avatar
Jacek Caban committed
394
                                { $$ = new_while_statement(ctx, FALSE, $3, $5); }
395 396 397 398 399 400 401 402 403 404 405 406
        | kFOR left_bracket ExpressionNoIn_opt
                                { if(!explicit_error(ctx, $3, ';')) YYABORT; }
        semicolon  Expression_opt
                                { if(!explicit_error(ctx, $6, ';')) YYABORT; }
        semicolon Expression_opt right_bracket Statement
                                { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
        | kFOR left_bracket kVAR VariableDeclarationListNoIn
                                { if(!explicit_error(ctx, $4, ';')) YYABORT; }
        semicolon Expression_opt
                                { if(!explicit_error(ctx, $7, ';')) YYABORT; }
        semicolon Expression_opt right_bracket Statement
                                { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
407
        | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
Jacek Caban's avatar
Jacek Caban committed
408
                                { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
409
        | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
Jacek Caban's avatar
Jacek Caban committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
                                { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }

/* ECMA-262 3rd Edition    12.7 */
ContinueStatement
        : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
                                { $$ = new_continue_statement(ctx, $2); }

/* ECMA-262 3rd Edition    12.8 */
BreakStatement
        : kBREAK /* NONL */ Identifier_opt semicolon_opt
                                { $$ = new_break_statement(ctx, $2); }

/* ECMA-262 3rd Edition    12.9 */
ReturnStatement
        : kRETURN /* NONL */ Expression_opt semicolon_opt
                                { $$ = new_return_statement(ctx, $2); }

/* ECMA-262 3rd Edition    12.10 */
WithStatement
429
        : kWITH left_bracket Expression right_bracket Statement
Jacek Caban's avatar
Jacek Caban committed
430 431 432 433 434 435 436 437 438
                                { $$ = new_with_statement(ctx, $3, $5); }

/* ECMA-262 3rd Edition    12.12 */
LabelledStatement
        : tIdentifier ':' Statement
                                { $$ = new_labelled_statement(ctx, $1, $3); }

/* ECMA-262 3rd Edition    12.11 */
SwitchStatement
439
        : kSWITCH left_bracket Expression right_bracket CaseBlock
440
                                { $$ = new_switch_statement(ctx, $3, $5); }
Jacek Caban's avatar
Jacek Caban committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

/* ECMA-262 3rd Edition    12.11 */
CaseBlock
        : '{' CaseClausules_opt '}'
                                 { $$ = new_case_block(ctx, $2, NULL, NULL); }
        | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
                                 { $$ = new_case_block(ctx, $2, $3, $4); }

/* ECMA-262 3rd Edition    12.11 */
CaseClausules_opt
        : /* empty */            { $$ = NULL; }
        | CaseClausules          { $$ = $1; }

/* ECMA-262 3rd Edition    12.11 */
CaseClausules
        : CaseClausule           { $$ = new_case_list(ctx, $1); }
        | CaseClausules CaseClausule
                                 { $$ = case_list_add(ctx, $1, $2); }

/* ECMA-262 3rd Edition    12.11 */
CaseClausule
        : kCASE Expression ':' StatementList_opt
                                 { $$ = new_case_clausule(ctx, $2, $4); }

/* ECMA-262 3rd Edition    12.11 */
DefaultClausule
        : kDEFAULT ':' StatementList_opt
                                 { $$ = new_case_clausule(ctx, NULL, $3); }

/* ECMA-262 3rd Edition    12.13 */
ThrowStatement
        : kTHROW /* NONL */ Expression semicolon_opt
                                { $$ = new_throw_statement(ctx, $2); }

/* ECMA-262 3rd Edition    12.14 */
TryStatement
        : kTRY Block Catch      { $$ = new_try_statement(ctx, $2, $3, NULL); }
        | kTRY Block Finally    { $$ = new_try_statement(ctx, $2, NULL, $3); }
        | kTRY Block Catch Finally
                                { $$ = new_try_statement(ctx, $2, $3, $4); }

/* ECMA-262 3rd Edition    12.14 */
Catch
484
        : kCATCH left_bracket tIdentifier right_bracket Block
485
                                { $$ = new_catch_block(ctx, $3, $5); }
Jacek Caban's avatar
Jacek Caban committed
486 487 488 489 490 491 492 493 494 495

/* ECMA-262 3rd Edition    12.14 */
Finally
        : kFINALLY Block        { $$ = $2; }

/* ECMA-262 3rd Edition    11.14 */
Expression_opt
        : /* empty */           { $$ = NULL; }
        | Expression            { $$ = $1; }

496 497
Expression_err
        : Expression            { $$ = $1; }
498
        | error                 { set_error(ctx, JS_E_SYNTAX); YYABORT; }
499

Jacek Caban's avatar
Jacek Caban committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
/* ECMA-262 3rd Edition    11.14 */
Expression
        : AssignmentExpression  { $$ = $1; }
        | Expression ',' AssignmentExpression
                                { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }

/* ECMA-262 3rd Edition    11.14 */
ExpressionNoIn_opt
        : /* empty */           { $$ = NULL; }
        | ExpressionNoIn        { $$ = $1; }

/* ECMA-262 3rd Edition    11.14 */
ExpressionNoIn
        : AssignmentExpressionNoIn
                                { $$ = $1; }
        | ExpressionNoIn ',' AssignmentExpressionNoIn
                                { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }

518 519 520 521
AssignOper
        : tAssignOper           { $$ = $1; }
        | kDIVEQ                { $$ = EXPR_ASSIGNDIV; }

Jacek Caban's avatar
Jacek Caban committed
522 523 524 525 526
/* ECMA-262 3rd Edition    11.13 */
AssignmentExpression
        : ConditionalExpression { $$ = $1; }
        | LeftHandSideExpression '=' AssignmentExpression
                                { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
527
        | LeftHandSideExpression AssignOper AssignmentExpression
Jacek Caban's avatar
Jacek Caban committed
528 529 530 531 532 533 534 535
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }

/* ECMA-262 3rd Edition    11.13 */
AssignmentExpressionNoIn
        : ConditionalExpressionNoIn
                                { $$ = $1; }
        | LeftHandSideExpression '=' AssignmentExpressionNoIn
                                { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
536
        | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
Jacek Caban's avatar
Jacek Caban committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }

/* ECMA-262 3rd Edition    11.12 */
ConditionalExpression
        : LogicalORExpression   { $$ = $1; }
        | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
                                { $$ = new_conditional_expression(ctx, $1, $3, $5); }

/* ECMA-262 3rd Edition    11.12 */
ConditionalExpressionNoIn
        : LogicalORExpressionNoIn
                                { $$ = $1; }
        | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
                                { $$ = new_conditional_expression(ctx, $1, $3, $5); }

/* ECMA-262 3rd Edition    11.11 */
LogicalORExpression
        : LogicalANDExpression  { $$ = $1; }
        | LogicalORExpression tOROR LogicalANDExpression
                                { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }

/* ECMA-262 3rd Edition    11.11 */
LogicalORExpressionNoIn
        : LogicalANDExpressionNoIn
                                { $$ = $1; }
        | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
                                { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }

/* ECMA-262 3rd Edition    11.11 */
LogicalANDExpression
        : BitwiseORExpression   { $$ = $1; }
        | LogicalANDExpression tANDAND BitwiseORExpression
                                { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }

/* ECMA-262 3rd Edition    11.11 */
LogicalANDExpressionNoIn
        : BitwiseORExpressionNoIn
                                { $$ = $1; }
        | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
                                { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }

/* ECMA-262 3rd Edition    11.10 */
BitwiseORExpression
        : BitwiseXORExpression   { $$ = $1; }
        | BitwiseORExpression '|' BitwiseXORExpression
                                { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }

/* ECMA-262 3rd Edition    11.10 */
BitwiseORExpressionNoIn
        : BitwiseXORExpressionNoIn
                                { $$ = $1; }
        | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
                                { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }

/* ECMA-262 3rd Edition    11.10 */
BitwiseXORExpression
        : BitwiseANDExpression  { $$ = $1; }
        | BitwiseXORExpression '^' BitwiseANDExpression
595
                                { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
Jacek Caban's avatar
Jacek Caban committed
596 597 598 599 600 601

/* ECMA-262 3rd Edition    11.10 */
BitwiseXORExpressionNoIn
        : BitwiseANDExpressionNoIn
                                { $$ = $1; }
        | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
602
                                { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
Jacek Caban's avatar
Jacek Caban committed
603 604 605 606 607

/* ECMA-262 3rd Edition    11.10 */
BitwiseANDExpression
        : EqualityExpression    { $$ = $1; }
        | BitwiseANDExpression '&' EqualityExpression
608
                                { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
Jacek Caban's avatar
Jacek Caban committed
609 610 611 612 613 614

/* ECMA-262 3rd Edition    11.10 */
BitwiseANDExpressionNoIn
        : EqualityExpressionNoIn
                                { $$ = $1; }
        | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
615
                                { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
Jacek Caban's avatar
Jacek Caban committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

/* ECMA-262 3rd Edition    11.9 */
EqualityExpression
        : RelationalExpression  { $$ = $1; }
        | EqualityExpression tEqOper RelationalExpression
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }

/* ECMA-262 3rd Edition    11.9 */
EqualityExpressionNoIn
        : RelationalExpressionNoIn  { $$ = $1; }
        | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }

/* ECMA-262 3rd Edition    11.8 */
RelationalExpression
        : ShiftExpression       { $$ = $1; }
        | RelationalExpression tRelOper ShiftExpression
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }
        | RelationalExpression kINSTANCEOF ShiftExpression
                                { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
        | RelationalExpression kIN ShiftExpression
                                { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }

/* ECMA-262 3rd Edition    11.8 */
RelationalExpressionNoIn
        : ShiftExpression       { $$ = $1; }
        | RelationalExpressionNoIn tRelOper ShiftExpression
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }
        | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
                                { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }

/* ECMA-262 3rd Edition    11.7 */
ShiftExpression
        : AdditiveExpression    { $$ = $1; }
        | ShiftExpression tShiftOper AdditiveExpression
                                { $$ = new_binary_expression(ctx, $2, $1, $3); }

/* ECMA-262 3rd Edition    11.6 */
AdditiveExpression
        : MultiplicativeExpression
                                { $$ = $1; }
        | AdditiveExpression '+' MultiplicativeExpression
                                { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
        | AdditiveExpression '-' MultiplicativeExpression
                                { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }

/* ECMA-262 3rd Edition    11.5 */
MultiplicativeExpression
        : UnaryExpression       { $$ = $1; }
        | MultiplicativeExpression '*' UnaryExpression
                                { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
        | MultiplicativeExpression '/' UnaryExpression
                                { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
        | MultiplicativeExpression '%' UnaryExpression
                                { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }

/* ECMA-262 3rd Edition    11.4 */
UnaryExpression
        : PostfixExpression     { $$ = $1; }
        | kDELETE UnaryExpression
                                { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
        | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
        | kTYPEOF UnaryExpression
                                { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
        | tINC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
        | tDEC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
        | '+' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
        | '-' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
        | '~' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
        | '!' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }

/* ECMA-262 3rd Edition    11.2 */
PostfixExpression
        : LeftHandSideExpression
                                { $$ = $1; }
        | LeftHandSideExpression /* NONL */ tINC
                                { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
        | LeftHandSideExpression /* NONL */ tDEC
                                { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }


/* ECMA-262 3rd Edition    11.2 */
LeftHandSideExpression
        : NewExpression         { $$ = $1; }
        | CallExpression        { $$ = $1; }

/* ECMA-262 3rd Edition    11.2 */
NewExpression
        : MemberExpression      { $$ = $1; }
705
        | kNEW NewExpression    { $$ = new_new_expression(ctx, $2, NULL); }
Jacek Caban's avatar
Jacek Caban committed
706 707 708 709 710 711

/* ECMA-262 3rd Edition    11.2 */
MemberExpression
        : PrimaryExpression     { $$ = $1; }
        | FunctionExpression    { $$ = $1; }
        | MemberExpression '[' Expression ']'
712
                                { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
Jacek Caban's avatar
Jacek Caban committed
713 714 715
        | MemberExpression '.' tIdentifier
                                { $$ = new_member_expression(ctx, $1, $3); }
        | kNEW MemberExpression Arguments
716
                                { $$ = new_new_expression(ctx, $2, $3); }
Jacek Caban's avatar
Jacek Caban committed
717 718 719 720 721 722 723 724

/* ECMA-262 3rd Edition    11.2 */
CallExpression
        : MemberExpression Arguments
                                { $$ = new_call_expression(ctx, $1, $2); }
        | CallExpression Arguments
                                { $$ = new_call_expression(ctx, $1, $2); }
        | CallExpression '[' Expression ']'
725
                                { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
Jacek Caban's avatar
Jacek Caban committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        | CallExpression '.' tIdentifier
                                { $$ = new_member_expression(ctx, $1, $3); }

/* ECMA-262 3rd Edition    11.2 */
Arguments
        : '(' ')'               { $$ = NULL; }
        | '(' ArgumentList ')'  { $$ = $2; }

/* ECMA-262 3rd Edition    11.2 */
ArgumentList
        : AssignmentExpression  { $$ = new_argument_list(ctx, $1); }
        | ArgumentList ',' AssignmentExpression
                                { $$ = argument_list_add(ctx, $1, $3); }

/* ECMA-262 3rd Edition    11.1 */
PrimaryExpression
742
        : kTHIS                 { $$ = new_expression(ctx, EXPR_THIS, 0); }
Jacek Caban's avatar
Jacek Caban committed
743 744 745 746 747 748 749 750
        | tIdentifier           { $$ = new_identifier_expression(ctx, $1); }
        | Literal               { $$ = new_literal_expression(ctx, $1); }
        | ArrayLiteral          { $$ = $1; }
        | ObjectLiteral         { $$ = $1; }
        | '(' Expression ')'    { $$ = $2; }

/* ECMA-262 3rd Edition    11.1.4 */
ArrayLiteral
751
        : '[' ']'               { $$ = new_array_literal_expression(ctx, NULL, 0); }
752
        | '[' Elision ']'       { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
Jacek Caban's avatar
Jacek Caban committed
753 754
        | '[' ElementList ']'   { $$ = new_array_literal_expression(ctx, $2, 0); }
        | '[' ElementList ',' Elision_opt ']'
755
                                { $$ = new_array_literal_expression(ctx, $2, $4+1); }
Jacek Caban's avatar
Jacek Caban committed
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

/* ECMA-262 3rd Edition    11.1.4 */
ElementList
        : Elision_opt AssignmentExpression
                                { $$ = new_element_list(ctx, $1, $2); }
        | ElementList ',' Elision_opt AssignmentExpression
                                { $$ = element_list_add(ctx, $1, $3, $4); }

/* ECMA-262 3rd Edition    11.1.4 */
Elision
        : ','                   { $$ = 1; }
        | Elision ','           { $$ = $1 + 1; }

/* ECMA-262 3rd Edition    11.1.4 */
Elision_opt
        : /* empty */           { $$ = 0; }
        | Elision               { $$ = $1; }

/* ECMA-262 3rd Edition    11.1.5 */
ObjectLiteral
        : '{' '}'               { $$ = new_prop_and_value_expression(ctx, NULL); }
        | '{' PropertyNameAndValueList '}'
                                { $$ = new_prop_and_value_expression(ctx, $2); }

/* ECMA-262 3rd Edition    11.1.5 */
PropertyNameAndValueList
        : PropertyName ':' AssignmentExpression
                                { $$ = new_property_list(ctx, $1, $3); }
        | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
                                { $$ = property_list_add(ctx, $1, $3, $5); }

/* ECMA-262 3rd Edition    11.1.5 */
PropertyName
        : tIdentifier           { $$ = new_string_literal(ctx, $1); }
        | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
        | tNumericLiteral       { $$ = $1; }

/* ECMA-262 3rd Edition    7.6 */
Identifier_opt
        : /* empty*/            { $$ = NULL; }
        | tIdentifier           { $$ = $1; }

/* ECMA-262 3rd Edition    7.8 */
Literal
        : kNULL                 { $$ = new_null_literal(ctx); }
        | BooleanLiteral        { $$ = $1; }
        | tNumericLiteral       { $$ = $1; }
        | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
804 805
        | '/'                   { $$ = parse_regexp(ctx);
                                  if(!$$) YYABORT; }
806 807
        | kDIVEQ                { $$ = parse_regexp(ctx);
                                  if(!$$) YYABORT; }
Jacek Caban's avatar
Jacek Caban committed
808 809 810

/* ECMA-262 3rd Edition    7.8.2 */
BooleanLiteral
811 812
        : kTRUE                 { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
        | kFALSE                { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
813
        | tBooleanLiteral       { $$ = $1; }
Jacek Caban's avatar
Jacek Caban committed
814 815 816 817 818

semicolon_opt
        : ';'
        | error                 { if(!allow_auto_semicolon(ctx)) {YYABORT;} }

819 820
left_bracket
        : '('
821
        | error                 { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
822

823 824
right_bracket
        : ')'
825
        | error                 { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
826

827 828
semicolon
        : ';'
829
        | error                 { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; }
830

Jacek Caban's avatar
Jacek Caban committed
831 832 833 834 835 836 837
%%

static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
{
    return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
}

838 839 840 841 842 843 844 845 846 847 848 849 850 851
static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
{
    statement_t *stat;

    stat = parser_alloc(ctx, size ? size : sizeof(*stat));
    if(!stat)
        return NULL;

    stat->type = type;
    stat->next = NULL;

    return stat;
}

Jacek Caban's avatar
Jacek Caban committed
852 853 854 855
static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
{
    literal_t *ret = parser_alloc(ctx, sizeof(literal_t));

856
    ret->type = LT_STRING;
Jacek Caban's avatar
Jacek Caban committed
857 858 859 860 861 862 863 864 865
    ret->u.wstr = str;

    return ret;
}

static literal_t *new_null_literal(parser_ctx_t *ctx)
{
    literal_t *ret = parser_alloc(ctx, sizeof(literal_t));

866
    ret->type = LT_NULL;
Jacek Caban's avatar
Jacek Caban committed
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

    return ret;
}

static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
{
    prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));

    ret->name = name;
    ret->value = value;
    ret->next = NULL;

    return ret;
}

static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
{
    property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));

    ret->head = ret->tail = new_prop_val(ctx, name, value);

    return ret;
}

static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
{
    list->tail = list->tail->next = new_prop_val(ctx, name, value);

    return list;
}

static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
{
    array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));

    ret->elision = elision;
    ret->expr = expr;
    ret->next = NULL;

    return ret;
}

static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
{
    element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));

    ret->head = ret->tail = new_array_element(ctx, elision, expr);

    return ret;
}

static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
{
    list->tail = list->tail->next = new_array_element(ctx, elision, expr);

    return list;
}

static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
{
    argument_t *ret = parser_alloc(ctx, sizeof(argument_t));

    ret->expr = expr;
    ret->next = NULL;

    return ret;
}

static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
{
    argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));

    ret->head = ret->tail = new_argument(ctx, expr);

    return ret;
}

static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
{
    list->tail = list->tail->next = new_argument(ctx, expr);

    return list;
}

static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
{
    catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));

    ret->identifier = identifier;
    ret->statement = statement;

    return ret;
}

static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
{
    case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));

    ret->expr = expr;
    ret->stat = stat_list ? stat_list->head : NULL;
    ret->next = NULL;

    return ret;
}

static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
{
    case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));

    ret->head = ret->tail = case_clausule;

    return ret;
}

static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
{
    list->tail = list->tail->next = case_clausule;

    return list;
}

static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
        case_clausule_t *default_clausule, case_list_t *case_list2)
{
991
    case_clausule_t *ret = NULL, *iter = NULL, *iter2;
Jacek Caban's avatar
Jacek Caban committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    statement_t *stat = NULL;

    if(case_list1) {
        ret = case_list1->head;
        iter = case_list1->tail;
    }

    if(default_clausule) {
        if(ret)
            iter = iter->next = default_clausule;
        else
            ret = iter = default_clausule;
    }

    if(case_list2) {
        if(ret)
            iter->next = case_list2->head;
        else
            ret = case_list2->head;
    }

    if(!ret)
        return NULL;

1016 1017
    for(iter = ret; iter; iter = iter->next) {
        for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
Jacek Caban's avatar
Jacek Caban committed
1018 1019 1020 1021 1022
        if(!iter2)
            break;

        while(iter != iter2) {
            iter->stat = iter2->stat;
1023
            iter = iter->next;
Jacek Caban's avatar
Jacek Caban committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
        }

        if(stat) {
            while(stat->next)
                stat = stat->next;
            stat->next = iter->stat;
        }else {
            stat = iter->stat;
        }
    }

    return ret;
}

static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
{
1040 1041 1042 1043 1044
    block_statement_t *ret;

    ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1045 1046 1047 1048 1049 1050 1051 1052 1053

    ret->stat_list = list ? list->head : NULL;

    return &ret->stat;
}

static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
{
    variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1054
    var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
Jacek Caban's avatar
Jacek Caban committed
1055 1056 1057 1058 1059

    ret->identifier = identifier;
    ret->expr = expr;
    ret->next = NULL;

1060 1061 1062 1063 1064 1065 1066 1067
    var_list->identifier = identifier;
    var_list->next = NULL;

    if(ctx->func_stack->var_tail)
        ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
    else
        ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;

Jacek Caban's avatar
Jacek Caban committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    return ret;
}

static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
{
    variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));

    ret->head = ret->tail = decl;

    return ret;
}

static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
{
    list->tail = list->tail->next = decl;

    return list;
}

static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
{
1089 1090 1091 1092 1093
    var_statement_t *ret;

    ret = new_statement(ctx, STAT_VAR, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1094 1095 1096 1097 1098 1099 1100 1101

    ret->variable_list = variable_list->head;

    return &ret->stat;
}

static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
{
1102 1103 1104 1105 1106
    expression_statement_t *ret;

    ret = new_statement(ctx, STAT_EXPR, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1107 1108 1109 1110 1111 1112 1113 1114

    ret->expr = expr;

    return &ret->stat;
}

static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
{
1115 1116 1117 1118 1119
    if_statement_t *ret;

    ret = new_statement(ctx, STAT_IF, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

    ret->expr = expr;
    ret->if_stat = if_stat;
    ret->else_stat = else_stat;

    return &ret->stat;
}

static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
{
1130 1131 1132 1133 1134
    while_statement_t *ret;

    ret = new_statement(ctx, STAT_WHILE, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1135

1136
    ret->do_while = dowhile;
Jacek Caban's avatar
Jacek Caban committed
1137 1138 1139 1140 1141 1142 1143 1144 1145
    ret->expr = expr;
    ret->statement = stat;

    return &ret->stat;
}

static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
        expression_t *expr, expression_t *end_expr, statement_t *statement)
{
1146 1147 1148 1149 1150
    for_statement_t *ret;

    ret = new_statement(ctx, STAT_FOR, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

    ret->variable_list = variable_list ? variable_list->head : NULL;
    ret->begin_expr = begin_expr;
    ret->expr = expr;
    ret->end_expr = end_expr;
    ret->statement = statement;

    return &ret->stat;
}

static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
        expression_t *in_expr, statement_t *statement)
{
1164 1165 1166 1167 1168
    forin_statement_t *ret;

    ret = new_statement(ctx, STAT_FORIN, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179

    ret->variable = variable;
    ret->expr = expr;
    ret->in_expr = in_expr;
    ret->statement = statement;

    return &ret->stat;
}

static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
{
1180 1181 1182 1183 1184
    branch_statement_t *ret;

    ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1185 1186 1187 1188 1189 1190 1191 1192

    ret->identifier = identifier;

    return &ret->stat;
}

static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
{
1193 1194 1195 1196 1197
    branch_statement_t *ret;

    ret = new_statement(ctx, STAT_BREAK, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1198 1199 1200 1201 1202 1203 1204 1205

    ret->identifier = identifier;

    return &ret->stat;
}

static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
{
1206 1207 1208 1209 1210
    expression_statement_t *ret;

    ret = new_statement(ctx, STAT_RETURN, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1211 1212 1213 1214 1215 1216 1217 1218

    ret->expr = expr;

    return &ret->stat;
}

static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
{
1219 1220 1221 1222 1223
    with_statement_t *ret;

    ret = new_statement(ctx, STAT_WITH, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1224 1225 1226 1227 1228 1229 1230 1231 1232

    ret->expr = expr;
    ret->statement = statement;

    return &ret->stat;
}

static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
{
1233 1234 1235 1236 1237
    labelled_statement_t *ret;

    ret = new_statement(ctx, STAT_LABEL, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1238 1239 1240 1241 1242 1243 1244 1245 1246

    ret->identifier = identifier;
    ret->statement = statement;

    return &ret->stat;
}

static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
{
1247 1248 1249 1250 1251
    switch_statement_t *ret;

    ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1252 1253 1254 1255 1256 1257 1258 1259 1260

    ret->expr = expr;
    ret->case_list = case_list;

    return &ret->stat;
}

static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
{
1261 1262 1263 1264 1265
    expression_statement_t *ret;

    ret = new_statement(ctx, STAT_THROW, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1266 1267 1268 1269 1270 1271 1272 1273 1274

    ret->expr = expr;

    return &ret->stat;
}

static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
       catch_block_t *catch_block, statement_t *finally_statement)
{
1275 1276 1277 1278 1279
    try_statement_t *ret;

    ret = new_statement(ctx, STAT_TRY, sizeof(*ret));
    if(!ret)
        return NULL;
Jacek Caban's avatar
Jacek Caban committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314

    ret->try_statement = try_statement;
    ret->catch_block = catch_block;
    ret->finally_statement = finally_statement;

    return &ret->stat;
}

static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
{
    parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));

    ret->identifier = identifier;
    ret->next = NULL;

    return ret;
}

static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
{
    parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));

    ret->head = ret->tail = new_parameter(ctx, identifier);

    return ret;
}

static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
{
    list->tail = list->tail->next = new_parameter(ctx, identifier);

    return list;
}

static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1315
       parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
Jacek Caban's avatar
Jacek Caban committed
1316
{
1317
    function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1318
    function_declaration_t *decl;
Jacek Caban's avatar
Jacek Caban committed
1319 1320 1321 1322

    ret->identifier = identifier;
    ret->parameter_list = parameter_list ? parameter_list->head : NULL;
    ret->source_elements = source_elements;
1323 1324
    ret->src_str = src_str;
    ret->src_len = src_len;
Jacek Caban's avatar
Jacek Caban committed
1325

1326
    decl = parser_alloc(ctx, sizeof(function_declaration_t));
1327

1328 1329
    decl->expr = ret;
    decl->next = NULL;
1330

1331 1332 1333 1334
    if(ctx->func_stack->func_tail)
        ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
    else
        ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1335

Jacek Caban's avatar
Jacek Caban committed
1336 1337 1338
    return &ret->expr;
}

1339 1340 1341 1342
static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
{
    expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));

1343
    ret->type = type;
1344 1345 1346 1347

    return ret;
}

Jacek Caban's avatar
Jacek Caban committed
1348 1349 1350
static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
       expression_t *expression1, expression_t *expression2)
{
1351
    binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1352 1353 1354 1355 1356 1357 1358 1359 1360

    ret->expression1 = expression1;
    ret->expression2 = expression2;

    return &ret->expr;
}

static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
{
1361
    unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1362 1363 1364 1365 1366 1367 1368 1369 1370

    ret->expression = expression;

    return &ret->expr;
}

static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
       expression_t *true_expression, expression_t *false_expression)
{
1371
    conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381

    ret->expression = expression;
    ret->true_expression = true_expression;
    ret->false_expression = false_expression;

    return &ret->expr;
}

static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
{
1382
    member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1383 1384 1385 1386 1387 1388 1389

    ret->expression = expression;
    ret->identifier = identifier;

    return &ret->expr;
}

1390
static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
Jacek Caban's avatar
Jacek Caban committed
1391
{
1392
    call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1393 1394 1395 1396 1397 1398 1399 1400 1401

    ret->expression = expression;
    ret->argument_list = argument_list ? argument_list->head : NULL;

    return &ret->expr;
}

static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
{
1402
    call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

    ret->expression = expression;
    ret->argument_list = argument_list ? argument_list->head : NULL;

    return &ret->expr;
}

static int parser_error(const char *str)
{
    return 0;
}

1415 1416
static void set_error(parser_ctx_t *ctx, UINT error)
{
1417
    ctx->hres = error;
1418 1419
}

1420 1421 1422 1423
static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
{
    if(obj || *(ctx->ptr-1)==next) return TRUE;

1424
    set_error(ctx, JS_E_SYNTAX);
1425 1426 1427
    return FALSE;
}

1428

Jacek Caban's avatar
Jacek Caban committed
1429 1430
static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
{
1431
    identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1432 1433 1434 1435 1436 1437 1438 1439

    ret->identifier = identifier;

    return &ret->expr;
}

static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
{
1440
    array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1441 1442 1443 1444 1445 1446 1447 1448 1449

    ret->element_list = element_list ? element_list->head : NULL;
    ret->length = length;

    return &ret->expr;
}

static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
{
1450
    property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1451 1452 1453 1454 1455 1456 1457 1458

    ret->property_list = property_list ? property_list->head : NULL;

    return &ret->expr;
}

static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
{
1459
    literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
Jacek Caban's avatar
Jacek Caban committed
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

    ret->literal = literal;

    return &ret->expr;
}

static source_elements_t *new_source_elements(parser_ctx_t *ctx)
{
    source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));

    memset(ret, 0, sizeof(*ret));
1471
    ret->instr_off = 0;
Jacek Caban's avatar
Jacek Caban committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485

    return ret;
}

static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
{
    if(source_elements->statement_tail)
        source_elements->statement_tail = source_elements->statement_tail->next = statement;
    else
        source_elements->statement = source_elements->statement_tail = statement;

    return source_elements;
}

1486
static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
Jacek Caban's avatar
Jacek Caban committed
1487 1488 1489 1490 1491 1492 1493 1494
{
    statement_list_t *ret =  parser_alloc_tmp(ctx, sizeof(statement_list_t));

    ret->head = ret->tail = statement;

    return ret;
}

1495
static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
Jacek Caban's avatar
Jacek Caban committed
1496 1497 1498 1499 1500 1501
{
    list->tail = list->tail->next = statement;

    return list;
}

1502 1503 1504 1505
static void push_func(parser_ctx_t *ctx)
{
    func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));

1506
    new_func->func_head = new_func->func_tail = NULL;
1507 1508 1509 1510 1511 1512 1513 1514
    new_func->var_head = new_func->var_tail = NULL;

    new_func->next = ctx->func_stack;
    ctx->func_stack = new_func;
}

static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
{
1515
    source->functions = ctx->func_stack->func_head;
1516 1517 1518 1519 1520 1521
    source->variables = ctx->func_stack->var_head;
    pop_func(ctx);

    return source;
}

Jacek Caban's avatar
Jacek Caban committed
1522 1523
static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
{
1524
    source->functions = ctx->func_stack->func_head;
1525 1526 1527
    source->variables = ctx->func_stack->var_head;
    pop_func(ctx);

Jacek Caban's avatar
Jacek Caban committed
1528
    ctx->source = source;
1529 1530
    if(!ctx->lexer_error)
        ctx->hres = S_OK;
Jacek Caban's avatar
Jacek Caban committed
1531 1532 1533 1534
}

void parser_release(parser_ctx_t *ctx)
{
1535
    script_release(ctx->script);
1536
    jsheap_free(&ctx->heap);
Jacek Caban's avatar
Jacek Caban committed
1537 1538 1539
    heap_free(ctx);
}

1540
HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval,
1541
        parser_ctx_t **ret)
Jacek Caban's avatar
Jacek Caban committed
1542 1543
{
    parser_ctx_t *parser_ctx;
1544
    jsheap_t *mark;
Jacek Caban's avatar
Jacek Caban committed
1545 1546
    HRESULT hres;

1547 1548
    const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};

Jacek Caban's avatar
Jacek Caban committed
1549 1550 1551 1552
    parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
    if(!parser_ctx)
        return E_OUTOFMEMORY;

1553
    parser_ctx->hres = JS_E_SYNTAX;
1554
    parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
Jacek Caban's avatar
Jacek Caban committed
1555

1556
    parser_ctx->begin = parser_ctx->ptr = code;
1557
    parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
Jacek Caban's avatar
Jacek Caban committed
1558 1559 1560 1561

    script_addref(ctx);
    parser_ctx->script = ctx;

1562
    mark = jsheap_mark(&ctx->tmp_heap);
1563 1564
    jsheap_init(&parser_ctx->heap);

1565 1566
    push_func(parser_ctx);

Jacek Caban's avatar
Jacek Caban committed
1567
    parser_parse(parser_ctx);
1568
    jsheap_clear(mark);
1569 1570
    hres = parser_ctx->hres;
    if(FAILED(hres)) {
Jacek Caban's avatar
Jacek Caban committed
1571 1572 1573 1574 1575 1576 1577
        parser_release(parser_ctx);
        return hres;
    }

    *ret = parser_ctx;
    return S_OK;
}