wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / jscript / parser.y
blobbfc83912ba1ec919a26543b8057ea27587ffd23d
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 typedef struct _statement_list_t {
35 statement_t *head;
36 statement_t *tail;
37 } statement_list_t;
39 static literal_t *new_string_literal(parser_ctx_t*,jsstr_t*);
40 static literal_t *new_null_literal(parser_ctx_t*);
42 typedef struct _property_list_t {
43 property_definition_t *head;
44 property_definition_t *tail;
45 } property_list_t;
47 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t,
48 literal_t *name, expression_t *value);
49 static property_list_t *new_property_list(parser_ctx_t*,property_definition_t*);
50 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,property_definition_t*);
52 typedef struct _element_list_t {
53 array_element_t *head;
54 array_element_t *tail;
55 } element_list_t;
57 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
58 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
60 typedef struct _argument_list_t {
61 argument_t *head;
62 argument_t *tail;
63 } argument_list_t;
65 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
66 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
68 typedef struct _case_list_t {
69 case_clausule_t *head;
70 case_clausule_t *tail;
71 } case_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*,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_list_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 struct statement_list_t {
107 statement_t *head;
108 statement_t *tail;
111 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
112 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
114 typedef struct _parameter_list_t {
115 parameter_t *head;
116 parameter_t *tail;
117 } parameter_list_t;
119 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
120 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
122 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
123 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
124 source_elements_t*,const WCHAR*,const WCHAR*,DWORD);
125 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
126 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
127 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
128 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
129 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
130 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
131 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
132 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
133 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
134 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
136 static source_elements_t *new_source_elements(parser_ctx_t*);
137 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
139 #define YYLTYPE unsigned
140 #define YYLLOC_DEFAULT(Cur, Rhs, N) Cur = YYRHSLOC((Rhs), (N) ? 1 : 0)
144 %lex-param { parser_ctx_t *ctx }
145 %parse-param { parser_ctx_t *ctx }
146 %define api.pure
147 %start Program
149 %union {
150 int ival;
151 jsstr_t *str;
152 literal_t *literal;
153 struct _argument_list_t *argument_list;
154 case_clausule_t *case_clausule;
155 struct _case_list_t *case_list;
156 catch_block_t *catch_block;
157 struct _element_list_t *element_list;
158 expression_t *expr;
159 const WCHAR *identifier;
160 struct _parameter_list_t *parameter_list;
161 struct _property_list_t *property_list;
162 property_definition_t *property_definition;
163 source_elements_t *source_elements;
164 statement_t *statement;
165 struct _statement_list_t *statement_list;
166 struct _variable_list_t *variable_list;
167 variable_declaration_t *variable_declaration;
170 /* keywords */
171 %token <identifier> kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kFUNCTION kIF kFINALLY kFOR kGET kIN kSET
172 %token <identifier> kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
173 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL
175 /* tokens */
176 %token <identifier> tIdentifier
177 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
178 %token <literal> tNumericLiteral tBooleanLiteral
179 %token <str> tStringLiteral
181 %type <source_elements> SourceElements
182 %type <source_elements> FunctionBody
183 %type <statement> Statement
184 %type <statement> Block
185 %type <statement> VariableStatement
186 %type <statement> EmptyStatement
187 %type <statement> ExpressionStatement
188 %type <statement> IfStatement
189 %type <statement> IterationStatement
190 %type <statement> ContinueStatement
191 %type <statement> BreakStatement
192 %type <statement> ReturnStatement
193 %type <statement> WithStatement
194 %type <statement> LabelledStatement
195 %type <statement> SwitchStatement
196 %type <statement> ThrowStatement
197 %type <statement> TryStatement
198 %type <statement> Finally
199 %type <statement_list> StatementList StatementList_opt
200 %type <parameter_list> FormalParameterList FormalParameterList_opt
201 %type <expr> Expression Expression_opt Expression_err
202 %type <expr> ExpressionNoIn ExpressionNoIn_opt
203 %type <expr> FunctionExpression
204 %type <expr> AssignmentExpression AssignmentExpressionNoIn
205 %type <expr> ConditionalExpression ConditionalExpressionNoIn
206 %type <expr> LeftHandSideExpression
207 %type <expr> LogicalORExpression LogicalORExpressionNoIn
208 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
209 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
210 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
211 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
212 %type <expr> EqualityExpression EqualityExpressionNoIn
213 %type <expr> RelationalExpression RelationalExpressionNoIn
214 %type <expr> ShiftExpression
215 %type <expr> AdditiveExpression
216 %type <expr> MultiplicativeExpression
217 %type <expr> Initialiser_opt Initialiser
218 %type <expr> InitialiserNoIn_opt InitialiserNoIn
219 %type <expr> UnaryExpression
220 %type <expr> PostfixExpression
221 %type <expr> NewExpression
222 %type <expr> CallExpression
223 %type <expr> MemberExpression
224 %type <expr> PrimaryExpression
225 %type <expr> GetterSetterMethod
226 %type <identifier> Identifier_opt
227 %type <variable_list> VariableDeclarationList
228 %type <variable_list> VariableDeclarationListNoIn
229 %type <variable_declaration> VariableDeclaration
230 %type <variable_declaration> VariableDeclarationNoIn
231 %type <case_list> CaseClausules CaseClausules_opt
232 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
233 %type <catch_block> Catch
234 %type <argument_list> Arguments
235 %type <argument_list> ArgumentList
236 %type <literal> Literal
237 %type <expr> ArrayLiteral
238 %type <expr> ObjectLiteral
239 %type <ival> Elision Elision_opt
240 %type <element_list> ElementList
241 %type <property_list> PropertyNameAndValueList
242 %type <property_definition> PropertyDefinition
243 %type <literal> PropertyName
244 %type <literal> BooleanLiteral
245 %type <ival> AssignOper
246 %type <identifier> IdentifierName ReservedAsIdentifier
248 %nonassoc LOWER_THAN_ELSE
249 %nonassoc kELSE
253 /* ECMA-262 3rd Edition 14 */
254 Program
255 : SourceElements HtmlComment { ctx->source = $1; }
257 HtmlComment
258 : tHTMLCOMMENT
259 | /* empty */
261 /* ECMA-262 3rd Edition 14 */
262 SourceElements
263 : /* empty */ { $$ = new_source_elements(ctx); }
264 | SourceElements Statement
265 { $$ = source_elements_add_statement($1, $2); }
267 /* ECMA-262 3rd Edition 13 */
268 FunctionExpression
269 : kFUNCTION left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
270 { $$ = new_function_expression(ctx, NULL, $3, $6, NULL, ctx->begin + @1, @7 - @1 + 1); }
271 | kFUNCTION tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
272 { $$ = new_function_expression(ctx, $2, $4, $7, NULL, ctx->begin + @1, @8 - @1 + 1); }
273 | kFUNCTION tIdentifier kDCOL tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
274 { $$ = new_function_expression(ctx, $4, $6, $9, $2, ctx->begin + @1, @10 - @1 + 1); }
276 /* ECMA-262 3rd Edition 13 */
277 FunctionBody
278 : SourceElements { $$ = $1; }
280 /* ECMA-262 3rd Edition 13 */
281 FormalParameterList
282 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
283 | FormalParameterList ',' tIdentifier
284 { $$ = parameter_list_add(ctx, $1, $3); }
286 /* ECMA-262 3rd Edition 13 */
287 FormalParameterList_opt
288 : /* empty */ { $$ = NULL; }
289 | FormalParameterList { $$ = $1; }
291 /* ECMA-262 3rd Edition 12 */
292 Statement
293 : Block { $$ = $1; }
294 | VariableStatement { $$ = $1; }
295 | EmptyStatement { $$ = $1; }
296 | FunctionExpression { $$ = new_expression_statement(ctx, @$, $1); }
297 | ExpressionStatement { $$ = $1; }
298 | IfStatement { $$ = $1; }
299 | IterationStatement { $$ = $1; }
300 | ContinueStatement { $$ = $1; }
301 | BreakStatement { $$ = $1; }
302 | ReturnStatement { $$ = $1; }
303 | WithStatement { $$ = $1; }
304 | LabelledStatement { $$ = $1; }
305 | SwitchStatement { $$ = $1; }
306 | ThrowStatement { $$ = $1; }
307 | TryStatement { $$ = $1; }
309 /* ECMA-262 3rd Edition 12.2 */
310 StatementList
311 : Statement { $$ = new_statement_list(ctx, $1); }
312 | StatementList Statement
313 { $$ = statement_list_add($1, $2); }
315 /* ECMA-262 3rd Edition 12.2 */
316 StatementList_opt
317 : /* empty */ { $$ = NULL; }
318 | StatementList { $$ = $1; }
320 /* ECMA-262 3rd Edition 12.1 */
321 Block
322 : '{' StatementList '}' { $$ = new_block_statement(ctx, @2, $2); }
323 | '{' '}' { $$ = new_block_statement(ctx, @$, NULL); }
325 /* ECMA-262 3rd Edition 12.2 */
326 VariableStatement
327 : kVAR VariableDeclarationList semicolon_opt
328 { $$ = new_var_statement(ctx, @$, $2); }
330 /* ECMA-262 3rd Edition 12.2 */
331 VariableDeclarationList
332 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
333 | VariableDeclarationList ',' VariableDeclaration
334 { $$ = variable_list_add(ctx, $1, $3); }
336 /* ECMA-262 3rd Edition 12.2 */
337 VariableDeclarationListNoIn
338 : VariableDeclarationNoIn
339 { $$ = new_variable_list(ctx, $1); }
340 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
341 { $$ = variable_list_add(ctx, $1, $3); }
343 /* ECMA-262 3rd Edition 12.2 */
344 VariableDeclaration
345 : tIdentifier Initialiser_opt
346 { $$ = new_variable_declaration(ctx, $1, $2); }
348 /* ECMA-262 3rd Edition 12.2 */
349 VariableDeclarationNoIn
350 : tIdentifier InitialiserNoIn_opt
351 { $$ = new_variable_declaration(ctx, $1, $2); }
353 /* ECMA-262 3rd Edition 12.2 */
354 Initialiser_opt
355 : /* empty */ { $$ = NULL; }
356 | Initialiser { $$ = $1; }
358 /* ECMA-262 3rd Edition 12.2 */
359 Initialiser
360 : '=' AssignmentExpression
361 { $$ = $2; }
363 /* ECMA-262 3rd Edition 12.2 */
364 InitialiserNoIn_opt
365 : /* empty */ { $$ = NULL; }
366 | InitialiserNoIn { $$ = $1; }
368 /* ECMA-262 3rd Edition 12.2 */
369 InitialiserNoIn
370 : '=' AssignmentExpressionNoIn
371 { $$ = $2; }
373 /* ECMA-262 3rd Edition 12.3 */
374 EmptyStatement
375 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0, @$); }
377 /* ECMA-262 3rd Edition 12.4 */
378 ExpressionStatement
379 : Expression semicolon_opt
380 { $$ = new_expression_statement(ctx, @$, $1); }
382 /* ECMA-262 3rd Edition 12.5 */
383 IfStatement
384 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
385 { $$ = new_if_statement(ctx, @$, $3, $5, $7); }
386 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
387 { $$ = new_if_statement(ctx, @$, $3, $5, NULL); }
389 /* ECMA-262 3rd Edition 12.6 */
390 IterationStatement
391 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
392 { $$ = new_while_statement(ctx, @3, TRUE, $5, $2); }
393 | kWHILE left_bracket Expression_err right_bracket Statement
394 { $$ = new_while_statement(ctx, @$, FALSE, $3, $5); }
395 | kFOR left_bracket ExpressionNoIn_opt
396 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
397 semicolon Expression_opt
398 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
399 semicolon Expression_opt right_bracket Statement
400 { $$ = new_for_statement(ctx, @3, NULL, $3, $6, @6, $9, @9, $11); }
401 | kFOR left_bracket kVAR VariableDeclarationListNoIn
402 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
403 semicolon Expression_opt
404 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
405 semicolon Expression_opt right_bracket Statement
406 { $$ = new_for_statement(ctx, @3, $4, NULL, $7, @7, $10, @10, $12); }
407 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
408 { $$ = new_forin_statement(ctx, @$, NULL, $3, $5, $7); }
409 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
410 { $$ = new_forin_statement(ctx, @$, $4, NULL, $6, $8); }
412 /* ECMA-262 3rd Edition 12.7 */
413 ContinueStatement
414 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
415 { $$ = new_continue_statement(ctx, @$, $2); }
417 /* ECMA-262 3rd Edition 12.8 */
418 BreakStatement
419 : kBREAK /* NONL */ Identifier_opt semicolon_opt
420 { $$ = new_break_statement(ctx, @$, $2); }
422 /* ECMA-262 3rd Edition 12.9 */
423 ReturnStatement
424 : kRETURN /* NONL */ Expression_opt semicolon_opt
425 { $$ = new_return_statement(ctx, @$, $2); }
427 /* ECMA-262 3rd Edition 12.10 */
428 WithStatement
429 : kWITH left_bracket Expression right_bracket Statement
430 { $$ = new_with_statement(ctx, @$, $3, $5); }
432 /* ECMA-262 3rd Edition 12.12 */
433 LabelledStatement
434 : tIdentifier ':' Statement
435 { $$ = new_labelled_statement(ctx, @$, $1, $3); }
437 /* ECMA-262 3rd Edition 12.11 */
438 SwitchStatement
439 : kSWITCH left_bracket Expression right_bracket CaseBlock
440 { $$ = new_switch_statement(ctx, @$, $3, $5); }
442 /* ECMA-262 3rd Edition 12.11 */
443 CaseBlock
444 : '{' CaseClausules_opt '}'
445 { $$ = new_case_block(ctx, $2, NULL, NULL); }
446 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
447 { $$ = new_case_block(ctx, $2, $3, $4); }
449 /* ECMA-262 3rd Edition 12.11 */
450 CaseClausules_opt
451 : /* empty */ { $$ = NULL; }
452 | CaseClausules { $$ = $1; }
454 /* ECMA-262 3rd Edition 12.11 */
455 CaseClausules
456 : CaseClausule { $$ = new_case_list(ctx, $1); }
457 | CaseClausules CaseClausule
458 { $$ = case_list_add(ctx, $1, $2); }
460 /* ECMA-262 3rd Edition 12.11 */
461 CaseClausule
462 : kCASE Expression ':' StatementList_opt
463 { $$ = new_case_clausule(ctx, @$, $2, $4); }
465 /* ECMA-262 3rd Edition 12.11 */
466 DefaultClausule
467 : kDEFAULT ':' StatementList_opt
468 { $$ = new_case_clausule(ctx, @$, NULL, $3); }
470 /* ECMA-262 3rd Edition 12.13 */
471 ThrowStatement
472 : kTHROW /* NONL */ Expression semicolon_opt
473 { $$ = new_throw_statement(ctx, @$, $2); }
475 /* ECMA-262 3rd Edition 12.14 */
476 TryStatement
477 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL, 0); }
478 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3, @3); }
479 | kTRY Block Catch Finally
480 { $$ = new_try_statement(ctx, $2, $3, $4, @4); }
482 /* ECMA-262 3rd Edition 12.14 */
483 Catch
484 : kCATCH left_bracket tIdentifier right_bracket Block
485 { $$ = new_catch_block(ctx, $3, $5); }
487 /* ECMA-262 3rd Edition 12.14 */
488 Finally
489 : kFINALLY Block { @$ = @2; $$ = $2; }
491 /* ECMA-262 3rd Edition 11.14 */
492 Expression_opt
493 : /* empty */ { $$ = NULL; }
494 | Expression { $$ = $1; }
496 Expression_err
497 : Expression { $$ = $1; }
498 | error { set_error(ctx, @$, JS_E_SYNTAX); YYABORT; }
500 /* ECMA-262 3rd Edition 11.14 */
501 Expression
502 : AssignmentExpression { $$ = $1; }
503 | Expression ',' AssignmentExpression
504 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
506 /* ECMA-262 3rd Edition 11.14 */
507 ExpressionNoIn_opt
508 : /* empty */ { $$ = NULL; }
509 | ExpressionNoIn { $$ = $1; }
511 /* ECMA-262 3rd Edition 11.14 */
512 ExpressionNoIn
513 : AssignmentExpressionNoIn
514 { $$ = $1; }
515 | ExpressionNoIn ',' AssignmentExpressionNoIn
516 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
518 AssignOper
519 : tAssignOper { $$ = $1; }
520 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
522 /* ECMA-262 3rd Edition 11.13 */
523 AssignmentExpression
524 : ConditionalExpression { $$ = $1; }
525 | LeftHandSideExpression '=' AssignmentExpression
526 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
527 | LeftHandSideExpression AssignOper AssignmentExpression
528 { $$ = new_binary_expression(ctx, $2, $1, $3); }
530 /* ECMA-262 3rd Edition 11.13 */
531 AssignmentExpressionNoIn
532 : ConditionalExpressionNoIn
533 { $$ = $1; }
534 | LeftHandSideExpression '=' AssignmentExpressionNoIn
535 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
536 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
537 { $$ = new_binary_expression(ctx, $2, $1, $3); }
539 /* ECMA-262 3rd Edition 11.12 */
540 ConditionalExpression
541 : LogicalORExpression { $$ = $1; }
542 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
543 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
545 /* ECMA-262 3rd Edition 11.12 */
546 ConditionalExpressionNoIn
547 : LogicalORExpressionNoIn
548 { $$ = $1; }
549 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
550 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
552 /* ECMA-262 3rd Edition 11.11 */
553 LogicalORExpression
554 : LogicalANDExpression { $$ = $1; }
555 | LogicalORExpression tOROR LogicalANDExpression
556 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
558 /* ECMA-262 3rd Edition 11.11 */
559 LogicalORExpressionNoIn
560 : LogicalANDExpressionNoIn
561 { $$ = $1; }
562 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
563 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
565 /* ECMA-262 3rd Edition 11.11 */
566 LogicalANDExpression
567 : BitwiseORExpression { $$ = $1; }
568 | LogicalANDExpression tANDAND BitwiseORExpression
569 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
571 /* ECMA-262 3rd Edition 11.11 */
572 LogicalANDExpressionNoIn
573 : BitwiseORExpressionNoIn
574 { $$ = $1; }
575 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
576 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
578 /* ECMA-262 3rd Edition 11.10 */
579 BitwiseORExpression
580 : BitwiseXORExpression { $$ = $1; }
581 | BitwiseORExpression '|' BitwiseXORExpression
582 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
584 /* ECMA-262 3rd Edition 11.10 */
585 BitwiseORExpressionNoIn
586 : BitwiseXORExpressionNoIn
587 { $$ = $1; }
588 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
589 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
591 /* ECMA-262 3rd Edition 11.10 */
592 BitwiseXORExpression
593 : BitwiseANDExpression { $$ = $1; }
594 | BitwiseXORExpression '^' BitwiseANDExpression
595 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
597 /* ECMA-262 3rd Edition 11.10 */
598 BitwiseXORExpressionNoIn
599 : BitwiseANDExpressionNoIn
600 { $$ = $1; }
601 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
602 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
604 /* ECMA-262 3rd Edition 11.10 */
605 BitwiseANDExpression
606 : EqualityExpression { $$ = $1; }
607 | BitwiseANDExpression '&' EqualityExpression
608 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
610 /* ECMA-262 3rd Edition 11.10 */
611 BitwiseANDExpressionNoIn
612 : EqualityExpressionNoIn
613 { $$ = $1; }
614 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
615 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
617 /* ECMA-262 3rd Edition 11.9 */
618 EqualityExpression
619 : RelationalExpression { $$ = $1; }
620 | EqualityExpression tEqOper RelationalExpression
621 { $$ = new_binary_expression(ctx, $2, $1, $3); }
623 /* ECMA-262 3rd Edition 11.9 */
624 EqualityExpressionNoIn
625 : RelationalExpressionNoIn { $$ = $1; }
626 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
627 { $$ = new_binary_expression(ctx, $2, $1, $3); }
629 /* ECMA-262 3rd Edition 11.8 */
630 RelationalExpression
631 : ShiftExpression { $$ = $1; }
632 | RelationalExpression tRelOper ShiftExpression
633 { $$ = new_binary_expression(ctx, $2, $1, $3); }
634 | RelationalExpression kINSTANCEOF ShiftExpression
635 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
636 | RelationalExpression kIN ShiftExpression
637 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
639 /* ECMA-262 3rd Edition 11.8 */
640 RelationalExpressionNoIn
641 : ShiftExpression { $$ = $1; }
642 | RelationalExpressionNoIn tRelOper ShiftExpression
643 { $$ = new_binary_expression(ctx, $2, $1, $3); }
644 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
645 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
647 /* ECMA-262 3rd Edition 11.7 */
648 ShiftExpression
649 : AdditiveExpression { $$ = $1; }
650 | ShiftExpression tShiftOper AdditiveExpression
651 { $$ = new_binary_expression(ctx, $2, $1, $3); }
653 /* ECMA-262 3rd Edition 11.6 */
654 AdditiveExpression
655 : MultiplicativeExpression
656 { $$ = $1; }
657 | AdditiveExpression '+' MultiplicativeExpression
658 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
659 | AdditiveExpression '-' MultiplicativeExpression
660 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
662 /* ECMA-262 3rd Edition 11.5 */
663 MultiplicativeExpression
664 : UnaryExpression { $$ = $1; }
665 | MultiplicativeExpression '*' UnaryExpression
666 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
667 | MultiplicativeExpression '/' UnaryExpression
668 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
669 | MultiplicativeExpression '%' UnaryExpression
670 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
672 /* ECMA-262 3rd Edition 11.4 */
673 UnaryExpression
674 : PostfixExpression { $$ = $1; }
675 | kDELETE UnaryExpression
676 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
677 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
678 | kTYPEOF UnaryExpression
679 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
680 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
681 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
682 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
683 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
684 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
685 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
687 /* ECMA-262 3rd Edition 11.2 */
688 PostfixExpression
689 : LeftHandSideExpression
690 { $$ = $1; }
691 | LeftHandSideExpression /* NONL */ tINC
692 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
693 | LeftHandSideExpression /* NONL */ tDEC
694 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
697 /* ECMA-262 3rd Edition 11.2 */
698 LeftHandSideExpression
699 : NewExpression { $$ = $1; }
700 | CallExpression { $$ = $1; }
702 /* ECMA-262 3rd Edition 11.2 */
703 NewExpression
704 : MemberExpression { $$ = $1; }
705 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
707 /* ECMA-262 3rd Edition 11.2 */
708 MemberExpression
709 : PrimaryExpression { $$ = $1; }
710 | FunctionExpression { $$ = $1; }
711 | MemberExpression '[' Expression ']'
712 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
713 | MemberExpression '.' IdentifierName
714 { $$ = new_member_expression(ctx, $1, $3); }
715 | kNEW MemberExpression Arguments
716 { $$ = new_new_expression(ctx, $2, $3); }
718 /* ECMA-262 3rd Edition 11.2 */
719 CallExpression
720 : MemberExpression Arguments
721 { $$ = new_call_expression(ctx, $1, $2); }
722 | CallExpression Arguments
723 { $$ = new_call_expression(ctx, $1, $2); }
724 | CallExpression '[' Expression ']'
725 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
726 | CallExpression '.' IdentifierName
727 { $$ = new_member_expression(ctx, $1, $3); }
729 /* ECMA-262 3rd Edition 11.2 */
730 Arguments
731 : '(' ')' { $$ = NULL; }
732 | '(' ArgumentList ')' { $$ = $2; }
734 /* ECMA-262 3rd Edition 11.2 */
735 ArgumentList
736 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
737 | ArgumentList ',' AssignmentExpression
738 { $$ = argument_list_add(ctx, $1, $3); }
740 /* ECMA-262 3rd Edition 11.1 */
741 PrimaryExpression
742 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
743 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
744 | Literal { $$ = new_literal_expression(ctx, $1); }
745 | ArrayLiteral { $$ = $1; }
746 | ObjectLiteral { $$ = $1; }
747 | '(' Expression ')' { $$ = $2; }
749 /* ECMA-262 3rd Edition 11.1.4 */
750 ArrayLiteral
751 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
752 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
753 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
754 | '[' ElementList ',' Elision_opt ']'
755 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
757 /* ECMA-262 3rd Edition 11.1.4 */
758 ElementList
759 : Elision_opt AssignmentExpression
760 { $$ = new_element_list(ctx, $1, $2); }
761 | ElementList ',' Elision_opt AssignmentExpression
762 { $$ = element_list_add(ctx, $1, $3, $4); }
764 /* ECMA-262 3rd Edition 11.1.4 */
765 Elision
766 : ',' { $$ = 1; }
767 | Elision ',' { $$ = $1 + 1; }
769 /* ECMA-262 3rd Edition 11.1.4 */
770 Elision_opt
771 : /* empty */ { $$ = 0; }
772 | Elision { $$ = $1; }
774 /* ECMA-262 3rd Edition 11.1.5 */
775 ObjectLiteral
776 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
777 | '{' PropertyNameAndValueList '}'
778 { $$ = new_prop_and_value_expression(ctx, $2); }
779 | '{' PropertyNameAndValueList ',' '}'
781 if(ctx->script->version < 2) {
782 WARN("Trailing comma in object literal is illegal in legacy mode.\n");
783 set_error(ctx, @3, JS_E_SYNTAX);
784 YYABORT;
786 $$ = new_prop_and_value_expression(ctx, $2);
789 /* ECMA-262 3rd Edition 11.1.5 */
790 PropertyNameAndValueList
791 : PropertyDefinition { $$ = new_property_list(ctx, $1); }
792 | PropertyNameAndValueList ',' PropertyDefinition
793 { $$ = property_list_add(ctx, $1, $3); }
795 /* ECMA-262 5.1 Edition 12.2.6 */
796 PropertyDefinition
797 : PropertyName ':' AssignmentExpression
798 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_VALUE, $1, $3); }
799 | kGET PropertyName GetterSetterMethod
800 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_GETTER, $2, $3); }
801 | kSET PropertyName GetterSetterMethod
802 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_SETTER, $2, $3); }
804 GetterSetterMethod
805 : left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
806 { $$ = new_function_expression(ctx, NULL, $2, $5, NULL, ctx->begin + @1, @6 - @1 + 1); }
808 /* Ecma-262 3rd Edition 11.1.5 */
809 PropertyName
810 : IdentifierName { $$ = new_string_literal(ctx, compiler_alloc_string_len(ctx->compiler, $1, lstrlenW($1))); }
811 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
812 | tNumericLiteral { $$ = $1; }
814 /* ECMA-262 3rd Edition 7.6 */
815 Identifier_opt
816 : /* empty*/ { $$ = NULL; }
817 | tIdentifier { $$ = $1; }
819 /* ECMA-262 5.1 Edition 7.6 */
820 IdentifierName
821 : tIdentifier { $$ = $1; }
822 | ReservedAsIdentifier
824 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
825 WARN("%s keyword used as an identifier in legacy mode.\n",
826 debugstr_w($1));
827 set_error(ctx, @$, JS_E_SYNTAX);
828 YYABORT;
830 $$ = $1;
833 ReservedAsIdentifier
834 : kBREAK { $$ = $1; }
835 | kCASE { $$ = $1; }
836 | kCATCH { $$ = $1; }
837 | kCONTINUE { $$ = $1; }
838 | kDEFAULT { $$ = $1; }
839 | kDELETE { $$ = $1; }
840 | kDO { $$ = $1; }
841 | kELSE { $$ = $1; }
842 | kFALSE { $$ = $1; }
843 | kFINALLY { $$ = $1; }
844 | kFOR { $$ = $1; }
845 | kFUNCTION { $$ = $1; }
846 | kGET { $$ = $1; }
847 | kIF { $$ = $1; }
848 | kIN { $$ = $1; }
849 | kINSTANCEOF { $$ = $1; }
850 | kNEW { $$ = $1; }
851 | kNULL { $$ = $1; }
852 | kRETURN { $$ = $1; }
853 | kSET { $$ = $1; }
854 | kSWITCH { $$ = $1; }
855 | kTHIS { $$ = $1; }
856 | kTHROW { $$ = $1; }
857 | kTRUE { $$ = $1; }
858 | kTRY { $$ = $1; }
859 | kTYPEOF { $$ = $1; }
860 | kVAR { $$ = $1; }
861 | kVOID { $$ = $1; }
862 | kWHILE { $$ = $1; }
863 | kWITH { $$ = $1; }
865 /* ECMA-262 3rd Edition 7.8 */
866 Literal
867 : kNULL { $$ = new_null_literal(ctx); }
868 | BooleanLiteral { $$ = $1; }
869 | tNumericLiteral { $$ = $1; }
870 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
871 | '/' { $$ = parse_regexp(ctx);
872 if(!$$) YYABORT; }
873 | kDIVEQ { $$ = parse_regexp(ctx);
874 if(!$$) YYABORT; }
876 /* ECMA-262 3rd Edition 7.8.2 */
877 BooleanLiteral
878 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
879 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
880 | tBooleanLiteral { $$ = $1; }
882 semicolon_opt
883 : ';'
884 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} else { ctx->hres = S_OK; ctx->error_loc = -1; } }
886 left_bracket
887 : '('
888 | error { set_error(ctx, @$, JS_E_MISSING_LBRACKET); YYABORT; }
890 right_bracket
891 : ')'
892 | error { set_error(ctx, @$, JS_E_MISSING_RBRACKET); YYABORT; }
894 semicolon
895 : ';'
896 | error { set_error(ctx, @$, JS_E_MISSING_SEMICOLON); YYABORT; }
900 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
902 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
905 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size, unsigned loc)
907 statement_t *stat;
909 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
910 if(!stat)
911 return NULL;
913 stat->type = type;
914 stat->loc = loc;
915 stat->next = NULL;
917 return stat;
920 static literal_t *new_string_literal(parser_ctx_t *ctx, jsstr_t *str)
922 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
924 ret->type = LT_STRING;
925 ret->u.str = str;
927 return ret;
930 static literal_t *new_null_literal(parser_ctx_t *ctx)
932 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
934 ret->type = LT_NULL;
936 return ret;
939 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t type,
940 literal_t *name, expression_t *value)
942 property_definition_t *ret = parser_alloc(ctx, sizeof(property_definition_t));
944 ret->type = type;
945 ret->name = name;
946 ret->value = value;
947 ret->next = NULL;
949 return ret;
952 static property_list_t *new_property_list(parser_ctx_t *ctx, property_definition_t *prop)
954 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
955 ret->head = ret->tail = prop;
956 return ret;
959 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, property_definition_t *prop)
961 list->tail = list->tail->next = prop;
962 return list;
965 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
967 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
969 ret->elision = elision;
970 ret->expr = expr;
971 ret->next = NULL;
973 return ret;
976 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
978 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
980 ret->head = ret->tail = new_array_element(ctx, elision, expr);
982 return ret;
985 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
987 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
989 return list;
992 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
994 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
996 ret->expr = expr;
997 ret->next = NULL;
999 return ret;
1002 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
1004 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
1006 ret->head = ret->tail = new_argument(ctx, expr);
1008 return ret;
1011 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
1013 list->tail = list->tail->next = new_argument(ctx, expr);
1015 return list;
1018 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1020 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
1022 ret->identifier = identifier;
1023 ret->statement = statement;
1025 return ret;
1028 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_list_t *stat_list)
1030 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
1032 ret->expr = expr;
1033 ret->stat = stat_list ? stat_list->head : NULL;
1034 ret->loc = loc;
1035 ret->next = NULL;
1037 return ret;
1040 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
1042 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
1044 ret->head = ret->tail = case_clausule;
1046 return ret;
1049 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
1051 list->tail = list->tail->next = case_clausule;
1053 return list;
1056 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
1057 case_clausule_t *default_clausule, case_list_t *case_list2)
1059 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
1060 statement_t *stat = NULL;
1062 if(case_list1) {
1063 ret = case_list1->head;
1064 iter = case_list1->tail;
1067 if(default_clausule) {
1068 if(ret)
1069 iter = iter->next = default_clausule;
1070 else
1071 ret = iter = default_clausule;
1074 if(case_list2) {
1075 if(ret)
1076 iter->next = case_list2->head;
1077 else
1078 ret = case_list2->head;
1081 if(!ret)
1082 return NULL;
1084 for(iter = ret; iter; iter = iter->next) {
1085 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1086 if(!iter2)
1087 break;
1089 while(iter != iter2) {
1090 iter->stat = iter2->stat;
1091 iter = iter->next;
1094 if(stat) {
1095 while(stat->next)
1096 stat = stat->next;
1097 stat->next = iter->stat;
1098 }else {
1099 stat = iter->stat;
1103 return ret;
1106 static statement_t *new_block_statement(parser_ctx_t *ctx, unsigned loc, statement_list_t *list)
1108 block_statement_t *ret;
1110 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret), loc);
1111 if(!ret)
1112 return NULL;
1114 ret->stat_list = list ? list->head : NULL;
1116 return &ret->stat;
1119 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1121 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1123 ret->identifier = identifier;
1124 ret->expr = expr;
1125 ret->next = NULL;
1126 ret->global_next = NULL;
1128 return ret;
1131 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1133 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1135 ret->head = ret->tail = decl;
1137 return ret;
1140 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1142 list->tail = list->tail->next = decl;
1144 return list;
1147 static statement_t *new_var_statement(parser_ctx_t *ctx, unsigned loc, variable_list_t *variable_list)
1149 var_statement_t *ret;
1151 ret = new_statement(ctx, STAT_VAR, sizeof(*ret), loc);
1152 if(!ret)
1153 return NULL;
1155 ret->variable_list = variable_list->head;
1157 return &ret->stat;
1160 static statement_t *new_expression_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1162 expression_statement_t *ret;
1164 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret), loc);
1165 if(!ret)
1166 return NULL;
1168 ret->expr = expr;
1170 return &ret->stat;
1173 static statement_t *new_if_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr,
1174 statement_t *if_stat, statement_t *else_stat)
1176 if_statement_t *ret;
1178 ret = new_statement(ctx, STAT_IF, sizeof(*ret), loc);
1179 if(!ret)
1180 return NULL;
1182 ret->expr = expr;
1183 ret->if_stat = if_stat;
1184 ret->else_stat = else_stat;
1186 return &ret->stat;
1189 static statement_t *new_while_statement(parser_ctx_t *ctx, unsigned loc, BOOL dowhile, expression_t *expr, statement_t *stat)
1191 while_statement_t *ret;
1193 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret), loc);
1194 if(!ret)
1195 return NULL;
1197 ret->do_while = dowhile;
1198 ret->expr = expr;
1199 ret->statement = stat;
1201 return &ret->stat;
1204 static statement_t *new_for_statement(parser_ctx_t *ctx, unsigned loc, variable_list_t *variable_list, expression_t *begin_expr,
1205 expression_t *expr, unsigned expr_loc, expression_t *end_expr, unsigned end_loc, statement_t *statement)
1207 for_statement_t *ret;
1209 ret = new_statement(ctx, STAT_FOR, sizeof(*ret), loc);
1210 if(!ret)
1211 return NULL;
1213 ret->variable_list = variable_list ? variable_list->head : NULL;
1214 ret->begin_expr = begin_expr;
1215 ret->expr = expr;
1216 ret->expr_loc = expr_loc;
1217 ret->end_expr = end_expr;
1218 ret->end_loc = end_loc;
1219 ret->statement = statement;
1221 return &ret->stat;
1224 static statement_t *new_forin_statement(parser_ctx_t *ctx, unsigned loc, variable_declaration_t *variable, expression_t *expr,
1225 expression_t *in_expr, statement_t *statement)
1227 forin_statement_t *ret;
1229 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret), loc);
1230 if(!ret)
1231 return NULL;
1233 ret->variable = variable;
1234 ret->expr = expr;
1235 ret->in_expr = in_expr;
1236 ret->statement = statement;
1238 return &ret->stat;
1241 static statement_t *new_continue_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier)
1243 branch_statement_t *ret;
1245 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret), loc);
1246 if(!ret)
1247 return NULL;
1249 ret->identifier = identifier;
1251 return &ret->stat;
1254 static statement_t *new_break_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier)
1256 branch_statement_t *ret;
1258 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret), loc);
1259 if(!ret)
1260 return NULL;
1262 ret->identifier = identifier;
1264 return &ret->stat;
1267 static statement_t *new_return_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1269 expression_statement_t *ret;
1271 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret), loc);
1272 if(!ret)
1273 return NULL;
1275 ret->expr = expr;
1277 return &ret->stat;
1280 static statement_t *new_with_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_t *statement)
1282 with_statement_t *ret;
1284 ret = new_statement(ctx, STAT_WITH, sizeof(*ret), loc);
1285 if(!ret)
1286 return NULL;
1288 ret->expr = expr;
1289 ret->statement = statement;
1291 return &ret->stat;
1294 static statement_t *new_labelled_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier, statement_t *statement)
1296 labelled_statement_t *ret;
1298 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret), loc);
1299 if(!ret)
1300 return NULL;
1302 ret->identifier = identifier;
1303 ret->statement = statement;
1305 return &ret->stat;
1308 static statement_t *new_switch_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, case_clausule_t *case_list)
1310 switch_statement_t *ret;
1312 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret), loc);
1313 if(!ret)
1314 return NULL;
1316 ret->expr = expr;
1317 ret->case_list = case_list;
1319 return &ret->stat;
1322 static statement_t *new_throw_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1324 expression_statement_t *ret;
1326 ret = new_statement(ctx, STAT_THROW, sizeof(*ret), loc);
1327 if(!ret)
1328 return NULL;
1330 ret->expr = expr;
1332 return &ret->stat;
1335 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1336 catch_block_t *catch_block, statement_t *finally_statement, unsigned finally_loc)
1338 try_statement_t *ret;
1340 ret = new_statement(ctx, STAT_TRY, sizeof(*ret), try_statement->loc);
1341 if(!ret)
1342 return NULL;
1344 ret->try_statement = try_statement;
1345 ret->catch_block = catch_block;
1346 ret->finally_statement = finally_statement;
1347 ret->finally_loc = finally_loc;
1349 return &ret->stat;
1352 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1354 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1356 ret->identifier = identifier;
1357 ret->next = NULL;
1359 return ret;
1362 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1364 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1366 ret->head = ret->tail = new_parameter(ctx, identifier);
1368 return ret;
1371 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1373 list->tail = list->tail->next = new_parameter(ctx, identifier);
1375 return list;
1378 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier, parameter_list_t *parameter_list,
1379 source_elements_t *source_elements, const WCHAR *event_target, const WCHAR *src_str, DWORD src_len)
1381 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1383 ret->identifier = identifier;
1384 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1385 ret->source_elements = source_elements;
1386 ret->event_target = event_target;
1387 ret->src_str = src_str;
1388 ret->src_len = src_len;
1389 ret->next = NULL;
1391 return &ret->expr;
1394 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1396 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1398 ret->type = type;
1400 return ret;
1403 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1404 expression_t *expression1, expression_t *expression2)
1406 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1408 ret->expression1 = expression1;
1409 ret->expression2 = expression2;
1411 return &ret->expr;
1414 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1416 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1418 ret->expression = expression;
1420 return &ret->expr;
1423 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1424 expression_t *true_expression, expression_t *false_expression)
1426 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1428 ret->expression = expression;
1429 ret->true_expression = true_expression;
1430 ret->false_expression = false_expression;
1432 return &ret->expr;
1435 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1437 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1439 ret->expression = expression;
1440 ret->identifier = identifier;
1442 return &ret->expr;
1445 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1447 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1449 ret->expression = expression;
1450 ret->argument_list = argument_list ? argument_list->head : NULL;
1452 return &ret->expr;
1455 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1457 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1459 ret->expression = expression;
1460 ret->argument_list = argument_list ? argument_list->head : NULL;
1462 return &ret->expr;
1465 static int parser_error(unsigned *loc, parser_ctx_t *ctx, const char *str)
1467 if(ctx->error_loc == -1)
1468 ctx->error_loc = *loc;
1469 if(ctx->hres == S_OK)
1470 ctx->hres = JS_E_SYNTAX;
1471 return 0;
1474 static void set_error(parser_ctx_t *ctx, unsigned loc, HRESULT error)
1476 ctx->hres = error;
1477 ctx->error_loc = loc;
1480 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1482 if(obj || *(ctx->ptr-1)==next) return TRUE;
1484 set_error(ctx, ctx->ptr - ctx->begin /* FIXME */, JS_E_SYNTAX);
1485 return FALSE;
1489 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1491 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1493 ret->identifier = identifier;
1495 return &ret->expr;
1498 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1500 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1502 ret->element_list = element_list ? element_list->head : NULL;
1503 ret->length = length;
1505 return &ret->expr;
1508 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1510 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1512 ret->property_list = property_list ? property_list->head : NULL;
1514 return &ret->expr;
1517 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1519 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1521 ret->literal = literal;
1523 return &ret->expr;
1526 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1528 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1530 memset(ret, 0, sizeof(*ret));
1532 return ret;
1535 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1537 if(source_elements->statement_tail)
1538 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1539 else
1540 source_elements->statement = source_elements->statement_tail = statement;
1542 return source_elements;
1545 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1547 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1549 ret->head = ret->tail = statement;
1551 return ret;
1554 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1556 list->tail = list->tail->next = statement;
1558 return list;
1561 void parser_release(parser_ctx_t *ctx)
1563 script_release(ctx->script);
1564 heap_pool_free(&ctx->heap);
1565 heap_free(ctx);
1568 HRESULT script_parse(script_ctx_t *ctx, struct _compiler_ctx_t *compiler, bytecode_t *code, const WCHAR *delimiter, BOOL from_eval,
1569 parser_ctx_t **ret)
1571 parser_ctx_t *parser_ctx;
1572 heap_pool_t *mark;
1573 HRESULT hres;
1575 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1576 if(!parser_ctx)
1577 return E_OUTOFMEMORY;
1579 parser_ctx->error_loc = -1;
1580 parser_ctx->is_html = delimiter && !wcsicmp(delimiter, L"</script>");
1582 parser_ctx->begin = parser_ctx->ptr = code->source;
1583 parser_ctx->end = parser_ctx->begin + lstrlenW(parser_ctx->begin);
1585 script_addref(ctx);
1586 parser_ctx->script = ctx;
1588 mark = heap_pool_mark(&ctx->tmp_heap);
1589 heap_pool_init(&parser_ctx->heap);
1591 parser_ctx->compiler = compiler;
1592 parser_parse(parser_ctx);
1593 parser_ctx->compiler = NULL;
1595 heap_pool_clear(mark);
1596 hres = parser_ctx->hres;
1597 if(FAILED(hres)) {
1598 unsigned int error_loc = parser_ctx->error_loc == -1 ? 0 : parser_ctx->error_loc;
1599 const WCHAR *line_start = code->source + error_loc, *line_end = line_start;
1600 jsstr_t *line_str;
1602 while(line_start > code->source && line_start[-1] != '\n')
1603 line_start--;
1604 while(*line_end && *line_end != '\n')
1605 line_end++;
1606 line_str = jsstr_alloc_len(line_start, line_end - line_start);
1608 WARN("parser failed around %s in line %s\n",
1609 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20),
1610 debugstr_jsstr(line_str));
1612 throw_error(ctx, hres, NULL);
1613 set_error_location(ctx->ei, code, error_loc, IDS_COMPILATION_ERROR, line_str);
1614 parser_release(parser_ctx);
1615 if(line_str)
1616 jsstr_release(line_str);
1617 return DISP_E_EXCEPTION;
1620 *ret = parser_ctx;
1621 return S_OK;