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