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