wined3d: Pass IWineD3DStateBlockImpl pointers to stateblock_copy().
[wine.git] / dlls / jscript / parser.y
blobd4b49089424fd469e9c82f72eeb036419fa35b2d
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"
24 #define YYLEX_PARAM ctx
25 #define YYPARSE_PARAM ctx
27 static int parser_error(const char*);
28 static void set_error(parser_ctx_t*,UINT);
29 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
30 static BOOL allow_auto_semicolon(parser_ctx_t*);
31 static void program_parsed(parser_ctx_t*,source_elements_t*);
32 static source_elements_t *function_body_parsed(parser_ctx_t*,source_elements_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*,const WCHAR*);
40 static literal_t *new_null_literal(parser_ctx_t*);
41 static literal_t *new_undefined_literal(parser_ctx_t*);
42 static literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL);
44 typedef struct _property_list_t {
45 prop_val_t *head;
46 prop_val_t *tail;
47 } property_list_t;
49 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
50 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_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*,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 statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
89 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
90 static statement_t *new_empty_statement(parser_ctx_t*);
91 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
92 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
93 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
94 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
95 expression_t*,statement_t*);
96 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
97 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
98 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
99 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
100 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
101 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
102 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
103 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
104 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
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 push_func(parser_ctx_t*);
123 static inline void pop_func(parser_ctx_t *ctx)
125 ctx->func_stack = ctx->func_stack->next;
128 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
129 source_elements_t*,const WCHAR*,DWORD);
130 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
131 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
132 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
133 static expression_t *new_array_expression(parser_ctx_t*,expression_t*,expression_t*);
134 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
135 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
136 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
137 static expression_t *new_this_expression(parser_ctx_t*);
138 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
139 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
140 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
141 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
143 static source_elements_t *new_source_elements(parser_ctx_t*);
144 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
148 %pure_parser
149 %start Program
151 %union {
152 int ival;
153 const WCHAR *srcptr;
154 LPCWSTR wstr;
155 literal_t *literal;
156 struct _argument_list_t *argument_list;
157 case_clausule_t *case_clausule;
158 struct _case_list_t *case_list;
159 catch_block_t *catch_block;
160 struct _element_list_t *element_list;
161 expression_t *expr;
162 const WCHAR *identifier;
163 struct _parameter_list_t *parameter_list;
164 struct _property_list_t *property_list;
165 source_elements_t *source_elements;
166 statement_t *statement;
167 struct _statement_list_t *statement_list;
168 struct _variable_list_t *variable_list;
169 variable_declaration_t *variable_declaration;
172 /* keywords */
173 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
174 %token kINSTANCEOF kNEW kNULL kUNDEFINED kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
175 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
177 %token <srcptr> kFUNCTION '}'
179 /* tokens */
180 %token <identifier> tIdentifier
181 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
182 %token <literal> tNumericLiteral
183 %token <wstr> tStringLiteral
185 %type <source_elements> SourceElements
186 %type <source_elements> FunctionBody
187 %type <statement> Statement
188 %type <statement> Block
189 %type <statement> VariableStatement
190 %type <statement> EmptyStatement
191 %type <statement> ExpressionStatement
192 %type <statement> IfStatement
193 %type <statement> IterationStatement
194 %type <statement> ContinueStatement
195 %type <statement> BreakStatement
196 %type <statement> ReturnStatement
197 %type <statement> WithStatement
198 %type <statement> LabelledStatement
199 %type <statement> SwitchStatement
200 %type <statement> ThrowStatement
201 %type <statement> TryStatement
202 %type <statement> Finally
203 %type <statement_list> StatementList StatementList_opt
204 %type <parameter_list> FormalParameterList FormalParameterList_opt
205 %type <expr> Expression Expression_opt Expression_err
206 %type <expr> ExpressionNoIn ExpressionNoIn_opt
207 %type <expr> FunctionExpression
208 %type <expr> AssignmentExpression AssignmentExpressionNoIn
209 %type <expr> ConditionalExpression ConditionalExpressionNoIn
210 %type <expr> LeftHandSideExpression
211 %type <expr> LogicalORExpression LogicalORExpressionNoIn
212 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
213 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
214 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
215 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
216 %type <expr> EqualityExpression EqualityExpressionNoIn
217 %type <expr> RelationalExpression RelationalExpressionNoIn
218 %type <expr> ShiftExpression
219 %type <expr> AdditiveExpression
220 %type <expr> MultiplicativeExpression
221 %type <expr> Initialiser_opt Initialiser
222 %type <expr> InitialiserNoIn_opt InitialiserNoIn
223 %type <expr> UnaryExpression
224 %type <expr> PostfixExpression
225 %type <expr> NewExpression
226 %type <expr> CallExpression
227 %type <expr> MemberExpression
228 %type <expr> PrimaryExpression
229 %type <identifier> Identifier_opt
230 %type <variable_list> VariableDeclarationList
231 %type <variable_list> VariableDeclarationListNoIn
232 %type <variable_declaration> VariableDeclaration
233 %type <variable_declaration> VariableDeclarationNoIn
234 %type <case_list> CaseClausules CaseClausules_opt
235 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
236 %type <catch_block> Catch
237 %type <argument_list> Arguments
238 %type <argument_list> ArgumentList
239 %type <literal> Literal
240 %type <expr> ArrayLiteral
241 %type <expr> ObjectLiteral
242 %type <ival> Elision Elision_opt
243 %type <element_list> ElementList
244 %type <property_list> PropertyNameAndValueList
245 %type <literal> PropertyName
246 %type <literal> BooleanLiteral
247 %type <srcptr> KFunction
248 %type <ival> AssignOper
250 %nonassoc LOWER_THAN_ELSE
251 %nonassoc kELSE
255 /* ECMA-262 3rd Edition 14 */
256 Program
257 : SourceElements HtmlComment
258 { program_parsed(ctx, $1); }
260 HtmlComment
261 : tHTMLCOMMENT {}
262 | /* empty */ {}
264 /* ECMA-262 3rd Edition 14 */
265 SourceElements
266 : /* empty */ { $$ = new_source_elements(ctx); }
267 | SourceElements Statement
268 { $$ = source_elements_add_statement($1, $2); }
270 /* ECMA-262 3rd Edition 13 */
271 FunctionExpression
272 : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
273 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
275 KFunction
276 : kFUNCTION { push_func(ctx); $$ = $1; }
278 /* ECMA-262 3rd Edition 13 */
279 FunctionBody
280 : SourceElements { $$ = function_body_parsed(ctx, $1); }
282 /* ECMA-262 3rd Edition 13 */
283 FormalParameterList
284 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
285 | FormalParameterList ',' tIdentifier
286 { $$ = parameter_list_add(ctx, $1, $3); }
288 /* ECMA-262 3rd Edition 13 */
289 FormalParameterList_opt
290 : /* empty */ { $$ = NULL; }
291 | FormalParameterList { $$ = $1; }
293 /* ECMA-262 3rd Edition 12 */
294 Statement
295 : Block { $$ = $1; }
296 | VariableStatement { $$ = $1; }
297 | EmptyStatement { $$ = $1; }
298 | FunctionExpression { $$ = new_empty_statement(ctx); } /* FIXME: return NULL */
299 | ExpressionStatement { $$ = $1; }
300 | IfStatement { $$ = $1; }
301 | IterationStatement { $$ = $1; }
302 | ContinueStatement { $$ = $1; }
303 | BreakStatement { $$ = $1; }
304 | ReturnStatement { $$ = $1; }
305 | WithStatement { $$ = $1; }
306 | LabelledStatement { $$ = $1; }
307 | SwitchStatement { $$ = $1; }
308 | ThrowStatement { $$ = $1; }
309 | TryStatement { $$ = $1; }
311 /* ECMA-262 3rd Edition 12.2 */
312 StatementList
313 : Statement { $$ = new_statement_list(ctx, $1); }
314 | StatementList Statement
315 { $$ = statement_list_add($1, $2); }
317 /* ECMA-262 3rd Edition 12.2 */
318 StatementList_opt
319 : /* empty */ { $$ = NULL; }
320 | StatementList { $$ = $1; }
322 /* ECMA-262 3rd Edition 12.1 */
323 Block
324 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
325 | '{' '}' { $$ = new_block_statement(ctx, NULL); }
327 /* ECMA-262 3rd Edition 12.2 */
328 VariableStatement
329 : kVAR VariableDeclarationList semicolon_opt
330 { $$ = new_var_statement(ctx, $2); }
332 /* ECMA-262 3rd Edition 12.2 */
333 VariableDeclarationList
334 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
335 | VariableDeclarationList ',' VariableDeclaration
336 { $$ = variable_list_add(ctx, $1, $3); }
338 /* ECMA-262 3rd Edition 12.2 */
339 VariableDeclarationListNoIn
340 : VariableDeclarationNoIn
341 { $$ = new_variable_list(ctx, $1); }
342 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
343 { $$ = variable_list_add(ctx, $1, $3); }
345 /* ECMA-262 3rd Edition 12.2 */
346 VariableDeclaration
347 : tIdentifier Initialiser_opt
348 { $$ = new_variable_declaration(ctx, $1, $2); }
350 /* ECMA-262 3rd Edition 12.2 */
351 VariableDeclarationNoIn
352 : tIdentifier InitialiserNoIn_opt
353 { $$ = new_variable_declaration(ctx, $1, $2); }
355 /* ECMA-262 3rd Edition 12.2 */
356 Initialiser_opt
357 : /* empty */ { $$ = NULL; }
358 | Initialiser { $$ = $1; }
360 /* ECMA-262 3rd Edition 12.2 */
361 Initialiser
362 : '=' AssignmentExpression
363 { $$ = $2; }
365 /* ECMA-262 3rd Edition 12.2 */
366 InitialiserNoIn_opt
367 : /* empty */ { $$ = NULL; }
368 | InitialiserNoIn { $$ = $1; }
370 /* ECMA-262 3rd Edition 12.2 */
371 InitialiserNoIn
372 : '=' AssignmentExpressionNoIn
373 { $$ = $2; }
375 /* ECMA-262 3rd Edition 12.3 */
376 EmptyStatement
377 : ';' { $$ = new_empty_statement(ctx); }
379 /* ECMA-262 3rd Edition 12.4 */
380 ExpressionStatement
381 : Expression semicolon_opt
382 { $$ = new_expression_statement(ctx, $1); }
384 /* ECMA-262 3rd Edition 12.5 */
385 IfStatement
386 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
387 { $$ = new_if_statement(ctx, $3, $5, $7); }
388 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
389 { $$ = new_if_statement(ctx, $3, $5, NULL); }
391 /* ECMA-262 3rd Edition 12.6 */
392 IterationStatement
393 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
394 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
395 | kWHILE left_bracket Expression_err right_bracket Statement
396 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
397 | kFOR left_bracket ExpressionNoIn_opt
398 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
399 semicolon Expression_opt
400 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
401 semicolon Expression_opt right_bracket Statement
402 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
403 | kFOR left_bracket kVAR VariableDeclarationListNoIn
404 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
405 semicolon Expression_opt
406 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
407 semicolon Expression_opt right_bracket Statement
408 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
409 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
410 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
411 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
412 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
414 /* ECMA-262 3rd Edition 12.7 */
415 ContinueStatement
416 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
417 { $$ = new_continue_statement(ctx, $2); }
419 /* ECMA-262 3rd Edition 12.8 */
420 BreakStatement
421 : kBREAK /* NONL */ Identifier_opt semicolon_opt
422 { $$ = new_break_statement(ctx, $2); }
424 /* ECMA-262 3rd Edition 12.9 */
425 ReturnStatement
426 : kRETURN /* NONL */ Expression_opt semicolon_opt
427 { $$ = new_return_statement(ctx, $2); }
429 /* ECMA-262 3rd Edition 12.10 */
430 WithStatement
431 : kWITH left_bracket Expression right_bracket Statement
432 { $$ = new_with_statement(ctx, $3, $5); }
434 /* ECMA-262 3rd Edition 12.12 */
435 LabelledStatement
436 : tIdentifier ':' Statement
437 { $$ = new_labelled_statement(ctx, $1, $3); }
439 /* ECMA-262 3rd Edition 12.11 */
440 SwitchStatement
441 : kSWITCH left_bracket Expression right_bracket CaseBlock
442 { $$ = new_switch_statement(ctx, $3, $5); }
444 /* ECMA-262 3rd Edition 12.11 */
445 CaseBlock
446 : '{' CaseClausules_opt '}'
447 { $$ = new_case_block(ctx, $2, NULL, NULL); }
448 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
449 { $$ = new_case_block(ctx, $2, $3, $4); }
451 /* ECMA-262 3rd Edition 12.11 */
452 CaseClausules_opt
453 : /* empty */ { $$ = NULL; }
454 | CaseClausules { $$ = $1; }
456 /* ECMA-262 3rd Edition 12.11 */
457 CaseClausules
458 : CaseClausule { $$ = new_case_list(ctx, $1); }
459 | CaseClausules CaseClausule
460 { $$ = case_list_add(ctx, $1, $2); }
462 /* ECMA-262 3rd Edition 12.11 */
463 CaseClausule
464 : kCASE Expression ':' StatementList_opt
465 { $$ = new_case_clausule(ctx, $2, $4); }
467 /* ECMA-262 3rd Edition 12.11 */
468 DefaultClausule
469 : kDEFAULT ':' StatementList_opt
470 { $$ = new_case_clausule(ctx, NULL, $3); }
472 /* ECMA-262 3rd Edition 12.13 */
473 ThrowStatement
474 : kTHROW /* NONL */ Expression semicolon_opt
475 { $$ = new_throw_statement(ctx, $2); }
477 /* ECMA-262 3rd Edition 12.14 */
478 TryStatement
479 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
480 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
481 | kTRY Block Catch Finally
482 { $$ = new_try_statement(ctx, $2, $3, $4); }
484 /* ECMA-262 3rd Edition 12.14 */
485 Catch
486 : kCATCH left_bracket tIdentifier right_bracket Block
487 { $$ = new_catch_block(ctx, $3, $5); }
489 /* ECMA-262 3rd Edition 12.14 */
490 Finally
491 : kFINALLY Block { $$ = $2; }
493 /* ECMA-262 3rd Edition 11.14 */
494 Expression_opt
495 : /* empty */ { $$ = NULL; }
496 | Expression { $$ = $1; }
498 Expression_err
499 : Expression { $$ = $1; }
500 | error { set_error(ctx, IDS_SYNTAX_ERROR); YYABORT; }
502 /* ECMA-262 3rd Edition 11.14 */
503 Expression
504 : AssignmentExpression { $$ = $1; }
505 | Expression ',' AssignmentExpression
506 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
508 /* ECMA-262 3rd Edition 11.14 */
509 ExpressionNoIn_opt
510 : /* empty */ { $$ = NULL; }
511 | ExpressionNoIn { $$ = $1; }
513 /* ECMA-262 3rd Edition 11.14 */
514 ExpressionNoIn
515 : AssignmentExpressionNoIn
516 { $$ = $1; }
517 | ExpressionNoIn ',' AssignmentExpressionNoIn
518 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
520 AssignOper
521 : tAssignOper { $$ = $1; }
522 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
524 /* ECMA-262 3rd Edition 11.13 */
525 AssignmentExpression
526 : ConditionalExpression { $$ = $1; }
527 | LeftHandSideExpression '=' AssignmentExpression
528 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
529 | LeftHandSideExpression AssignOper AssignmentExpression
530 { $$ = new_binary_expression(ctx, $2, $1, $3); }
532 /* ECMA-262 3rd Edition 11.13 */
533 AssignmentExpressionNoIn
534 : ConditionalExpressionNoIn
535 { $$ = $1; }
536 | LeftHandSideExpression '=' AssignmentExpressionNoIn
537 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
538 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
539 { $$ = new_binary_expression(ctx, $2, $1, $3); }
541 /* ECMA-262 3rd Edition 11.12 */
542 ConditionalExpression
543 : LogicalORExpression { $$ = $1; }
544 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
545 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
547 /* ECMA-262 3rd Edition 11.12 */
548 ConditionalExpressionNoIn
549 : LogicalORExpressionNoIn
550 { $$ = $1; }
551 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
552 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
554 /* ECMA-262 3rd Edition 11.11 */
555 LogicalORExpression
556 : LogicalANDExpression { $$ = $1; }
557 | LogicalORExpression tOROR LogicalANDExpression
558 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
560 /* ECMA-262 3rd Edition 11.11 */
561 LogicalORExpressionNoIn
562 : LogicalANDExpressionNoIn
563 { $$ = $1; }
564 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
565 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
567 /* ECMA-262 3rd Edition 11.11 */
568 LogicalANDExpression
569 : BitwiseORExpression { $$ = $1; }
570 | LogicalANDExpression tANDAND BitwiseORExpression
571 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
573 /* ECMA-262 3rd Edition 11.11 */
574 LogicalANDExpressionNoIn
575 : BitwiseORExpressionNoIn
576 { $$ = $1; }
577 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
578 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
580 /* ECMA-262 3rd Edition 11.10 */
581 BitwiseORExpression
582 : BitwiseXORExpression { $$ = $1; }
583 | BitwiseORExpression '|' BitwiseXORExpression
584 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
586 /* ECMA-262 3rd Edition 11.10 */
587 BitwiseORExpressionNoIn
588 : BitwiseXORExpressionNoIn
589 { $$ = $1; }
590 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
591 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
593 /* ECMA-262 3rd Edition 11.10 */
594 BitwiseXORExpression
595 : BitwiseANDExpression { $$ = $1; }
596 | BitwiseXORExpression '^' BitwiseANDExpression
597 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
599 /* ECMA-262 3rd Edition 11.10 */
600 BitwiseXORExpressionNoIn
601 : BitwiseANDExpressionNoIn
602 { $$ = $1; }
603 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
604 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
606 /* ECMA-262 3rd Edition 11.10 */
607 BitwiseANDExpression
608 : EqualityExpression { $$ = $1; }
609 | BitwiseANDExpression '&' EqualityExpression
610 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
612 /* ECMA-262 3rd Edition 11.10 */
613 BitwiseANDExpressionNoIn
614 : EqualityExpressionNoIn
615 { $$ = $1; }
616 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
617 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
619 /* ECMA-262 3rd Edition 11.9 */
620 EqualityExpression
621 : RelationalExpression { $$ = $1; }
622 | EqualityExpression tEqOper RelationalExpression
623 { $$ = new_binary_expression(ctx, $2, $1, $3); }
625 /* ECMA-262 3rd Edition 11.9 */
626 EqualityExpressionNoIn
627 : RelationalExpressionNoIn { $$ = $1; }
628 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
629 { $$ = new_binary_expression(ctx, $2, $1, $3); }
631 /* ECMA-262 3rd Edition 11.8 */
632 RelationalExpression
633 : ShiftExpression { $$ = $1; }
634 | RelationalExpression tRelOper ShiftExpression
635 { $$ = new_binary_expression(ctx, $2, $1, $3); }
636 | RelationalExpression kINSTANCEOF ShiftExpression
637 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
638 | RelationalExpression kIN ShiftExpression
639 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
641 /* ECMA-262 3rd Edition 11.8 */
642 RelationalExpressionNoIn
643 : ShiftExpression { $$ = $1; }
644 | RelationalExpressionNoIn tRelOper ShiftExpression
645 { $$ = new_binary_expression(ctx, $2, $1, $3); }
646 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
647 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
649 /* ECMA-262 3rd Edition 11.7 */
650 ShiftExpression
651 : AdditiveExpression { $$ = $1; }
652 | ShiftExpression tShiftOper AdditiveExpression
653 { $$ = new_binary_expression(ctx, $2, $1, $3); }
655 /* ECMA-262 3rd Edition 11.6 */
656 AdditiveExpression
657 : MultiplicativeExpression
658 { $$ = $1; }
659 | AdditiveExpression '+' MultiplicativeExpression
660 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
661 | AdditiveExpression '-' MultiplicativeExpression
662 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
664 /* ECMA-262 3rd Edition 11.5 */
665 MultiplicativeExpression
666 : UnaryExpression { $$ = $1; }
667 | MultiplicativeExpression '*' UnaryExpression
668 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
669 | MultiplicativeExpression '/' UnaryExpression
670 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
671 | MultiplicativeExpression '%' UnaryExpression
672 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
674 /* ECMA-262 3rd Edition 11.4 */
675 UnaryExpression
676 : PostfixExpression { $$ = $1; }
677 | kDELETE UnaryExpression
678 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
679 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
680 | kTYPEOF UnaryExpression
681 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
682 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
683 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
684 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
685 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
686 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
687 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
689 /* ECMA-262 3rd Edition 11.2 */
690 PostfixExpression
691 : LeftHandSideExpression
692 { $$ = $1; }
693 | LeftHandSideExpression /* NONL */ tINC
694 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
695 | LeftHandSideExpression /* NONL */ tDEC
696 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
699 /* ECMA-262 3rd Edition 11.2 */
700 LeftHandSideExpression
701 : NewExpression { $$ = $1; }
702 | CallExpression { $$ = $1; }
704 /* ECMA-262 3rd Edition 11.2 */
705 NewExpression
706 : MemberExpression { $$ = $1; }
707 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
709 /* ECMA-262 3rd Edition 11.2 */
710 MemberExpression
711 : PrimaryExpression { $$ = $1; }
712 | FunctionExpression { $$ = $1; }
713 | MemberExpression '[' Expression ']'
714 { $$ = new_array_expression(ctx, $1, $3); }
715 | MemberExpression '.' tIdentifier
716 { $$ = new_member_expression(ctx, $1, $3); }
717 | kNEW MemberExpression Arguments
718 { $$ = new_new_expression(ctx, $2, $3); }
720 /* ECMA-262 3rd Edition 11.2 */
721 CallExpression
722 : MemberExpression Arguments
723 { $$ = new_call_expression(ctx, $1, $2); }
724 | CallExpression Arguments
725 { $$ = new_call_expression(ctx, $1, $2); }
726 | CallExpression '[' Expression ']'
727 { $$ = new_array_expression(ctx, $1, $3); }
728 | CallExpression '.' tIdentifier
729 { $$ = new_member_expression(ctx, $1, $3); }
731 /* ECMA-262 3rd Edition 11.2 */
732 Arguments
733 : '(' ')' { $$ = NULL; }
734 | '(' ArgumentList ')' { $$ = $2; }
736 /* ECMA-262 3rd Edition 11.2 */
737 ArgumentList
738 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
739 | ArgumentList ',' AssignmentExpression
740 { $$ = argument_list_add(ctx, $1, $3); }
742 /* ECMA-262 3rd Edition 11.1 */
743 PrimaryExpression
744 : kTHIS { $$ = new_this_expression(ctx); }
745 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
746 | Literal { $$ = new_literal_expression(ctx, $1); }
747 | ArrayLiteral { $$ = $1; }
748 | ObjectLiteral { $$ = $1; }
749 | '(' Expression ')' { $$ = $2; }
751 /* ECMA-262 3rd Edition 11.1.4 */
752 ArrayLiteral
753 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
754 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
755 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
756 | '[' ElementList ',' Elision_opt ']'
757 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
759 /* ECMA-262 3rd Edition 11.1.4 */
760 ElementList
761 : Elision_opt AssignmentExpression
762 { $$ = new_element_list(ctx, $1, $2); }
763 | ElementList ',' Elision_opt AssignmentExpression
764 { $$ = element_list_add(ctx, $1, $3, $4); }
766 /* ECMA-262 3rd Edition 11.1.4 */
767 Elision
768 : ',' { $$ = 1; }
769 | Elision ',' { $$ = $1 + 1; }
771 /* ECMA-262 3rd Edition 11.1.4 */
772 Elision_opt
773 : /* empty */ { $$ = 0; }
774 | Elision { $$ = $1; }
776 /* ECMA-262 3rd Edition 11.1.5 */
777 ObjectLiteral
778 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
779 | '{' PropertyNameAndValueList '}'
780 { $$ = new_prop_and_value_expression(ctx, $2); }
782 /* ECMA-262 3rd Edition 11.1.5 */
783 PropertyNameAndValueList
784 : PropertyName ':' AssignmentExpression
785 { $$ = new_property_list(ctx, $1, $3); }
786 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
787 { $$ = property_list_add(ctx, $1, $3, $5); }
789 /* ECMA-262 3rd Edition 11.1.5 */
790 PropertyName
791 : tIdentifier { $$ = new_string_literal(ctx, $1); }
792 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
793 | tNumericLiteral { $$ = $1; }
795 /* ECMA-262 3rd Edition 7.6 */
796 Identifier_opt
797 : /* empty*/ { $$ = NULL; }
798 | tIdentifier { $$ = $1; }
800 /* ECMA-262 3rd Edition 7.8 */
801 Literal
802 : kNULL { $$ = new_null_literal(ctx); }
803 | kUNDEFINED { $$ = new_undefined_literal(ctx); }
804 | BooleanLiteral { $$ = $1; }
805 | tNumericLiteral { $$ = $1; }
806 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
807 | '/' { $$ = parse_regexp(ctx);
808 if(!$$) YYABORT; }
809 | kDIVEQ { $$ = parse_regexp(ctx);
810 if(!$$) YYABORT; }
812 /* ECMA-262 3rd Edition 7.8.2 */
813 BooleanLiteral
814 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
815 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
817 semicolon_opt
818 : ';'
819 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
821 left_bracket
822 : '('
823 | error { set_error(ctx, IDS_LBRACKET); YYABORT; }
825 right_bracket
826 : ')'
827 | error { set_error(ctx, IDS_RBRACKET); YYABORT; }
829 semicolon
830 : ';'
831 | error { set_error(ctx, IDS_SEMICOLON); YYABORT; }
835 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
837 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
840 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
842 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
844 ret->vt = VT_BSTR;
845 ret->u.wstr = str;
847 return ret;
850 static literal_t *new_null_literal(parser_ctx_t *ctx)
852 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
854 ret->vt = VT_NULL;
856 return ret;
859 static literal_t *new_undefined_literal(parser_ctx_t *ctx)
861 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
863 ret->vt = VT_EMPTY;
865 return ret;
868 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
870 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
872 ret->vt = VT_BOOL;
873 ret->u.bval = bval;
875 return ret;
878 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
880 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
882 ret->name = name;
883 ret->value = value;
884 ret->next = NULL;
886 return ret;
889 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
891 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
893 ret->head = ret->tail = new_prop_val(ctx, name, value);
895 return ret;
898 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
900 list->tail = list->tail->next = new_prop_val(ctx, name, value);
902 return list;
905 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
907 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
909 ret->elision = elision;
910 ret->expr = expr;
911 ret->next = NULL;
913 return ret;
916 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
918 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
920 ret->head = ret->tail = new_array_element(ctx, elision, expr);
922 return ret;
925 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
927 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
929 return list;
932 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
934 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
936 ret->expr = expr;
937 ret->next = NULL;
939 return ret;
942 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
944 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
946 ret->head = ret->tail = new_argument(ctx, expr);
948 return ret;
951 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
953 list->tail = list->tail->next = new_argument(ctx, expr);
955 return list;
958 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
960 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
962 ret->identifier = identifier;
963 ret->statement = statement;
965 return ret;
968 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
970 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
972 ret->expr = expr;
973 ret->stat = stat_list ? stat_list->head : NULL;
974 ret->next = NULL;
976 return ret;
979 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
981 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
983 ret->head = ret->tail = case_clausule;
985 return ret;
988 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
990 list->tail = list->tail->next = case_clausule;
992 return list;
995 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
996 case_clausule_t *default_clausule, case_list_t *case_list2)
998 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
999 statement_t *stat = NULL;
1001 if(case_list1) {
1002 ret = case_list1->head;
1003 iter = case_list1->tail;
1006 if(default_clausule) {
1007 if(ret)
1008 iter = iter->next = default_clausule;
1009 else
1010 ret = iter = default_clausule;
1013 if(case_list2) {
1014 if(ret)
1015 iter->next = case_list2->head;
1016 else
1017 ret = case_list2->head;
1020 if(!ret)
1021 return NULL;
1023 for(iter = ret; iter; iter = iter->next) {
1024 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1025 if(!iter2)
1026 break;
1028 while(iter != iter2) {
1029 iter->stat = iter2->stat;
1030 iter = iter->next;
1033 if(stat) {
1034 while(stat->next)
1035 stat = stat->next;
1036 stat->next = iter->stat;
1037 }else {
1038 stat = iter->stat;
1042 return ret;
1045 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1047 block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1049 ret->stat.eval = block_statement_eval;
1050 ret->stat.next = NULL;
1051 ret->stat_list = list ? list->head : NULL;
1053 return &ret->stat;
1056 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1058 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1059 var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1061 ret->identifier = identifier;
1062 ret->expr = expr;
1063 ret->next = NULL;
1065 var_list->identifier = identifier;
1066 var_list->next = NULL;
1068 if(ctx->func_stack->var_tail)
1069 ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1070 else
1071 ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1073 return ret;
1076 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1078 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1080 ret->head = ret->tail = decl;
1082 return ret;
1085 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1087 list->tail = list->tail->next = decl;
1089 return list;
1092 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1094 var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1096 ret->stat.eval = var_statement_eval;
1097 ret->stat.next = NULL;
1098 ret->variable_list = variable_list->head;
1100 return &ret->stat;
1103 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1105 statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1107 ret->eval = empty_statement_eval;
1108 ret->next = NULL;
1110 return ret;
1113 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1115 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1117 ret->stat.eval = expression_statement_eval;
1118 ret->stat.next = NULL;
1119 ret->expr = expr;
1121 return &ret->stat;
1124 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1126 if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1128 ret->stat.eval = if_statement_eval;
1129 ret->stat.next = NULL;
1130 ret->expr = expr;
1131 ret->if_stat = if_stat;
1132 ret->else_stat = else_stat;
1134 return &ret->stat;
1137 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1139 while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1141 ret->stat.eval = while_statement_eval;
1142 ret->stat.next = NULL;
1143 ret->do_while = dowhile;
1144 ret->expr = expr;
1145 ret->statement = stat;
1147 return &ret->stat;
1150 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1151 expression_t *expr, expression_t *end_expr, statement_t *statement)
1153 for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1155 ret->stat.eval = for_statement_eval;
1156 ret->stat.next = NULL;
1157 ret->variable_list = variable_list ? variable_list->head : NULL;
1158 ret->begin_expr = begin_expr;
1159 ret->expr = expr;
1160 ret->end_expr = end_expr;
1161 ret->statement = statement;
1163 return &ret->stat;
1166 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1167 expression_t *in_expr, statement_t *statement)
1169 forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1171 ret->stat.eval = forin_statement_eval;
1172 ret->stat.next = NULL;
1173 ret->variable = variable;
1174 ret->expr = expr;
1175 ret->in_expr = in_expr;
1176 ret->statement = statement;
1178 return &ret->stat;
1181 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1183 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1185 ret->stat.eval = continue_statement_eval;
1186 ret->stat.next = NULL;
1187 ret->identifier = identifier;
1189 return &ret->stat;
1192 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1194 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1196 ret->stat.eval = break_statement_eval;
1197 ret->stat.next = NULL;
1198 ret->identifier = identifier;
1200 return &ret->stat;
1203 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1205 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1207 ret->stat.eval = return_statement_eval;
1208 ret->stat.next = NULL;
1209 ret->expr = expr;
1211 return &ret->stat;
1214 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1216 with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1218 ret->stat.eval = with_statement_eval;
1219 ret->stat.next = NULL;
1220 ret->expr = expr;
1221 ret->statement = statement;
1223 return &ret->stat;
1226 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1228 labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1230 ret->stat.eval = labelled_statement_eval;
1231 ret->stat.next = NULL;
1232 ret->identifier = identifier;
1233 ret->statement = statement;
1235 return &ret->stat;
1238 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1240 switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1242 ret->stat.eval = switch_statement_eval;
1243 ret->stat.next = NULL;
1244 ret->expr = expr;
1245 ret->case_list = case_list;
1247 return &ret->stat;
1250 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1252 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1254 ret->stat.eval = throw_statement_eval;
1255 ret->stat.next = NULL;
1256 ret->expr = expr;
1258 return &ret->stat;
1261 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1262 catch_block_t *catch_block, statement_t *finally_statement)
1264 try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1266 ret->stat.eval = try_statement_eval;
1267 ret->stat.next = NULL;
1268 ret->try_statement = try_statement;
1269 ret->catch_block = catch_block;
1270 ret->finally_statement = finally_statement;
1272 return &ret->stat;
1275 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1277 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1279 ret->identifier = identifier;
1280 ret->next = NULL;
1282 return ret;
1285 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1287 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1289 ret->head = ret->tail = new_parameter(ctx, identifier);
1291 return ret;
1294 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1296 list->tail = list->tail->next = new_parameter(ctx, identifier);
1298 return list;
1301 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1302 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1304 function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1306 ret->expr.eval = function_expression_eval;
1307 ret->identifier = identifier;
1308 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1309 ret->source_elements = source_elements;
1310 ret->src_str = src_str;
1311 ret->src_len = src_len;
1313 if(ret->identifier) {
1314 function_declaration_t *decl = parser_alloc(ctx, sizeof(function_declaration_t));
1316 decl->expr = ret;
1317 decl->next = NULL;
1319 if(ctx->func_stack->func_tail)
1320 ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
1321 else
1322 ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1325 return &ret->expr;
1328 static const expression_eval_t expression_eval_table[] = {
1329 comma_expression_eval,
1330 logical_or_expression_eval,
1331 logical_and_expression_eval,
1332 binary_or_expression_eval,
1333 binary_xor_expression_eval,
1334 binary_and_expression_eval,
1335 instanceof_expression_eval,
1336 in_expression_eval,
1337 add_expression_eval,
1338 sub_expression_eval,
1339 mul_expression_eval,
1340 div_expression_eval,
1341 mod_expression_eval,
1342 delete_expression_eval,
1343 void_expression_eval,
1344 typeof_expression_eval,
1345 minus_expression_eval,
1346 plus_expression_eval,
1347 post_increment_expression_eval,
1348 post_decrement_expression_eval,
1349 pre_increment_expression_eval,
1350 pre_decrement_expression_eval,
1351 equal_expression_eval,
1352 equal2_expression_eval,
1353 not_equal_expression_eval,
1354 not_equal2_expression_eval,
1355 less_expression_eval,
1356 lesseq_expression_eval,
1357 greater_expression_eval,
1358 greatereq_expression_eval,
1359 binary_negation_expression_eval,
1360 logical_negation_expression_eval,
1361 left_shift_expression_eval,
1362 right_shift_expression_eval,
1363 right2_shift_expression_eval,
1364 assign_expression_eval,
1365 assign_lshift_expression_eval,
1366 assign_rshift_expression_eval,
1367 assign_rrshift_expression_eval,
1368 assign_add_expression_eval,
1369 assign_sub_expression_eval,
1370 assign_mul_expression_eval,
1371 assign_div_expression_eval,
1372 assign_mod_expression_eval,
1373 assign_and_expression_eval,
1374 assign_or_expression_eval,
1375 assign_xor_expression_eval,
1378 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1379 expression_t *expression1, expression_t *expression2)
1381 binary_expression_t *ret = parser_alloc(ctx, sizeof(binary_expression_t));
1383 ret->expr.eval = expression_eval_table[type];
1384 ret->expression1 = expression1;
1385 ret->expression2 = expression2;
1387 return &ret->expr;
1390 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1392 unary_expression_t *ret = parser_alloc(ctx, sizeof(unary_expression_t));
1394 ret->expr.eval = expression_eval_table[type];
1395 ret->expression = expression;
1397 return &ret->expr;
1400 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1401 expression_t *true_expression, expression_t *false_expression)
1403 conditional_expression_t *ret = parser_alloc(ctx, sizeof(conditional_expression_t));
1405 ret->expr.eval = conditional_expression_eval;
1406 ret->expression = expression;
1407 ret->true_expression = true_expression;
1408 ret->false_expression = false_expression;
1410 return &ret->expr;
1413 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1415 array_expression_t *ret = parser_alloc(ctx, sizeof(array_expression_t));
1417 ret->expr.eval = array_expression_eval;
1418 ret->member_expr = member_expr;
1419 ret->expression = expression;
1421 return &ret->expr;
1424 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1426 member_expression_t *ret = parser_alloc(ctx, sizeof(member_expression_t));
1428 ret->expr.eval = member_expression_eval;
1429 ret->expression = expression;
1430 ret->identifier = identifier;
1432 return &ret->expr;
1435 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1437 call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1439 ret->expr.eval = new_expression_eval;
1440 ret->expression = expression;
1441 ret->argument_list = argument_list ? argument_list->head : NULL;
1443 return &ret->expr;
1446 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1448 call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1450 ret->expr.eval = call_expression_eval;
1451 ret->expression = expression;
1452 ret->argument_list = argument_list ? argument_list->head : NULL;
1454 return &ret->expr;
1457 static expression_t *new_this_expression(parser_ctx_t *ctx)
1459 expression_t *ret = parser_alloc(ctx, sizeof(expression_t));
1461 ret->eval = this_expression_eval;
1463 return ret;
1466 static int parser_error(const char *str)
1468 return 0;
1471 static void set_error(parser_ctx_t *ctx, UINT error)
1473 ctx->hres = JSCRIPT_ERROR|error;
1476 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1478 if(obj || *(ctx->ptr-1)==next) return TRUE;
1480 set_error(ctx, IDS_SYNTAX_ERROR);
1481 return FALSE;
1485 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1487 identifier_expression_t *ret = parser_alloc(ctx, sizeof(identifier_expression_t));
1489 ret->expr.eval = identifier_expression_eval;
1490 ret->identifier = identifier;
1492 return &ret->expr;
1495 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1497 array_literal_expression_t *ret = parser_alloc(ctx, sizeof(array_literal_expression_t));
1499 ret->expr.eval = array_literal_expression_eval;
1500 ret->element_list = element_list ? element_list->head : NULL;
1501 ret->length = length;
1503 return &ret->expr;
1506 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1508 property_value_expression_t *ret = parser_alloc(ctx, sizeof(property_value_expression_t));
1510 ret->expr.eval = property_value_expression_eval;
1511 ret->property_list = property_list ? property_list->head : NULL;
1513 return &ret->expr;
1516 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1518 literal_expression_t *ret = parser_alloc(ctx, sizeof(literal_expression_t));
1520 ret->expr.eval = literal_expression_eval;
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 static void push_func(parser_ctx_t *ctx)
1563 func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1565 new_func->func_head = new_func->func_tail = NULL;
1566 new_func->var_head = new_func->var_tail = NULL;
1568 new_func->next = ctx->func_stack;
1569 ctx->func_stack = new_func;
1572 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1574 source->functions = ctx->func_stack->func_head;
1575 source->variables = ctx->func_stack->var_head;
1576 pop_func(ctx);
1578 return source;
1581 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1583 source->functions = ctx->func_stack->func_head;
1584 source->variables = ctx->func_stack->var_head;
1585 pop_func(ctx);
1587 ctx->source = source;
1588 if(!ctx->lexer_error)
1589 ctx->hres = S_OK;
1592 void parser_release(parser_ctx_t *ctx)
1594 obj_literal_t *iter;
1596 if(--ctx->ref)
1597 return;
1599 for(iter = ctx->obj_literals; iter; iter = iter->next)
1600 jsdisp_release(iter->obj);
1602 jsheap_free(&ctx->heap);
1603 heap_free(ctx);
1606 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter,
1607 parser_ctx_t **ret)
1609 parser_ctx_t *parser_ctx;
1610 jsheap_t *mark;
1611 HRESULT hres;
1613 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1615 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1616 if(!parser_ctx)
1617 return E_OUTOFMEMORY;
1619 parser_ctx->ref = 1;
1620 parser_ctx->hres = JSCRIPT_ERROR|IDS_SYNTAX_ERROR;
1621 parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1623 parser_ctx->begin = parser_ctx->ptr = code;
1624 parser_ctx->end = code + strlenW(code);
1626 script_addref(ctx);
1627 parser_ctx->script = ctx;
1629 mark = jsheap_mark(&ctx->tmp_heap);
1630 jsheap_init(&parser_ctx->heap);
1632 push_func(parser_ctx);
1634 parser_parse(parser_ctx);
1635 jsheap_clear(mark);
1636 if(FAILED(parser_ctx->hres)) {
1637 hres = parser_ctx->hres;
1638 parser_release(parser_ctx);
1639 return hres;
1642 *ret = parser_ctx;
1643 return S_OK;