1 /* 2 * Copyright 2008 Jacek Caban for CodeWeavers 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 17 */ 18 19 %{ 20 21 #include "jscript.h" 22 #include "engine.h" 23 #include "parser.h" 24 25 #include "wine/debug.h" 26 27 WINE_DEFAULT_DEBUG_CHANNEL(jscript); 28 29 static int parser_error(parser_ctx_t*,const char*); 30 static void set_error(parser_ctx_t*,UINT); 31 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR); 32 static BOOL allow_auto_semicolon(parser_ctx_t*); 33 static void program_parsed(parser_ctx_t*,source_elements_t*); 34 35 typedef struct _statement_list_t { 36 statement_t *head; 37 statement_t *tail; 38 } statement_list_t; 39 40 static literal_t *new_string_literal(parser_ctx_t*,jsstr_t*); 41 static literal_t *new_null_literal(parser_ctx_t*); 42 43 typedef struct _property_list_t { 44 property_definition_t *head; 45 property_definition_t *tail; 46 } property_list_t; 47 48 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t, 49 literal_t *name, expression_t *value); 50 static property_list_t *new_property_list(parser_ctx_t*,property_definition_t*); 51 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,property_definition_t*); 52 53 typedef struct _element_list_t { 54 array_element_t *head; 55 array_element_t *tail; 56 } element_list_t; 57 58 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*); 59 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*); 60 61 typedef struct _argument_list_t { 62 argument_t *head; 63 argument_t *tail; 64 } argument_list_t; 65 66 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*); 67 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*); 68 69 typedef struct _case_list_t { 70 case_clausule_t *head; 71 case_clausule_t *tail; 72 } case_list_t; 73 74 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*); 75 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*); 76 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*); 77 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*); 78 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*); 79 80 typedef struct _variable_list_t { 81 variable_declaration_t *head; 82 variable_declaration_t *tail; 83 } variable_list_t; 84 85 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*); 86 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*); 87 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*); 88 89 static void *new_statement(parser_ctx_t*,statement_type_t,size_t); 90 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*); 91 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*); 92 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*); 93 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*); 94 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*); 95 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*, 96 expression_t*,statement_t*); 97 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*); 98 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*); 99 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*); 100 static statement_t *new_return_statement(parser_ctx_t*,expression_t*); 101 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*); 102 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*); 103 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*); 104 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*); 105 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*); 106 107 struct statement_list_t { 108 statement_t *head; 109 statement_t *tail; 110 }; 111 112 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*); 113 static statement_list_t *statement_list_add(statement_list_t*,statement_t*); 114 115 typedef struct _parameter_list_t { 116 parameter_t *head; 117 parameter_t *tail; 118 } parameter_list_t; 119 120 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*); 121 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*); 122 123 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t); 124 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*, 125 source_elements_t*,const WCHAR*,const WCHAR*,DWORD); 126 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*); 127 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*); 128 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*); 129 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*); 130 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*); 131 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*); 132 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*); 133 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*); 134 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int); 135 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*); 136 137 static source_elements_t *new_source_elements(parser_ctx_t*); 138 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*); 139 140 %} 141 142 %lex-param { parser_ctx_t *ctx } 143 %parse-param { parser_ctx_t *ctx } 144 %define api.pure 145 %start Program 146 147 %union { 148 int ival; 149 const WCHAR *srcptr; 150 jsstr_t *str; 151 literal_t *literal; 152 struct _argument_list_t *argument_list; 153 case_clausule_t *case_clausule; 154 struct _case_list_t *case_list; 155 catch_block_t *catch_block; 156 struct _element_list_t *element_list; 157 expression_t *expr; 158 const WCHAR *identifier; 159 struct _parameter_list_t *parameter_list; 160 struct _property_list_t *property_list; 161 property_definition_t *property_definition; 162 source_elements_t *source_elements; 163 statement_t *statement; 164 struct _statement_list_t *statement_list; 165 struct _variable_list_t *variable_list; 166 variable_declaration_t *variable_declaration; 167 } 168 169 /* keywords */ 170 %token <identifier> kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kFUNCTION kIF kFINALLY kFOR kGET kIN kSET 171 %token <identifier> kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH 172 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL 173 174 %token <srcptr> '}' 175 176 /* tokens */ 177 %token <identifier> tIdentifier 178 %token <ival> tAssignOper tEqOper tShiftOper tRelOper 179 %token <literal> tNumericLiteral tBooleanLiteral 180 %token <str> tStringLiteral 181 %token tEOF 182 183 %type <source_elements> SourceElements 184 %type <source_elements> FunctionBody 185 %type <statement> Statement 186 %type <statement> Block 187 %type <statement> VariableStatement 188 %type <statement> EmptyStatement 189 %type <statement> ExpressionStatement 190 %type <statement> IfStatement 191 %type <statement> IterationStatement 192 %type <statement> ContinueStatement 193 %type <statement> BreakStatement 194 %type <statement> ReturnStatement 195 %type <statement> WithStatement 196 %type <statement> LabelledStatement 197 %type <statement> SwitchStatement 198 %type <statement> ThrowStatement 199 %type <statement> TryStatement 200 %type <statement> Finally 201 %type <statement_list> StatementList StatementList_opt 202 %type <parameter_list> FormalParameterList FormalParameterList_opt 203 %type <expr> Expression Expression_opt Expression_err 204 %type <expr> ExpressionNoIn ExpressionNoIn_opt 205 %type <expr> FunctionExpression 206 %type <expr> AssignmentExpression AssignmentExpressionNoIn 207 %type <expr> ConditionalExpression ConditionalExpressionNoIn 208 %type <expr> LeftHandSideExpression 209 %type <expr> LogicalORExpression LogicalORExpressionNoIn 210 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn 211 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn 212 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn 213 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn 214 %type <expr> EqualityExpression EqualityExpressionNoIn 215 %type <expr> RelationalExpression RelationalExpressionNoIn 216 %type <expr> ShiftExpression 217 %type <expr> AdditiveExpression 218 %type <expr> MultiplicativeExpression 219 %type <expr> Initialiser_opt Initialiser 220 %type <expr> InitialiserNoIn_opt InitialiserNoIn 221 %type <expr> UnaryExpression 222 %type <expr> PostfixExpression 223 %type <expr> NewExpression 224 %type <expr> CallExpression 225 %type <expr> MemberExpression 226 %type <expr> PrimaryExpression 227 %type <expr> GetterSetterMethod 228 %type <identifier> Identifier_opt 229 %type <variable_list> VariableDeclarationList 230 %type <variable_list> VariableDeclarationListNoIn 231 %type <variable_declaration> VariableDeclaration 232 %type <variable_declaration> VariableDeclarationNoIn 233 %type <case_list> CaseClausules CaseClausules_opt 234 %type <case_clausule> CaseClausule DefaultClausule CaseBlock 235 %type <catch_block> Catch 236 %type <argument_list> Arguments 237 %type <argument_list> ArgumentList 238 %type <literal> Literal 239 %type <expr> ArrayLiteral 240 %type <expr> ObjectLiteral 241 %type <ival> Elision Elision_opt 242 %type <element_list> ElementList 243 %type <property_list> PropertyNameAndValueList 244 %type <property_definition> PropertyDefinition 245 %type <literal> PropertyName 246 %type <literal> BooleanLiteral 247 %type <srcptr> KFunction left_bracket 248 %type <ival> AssignOper 249 %type <identifier> IdentifierName ReservedAsIdentifier 250 251 %nonassoc LOWER_THAN_ELSE 252 %nonassoc kELSE 253 254 %% 255 256 /* ECMA-262 3rd Edition 14 */ 257 Program 258 : SourceElements HtmlComment tEOF 259 { program_parsed(ctx, $1); } 260 261 HtmlComment 262 : tHTMLCOMMENT {} 263 | /* empty */ {} 264 265 /* ECMA-262 3rd Edition 14 */ 266 SourceElements 267 : /* empty */ { $$ = new_source_elements(ctx); } 268 | SourceElements Statement 269 { $$ = source_elements_add_statement($1, $2); } 270 271 /* ECMA-262 3rd Edition 13 */ 272 FunctionExpression 273 : KFunction left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}' 274 { $$ = new_function_expression(ctx, NULL, $3, $6, NULL, $1, $7-$1+1); } 275 | KFunction tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}' 276 { $$ = new_function_expression(ctx, $2, $4, $7, NULL, $1, $8-$1+1); } 277 | KFunction tIdentifier kDCOL tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}' 278 { $$ = new_function_expression(ctx, $4, $6, $9, $2, $1, $10-$1+1); } 279 280 KFunction 281 : kFUNCTION { $$ = ctx->ptr - 8; } 282 283 /* ECMA-262 3rd Edition 13 */ 284 FunctionBody 285 : SourceElements { $$ = $1; } 286 287 /* ECMA-262 3rd Edition 13 */ 288 FormalParameterList 289 : tIdentifier { $$ = new_parameter_list(ctx, $1); } 290 | FormalParameterList ',' tIdentifier 291 { $$ = parameter_list_add(ctx, $1, $3); } 292 293 /* ECMA-262 3rd Edition 13 */ 294 FormalParameterList_opt 295 : /* empty */ { $$ = NULL; } 296 | FormalParameterList { $$ = $1; } 297 298 /* ECMA-262 3rd Edition 12 */ 299 Statement 300 : Block { $$ = $1; } 301 | VariableStatement { $$ = $1; } 302 | EmptyStatement { $$ = $1; } 303 | FunctionExpression { $$ = new_expression_statement(ctx, $1); } 304 | ExpressionStatement { $$ = $1; } 305 | IfStatement { $$ = $1; } 306 | IterationStatement { $$ = $1; } 307 | ContinueStatement { $$ = $1; } 308 | BreakStatement { $$ = $1; } 309 | ReturnStatement { $$ = $1; } 310 | WithStatement { $$ = $1; } 311 | LabelledStatement { $$ = $1; } 312 | SwitchStatement { $$ = $1; } 313 | ThrowStatement { $$ = $1; } 314 | TryStatement { $$ = $1; } 315 316 /* ECMA-262 3rd Edition 12.2 */ 317 StatementList 318 : Statement { $$ = new_statement_list(ctx, $1); } 319 | StatementList Statement 320 { $$ = statement_list_add($1, $2); } 321 322 /* ECMA-262 3rd Edition 12.2 */ 323 StatementList_opt 324 : /* empty */ { $$ = NULL; } 325 | StatementList { $$ = $1; } 326 327 /* ECMA-262 3rd Edition 12.1 */ 328 Block 329 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); } 330 | '{' '}' { $$ = new_block_statement(ctx, NULL); } 331 332 /* ECMA-262 3rd Edition 12.2 */ 333 VariableStatement 334 : kVAR VariableDeclarationList semicolon_opt 335 { $$ = new_var_statement(ctx, $2); } 336 337 /* ECMA-262 3rd Edition 12.2 */ 338 VariableDeclarationList 339 : VariableDeclaration { $$ = new_variable_list(ctx, $1); } 340 | VariableDeclarationList ',' VariableDeclaration 341 { $$ = variable_list_add(ctx, $1, $3); } 342 343 /* ECMA-262 3rd Edition 12.2 */ 344 VariableDeclarationListNoIn 345 : VariableDeclarationNoIn 346 { $$ = new_variable_list(ctx, $1); } 347 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn 348 { $$ = variable_list_add(ctx, $1, $3); } 349 350 /* ECMA-262 3rd Edition 12.2 */ 351 VariableDeclaration 352 : tIdentifier Initialiser_opt 353 { $$ = new_variable_declaration(ctx, $1, $2); } 354 355 /* ECMA-262 3rd Edition 12.2 */ 356 VariableDeclarationNoIn 357 : tIdentifier InitialiserNoIn_opt 358 { $$ = new_variable_declaration(ctx, $1, $2); } 359 360 /* ECMA-262 3rd Edition 12.2 */ 361 Initialiser_opt 362 : /* empty */ { $$ = NULL; } 363 | Initialiser { $$ = $1; } 364 365 /* ECMA-262 3rd Edition 12.2 */ 366 Initialiser 367 : '=' AssignmentExpression 368 { $$ = $2; } 369 370 /* ECMA-262 3rd Edition 12.2 */ 371 InitialiserNoIn_opt 372 : /* empty */ { $$ = NULL; } 373 | InitialiserNoIn { $$ = $1; } 374 375 /* ECMA-262 3rd Edition 12.2 */ 376 InitialiserNoIn 377 : '=' AssignmentExpressionNoIn 378 { $$ = $2; } 379 380 /* ECMA-262 3rd Edition 12.3 */ 381 EmptyStatement 382 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0); } 383 384 /* ECMA-262 3rd Edition 12.4 */ 385 ExpressionStatement 386 : Expression semicolon_opt 387 { $$ = new_expression_statement(ctx, $1); } 388 389 /* ECMA-262 3rd Edition 12.5 */ 390 IfStatement 391 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement 392 { $$ = new_if_statement(ctx, $3, $5, $7); } 393 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE 394 { $$ = new_if_statement(ctx, $3, $5, NULL); } 395 396 /* ECMA-262 3rd Edition 12.6 */ 397 IterationStatement 398 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt 399 { $$ = new_while_statement(ctx, TRUE, $5, $2); } 400 | kWHILE left_bracket Expression_err right_bracket Statement 401 { $$ = new_while_statement(ctx, FALSE, $3, $5); } 402 | kFOR left_bracket ExpressionNoIn_opt 403 { if(!explicit_error(ctx, $3, ';')) YYABORT; } 404 semicolon Expression_opt 405 { if(!explicit_error(ctx, $6, ';')) YYABORT; } 406 semicolon Expression_opt right_bracket Statement 407 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); } 408 | kFOR left_bracket kVAR VariableDeclarationListNoIn 409 { if(!explicit_error(ctx, $4, ';')) YYABORT; } 410 semicolon Expression_opt 411 { if(!explicit_error(ctx, $7, ';')) YYABORT; } 412 semicolon Expression_opt right_bracket Statement 413 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); } 414 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement 415 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); } 416 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement 417 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); } 418 419 /* ECMA-262 3rd Edition 12.7 */ 420 ContinueStatement 421 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt 422 { $$ = new_continue_statement(ctx, $2); } 423 424 /* ECMA-262 3rd Edition 12.8 */ 425 BreakStatement 426 : kBREAK /* NONL */ Identifier_opt semicolon_opt 427 { $$ = new_break_statement(ctx, $2); } 428 429 /* ECMA-262 3rd Edition 12.9 */ 430 ReturnStatement 431 : kRETURN /* NONL */ Expression_opt semicolon_opt 432 { $$ = new_return_statement(ctx, $2); } 433 434 /* ECMA-262 3rd Edition 12.10 */ 435 WithStatement 436 : kWITH left_bracket Expression right_bracket Statement 437 { $$ = new_with_statement(ctx, $3, $5); } 438 439 /* ECMA-262 3rd Edition 12.12 */ 440 LabelledStatement 441 : tIdentifier ':' Statement 442 { $$ = new_labelled_statement(ctx, $1, $3); } 443 444 /* ECMA-262 3rd Edition 12.11 */ 445 SwitchStatement 446 : kSWITCH left_bracket Expression right_bracket CaseBlock 447 { $$ = new_switch_statement(ctx, $3, $5); } 448 449 /* ECMA-262 3rd Edition 12.11 */ 450 CaseBlock 451 : '{' CaseClausules_opt '}' 452 { $$ = new_case_block(ctx, $2, NULL, NULL); } 453 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}' 454 { $$ = new_case_block(ctx, $2, $3, $4); } 455 456 /* ECMA-262 3rd Edition 12.11 */ 457 CaseClausules_opt 458 : /* empty */ { $$ = NULL; } 459 | CaseClausules { $$ = $1; } 460 461 /* ECMA-262 3rd Edition 12.11 */ 462 CaseClausules 463 : CaseClausule { $$ = new_case_list(ctx, $1); } 464 | CaseClausules CaseClausule 465 { $$ = case_list_add(ctx, $1, $2); } 466 467 /* ECMA-262 3rd Edition 12.11 */ 468 CaseClausule 469 : kCASE Expression ':' StatementList_opt 470 { $$ = new_case_clausule(ctx, $2, $4); } 471 472 /* ECMA-262 3rd Edition 12.11 */ 473 DefaultClausule 474 : kDEFAULT ':' StatementList_opt 475 { $$ = new_case_clausule(ctx, NULL, $3); } 476 477 /* ECMA-262 3rd Edition 12.13 */ 478 ThrowStatement 479 : kTHROW /* NONL */ Expression semicolon_opt 480 { $$ = new_throw_statement(ctx, $2); } 481 482 /* ECMA-262 3rd Edition 12.14 */ 483 TryStatement 484 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); } 485 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); } 486 | kTRY Block Catch Finally 487 { $$ = new_try_statement(ctx, $2, $3, $4); } 488 489 /* ECMA-262 3rd Edition 12.14 */ 490 Catch 491 : kCATCH left_bracket tIdentifier right_bracket Block 492 { $$ = new_catch_block(ctx, $3, $5); } 493 494 /* ECMA-262 3rd Edition 12.14 */ 495 Finally 496 : kFINALLY Block { $$ = $2; } 497 498 /* ECMA-262 3rd Edition 11.14 */ 499 Expression_opt 500 : /* empty */ { $$ = NULL; } 501 | Expression { $$ = $1; } 502 503 Expression_err 504 : Expression { $$ = $1; } 505 | error { set_error(ctx, JS_E_SYNTAX); YYABORT; } 506 507 /* ECMA-262 3rd Edition 11.14 */ 508 Expression 509 : AssignmentExpression { $$ = $1; } 510 | Expression ',' AssignmentExpression 511 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); } 512 513 /* ECMA-262 3rd Edition 11.14 */ 514 ExpressionNoIn_opt 515 : /* empty */ { $$ = NULL; } 516 | ExpressionNoIn { $$ = $1; } 517 518 /* ECMA-262 3rd Edition 11.14 */ 519 ExpressionNoIn 520 : AssignmentExpressionNoIn 521 { $$ = $1; } 522 | ExpressionNoIn ',' AssignmentExpressionNoIn 523 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); } 524 525 AssignOper 526 : tAssignOper { $$ = $1; } 527 | kDIVEQ { $$ = EXPR_ASSIGNDIV; } 528 529 /* ECMA-262 3rd Edition 11.13 */ 530 AssignmentExpression 531 : ConditionalExpression { $$ = $1; } 532 | LeftHandSideExpression '=' AssignmentExpression 533 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); } 534 | LeftHandSideExpression AssignOper AssignmentExpression 535 { $$ = new_binary_expression(ctx, $2, $1, $3); } 536 537 /* ECMA-262 3rd Edition 11.13 */ 538 AssignmentExpressionNoIn 539 : ConditionalExpressionNoIn 540 { $$ = $1; } 541 | LeftHandSideExpression '=' AssignmentExpressionNoIn 542 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); } 543 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn 544 { $$ = new_binary_expression(ctx, $2, $1, $3); } 545 546 /* ECMA-262 3rd Edition 11.12 */ 547 ConditionalExpression 548 : LogicalORExpression { $$ = $1; } 549 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression 550 { $$ = new_conditional_expression(ctx, $1, $3, $5); } 551 552 /* ECMA-262 3rd Edition 11.12 */ 553 ConditionalExpressionNoIn 554 : LogicalORExpressionNoIn 555 { $$ = $1; } 556 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn 557 { $$ = new_conditional_expression(ctx, $1, $3, $5); } 558 559 /* ECMA-262 3rd Edition 11.11 */ 560 LogicalORExpression 561 : LogicalANDExpression { $$ = $1; } 562 | LogicalORExpression tOROR LogicalANDExpression 563 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); } 564 565 /* ECMA-262 3rd Edition 11.11 */ 566 LogicalORExpressionNoIn 567 : LogicalANDExpressionNoIn 568 { $$ = $1; } 569 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn 570 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); } 571 572 /* ECMA-262 3rd Edition 11.11 */ 573 LogicalANDExpression 574 : BitwiseORExpression { $$ = $1; } 575 | LogicalANDExpression tANDAND BitwiseORExpression 576 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); } 577 578 /* ECMA-262 3rd Edition 11.11 */ 579 LogicalANDExpressionNoIn 580 : BitwiseORExpressionNoIn 581 { $$ = $1; } 582 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn 583 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); } 584 585 /* ECMA-262 3rd Edition 11.10 */ 586 BitwiseORExpression 587 : BitwiseXORExpression { $$ = $1; } 588 | BitwiseORExpression '|' BitwiseXORExpression 589 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); } 590 591 /* ECMA-262 3rd Edition 11.10 */ 592 BitwiseORExpressionNoIn 593 : BitwiseXORExpressionNoIn 594 { $$ = $1; } 595 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn 596 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); } 597 598 /* ECMA-262 3rd Edition 11.10 */ 599 BitwiseXORExpression 600 : BitwiseANDExpression { $$ = $1; } 601 | BitwiseXORExpression '^' BitwiseANDExpression 602 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); } 603 604 /* ECMA-262 3rd Edition 11.10 */ 605 BitwiseXORExpressionNoIn 606 : BitwiseANDExpressionNoIn 607 { $$ = $1; } 608 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn 609 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); } 610 611 /* ECMA-262 3rd Edition 11.10 */ 612 BitwiseANDExpression 613 : EqualityExpression { $$ = $1; } 614 | BitwiseANDExpression '&' EqualityExpression 615 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); } 616 617 /* ECMA-262 3rd Edition 11.10 */ 618 BitwiseANDExpressionNoIn 619 : EqualityExpressionNoIn 620 { $$ = $1; } 621 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn 622 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); } 623 624 /* ECMA-262 3rd Edition 11.9 */ 625 EqualityExpression 626 : RelationalExpression { $$ = $1; } 627 | EqualityExpression tEqOper RelationalExpression 628 { $$ = new_binary_expression(ctx, $2, $1, $3); } 629 630 /* ECMA-262 3rd Edition 11.9 */ 631 EqualityExpressionNoIn 632 : RelationalExpressionNoIn { $$ = $1; } 633 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn 634 { $$ = new_binary_expression(ctx, $2, $1, $3); } 635 636 /* ECMA-262 3rd Edition 11.8 */ 637 RelationalExpression 638 : ShiftExpression { $$ = $1; } 639 | RelationalExpression tRelOper ShiftExpression 640 { $$ = new_binary_expression(ctx, $2, $1, $3); } 641 | RelationalExpression kINSTANCEOF ShiftExpression 642 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); } 643 | RelationalExpression kIN ShiftExpression 644 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); } 645 646 /* ECMA-262 3rd Edition 11.8 */ 647 RelationalExpressionNoIn 648 : ShiftExpression { $$ = $1; } 649 | RelationalExpressionNoIn tRelOper ShiftExpression 650 { $$ = new_binary_expression(ctx, $2, $1, $3); } 651 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression 652 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); } 653 654 /* ECMA-262 3rd Edition 11.7 */ 655 ShiftExpression 656 : AdditiveExpression { $$ = $1; } 657 | ShiftExpression tShiftOper AdditiveExpression 658 { $$ = new_binary_expression(ctx, $2, $1, $3); } 659 660 /* ECMA-262 3rd Edition 11.6 */ 661 AdditiveExpression 662 : MultiplicativeExpression 663 { $$ = $1; } 664 | AdditiveExpression '+' MultiplicativeExpression 665 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); } 666 | AdditiveExpression '-' MultiplicativeExpression 667 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); } 668 669 /* ECMA-262 3rd Edition 11.5 */ 670 MultiplicativeExpression 671 : UnaryExpression { $$ = $1; } 672 | MultiplicativeExpression '*' UnaryExpression 673 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); } 674 | MultiplicativeExpression '/' UnaryExpression 675 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); } 676 | MultiplicativeExpression '%' UnaryExpression 677 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); } 678 679 /* ECMA-262 3rd Edition 11.4 */ 680 UnaryExpression 681 : PostfixExpression { $$ = $1; } 682 | kDELETE UnaryExpression 683 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); } 684 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); } 685 | kTYPEOF UnaryExpression 686 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); } 687 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); } 688 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); } 689 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); } 690 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); } 691 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); } 692 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); } 693 694 /* ECMA-262 3rd Edition 11.2 */ 695 PostfixExpression 696 : LeftHandSideExpression 697 { $$ = $1; } 698 | LeftHandSideExpression /* NONL */ tINC 699 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); } 700 | LeftHandSideExpression /* NONL */ tDEC 701 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); } 702 703 704 /* ECMA-262 3rd Edition 11.2 */ 705 LeftHandSideExpression 706 : NewExpression { $$ = $1; } 707 | CallExpression { $$ = $1; } 708 709 /* ECMA-262 3rd Edition 11.2 */ 710 NewExpression 711 : MemberExpression { $$ = $1; } 712 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); } 713 714 /* ECMA-262 3rd Edition 11.2 */ 715 MemberExpression 716 : PrimaryExpression { $$ = $1; } 717 | FunctionExpression { $$ = $1; } 718 | MemberExpression '[' Expression ']' 719 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); } 720 | MemberExpression '.' IdentifierName 721 { $$ = new_member_expression(ctx, $1, $3); } 722 | kNEW MemberExpression Arguments 723 { $$ = new_new_expression(ctx, $2, $3); } 724 725 /* ECMA-262 3rd Edition 11.2 */ 726 CallExpression 727 : MemberExpression Arguments 728 { $$ = new_call_expression(ctx, $1, $2); } 729 | CallExpression Arguments 730 { $$ = new_call_expression(ctx, $1, $2); } 731 | CallExpression '[' Expression ']' 732 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); } 733 | CallExpression '.' IdentifierName 734 { $$ = new_member_expression(ctx, $1, $3); } 735 736 /* ECMA-262 3rd Edition 11.2 */ 737 Arguments 738 : '(' ')' { $$ = NULL; } 739 | '(' ArgumentList ')' { $$ = $2; } 740 741 /* ECMA-262 3rd Edition 11.2 */ 742 ArgumentList 743 : AssignmentExpression { $$ = new_argument_list(ctx, $1); } 744 | ArgumentList ',' AssignmentExpression 745 { $$ = argument_list_add(ctx, $1, $3); } 746 747 /* ECMA-262 3rd Edition 11.1 */ 748 PrimaryExpression 749 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); } 750 | tIdentifier { $$ = new_identifier_expression(ctx, $1); } 751 | Literal { $$ = new_literal_expression(ctx, $1); } 752 | ArrayLiteral { $$ = $1; } 753 | ObjectLiteral { $$ = $1; } 754 | '(' Expression ')' { $$ = $2; } 755 756 /* ECMA-262 3rd Edition 11.1.4 */ 757 ArrayLiteral 758 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); } 759 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); } 760 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); } 761 | '[' ElementList ',' Elision_opt ']' 762 { $$ = new_array_literal_expression(ctx, $2, $4+1); } 763 764 /* ECMA-262 3rd Edition 11.1.4 */ 765 ElementList 766 : Elision_opt AssignmentExpression 767 { $$ = new_element_list(ctx, $1, $2); } 768 | ElementList ',' Elision_opt AssignmentExpression 769 { $$ = element_list_add(ctx, $1, $3, $4); } 770 771 /* ECMA-262 3rd Edition 11.1.4 */ 772 Elision 773 : ',' { $$ = 1; } 774 | Elision ',' { $$ = $1 + 1; } 775 776 /* ECMA-262 3rd Edition 11.1.4 */ 777 Elision_opt 778 : /* empty */ { $$ = 0; } 779 | Elision { $$ = $1; } 780 781 /* ECMA-262 3rd Edition 11.1.5 */ 782 ObjectLiteral 783 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); } 784 | '{' PropertyNameAndValueList '}' 785 { $$ = new_prop_and_value_expression(ctx, $2); } 786 | '{' PropertyNameAndValueList ',' '}' 787 { 788 if(ctx->script->version < 2) { 789 WARN("Trailing comma in object literal is illegal in legacy mode.\n"); 790 YYABORT; 791 } 792 $$ = new_prop_and_value_expression(ctx, $2); 793 } 794 795 /* ECMA-262 3rd Edition 11.1.5 */ 796 PropertyNameAndValueList 797 : PropertyDefinition { $$ = new_property_list(ctx, $1); } 798 | PropertyNameAndValueList ',' PropertyDefinition 799 { $$ = property_list_add(ctx, $1, $3); } 800 801 /* ECMA-262 5.1 Edition 12.2.6 */ 802 PropertyDefinition 803 : PropertyName ':' AssignmentExpression 804 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_VALUE, $1, $3); } 805 | kGET PropertyName GetterSetterMethod 806 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_GETTER, $2, $3); } 807 | kSET PropertyName GetterSetterMethod 808 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_SETTER, $2, $3); } 809 810 GetterSetterMethod 811 : left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}' 812 { $$ = new_function_expression(ctx, NULL, $2, $5, NULL, $1, $6-$1); } 813 814 /* Ecma-262 3rd Edition 11.1.5 */ 815 PropertyName 816 : IdentifierName { $$ = new_string_literal(ctx, compiler_alloc_string_len(ctx->compiler, $1, lstrlenW($1))); } 817 | tStringLiteral { $$ = new_string_literal(ctx, $1); } 818 | tNumericLiteral { $$ = $1; } 819 820 /* ECMA-262 3rd Edition 7.6 */ 821 Identifier_opt 822 : /* empty*/ { $$ = NULL; } 823 | tIdentifier { $$ = $1; } 824 825 /* ECMA-262 5.1 Edition 7.6 */ 826 IdentifierName 827 : tIdentifier { $$ = $1; } 828 | ReservedAsIdentifier 829 { 830 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) { 831 WARN("%s keyword used as an identifier in legacy mode.\n", 832 debugstr_w($1)); 833 YYABORT; 834 } 835 $$ = $1; 836 } 837 838 ReservedAsIdentifier 839 : kBREAK { $$ = $1; } 840 | kCASE { $$ = $1; } 841 | kCATCH { $$ = $1; } 842 | kCONTINUE { $$ = $1; } 843 | kDEFAULT { $$ = $1; } 844 | kDELETE { $$ = $1; } 845 | kDO { $$ = $1; } 846 | kELSE { $$ = $1; } 847 | kFALSE { $$ = $1; } 848 | kFINALLY { $$ = $1; } 849 | kFOR { $$ = $1; } 850 | kFUNCTION { $$ = $1; } 851 | kGET { $$ = $1; } 852 | kIF { $$ = $1; } 853 | kIN { $$ = $1; } 854 | kINSTANCEOF { $$ = $1; } 855 | kNEW { $$ = $1; } 856 | kNULL { $$ = $1; } 857 | kRETURN { $$ = $1; } 858 | kSET { $$ = $1; } 859 | kSWITCH { $$ = $1; } 860 | kTHIS { $$ = $1; } 861 | kTHROW { $$ = $1; } 862 | kTRUE { $$ = $1; } 863 | kTRY { $$ = $1; } 864 | kTYPEOF { $$ = $1; } 865 | kVAR { $$ = $1; } 866 | kVOID { $$ = $1; } 867 | kWHILE { $$ = $1; } 868 | kWITH { $$ = $1; } 869 870 /* ECMA-262 3rd Edition 7.8 */ 871 Literal 872 : kNULL { $$ = new_null_literal(ctx); } 873 | BooleanLiteral { $$ = $1; } 874 | tNumericLiteral { $$ = $1; } 875 | tStringLiteral { $$ = new_string_literal(ctx, $1); } 876 | '/' { $$ = parse_regexp(ctx); 877 if(!$$) YYABORT; } 878 | kDIVEQ { $$ = parse_regexp(ctx); 879 if(!$$) YYABORT; } 880 881 /* ECMA-262 3rd Edition 7.8.2 */ 882 BooleanLiteral 883 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); } 884 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); } 885 | tBooleanLiteral { $$ = $1; } 886 887 semicolon_opt 888 : ';' 889 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} } 890 891 left_bracket 892 : '(' { $$ = ctx->ptr; } 893 | error { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; } 894 895 right_bracket 896 : ')' 897 | error { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; } 898 899 semicolon 900 : ';' 901 | error { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; } 902 903 %% 904 905 static BOOL allow_auto_semicolon(parser_ctx_t *ctx) 906 { 907 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}'; 908 } 909 910 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size) 911 { 912 statement_t *stat; 913 914 stat = parser_alloc(ctx, size ? size : sizeof(*stat)); 915 if(!stat) 916 return NULL; 917 918 stat->type = type; 919 stat->next = NULL; 920 921 return stat; 922 } 923 924 static literal_t *new_string_literal(parser_ctx_t *ctx, jsstr_t *str) 925 { 926 literal_t *ret = parser_alloc(ctx, sizeof(literal_t)); 927 928 ret->type = LT_STRING; 929 ret->u.str = str; 930 931 return ret; 932 } 933 934 static literal_t *new_null_literal(parser_ctx_t *ctx) 935 { 936 literal_t *ret = parser_alloc(ctx, sizeof(literal_t)); 937 938 ret->type = LT_NULL; 939 940 return ret; 941 } 942 943 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t type, 944 literal_t *name, expression_t *value) 945 { 946 property_definition_t *ret = parser_alloc(ctx, sizeof(property_definition_t)); 947 948 ret->type = type; 949 ret->name = name; 950 ret->value = value; 951 ret->next = NULL; 952 953 return ret; 954 } 955 956 static property_list_t *new_property_list(parser_ctx_t *ctx, property_definition_t *prop) 957 { 958 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t)); 959 ret->head = ret->tail = prop; 960 return ret; 961 } 962 963 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, property_definition_t *prop) 964 { 965 list->tail = list->tail->next = prop; 966 return list; 967 } 968 969 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr) 970 { 971 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t)); 972 973 ret->elision = elision; 974 ret->expr = expr; 975 ret->next = NULL; 976 977 return ret; 978 } 979 980 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr) 981 { 982 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t)); 983 984 ret->head = ret->tail = new_array_element(ctx, elision, expr); 985 986 return ret; 987 } 988 989 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr) 990 { 991 list->tail = list->tail->next = new_array_element(ctx, elision, expr); 992 993 return list; 994 } 995 996 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr) 997 { 998 argument_t *ret = parser_alloc(ctx, sizeof(argument_t)); 999 1000 ret->expr = expr; 1001 ret->next = NULL; 1002 1003 return ret; 1004 } 1005 1006 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr) 1007 { 1008 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t)); 1009 1010 ret->head = ret->tail = new_argument(ctx, expr); 1011 1012 return ret; 1013 } 1014 1015 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr) 1016 { 1017 list->tail = list->tail->next = new_argument(ctx, expr); 1018 1019 return list; 1020 } 1021 1022 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement) 1023 { 1024 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t)); 1025 1026 ret->identifier = identifier; 1027 ret->statement = statement; 1028 1029 return ret; 1030 } 1031 1032 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list) 1033 { 1034 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t)); 1035 1036 ret->expr = expr; 1037 ret->stat = stat_list ? stat_list->head : NULL; 1038 ret->next = NULL; 1039 1040 return ret; 1041 } 1042 1043 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule) 1044 { 1045 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t)); 1046 1047 ret->head = ret->tail = case_clausule; 1048 1049 return ret; 1050 } 1051 1052 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule) 1053 { 1054 list->tail = list->tail->next = case_clausule; 1055 1056 return list; 1057 } 1058 1059 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1, 1060 case_clausule_t *default_clausule, case_list_t *case_list2) 1061 { 1062 case_clausule_t *ret = NULL, *iter = NULL, *iter2; 1063 statement_t *stat = NULL; 1064 1065 if(case_list1) { 1066 ret = case_list1->head; 1067 iter = case_list1->tail; 1068 } 1069 1070 if(default_clausule) { 1071 if(ret) 1072 iter = iter->next = default_clausule; 1073 else 1074 ret = iter = default_clausule; 1075 } 1076 1077 if(case_list2) { 1078 if(ret) 1079 iter->next = case_list2->head; 1080 else 1081 ret = case_list2->head; 1082 } 1083 1084 if(!ret) 1085 return NULL; 1086 1087 for(iter = ret; iter; iter = iter->next) { 1088 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next); 1089 if(!iter2) 1090 break; 1091 1092 while(iter != iter2) { 1093 iter->stat = iter2->stat; 1094 iter = iter->next; 1095 } 1096 1097 if(stat) { 1098 while(stat->next) 1099 stat = stat->next; 1100 stat->next = iter->stat; 1101 }else { 1102 stat = iter->stat; 1103 } 1104 } 1105 1106 return ret; 1107 } 1108 1109 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list) 1110 { 1111 block_statement_t *ret; 1112 1113 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret)); 1114 if(!ret) 1115 return NULL; 1116 1117 ret->stat_list = list ? list->head : NULL; 1118 1119 return &ret->stat; 1120 } 1121 1122 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr) 1123 { 1124 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t)); 1125 1126 ret->identifier = identifier; 1127 ret->expr = expr; 1128 ret->next = NULL; 1129 ret->global_next = NULL; 1130 1131 return ret; 1132 } 1133 1134 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl) 1135 { 1136 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t)); 1137 1138 ret->head = ret->tail = decl; 1139 1140 return ret; 1141 } 1142 1143 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl) 1144 { 1145 list->tail = list->tail->next = decl; 1146 1147 return list; 1148 } 1149 1150 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list) 1151 { 1152 var_statement_t *ret; 1153 1154 ret = new_statement(ctx, STAT_VAR, sizeof(*ret)); 1155 if(!ret) 1156 return NULL; 1157 1158 ret->variable_list = variable_list->head; 1159 1160 return &ret->stat; 1161 } 1162 1163 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr) 1164 { 1165 expression_statement_t *ret; 1166 1167 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret)); 1168 if(!ret) 1169 return NULL; 1170 1171 ret->expr = expr; 1172 1173 return &ret->stat; 1174 } 1175 1176 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat) 1177 { 1178 if_statement_t *ret; 1179 1180 ret = new_statement(ctx, STAT_IF, sizeof(*ret)); 1181 if(!ret) 1182 return NULL; 1183 1184 ret->expr = expr; 1185 ret->if_stat = if_stat; 1186 ret->else_stat = else_stat; 1187 1188 return &ret->stat; 1189 } 1190 1191 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat) 1192 { 1193 while_statement_t *ret; 1194 1195 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret)); 1196 if(!ret) 1197 return NULL; 1198 1199 ret->do_while = dowhile; 1200 ret->expr = expr; 1201 ret->statement = stat; 1202 1203 return &ret->stat; 1204 } 1205 1206 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr, 1207 expression_t *expr, expression_t *end_expr, statement_t *statement) 1208 { 1209 for_statement_t *ret; 1210 1211 ret = new_statement(ctx, STAT_FOR, sizeof(*ret)); 1212 if(!ret) 1213 return NULL; 1214 1215 ret->variable_list = variable_list ? variable_list->head : NULL; 1216 ret->begin_expr = begin_expr; 1217 ret->expr = expr; 1218 ret->end_expr = end_expr; 1219 ret->statement = statement; 1220 1221 return &ret->stat; 1222 } 1223 1224 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr, 1225 expression_t *in_expr, statement_t *statement) 1226 { 1227 forin_statement_t *ret; 1228 1229 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret)); 1230 if(!ret) 1231 return NULL; 1232 1233 ret->variable = variable; 1234 ret->expr = expr; 1235 ret->in_expr = in_expr; 1236 ret->statement = statement; 1237 1238 return &ret->stat; 1239 } 1240 1241 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier) 1242 { 1243 branch_statement_t *ret; 1244 1245 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret)); 1246 if(!ret) 1247 return NULL; 1248 1249 ret->identifier = identifier; 1250 1251 return &ret->stat; 1252 } 1253 1254 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier) 1255 { 1256 branch_statement_t *ret; 1257 1258 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret)); 1259 if(!ret) 1260 return NULL; 1261 1262 ret->identifier = identifier; 1263 1264 return &ret->stat; 1265 } 1266 1267 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr) 1268 { 1269 expression_statement_t *ret; 1270 1271 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret)); 1272 if(!ret) 1273 return NULL; 1274 1275 ret->expr = expr; 1276 1277 return &ret->stat; 1278 } 1279 1280 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement) 1281 { 1282 with_statement_t *ret; 1283 1284 ret = new_statement(ctx, STAT_WITH, sizeof(*ret)); 1285 if(!ret) 1286 return NULL; 1287 1288 ret->expr = expr; 1289 ret->statement = statement; 1290 1291 return &ret->stat; 1292 } 1293 1294 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement) 1295 { 1296 labelled_statement_t *ret; 1297 1298 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret)); 1299 if(!ret) 1300 return NULL; 1301 1302 ret->identifier = identifier; 1303 ret->statement = statement; 1304 1305 return &ret->stat; 1306 } 1307 1308 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list) 1309 { 1310 switch_statement_t *ret; 1311 1312 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret)); 1313 if(!ret) 1314 return NULL; 1315 1316 ret->expr = expr; 1317 ret->case_list = case_list; 1318 1319 return &ret->stat; 1320 } 1321 1322 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr) 1323 { 1324 expression_statement_t *ret; 1325 1326 ret = new_statement(ctx, STAT_THROW, sizeof(*ret)); 1327 if(!ret) 1328 return NULL; 1329 1330 ret->expr = expr; 1331 1332 return &ret->stat; 1333 } 1334 1335 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement, 1336 catch_block_t *catch_block, statement_t *finally_statement) 1337 { 1338 try_statement_t *ret; 1339 1340 ret = new_statement(ctx, STAT_TRY, sizeof(*ret)); 1341 if(!ret) 1342 return NULL; 1343 1344 ret->try_statement = try_statement; 1345 ret->catch_block = catch_block; 1346 ret->finally_statement = finally_statement; 1347 1348 return &ret->stat; 1349 } 1350 1351 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier) 1352 { 1353 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t)); 1354 1355 ret->identifier = identifier; 1356 ret->next = NULL; 1357 1358 return ret; 1359 } 1360 1361 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier) 1362 { 1363 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t)); 1364 1365 ret->head = ret->tail = new_parameter(ctx, identifier); 1366 1367 return ret; 1368 } 1369 1370 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier) 1371 { 1372 list->tail = list->tail->next = new_parameter(ctx, identifier); 1373 1374 return list; 1375 } 1376 1377 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier, parameter_list_t *parameter_list, 1378 source_elements_t *source_elements, const WCHAR *event_target, const WCHAR *src_str, DWORD src_len) 1379 { 1380 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret)); 1381 1382 ret->identifier = identifier; 1383 ret->parameter_list = parameter_list ? parameter_list->head : NULL; 1384 ret->source_elements = source_elements; 1385 ret->event_target = event_target; 1386 ret->src_str = src_str; 1387 ret->src_len = src_len; 1388 ret->next = NULL; 1389 1390 return &ret->expr; 1391 } 1392 1393 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size) 1394 { 1395 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret)); 1396 1397 ret->type = type; 1398 1399 return ret; 1400 } 1401 1402 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, 1403 expression_t *expression1, expression_t *expression2) 1404 { 1405 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret)); 1406 1407 ret->expression1 = expression1; 1408 ret->expression2 = expression2; 1409 1410 return &ret->expr; 1411 } 1412 1413 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression) 1414 { 1415 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret)); 1416 1417 ret->expression = expression; 1418 1419 return &ret->expr; 1420 } 1421 1422 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression, 1423 expression_t *true_expression, expression_t *false_expression) 1424 { 1425 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret)); 1426 1427 ret->expression = expression; 1428 ret->true_expression = true_expression; 1429 ret->false_expression = false_expression; 1430 1431 return &ret->expr; 1432 } 1433 1434 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier) 1435 { 1436 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret)); 1437 1438 ret->expression = expression; 1439 ret->identifier = identifier; 1440 1441 return &ret->expr; 1442 } 1443 1444 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list) 1445 { 1446 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret)); 1447 1448 ret->expression = expression; 1449 ret->argument_list = argument_list ? argument_list->head : NULL; 1450 1451 return &ret->expr; 1452 } 1453 1454 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list) 1455 { 1456 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret)); 1457 1458 ret->expression = expression; 1459 ret->argument_list = argument_list ? argument_list->head : NULL; 1460 1461 return &ret->expr; 1462 } 1463 1464 static int parser_error(parser_ctx_t *ctx, const char *str) 1465 { 1466 return 0; 1467 } 1468 1469 static void set_error(parser_ctx_t *ctx, UINT error) 1470 { 1471 ctx->hres = error; 1472 } 1473 1474 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next) 1475 { 1476 if(obj || *(ctx->ptr-1)==next) return TRUE; 1477 1478 set_error(ctx, JS_E_SYNTAX); 1479 return FALSE; 1480 } 1481 1482 1483 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier) 1484 { 1485 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret)); 1486 1487 ret->identifier = identifier; 1488 1489 return &ret->expr; 1490 } 1491 1492 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length) 1493 { 1494 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret)); 1495 1496 ret->element_list = element_list ? element_list->head : NULL; 1497 ret->length = length; 1498 1499 return &ret->expr; 1500 } 1501 1502 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list) 1503 { 1504 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret)); 1505 1506 ret->property_list = property_list ? property_list->head : NULL; 1507 1508 return &ret->expr; 1509 } 1510 1511 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal) 1512 { 1513 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret)); 1514 1515 ret->literal = literal; 1516 1517 return &ret->expr; 1518 } 1519 1520 static source_elements_t *new_source_elements(parser_ctx_t *ctx) 1521 { 1522 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t)); 1523 1524 memset(ret, 0, sizeof(*ret)); 1525 1526 return ret; 1527 } 1528 1529 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement) 1530 { 1531 if(source_elements->statement_tail) 1532 source_elements->statement_tail = source_elements->statement_tail->next = statement; 1533 else 1534 source_elements->statement = source_elements->statement_tail = statement; 1535 1536 return source_elements; 1537 } 1538 1539 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement) 1540 { 1541 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t)); 1542 1543 ret->head = ret->tail = statement; 1544 1545 return ret; 1546 } 1547 1548 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement) 1549 { 1550 list->tail = list->tail->next = statement; 1551 1552 return list; 1553 } 1554 1555 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source) 1556 { 1557 ctx->source = source; 1558 if(!ctx->lexer_error) 1559 ctx->hres = S_OK; 1560 } 1561 1562 void parser_release(parser_ctx_t *ctx) 1563 { 1564 script_release(ctx->script); 1565 heap_pool_free(&ctx->heap); 1566 heap_free(ctx); 1567 } 1568 1569 HRESULT script_parse(script_ctx_t *ctx, struct _compiler_ctx_t *compiler, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval, 1570 parser_ctx_t **ret) 1571 { 1572 parser_ctx_t *parser_ctx; 1573 heap_pool_t *mark; 1574 HRESULT hres; 1575 1576 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0}; 1577 1578 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t)); 1579 if(!parser_ctx) 1580 return E_OUTOFMEMORY; 1581 1582 parser_ctx->hres = JS_E_SYNTAX; 1583 parser_ctx->is_html = delimiter && !wcsicmp(delimiter, html_tagW); 1584 1585 parser_ctx->begin = parser_ctx->ptr = code; 1586 parser_ctx->end = parser_ctx->begin + lstrlenW(parser_ctx->begin); 1587 1588 script_addref(ctx); 1589 parser_ctx->script = ctx; 1590 1591 mark = heap_pool_mark(&ctx->tmp_heap); 1592 heap_pool_init(&parser_ctx->heap); 1593 1594 parser_ctx->compiler = compiler; 1595 parser_parse(parser_ctx); 1596 parser_ctx->compiler = NULL; 1597 1598 heap_pool_clear(mark); 1599 hres = parser_ctx->hres; 1600 if(FAILED(hres)) { 1601 WARN("parser failed around %s\n", 1602 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20)); 1603 parser_release(parser_ctx); 1604 return hres; 1605 } 1606 1607 *ret = parser_ctx; 1608 return S_OK; 1609 } 1610