user32/tests: Fix test_listbox_messages() message sequences to support WinEvents.
[wine.git] / dlls / jscript / parser.y
blob0ebdc2b932b404a23233530262bad6f45c1501be
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
21 #include "jscript.h"
22 #include "engine.h"
23 #include "parser.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 static int parser_error(unsigned*,parser_ctx_t*,const char*);
30 static void set_error(parser_ctx_t*,unsigned,HRESULT);
31 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
32 static BOOL allow_auto_semicolon(parser_ctx_t*);
34 static literal_t *new_string_literal(parser_ctx_t*,jsstr_t*);
35 static literal_t *new_null_literal(parser_ctx_t*);
37 typedef struct _property_list_t {
38 property_definition_t *head;
39 property_definition_t *tail;
40 } property_list_t;
42 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t,
43 literal_t *name, expression_t *value);
44 static property_list_t *new_property_list(parser_ctx_t*,property_definition_t*);
45 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,property_definition_t*);
47 typedef struct _element_list_t {
48 array_element_t *head;
49 array_element_t *tail;
50 } element_list_t;
52 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
53 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
55 typedef struct _argument_list_t {
56 argument_t *head;
57 argument_t *tail;
58 } argument_list_t;
60 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
61 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
63 typedef struct _case_list_t {
64 case_clausule_t *head;
65 case_clausule_t *tail;
66 } case_list_t;
68 typedef struct _statement_list_t {
69 statement_t *head;
70 statement_t *tail;
71 } statement_list_t;
73 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
74 static case_clausule_t *new_case_clausule(parser_ctx_t*,unsigned,expression_t*,statement_list_t*);
75 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
76 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
77 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
79 typedef struct _variable_list_t {
80 variable_declaration_t *head;
81 variable_declaration_t *tail;
82 } variable_list_t;
84 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
85 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
86 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
88 static void *new_statement(parser_ctx_t*,statement_type_t,size_t,unsigned);
89 static statement_t *new_block_statement(parser_ctx_t*,unsigned,statement_list_t*);
90 static statement_t *new_var_statement(parser_ctx_t*,BOOL,BOOL,unsigned,variable_list_t*);
91 static statement_t *new_expression_statement(parser_ctx_t*,unsigned,expression_t*);
92 static statement_t *new_if_statement(parser_ctx_t*,unsigned,expression_t*,statement_t*,statement_t*);
93 static statement_t *new_while_statement(parser_ctx_t*,unsigned,BOOL,expression_t*,statement_t*);
94 static statement_t *new_for_statement(parser_ctx_t*,unsigned,variable_declaration_t*,expression_t*,expression_t*,unsigned,
95 expression_t*,unsigned,statement_t*);
96 static statement_t *new_forin_statement(parser_ctx_t*,unsigned,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
97 static statement_t *new_continue_statement(parser_ctx_t*,unsigned,const WCHAR*);
98 static statement_t *new_break_statement(parser_ctx_t*,unsigned,const WCHAR*);
99 static statement_t *new_return_statement(parser_ctx_t*,unsigned,expression_t*);
100 static statement_t *new_with_statement(parser_ctx_t*,unsigned,expression_t*,statement_t*);
101 static statement_t *new_labelled_statement(parser_ctx_t*,unsigned,const WCHAR*,statement_t*);
102 static statement_t *new_switch_statement(parser_ctx_t*,unsigned,expression_t*,case_clausule_t*);
103 static statement_t *new_throw_statement(parser_ctx_t*,unsigned,expression_t*);
104 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*,unsigned);
106 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
107 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
109 typedef struct _parameter_list_t {
110 parameter_t *head;
111 parameter_t *tail;
112 } parameter_list_t;
114 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
115 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
117 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
118 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
119 statement_list_t*,const WCHAR*,const WCHAR*,DWORD);
120 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
121 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
122 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
123 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
124 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
125 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
126 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
127 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
128 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
129 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
131 #define YYLTYPE unsigned
132 #define YYLLOC_DEFAULT(Cur, Rhs, N) Cur = YYRHSLOC((Rhs), (N) ? 1 : 0)
136 %lex-param { parser_ctx_t *ctx }
137 %parse-param { parser_ctx_t *ctx }
138 %define api.pure
139 %start Script
141 %union {
142 int ival;
143 jsstr_t *str;
144 literal_t *literal;
145 struct _argument_list_t *argument_list;
146 case_clausule_t *case_clausule;
147 struct _case_list_t *case_list;
148 catch_block_t *catch_block;
149 struct _element_list_t *element_list;
150 expression_t *expr;
151 const WCHAR *identifier;
152 struct _parameter_list_t *parameter_list;
153 struct _property_list_t *property_list;
154 property_definition_t *property_definition;
155 statement_t *statement;
156 struct _statement_list_t *statement_list;
157 struct _variable_list_t *variable_list;
158 variable_declaration_t *variable_declaration;
161 /* keywords */
162 %token <identifier> kBREAK kCASE kCATCH kCONST kCONTINUE kDEFAULT kDELETE kDO kELSE kFUNCTION kIF kFINALLY kFOR
163 %token <identifier> kGET kIN kLET kSET kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE
164 %token <identifier> kTRY kTYPEOF kVAR kVOID kWHILE kWITH
165 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL
167 /* tokens */
168 %token <identifier> tIdentifier
169 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
170 %token <literal> tNumericLiteral tBooleanLiteral
171 %token <str> tStringLiteral
173 %type <statement_list> FunctionBody
174 %type <statement_list> ScriptBody
175 %type <statement_list> FunctionStatementList
176 %type <statement> Statement
177 %type <statement> Declaration
178 %type <statement> Block
179 %type <statement> LexicalDeclaration
180 %type <statement> LexicalDeclarationNoIn
181 %type <statement> VariableStatement
182 %type <statement> EmptyStatement
183 %type <statement> ExpressionStatement
184 %type <statement> IfStatement
185 %type <statement> IterationStatement
186 %type <statement> ContinueStatement
187 %type <statement> BreakStatement
188 %type <statement> ReturnStatement
189 %type <statement> WithStatement
190 %type <statement> LabelledStatement
191 %type <statement> SwitchStatement
192 %type <statement> ThrowStatement
193 %type <statement> TryStatement
194 %type <statement> Finally
195 %type <statement> StatementListItem
196 %type <statement_list> StatementList StatementList_opt
197 %type <parameter_list> FormalParameterList FormalParameterList_opt
198 %type <expr> Expression Expression_opt Expression_err
199 %type <expr> ExpressionNoIn ExpressionNoIn_opt
200 %type <expr> FunctionExpression
201 %type <expr> AssignmentExpression AssignmentExpressionNoIn
202 %type <expr> ConditionalExpression ConditionalExpressionNoIn
203 %type <expr> LeftHandSideExpression
204 %type <expr> LogicalORExpression LogicalORExpressionNoIn
205 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
206 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
207 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
208 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
209 %type <expr> EqualityExpression EqualityExpressionNoIn
210 %type <expr> RelationalExpression RelationalExpressionNoIn
211 %type <expr> ShiftExpression
212 %type <expr> AdditiveExpression
213 %type <expr> MultiplicativeExpression
214 %type <expr> Initialiser_opt Initialiser
215 %type <expr> InitialiserNoIn_opt InitialiserNoIn
216 %type <expr> UnaryExpression
217 %type <expr> PostfixExpression
218 %type <expr> NewExpression
219 %type <expr> CallExpression
220 %type <expr> MemberExpression
221 %type <expr> PrimaryExpression
222 %type <expr> GetterSetterMethod
223 %type <identifier> Identifier_opt
224 %type <variable_list> VariableDeclarationList
225 %type <variable_list> VariableDeclarationListNoIn
226 %type <variable_declaration> VariableDeclaration
227 %type <variable_declaration> VariableDeclarationNoIn
228 %type <case_list> CaseClausules CaseClausules_opt
229 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
230 %type <catch_block> Catch
231 %type <argument_list> Arguments
232 %type <argument_list> ArgumentList
233 %type <literal> Literal
234 %type <expr> ArrayLiteral
235 %type <expr> ObjectLiteral
236 %type <ival> Elision Elision_opt
237 %type <element_list> ElementList
238 %type <property_list> PropertyNameAndValueList
239 %type <property_definition> PropertyDefinition
240 %type <literal> PropertyName
241 %type <literal> BooleanLiteral
242 %type <ival> AssignOper
243 %type <identifier> IdentifierName ReservedAsIdentifier
245 %nonassoc LOWER_THAN_ELSE
246 %nonassoc kELSE
250 /* ECMA-262 10th Edition 15.1 */
251 Script
252 : ScriptBody HtmlComment { ctx->source = $1 ? $1->head : NULL; }
254 /* ECMA-262 10th Edition 15.1 */
255 ScriptBody
256 : StatementList_opt { $$ = $1; }
258 HtmlComment
259 : tHTMLCOMMENT
260 | /* empty */
262 /* ECMA-262 10th Edition 14.1 */
263 FunctionStatementList
264 : StatementList_opt { $$ = $1; }
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, ctx->begin + @1, @7 - @1 + 1); }
270 | kFUNCTION tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
271 { $$ = new_function_expression(ctx, $2, $4, $7, NULL, ctx->begin + @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, ctx->begin + @1, @10 - @1 + 1); }
275 /* ECMA-262 10th Edition 14.1 */
276 FunctionBody
277 : FunctionStatementList { $$ = $1; }
279 /* ECMA-262 3rd Edition 13 */
280 FormalParameterList
281 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
282 | FormalParameterList ',' tIdentifier
283 { $$ = parameter_list_add(ctx, $1, $3); }
285 /* ECMA-262 3rd Edition 13 */
286 FormalParameterList_opt
287 : /* empty */ { $$ = NULL; }
288 | FormalParameterList { $$ = $1; }
290 /* ECMA-262 3rd Edition 12 */
291 Statement
292 : Block { $$ = $1; }
293 | VariableStatement { $$ = $1; }
294 | EmptyStatement { $$ = $1; }
295 | FunctionExpression { $$ = new_expression_statement(ctx, @$, $1); }
296 | ExpressionStatement { $$ = $1; }
297 | IfStatement { $$ = $1; }
298 | IterationStatement { $$ = $1; }
299 | ContinueStatement { $$ = $1; }
300 | BreakStatement { $$ = $1; }
301 | ReturnStatement { $$ = $1; }
302 | WithStatement { $$ = $1; }
303 | LabelledStatement { $$ = $1; }
304 | SwitchStatement { $$ = $1; }
305 | ThrowStatement { $$ = $1; }
306 | TryStatement { $$ = $1; }
308 /* ECMA-262 10th Edition 13. TODO: HoistableDeclaration */
309 Declaration
310 : LexicalDeclaration { $$ = $1; }
312 /* ECMA-262 10th Edition 13.2 */
313 StatementListItem
314 : Statement { $$ = $1; }
315 | Declaration { $$ = $1; }
317 /* ECMA-262 10th Edition 13.2 */
318 StatementList
319 : StatementListItem { $$ = new_statement_list(ctx, $1); }
320 | StatementList StatementListItem
321 { $$ = statement_list_add($1, $2); }
323 /* ECMA-262 3rd Edition 12.2 */
324 StatementList_opt
325 : /* empty */ { $$ = NULL; }
326 | StatementList { $$ = $1; }
328 /* ECMA-262 3rd Edition 12.1 */
329 Block
330 : '{' StatementList '}' { $$ = new_block_statement(ctx, @2, $2); }
331 | '{' '}' { $$ = new_block_statement(ctx, @$, NULL); }
333 /* ECMA-262 10th Edition 13.3.1, TODO: BindingList*/
334 LexicalDeclaration
335 : kLET VariableDeclarationList semicolon_opt
336 { $$ = new_var_statement(ctx, TRUE, FALSE, @$, $2); }
337 | kCONST VariableDeclarationList semicolon_opt
339 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
340 WARN("const var declaration %s in legacy mode.\n",
341 debugstr_w($1));
342 set_error(ctx, @$, JS_E_SYNTAX);
343 YYABORT;
345 $$ = new_var_statement(ctx, TRUE, TRUE, @$, $2);
348 /* ECMA-262 10th Edition 13.3.1, TODO: BindingList*/
349 LexicalDeclarationNoIn
350 : kLET VariableDeclarationListNoIn semicolon_opt
351 { $$ = new_var_statement(ctx, TRUE, FALSE, @$, $2); }
352 | kCONST VariableDeclarationListNoIn semicolon_opt
354 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
355 WARN("const var declaration %s in legacy mode.\n",
356 debugstr_w($1));
357 set_error(ctx, @$, JS_E_SYNTAX);
358 YYABORT;
360 $$ = new_var_statement(ctx, TRUE, TRUE, @$, $2);
363 /* ECMA-262 3rd Edition 12.2 */
364 VariableStatement
365 : kVAR VariableDeclarationList semicolon_opt
366 { $$ = new_var_statement(ctx, FALSE, FALSE, @$, $2); }
368 /* ECMA-262 3rd Edition 12.2 */
369 VariableDeclarationList
370 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
371 | VariableDeclarationList ',' VariableDeclaration
372 { $$ = variable_list_add(ctx, $1, $3); }
374 /* ECMA-262 3rd Edition 12.2 */
375 VariableDeclarationListNoIn
376 : VariableDeclarationNoIn
377 { $$ = new_variable_list(ctx, $1); }
378 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
379 { $$ = variable_list_add(ctx, $1, $3); }
381 /* ECMA-262 3rd Edition 12.2 */
382 VariableDeclaration
383 : tIdentifier Initialiser_opt
384 { $$ = new_variable_declaration(ctx, $1, $2); }
386 /* ECMA-262 3rd Edition 12.2 */
387 VariableDeclarationNoIn
388 : tIdentifier InitialiserNoIn_opt
389 { $$ = new_variable_declaration(ctx, $1, $2); }
391 /* ECMA-262 3rd Edition 12.2 */
392 Initialiser_opt
393 : /* empty */ { $$ = NULL; }
394 | Initialiser { $$ = $1; }
396 /* ECMA-262 3rd Edition 12.2 */
397 Initialiser
398 : '=' AssignmentExpression
399 { $$ = $2; }
401 /* ECMA-262 3rd Edition 12.2 */
402 InitialiserNoIn_opt
403 : /* empty */ { $$ = NULL; }
404 | InitialiserNoIn { $$ = $1; }
406 /* ECMA-262 3rd Edition 12.2 */
407 InitialiserNoIn
408 : '=' AssignmentExpressionNoIn
409 { $$ = $2; }
411 /* ECMA-262 3rd Edition 12.3 */
412 EmptyStatement
413 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0, @$); }
415 /* ECMA-262 3rd Edition 12.4 */
416 ExpressionStatement
417 : Expression semicolon_opt
418 { $$ = new_expression_statement(ctx, @$, $1); }
420 /* ECMA-262 3rd Edition 12.5 */
421 IfStatement
422 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
423 { $$ = new_if_statement(ctx, @$, $3, $5, $7); }
424 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
425 { $$ = new_if_statement(ctx, @$, $3, $5, NULL); }
427 /* ECMA-262 10th Edition 13.7 */
428 IterationStatement
429 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
430 { $$ = new_while_statement(ctx, @3, TRUE, $5, $2); }
431 | kWHILE left_bracket Expression_err right_bracket Statement
432 { $$ = new_while_statement(ctx, @$, FALSE, $3, $5); }
433 | kFOR left_bracket ExpressionNoIn_opt
434 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
435 semicolon Expression_opt
436 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
437 semicolon Expression_opt right_bracket Statement
438 { $$ = new_for_statement(ctx, @3, NULL, $3, $6, @6, $9, @9, $11); }
439 | kFOR left_bracket kVAR VariableDeclarationListNoIn
440 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
441 semicolon Expression_opt
442 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
443 semicolon Expression_opt right_bracket Statement
444 { $$ = new_for_statement(ctx, @3, $4 ? $4->head : NULL, NULL, $7, @7, $10, @10, $12); }
445 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
446 { $$ = new_forin_statement(ctx, @$, NULL, $3, $5, $7); }
447 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
448 { $$ = new_forin_statement(ctx, @$, $4, NULL, $6, $8); }
449 | kFOR left_bracket LexicalDeclarationNoIn
450 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
451 Expression_opt
452 { if(!explicit_error(ctx, $5, ';')) YYABORT; }
453 semicolon Expression_opt right_bracket Statement
454 { $$ = new_for_statement(ctx, @3, ((var_statement_t *)$3)->variable_list,
455 NULL, $5, @5, $8, @8, $10); }
457 /* ECMA-262 3rd Edition 12.7 */
458 ContinueStatement
459 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
460 { $$ = new_continue_statement(ctx, @$, $2); }
462 /* ECMA-262 3rd Edition 12.8 */
463 BreakStatement
464 : kBREAK /* NONL */ Identifier_opt semicolon_opt
465 { $$ = new_break_statement(ctx, @$, $2); }
467 /* ECMA-262 3rd Edition 12.9 */
468 ReturnStatement
469 : kRETURN /* NONL */ Expression_opt semicolon_opt
470 { $$ = new_return_statement(ctx, @$, $2); }
472 /* ECMA-262 3rd Edition 12.10 */
473 WithStatement
474 : kWITH left_bracket Expression right_bracket Statement
475 { $$ = new_with_statement(ctx, @$, $3, $5); }
477 /* ECMA-262 3rd Edition 12.12 */
478 LabelledStatement
479 : tIdentifier ':' Statement
480 { $$ = new_labelled_statement(ctx, @$, $1, $3); }
482 /* ECMA-262 3rd Edition 12.11 */
483 SwitchStatement
484 : kSWITCH left_bracket Expression right_bracket CaseBlock
485 { $$ = new_switch_statement(ctx, @$, $3, $5); }
487 /* ECMA-262 3rd Edition 12.11 */
488 CaseBlock
489 : '{' CaseClausules_opt '}'
490 { $$ = new_case_block(ctx, $2, NULL, NULL); }
491 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
492 { $$ = new_case_block(ctx, $2, $3, $4); }
494 /* ECMA-262 3rd Edition 12.11 */
495 CaseClausules_opt
496 : /* empty */ { $$ = NULL; }
497 | CaseClausules { $$ = $1; }
499 /* ECMA-262 3rd Edition 12.11 */
500 CaseClausules
501 : CaseClausule { $$ = new_case_list(ctx, $1); }
502 | CaseClausules CaseClausule
503 { $$ = case_list_add(ctx, $1, $2); }
505 /* ECMA-262 3rd Edition 12.11 */
506 CaseClausule
507 : kCASE Expression ':' StatementList_opt
508 { $$ = new_case_clausule(ctx, @$, $2, $4); }
510 /* ECMA-262 3rd Edition 12.11 */
511 DefaultClausule
512 : kDEFAULT ':' StatementList_opt
513 { $$ = new_case_clausule(ctx, @$, NULL, $3); }
515 /* ECMA-262 3rd Edition 12.13 */
516 ThrowStatement
517 : kTHROW /* NONL */ Expression semicolon_opt
518 { $$ = new_throw_statement(ctx, @$, $2); }
520 /* ECMA-262 3rd Edition 12.14 */
521 TryStatement
522 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL, 0); }
523 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3, @3); }
524 | kTRY Block Catch Finally
525 { $$ = new_try_statement(ctx, $2, $3, $4, @4); }
527 /* ECMA-262 3rd Edition 12.14 */
528 Catch
529 : kCATCH left_bracket tIdentifier right_bracket Block
530 { $$ = new_catch_block(ctx, $3, $5); }
532 /* ECMA-262 3rd Edition 12.14 */
533 Finally
534 : kFINALLY Block { @$ = @2; $$ = $2; }
536 /* ECMA-262 3rd Edition 11.14 */
537 Expression_opt
538 : /* empty */ { $$ = NULL; }
539 | Expression { $$ = $1; }
541 Expression_err
542 : Expression { $$ = $1; }
543 | error { set_error(ctx, @$, JS_E_SYNTAX); YYABORT; }
545 /* ECMA-262 3rd Edition 11.14 */
546 Expression
547 : AssignmentExpression { $$ = $1; }
548 | Expression ',' AssignmentExpression
549 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
551 /* ECMA-262 3rd Edition 11.14 */
552 ExpressionNoIn_opt
553 : /* empty */ { $$ = NULL; }
554 | ExpressionNoIn { $$ = $1; }
556 /* ECMA-262 3rd Edition 11.14 */
557 ExpressionNoIn
558 : AssignmentExpressionNoIn
559 { $$ = $1; }
560 | ExpressionNoIn ',' AssignmentExpressionNoIn
561 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
563 AssignOper
564 : tAssignOper { $$ = $1; }
565 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
567 /* ECMA-262 3rd Edition 11.13 */
568 AssignmentExpression
569 : ConditionalExpression { $$ = $1; }
570 | LeftHandSideExpression '=' AssignmentExpression
571 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
572 | LeftHandSideExpression AssignOper AssignmentExpression
573 { $$ = new_binary_expression(ctx, $2, $1, $3); }
575 /* ECMA-262 3rd Edition 11.13 */
576 AssignmentExpressionNoIn
577 : ConditionalExpressionNoIn
578 { $$ = $1; }
579 | LeftHandSideExpression '=' AssignmentExpressionNoIn
580 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
581 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
582 { $$ = new_binary_expression(ctx, $2, $1, $3); }
584 /* ECMA-262 3rd Edition 11.12 */
585 ConditionalExpression
586 : LogicalORExpression { $$ = $1; }
587 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
588 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
590 /* ECMA-262 3rd Edition 11.12 */
591 ConditionalExpressionNoIn
592 : LogicalORExpressionNoIn
593 { $$ = $1; }
594 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
595 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
597 /* ECMA-262 3rd Edition 11.11 */
598 LogicalORExpression
599 : LogicalANDExpression { $$ = $1; }
600 | LogicalORExpression tOROR LogicalANDExpression
601 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
603 /* ECMA-262 3rd Edition 11.11 */
604 LogicalORExpressionNoIn
605 : LogicalANDExpressionNoIn
606 { $$ = $1; }
607 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
608 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
610 /* ECMA-262 3rd Edition 11.11 */
611 LogicalANDExpression
612 : BitwiseORExpression { $$ = $1; }
613 | LogicalANDExpression tANDAND BitwiseORExpression
614 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
616 /* ECMA-262 3rd Edition 11.11 */
617 LogicalANDExpressionNoIn
618 : BitwiseORExpressionNoIn
619 { $$ = $1; }
620 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
621 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
623 /* ECMA-262 3rd Edition 11.10 */
624 BitwiseORExpression
625 : BitwiseXORExpression { $$ = $1; }
626 | BitwiseORExpression '|' BitwiseXORExpression
627 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
629 /* ECMA-262 3rd Edition 11.10 */
630 BitwiseORExpressionNoIn
631 : BitwiseXORExpressionNoIn
632 { $$ = $1; }
633 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
634 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
636 /* ECMA-262 3rd Edition 11.10 */
637 BitwiseXORExpression
638 : BitwiseANDExpression { $$ = $1; }
639 | BitwiseXORExpression '^' BitwiseANDExpression
640 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
642 /* ECMA-262 3rd Edition 11.10 */
643 BitwiseXORExpressionNoIn
644 : BitwiseANDExpressionNoIn
645 { $$ = $1; }
646 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
647 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
649 /* ECMA-262 3rd Edition 11.10 */
650 BitwiseANDExpression
651 : EqualityExpression { $$ = $1; }
652 | BitwiseANDExpression '&' EqualityExpression
653 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
655 /* ECMA-262 3rd Edition 11.10 */
656 BitwiseANDExpressionNoIn
657 : EqualityExpressionNoIn
658 { $$ = $1; }
659 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
660 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
662 /* ECMA-262 3rd Edition 11.9 */
663 EqualityExpression
664 : RelationalExpression { $$ = $1; }
665 | EqualityExpression tEqOper RelationalExpression
666 { $$ = new_binary_expression(ctx, $2, $1, $3); }
668 /* ECMA-262 3rd Edition 11.9 */
669 EqualityExpressionNoIn
670 : RelationalExpressionNoIn { $$ = $1; }
671 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
672 { $$ = new_binary_expression(ctx, $2, $1, $3); }
674 /* ECMA-262 3rd Edition 11.8 */
675 RelationalExpression
676 : ShiftExpression { $$ = $1; }
677 | RelationalExpression tRelOper ShiftExpression
678 { $$ = new_binary_expression(ctx, $2, $1, $3); }
679 | RelationalExpression kINSTANCEOF ShiftExpression
680 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
681 | RelationalExpression kIN ShiftExpression
682 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
684 /* ECMA-262 3rd Edition 11.8 */
685 RelationalExpressionNoIn
686 : ShiftExpression { $$ = $1; }
687 | RelationalExpressionNoIn tRelOper ShiftExpression
688 { $$ = new_binary_expression(ctx, $2, $1, $3); }
689 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
690 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
692 /* ECMA-262 3rd Edition 11.7 */
693 ShiftExpression
694 : AdditiveExpression { $$ = $1; }
695 | ShiftExpression tShiftOper AdditiveExpression
696 { $$ = new_binary_expression(ctx, $2, $1, $3); }
698 /* ECMA-262 3rd Edition 11.6 */
699 AdditiveExpression
700 : MultiplicativeExpression
701 { $$ = $1; }
702 | AdditiveExpression '+' MultiplicativeExpression
703 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
704 | AdditiveExpression '-' MultiplicativeExpression
705 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
707 /* ECMA-262 3rd Edition 11.5 */
708 MultiplicativeExpression
709 : UnaryExpression { $$ = $1; }
710 | MultiplicativeExpression '*' UnaryExpression
711 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
712 | MultiplicativeExpression '/' UnaryExpression
713 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
714 | MultiplicativeExpression '%' UnaryExpression
715 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
717 /* ECMA-262 3rd Edition 11.4 */
718 UnaryExpression
719 : PostfixExpression { $$ = $1; }
720 | kDELETE UnaryExpression
721 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
722 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
723 | kTYPEOF UnaryExpression
724 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
725 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
726 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
727 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
728 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
729 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
730 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
732 /* ECMA-262 3rd Edition 11.2 */
733 PostfixExpression
734 : LeftHandSideExpression
735 { $$ = $1; }
736 | LeftHandSideExpression /* NONL */ tINC
737 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
738 | LeftHandSideExpression /* NONL */ tDEC
739 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
742 /* ECMA-262 3rd Edition 11.2 */
743 LeftHandSideExpression
744 : NewExpression { $$ = $1; }
745 | CallExpression { $$ = $1; }
747 /* ECMA-262 3rd Edition 11.2 */
748 NewExpression
749 : MemberExpression { $$ = $1; }
750 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
752 /* ECMA-262 3rd Edition 11.2 */
753 MemberExpression
754 : PrimaryExpression { $$ = $1; }
755 | FunctionExpression { $$ = $1; }
756 | MemberExpression '[' Expression ']'
757 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
758 | MemberExpression '.' IdentifierName
759 { $$ = new_member_expression(ctx, $1, $3); }
760 | kNEW MemberExpression Arguments
761 { $$ = new_new_expression(ctx, $2, $3); }
763 /* ECMA-262 3rd Edition 11.2 */
764 CallExpression
765 : MemberExpression Arguments
766 { $$ = new_call_expression(ctx, $1, $2); }
767 | CallExpression Arguments
768 { $$ = new_call_expression(ctx, $1, $2); }
769 | CallExpression '[' Expression ']'
770 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
771 | CallExpression '.' IdentifierName
772 { $$ = new_member_expression(ctx, $1, $3); }
774 /* ECMA-262 3rd Edition 11.2 */
775 Arguments
776 : '(' ')' { $$ = NULL; }
777 | '(' ArgumentList ')' { $$ = $2; }
779 /* ECMA-262 3rd Edition 11.2 */
780 ArgumentList
781 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
782 | ArgumentList ',' AssignmentExpression
783 { $$ = argument_list_add(ctx, $1, $3); }
785 /* ECMA-262 3rd Edition 11.1 */
786 PrimaryExpression
787 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
788 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
789 | Literal { $$ = new_literal_expression(ctx, $1); }
790 | ArrayLiteral { $$ = $1; }
791 | ObjectLiteral { $$ = $1; }
792 | '(' Expression ')' { $$ = $2; }
794 /* ECMA-262 3rd Edition 11.1.4 */
795 ArrayLiteral
796 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
797 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
798 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
799 | '[' ElementList ',' Elision_opt ']'
800 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
802 /* ECMA-262 3rd Edition 11.1.4 */
803 ElementList
804 : Elision_opt AssignmentExpression
805 { $$ = new_element_list(ctx, $1, $2); }
806 | ElementList ',' Elision_opt AssignmentExpression
807 { $$ = element_list_add(ctx, $1, $3, $4); }
809 /* ECMA-262 3rd Edition 11.1.4 */
810 Elision
811 : ',' { $$ = 1; }
812 | Elision ',' { $$ = $1 + 1; }
814 /* ECMA-262 3rd Edition 11.1.4 */
815 Elision_opt
816 : /* empty */ { $$ = 0; }
817 | Elision { $$ = $1; }
819 /* ECMA-262 3rd Edition 11.1.5 */
820 ObjectLiteral
821 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
822 | '{' PropertyNameAndValueList '}'
823 { $$ = new_prop_and_value_expression(ctx, $2); }
824 | '{' PropertyNameAndValueList ',' '}'
826 if(ctx->script->version < 2) {
827 WARN("Trailing comma in object literal is illegal in legacy mode.\n");
828 set_error(ctx, @3, JS_E_SYNTAX);
829 YYABORT;
831 $$ = new_prop_and_value_expression(ctx, $2);
834 /* ECMA-262 3rd Edition 11.1.5 */
835 PropertyNameAndValueList
836 : PropertyDefinition { $$ = new_property_list(ctx, $1); }
837 | PropertyNameAndValueList ',' PropertyDefinition
838 { $$ = property_list_add(ctx, $1, $3); }
840 /* ECMA-262 5.1 Edition 12.2.6 */
841 PropertyDefinition
842 : PropertyName ':' AssignmentExpression
843 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_VALUE, $1, $3); }
844 | kGET PropertyName GetterSetterMethod
845 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_GETTER, $2, $3); }
846 | kSET PropertyName GetterSetterMethod
847 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_SETTER, $2, $3); }
849 GetterSetterMethod
850 : left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
851 { $$ = new_function_expression(ctx, NULL, $2, $5, NULL, ctx->begin + @1, @6 - @1 + 1); }
853 /* Ecma-262 3rd Edition 11.1.5 */
854 PropertyName
855 : IdentifierName { $$ = new_string_literal(ctx, compiler_alloc_string_len(ctx->compiler, $1, lstrlenW($1))); }
856 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
857 | tNumericLiteral { $$ = $1; }
859 /* ECMA-262 3rd Edition 7.6 */
860 Identifier_opt
861 : /* empty*/ { $$ = NULL; }
862 | tIdentifier { $$ = $1; }
864 /* ECMA-262 5.1 Edition 7.6 */
865 IdentifierName
866 : tIdentifier { $$ = $1; }
867 | ReservedAsIdentifier
869 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
870 WARN("%s keyword used as an identifier in legacy mode.\n",
871 debugstr_w($1));
872 set_error(ctx, @$, JS_E_SYNTAX);
873 YYABORT;
875 $$ = $1;
878 ReservedAsIdentifier
879 : kBREAK { $$ = $1; }
880 | kCASE { $$ = $1; }
881 | kCATCH { $$ = $1; }
882 | kCONST { $$ = $1; }
883 | kCONTINUE { $$ = $1; }
884 | kDEFAULT { $$ = $1; }
885 | kDELETE { $$ = $1; }
886 | kDO { $$ = $1; }
887 | kELSE { $$ = $1; }
888 | kFALSE { $$ = $1; }
889 | kFINALLY { $$ = $1; }
890 | kFOR { $$ = $1; }
891 | kFUNCTION { $$ = $1; }
892 | kGET { $$ = $1; }
893 | kIF { $$ = $1; }
894 | kIN { $$ = $1; }
895 | kINSTANCEOF { $$ = $1; }
896 | kLET { $$ = $1; }
897 | kNEW { $$ = $1; }
898 | kNULL { $$ = $1; }
899 | kRETURN { $$ = $1; }
900 | kSET { $$ = $1; }
901 | kSWITCH { $$ = $1; }
902 | kTHIS { $$ = $1; }
903 | kTHROW { $$ = $1; }
904 | kTRUE { $$ = $1; }
905 | kTRY { $$ = $1; }
906 | kTYPEOF { $$ = $1; }
907 | kVAR { $$ = $1; }
908 | kVOID { $$ = $1; }
909 | kWHILE { $$ = $1; }
910 | kWITH { $$ = $1; }
912 /* ECMA-262 3rd Edition 7.8 */
913 Literal
914 : kNULL { $$ = new_null_literal(ctx); }
915 | BooleanLiteral { $$ = $1; }
916 | tNumericLiteral { $$ = $1; }
917 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
918 | '/' { $$ = parse_regexp(ctx);
919 if(!$$) YYABORT; }
920 | kDIVEQ { $$ = parse_regexp(ctx);
921 if(!$$) YYABORT; }
923 /* ECMA-262 3rd Edition 7.8.2 */
924 BooleanLiteral
925 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
926 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
927 | tBooleanLiteral { $$ = $1; }
929 semicolon_opt
930 : ';'
931 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} else { ctx->hres = S_OK; ctx->error_loc = -1; } }
933 left_bracket
934 : '('
935 | error { set_error(ctx, @$, JS_E_MISSING_LBRACKET); YYABORT; }
937 right_bracket
938 : ')'
939 | error { set_error(ctx, @$, JS_E_MISSING_RBRACKET); YYABORT; }
941 semicolon
942 : ';'
943 | error { set_error(ctx, @$, JS_E_MISSING_SEMICOLON); YYABORT; }
947 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
949 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
952 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size, unsigned loc)
954 statement_t *stat;
956 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
957 if(!stat)
958 return NULL;
960 stat->type = type;
961 stat->loc = loc;
962 stat->next = NULL;
964 return stat;
967 static literal_t *new_string_literal(parser_ctx_t *ctx, jsstr_t *str)
969 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
971 ret->type = LT_STRING;
972 ret->u.str = str;
974 return ret;
977 static literal_t *new_null_literal(parser_ctx_t *ctx)
979 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
981 ret->type = LT_NULL;
983 return ret;
986 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t type,
987 literal_t *name, expression_t *value)
989 property_definition_t *ret = parser_alloc(ctx, sizeof(property_definition_t));
991 ret->type = type;
992 ret->name = name;
993 ret->value = value;
994 ret->next = NULL;
996 return ret;
999 static property_list_t *new_property_list(parser_ctx_t *ctx, property_definition_t *prop)
1001 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
1002 ret->head = ret->tail = prop;
1003 return ret;
1006 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, property_definition_t *prop)
1008 list->tail = list->tail->next = prop;
1009 return list;
1012 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
1014 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
1016 ret->elision = elision;
1017 ret->expr = expr;
1018 ret->next = NULL;
1020 return ret;
1023 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
1025 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
1027 ret->head = ret->tail = new_array_element(ctx, elision, expr);
1029 return ret;
1032 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
1034 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
1036 return list;
1039 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
1041 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
1043 ret->expr = expr;
1044 ret->next = NULL;
1046 return ret;
1049 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
1051 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
1053 ret->head = ret->tail = new_argument(ctx, expr);
1055 return ret;
1058 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
1060 list->tail = list->tail->next = new_argument(ctx, expr);
1062 return list;
1065 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1067 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
1069 ret->identifier = identifier;
1070 ret->statement = statement;
1072 return ret;
1075 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_list_t *stat_list)
1077 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
1079 ret->expr = expr;
1080 ret->stat = stat_list ? stat_list->head : NULL;
1081 ret->loc = loc;
1082 ret->next = NULL;
1084 return ret;
1087 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
1089 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
1091 ret->head = ret->tail = case_clausule;
1093 return ret;
1096 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
1098 list->tail = list->tail->next = case_clausule;
1100 return list;
1103 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
1104 case_clausule_t *default_clausule, case_list_t *case_list2)
1106 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
1107 statement_t *stat = NULL;
1109 if(case_list1) {
1110 ret = case_list1->head;
1111 iter = case_list1->tail;
1114 if(default_clausule) {
1115 if(ret)
1116 iter = iter->next = default_clausule;
1117 else
1118 ret = iter = default_clausule;
1121 if(case_list2) {
1122 if(ret)
1123 iter->next = case_list2->head;
1124 else
1125 ret = case_list2->head;
1128 if(!ret)
1129 return NULL;
1131 for(iter = ret; iter; iter = iter->next) {
1132 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1133 if(!iter2)
1134 break;
1136 while(iter != iter2) {
1137 iter->stat = iter2->stat;
1138 iter = iter->next;
1141 if(stat) {
1142 while(stat->next)
1143 stat = stat->next;
1144 stat->next = iter->stat;
1145 }else {
1146 stat = iter->stat;
1150 return ret;
1153 static statement_t *new_block_statement(parser_ctx_t *ctx, unsigned loc, statement_list_t *list)
1155 block_statement_t *ret;
1157 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret), loc);
1158 if(!ret)
1159 return NULL;
1161 ret->scope_index = 0;
1162 ret->stat_list = list ? list->head : NULL;
1164 return &ret->stat;
1167 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1169 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1171 ret->identifier = identifier;
1172 ret->expr = expr;
1173 ret->next = NULL;
1174 ret->global_next = NULL;
1175 ret->block_scope = FALSE;
1176 ret->constant = FALSE;
1178 return ret;
1181 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1183 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1185 ret->head = ret->tail = decl;
1187 return ret;
1190 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1192 list->tail = list->tail->next = decl;
1194 return list;
1197 static statement_t *new_var_statement(parser_ctx_t *ctx, BOOL block_scope, BOOL constant, unsigned loc,
1198 variable_list_t *variable_list)
1200 variable_declaration_t *var;
1201 var_statement_t *ret;
1203 ret = new_statement(ctx, STAT_VAR, sizeof(*ret), loc);
1204 if(!ret)
1205 return NULL;
1207 ret->variable_list = variable_list->head;
1208 for (var = ret->variable_list; var; var = var->next)
1210 var->block_scope = block_scope;
1211 var->constant = constant;
1214 return &ret->stat;
1217 static statement_t *new_expression_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1219 expression_statement_t *ret;
1221 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret), loc);
1222 if(!ret)
1223 return NULL;
1225 ret->expr = expr;
1227 return &ret->stat;
1230 static statement_t *new_if_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr,
1231 statement_t *if_stat, statement_t *else_stat)
1233 if_statement_t *ret;
1235 ret = new_statement(ctx, STAT_IF, sizeof(*ret), loc);
1236 if(!ret)
1237 return NULL;
1239 ret->expr = expr;
1240 ret->if_stat = if_stat;
1241 ret->else_stat = else_stat;
1243 return &ret->stat;
1246 static statement_t *new_while_statement(parser_ctx_t *ctx, unsigned loc, BOOL dowhile, expression_t *expr, statement_t *stat)
1248 while_statement_t *ret;
1250 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret), loc);
1251 if(!ret)
1252 return NULL;
1254 ret->do_while = dowhile;
1255 ret->expr = expr;
1256 ret->statement = stat;
1258 return &ret->stat;
1261 static statement_t *new_for_statement(parser_ctx_t *ctx, unsigned loc, variable_declaration_t *variable_list, expression_t *begin_expr,
1262 expression_t *expr, unsigned expr_loc, expression_t *end_expr, unsigned end_loc, statement_t *statement)
1264 for_statement_t *ret;
1266 ret = new_statement(ctx, STAT_FOR, sizeof(*ret), loc);
1267 if(!ret)
1268 return NULL;
1270 ret->variable_list = variable_list;
1271 ret->begin_expr = begin_expr;
1272 ret->expr = expr;
1273 ret->expr_loc = expr_loc;
1274 ret->end_expr = end_expr;
1275 ret->end_loc = end_loc;
1276 ret->statement = statement;
1277 ret->scope_index = 0;
1279 return &ret->stat;
1282 static statement_t *new_forin_statement(parser_ctx_t *ctx, unsigned loc, variable_declaration_t *variable, expression_t *expr,
1283 expression_t *in_expr, statement_t *statement)
1285 forin_statement_t *ret;
1287 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret), loc);
1288 if(!ret)
1289 return NULL;
1291 ret->variable = variable;
1292 ret->expr = expr;
1293 ret->in_expr = in_expr;
1294 ret->statement = statement;
1296 return &ret->stat;
1299 static statement_t *new_continue_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier)
1301 branch_statement_t *ret;
1303 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret), loc);
1304 if(!ret)
1305 return NULL;
1307 ret->identifier = identifier;
1309 return &ret->stat;
1312 static statement_t *new_break_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier)
1314 branch_statement_t *ret;
1316 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret), loc);
1317 if(!ret)
1318 return NULL;
1320 ret->identifier = identifier;
1322 return &ret->stat;
1325 static statement_t *new_return_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1327 expression_statement_t *ret;
1329 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret), loc);
1330 if(!ret)
1331 return NULL;
1333 ret->expr = expr;
1335 return &ret->stat;
1338 static statement_t *new_with_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_t *statement)
1340 with_statement_t *ret;
1342 ret = new_statement(ctx, STAT_WITH, sizeof(*ret), loc);
1343 if(!ret)
1344 return NULL;
1346 ret->expr = expr;
1347 ret->statement = statement;
1349 return &ret->stat;
1352 static statement_t *new_labelled_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier, statement_t *statement)
1354 labelled_statement_t *ret;
1356 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret), loc);
1357 if(!ret)
1358 return NULL;
1360 ret->identifier = identifier;
1361 ret->statement = statement;
1363 return &ret->stat;
1366 static statement_t *new_switch_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, case_clausule_t *case_list)
1368 switch_statement_t *ret;
1370 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret), loc);
1371 if(!ret)
1372 return NULL;
1374 ret->expr = expr;
1375 ret->case_list = case_list;
1377 return &ret->stat;
1380 static statement_t *new_throw_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1382 expression_statement_t *ret;
1384 ret = new_statement(ctx, STAT_THROW, sizeof(*ret), loc);
1385 if(!ret)
1386 return NULL;
1388 ret->expr = expr;
1390 return &ret->stat;
1393 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1394 catch_block_t *catch_block, statement_t *finally_statement, unsigned finally_loc)
1396 try_statement_t *ret;
1398 ret = new_statement(ctx, STAT_TRY, sizeof(*ret), try_statement->loc);
1399 if(!ret)
1400 return NULL;
1402 ret->try_statement = try_statement;
1403 ret->catch_block = catch_block;
1404 ret->finally_statement = finally_statement;
1405 ret->finally_loc = finally_loc;
1407 return &ret->stat;
1410 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1412 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1414 ret->identifier = identifier;
1415 ret->next = NULL;
1417 return ret;
1420 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1422 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1424 ret->head = ret->tail = new_parameter(ctx, identifier);
1426 return ret;
1429 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1431 list->tail = list->tail->next = new_parameter(ctx, identifier);
1433 return list;
1436 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier, parameter_list_t *parameter_list,
1437 statement_list_t *statement_list, const WCHAR *event_target, const WCHAR *src_str, DWORD src_len)
1439 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1441 ret->identifier = identifier;
1442 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1443 ret->statement_list = statement_list ? statement_list->head : NULL;
1444 ret->event_target = event_target;
1445 ret->src_str = src_str;
1446 ret->src_len = src_len;
1447 ret->is_statement = FALSE;
1448 ret->scope_index = 0;
1449 ret->next = NULL;
1451 return &ret->expr;
1454 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1456 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1458 ret->type = type;
1460 return ret;
1463 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1464 expression_t *expression1, expression_t *expression2)
1466 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1468 ret->expression1 = expression1;
1469 ret->expression2 = expression2;
1471 return &ret->expr;
1474 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1476 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1478 ret->expression = expression;
1480 return &ret->expr;
1483 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1484 expression_t *true_expression, expression_t *false_expression)
1486 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1488 ret->expression = expression;
1489 ret->true_expression = true_expression;
1490 ret->false_expression = false_expression;
1492 return &ret->expr;
1495 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1497 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1499 ret->expression = expression;
1500 ret->identifier = identifier;
1502 return &ret->expr;
1505 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1507 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1509 ret->expression = expression;
1510 ret->argument_list = argument_list ? argument_list->head : NULL;
1512 return &ret->expr;
1515 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1517 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1519 ret->expression = expression;
1520 ret->argument_list = argument_list ? argument_list->head : NULL;
1522 return &ret->expr;
1525 static int parser_error(unsigned *loc, parser_ctx_t *ctx, const char *str)
1527 if(ctx->error_loc == -1)
1528 ctx->error_loc = *loc;
1529 if(ctx->hres == S_OK)
1530 ctx->hres = JS_E_SYNTAX;
1531 return 0;
1534 static void set_error(parser_ctx_t *ctx, unsigned loc, HRESULT error)
1536 ctx->hres = error;
1537 ctx->error_loc = loc;
1540 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1542 if(obj || *(ctx->ptr-1)==next) return TRUE;
1544 set_error(ctx, ctx->ptr - ctx->begin /* FIXME */, JS_E_SYNTAX);
1545 return FALSE;
1549 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1551 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1553 ret->identifier = identifier;
1555 return &ret->expr;
1558 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1560 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1562 ret->element_list = element_list ? element_list->head : NULL;
1563 ret->length = length;
1565 return &ret->expr;
1568 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1570 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1572 ret->property_list = property_list ? property_list->head : NULL;
1574 return &ret->expr;
1577 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1579 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1581 ret->literal = literal;
1583 return &ret->expr;
1586 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1588 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1590 ret->head = ret->tail = statement;
1592 return ret;
1595 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1597 list->tail = list->tail->next = statement;
1599 return list;
1602 void parser_release(parser_ctx_t *ctx)
1604 script_release(ctx->script);
1605 heap_pool_free(&ctx->heap);
1606 heap_free(ctx);
1609 HRESULT script_parse(script_ctx_t *ctx, struct _compiler_ctx_t *compiler, bytecode_t *code, const WCHAR *delimiter, BOOL from_eval,
1610 parser_ctx_t **ret)
1612 parser_ctx_t *parser_ctx;
1613 heap_pool_t *mark;
1614 HRESULT hres;
1616 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1617 if(!parser_ctx)
1618 return E_OUTOFMEMORY;
1620 parser_ctx->error_loc = -1;
1621 parser_ctx->is_html = delimiter && !wcsicmp(delimiter, L"</script>");
1623 parser_ctx->begin = parser_ctx->ptr = code->source;
1624 parser_ctx->end = parser_ctx->begin + lstrlenW(parser_ctx->begin);
1626 script_addref(ctx);
1627 parser_ctx->script = ctx;
1629 mark = heap_pool_mark(&ctx->tmp_heap);
1630 heap_pool_init(&parser_ctx->heap);
1632 parser_ctx->compiler = compiler;
1633 parser_parse(parser_ctx);
1634 parser_ctx->compiler = NULL;
1636 heap_pool_clear(mark);
1637 hres = parser_ctx->hres;
1638 if(FAILED(hres)) {
1639 unsigned int error_loc = parser_ctx->error_loc == -1 ? 0 : parser_ctx->error_loc;
1640 const WCHAR *line_start = code->source + error_loc, *line_end = line_start;
1641 jsstr_t *line_str;
1643 while(line_start > code->source && line_start[-1] != '\n')
1644 line_start--;
1645 while(*line_end && *line_end != '\n')
1646 line_end++;
1647 line_str = jsstr_alloc_len(line_start, line_end - line_start);
1649 WARN("parser failed around %s in line %s\n",
1650 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20),
1651 debugstr_jsstr(line_str));
1653 throw_error(ctx, hres, NULL);
1654 set_error_location(ctx->ei, code, error_loc, IDS_COMPILATION_ERROR, line_str);
1655 parser_release(parser_ctx);
1656 if(line_str)
1657 jsstr_release(line_str);
1658 return DISP_E_EXCEPTION;
1661 *ret = parser_ctx;
1662 return S_OK;