winecfg: Renamed 'Shell Folder' to 'Folder'.
[wine/multimedia.git] / dlls / jscript / parser.y
blob7a74e467e0617891966a58c802ee6ccc2ae76cb8
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 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 #define YYLEX_PARAM ctx
29 #define YYPARSE_PARAM ctx
31 static int parser_error(const char*);
32 static BOOL allow_auto_semicolon(parser_ctx_t*);
33 static void program_parsed(parser_ctx_t*,source_elements_t*);
34 static source_elements_t *function_body_parsed(parser_ctx_t*,source_elements_t*);
36 typedef struct _statement_list_t {
37 statement_t *head;
38 statement_t *tail;
39 } statement_list_t;
41 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
42 static literal_t *new_null_literal(parser_ctx_t*);
43 static literal_t *new_undefined_literal(parser_ctx_t*);
44 static literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL);
46 typedef struct _property_list_t {
47 prop_val_t *head;
48 prop_val_t *tail;
49 } property_list_t;
51 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
52 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
54 typedef struct _element_list_t {
55 array_element_t *head;
56 array_element_t *tail;
57 } element_list_t;
59 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
60 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
62 typedef struct _argument_list_t {
63 argument_t *head;
64 argument_t *tail;
65 } argument_list_t;
67 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
68 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
70 typedef struct _case_list_t {
71 case_clausule_t *head;
72 case_clausule_t *tail;
73 } case_list_t;
75 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
76 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
77 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
78 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
79 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
81 typedef struct _variable_list_t {
82 variable_declaration_t *head;
83 variable_declaration_t *tail;
84 } variable_list_t;
86 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
87 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
88 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
90 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
91 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
92 static statement_t *new_empty_statement(parser_ctx_t*);
93 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
94 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
95 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
96 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
97 expression_t*,statement_t*);
98 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
99 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
100 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
101 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
102 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
103 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
104 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
105 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
106 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
108 struct statement_list_t {
109 statement_t *head;
110 statement_t *tail;
113 statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
114 statement_list_t *statement_list_add(statement_list_t*,statement_t*);
116 typedef struct _parameter_list_t {
117 parameter_t *head;
118 parameter_t *tail;
119 } parameter_list_t;
121 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
122 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
124 static void push_func(parser_ctx_t*);
125 static inline void pop_func(parser_ctx_t *ctx)
127 ctx->func_stack = ctx->func_stack->next;
130 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
131 source_elements_t*,const WCHAR*,DWORD);
132 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
133 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
134 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
135 static expression_t *new_array_expression(parser_ctx_t*,expression_t*,expression_t*);
136 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
137 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
138 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
139 static expression_t *new_this_expression(parser_ctx_t*);
140 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
141 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
142 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
143 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
145 static source_elements_t *new_source_elements(parser_ctx_t*);
146 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
150 %pure_parser
151 %start Program
153 %union {
154 int ival;
155 const WCHAR *srcptr;
156 LPCWSTR wstr;
157 literal_t *literal;
158 struct _argument_list_t *argument_list;
159 case_clausule_t *case_clausule;
160 struct _case_list_t *case_list;
161 catch_block_t *catch_block;
162 struct _element_list_t *element_list;
163 expression_t *expr;
164 const WCHAR *identifier;
165 struct _parameter_list_t *parameter_list;
166 struct _property_list_t *property_list;
167 source_elements_t *source_elements;
168 statement_t *statement;
169 struct _statement_list_t *statement_list;
170 struct _variable_list_t *variable_list;
171 variable_declaration_t *variable_declaration;
174 /* keywords */
175 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
176 %token kINSTANCEOF kNEW kNULL kUNDEFINED kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
177 %token tANDAND tOROR tINC tDEC
179 %token <srcptr> kFUNCTION '}'
181 /* tokens */
182 %token <identifier> tIdentifier
183 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
184 %token <literal> tNumericLiteral
185 %token <wstr> tStringLiteral
187 %type <source_elements> SourceElements
188 %type <source_elements> FunctionBody
189 %type <statement> Statement
190 %type <statement> Block
191 %type <statement> VariableStatement
192 %type <statement> EmptyStatement
193 %type <statement> ExpressionStatement
194 %type <statement> IfStatement
195 %type <statement> IterationStatement
196 %type <statement> ContinueStatement
197 %type <statement> BreakStatement
198 %type <statement> ReturnStatement
199 %type <statement> WithStatement
200 %type <statement> LabelledStatement
201 %type <statement> SwitchStatement
202 %type <statement> ThrowStatement
203 %type <statement> TryStatement
204 %type <statement> Finally
205 %type <statement_list> StatementList StatementList_opt
206 %type <parameter_list> FormalParameterList FormalParameterList_opt
207 %type <expr> Expression Expression_opt
208 %type <expr> ExpressionNoIn ExpressionNoIn_opt
209 %type <expr> FunctionExpression
210 %type <expr> AssignmentExpression AssignmentExpressionNoIn
211 %type <expr> ConditionalExpression ConditionalExpressionNoIn
212 %type <expr> LeftHandSideExpression
213 %type <expr> LogicalORExpression LogicalORExpressionNoIn
214 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
215 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
216 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
217 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
218 %type <expr> EqualityExpression EqualityExpressionNoIn
219 %type <expr> RelationalExpression RelationalExpressionNoIn
220 %type <expr> ShiftExpression
221 %type <expr> AdditiveExpression
222 %type <expr> MultiplicativeExpression
223 %type <expr> Initialiser_opt Initialiser
224 %type <expr> InitialiserNoIn_opt InitialiserNoIn
225 %type <expr> UnaryExpression
226 %type <expr> PostfixExpression
227 %type <expr> NewExpression
228 %type <expr> CallExpression
229 %type <expr> MemberExpression
230 %type <expr> PrimaryExpression
231 %type <identifier> Identifier_opt
232 %type <variable_list> VariableDeclarationList
233 %type <variable_list> VariableDeclarationListNoIn
234 %type <variable_declaration> VariableDeclaration
235 %type <variable_declaration> VariableDeclarationNoIn
236 %type <case_list> CaseClausules CaseClausules_opt
237 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
238 %type <catch_block> Catch
239 %type <argument_list> Arguments
240 %type <argument_list> ArgumentList
241 %type <literal> Literal
242 %type <expr> ArrayLiteral
243 %type <expr> ObjectLiteral
244 %type <ival> Elision Elision_opt
245 %type <element_list> ElementList
246 %type <property_list> PropertyNameAndValueList
247 %type <literal> PropertyName
248 %type <literal> BooleanLiteral
249 %type <srcptr> KFunction
251 %nonassoc LOWER_THAN_ELSE
252 %nonassoc kELSE
256 /* ECMA-262 3rd Edition 14 */
257 Program
258 : SourceElements { program_parsed(ctx, $1); }
260 /* ECMA-262 3rd Edition 14 */
261 SourceElements
262 : /* empty */ { $$ = new_source_elements(ctx); }
263 | SourceElements Statement
264 { $$ = source_elements_add_statement($1, $2); }
266 /* ECMA-262 3rd Edition 13 */
267 FunctionExpression
268 : KFunction Identifier_opt '(' FormalParameterList_opt ')' '{' FunctionBody '}'
269 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
271 KFunction
272 : kFUNCTION { push_func(ctx); $$ = $1; }
274 /* ECMA-262 3rd Edition 13 */
275 FunctionBody
276 : SourceElements { $$ = function_body_parsed(ctx, $1); }
278 /* ECMA-262 3rd Edition 13 */
279 FormalParameterList
280 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
281 | FormalParameterList ',' tIdentifier
282 { $$ = parameter_list_add(ctx, $1, $3); }
284 /* ECMA-262 3rd Edition 13 */
285 FormalParameterList_opt
286 : /* empty */ { $$ = NULL; }
287 | FormalParameterList { $$ = $1; }
289 /* ECMA-262 3rd Edition 12 */
290 Statement
291 : Block { $$ = $1; }
292 | VariableStatement { $$ = $1; }
293 | EmptyStatement { $$ = $1; }
294 | ExpressionStatement { $$ = $1; }
295 | IfStatement { $$ = $1; }
296 | IterationStatement { $$ = $1; }
297 | ContinueStatement { $$ = $1; }
298 | BreakStatement { $$ = $1; }
299 | ReturnStatement { $$ = $1; }
300 | WithStatement { $$ = $1; }
301 | LabelledStatement { $$ = $1; }
302 | SwitchStatement { $$ = $1; }
303 | ThrowStatement { $$ = $1; }
304 | TryStatement { $$ = $1; }
306 /* ECMA-262 3rd Edition 12.2 */
307 StatementList
308 : Statement { $$ = new_statement_list(ctx, $1); }
309 | StatementList Statement
310 { $$ = statement_list_add($1, $2); }
312 /* ECMA-262 3rd Edition 12.2 */
313 StatementList_opt
314 : /* empty */ { $$ = NULL; }
315 | StatementList { $$ = $1; }
317 /* ECMA-262 3rd Edition 12.1 */
318 Block
319 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
320 | '{' '}' { $$ = new_block_statement(ctx, NULL) }
322 /* ECMA-262 3rd Edition 12.2 */
323 VariableStatement
324 : kVAR VariableDeclarationList semicolon_opt
325 { $$ = new_var_statement(ctx, $2); }
327 /* ECMA-262 3rd Edition 12.2 */
328 VariableDeclarationList
329 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
330 | VariableDeclarationList ',' VariableDeclaration
331 { $$ = variable_list_add(ctx, $1, $3); }
333 /* ECMA-262 3rd Edition 12.2 */
334 VariableDeclarationListNoIn
335 : VariableDeclarationNoIn
336 { $$ = new_variable_list(ctx, $1); }
337 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
338 { $$ = variable_list_add(ctx, $1, $3); }
340 /* ECMA-262 3rd Edition 12.2 */
341 VariableDeclaration
342 : tIdentifier Initialiser_opt
343 { $$ = new_variable_declaration(ctx, $1, $2); }
345 /* ECMA-262 3rd Edition 12.2 */
346 VariableDeclarationNoIn
347 : tIdentifier InitialiserNoIn_opt
348 { $$ = new_variable_declaration(ctx, $1, $2); }
350 /* ECMA-262 3rd Edition 12.2 */
351 Initialiser_opt
352 : /* empty */ { $$ = NULL; }
353 | Initialiser { $$ = $1; }
355 /* ECMA-262 3rd Edition 12.2 */
356 Initialiser
357 : '=' AssignmentExpression
358 { $$ = $2; }
360 /* ECMA-262 3rd Edition 12.2 */
361 InitialiserNoIn_opt
362 : /* empty */ { $$ = NULL; }
363 | InitialiserNoIn { $$ = $1; }
365 /* ECMA-262 3rd Edition 12.2 */
366 InitialiserNoIn
367 : '=' AssignmentExpressionNoIn
368 { $$ = $2; }
370 /* ECMA-262 3rd Edition 12.3 */
371 EmptyStatement
372 : ';' { $$ = new_empty_statement(ctx); }
374 /* ECMA-262 3rd Edition 12.4 */
375 ExpressionStatement
376 : Expression semicolon_opt
377 { $$ = new_expression_statement(ctx, $1); }
379 /* ECMA-262 3rd Edition 12.5 */
380 IfStatement
381 : kIF '(' Expression ')' Statement kELSE Statement
382 { $$ = new_if_statement(ctx, $3, $5, $7); }
383 | kIF '(' Expression ')' Statement %prec LOWER_THAN_ELSE
384 { $$ = new_if_statement(ctx, $3, $5, NULL); }
386 /* ECMA-262 3rd Edition 12.6 */
387 IterationStatement
388 : kDO Statement kWHILE '(' Expression ')' ';'
389 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
390 | kWHILE '(' Expression ')' Statement
391 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
392 | kFOR '(' ExpressionNoIn_opt ';' Expression_opt ';' Expression_opt ')' Statement
393 { $$ = new_for_statement(ctx, NULL, $3, $5, $7, $9); }
394 | kFOR '(' kVAR VariableDeclarationListNoIn ';' Expression_opt ';' Expression_opt ')' Statement
395 { $$ = new_for_statement(ctx, $4, NULL, $6, $8, $10); }
396 | kFOR '(' LeftHandSideExpression kIN Expression ')' Statement
397 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
398 | kFOR '(' kVAR VariableDeclarationNoIn kIN Expression ')' Statement
399 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
401 /* ECMA-262 3rd Edition 12.7 */
402 ContinueStatement
403 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
404 { $$ = new_continue_statement(ctx, $2); }
406 /* ECMA-262 3rd Edition 12.8 */
407 BreakStatement
408 : kBREAK /* NONL */ Identifier_opt semicolon_opt
409 { $$ = new_break_statement(ctx, $2); }
411 /* ECMA-262 3rd Edition 12.9 */
412 ReturnStatement
413 : kRETURN /* NONL */ Expression_opt semicolon_opt
414 { $$ = new_return_statement(ctx, $2); }
416 /* ECMA-262 3rd Edition 12.10 */
417 WithStatement
418 : kWITH '(' Expression ')' Statement
419 { $$ = new_with_statement(ctx, $3, $5); }
421 /* ECMA-262 3rd Edition 12.12 */
422 LabelledStatement
423 : tIdentifier ':' Statement
424 { $$ = new_labelled_statement(ctx, $1, $3); }
426 /* ECMA-262 3rd Edition 12.11 */
427 SwitchStatement
428 : kSWITCH '(' Expression ')' CaseBlock
429 { $$ = new_switch_statement(ctx, $3, $5); }
431 /* ECMA-262 3rd Edition 12.11 */
432 CaseBlock
433 : '{' CaseClausules_opt '}'
434 { $$ = new_case_block(ctx, $2, NULL, NULL); }
435 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
436 { $$ = new_case_block(ctx, $2, $3, $4); }
438 /* ECMA-262 3rd Edition 12.11 */
439 CaseClausules_opt
440 : /* empty */ { $$ = NULL; }
441 | CaseClausules { $$ = $1; }
443 /* ECMA-262 3rd Edition 12.11 */
444 CaseClausules
445 : CaseClausule { $$ = new_case_list(ctx, $1); }
446 | CaseClausules CaseClausule
447 { $$ = case_list_add(ctx, $1, $2); }
449 /* ECMA-262 3rd Edition 12.11 */
450 CaseClausule
451 : kCASE Expression ':' StatementList_opt
452 { $$ = new_case_clausule(ctx, $2, $4); }
454 /* ECMA-262 3rd Edition 12.11 */
455 DefaultClausule
456 : kDEFAULT ':' StatementList_opt
457 { $$ = new_case_clausule(ctx, NULL, $3); }
459 /* ECMA-262 3rd Edition 12.13 */
460 ThrowStatement
461 : kTHROW /* NONL */ Expression semicolon_opt
462 { $$ = new_throw_statement(ctx, $2); }
464 /* ECMA-262 3rd Edition 12.14 */
465 TryStatement
466 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
467 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
468 | kTRY Block Catch Finally
469 { $$ = new_try_statement(ctx, $2, $3, $4); }
471 /* ECMA-262 3rd Edition 12.14 */
472 Catch
473 : kCATCH '(' tIdentifier ')' Block
474 { $$ = new_catch_block(ctx, $3, $5); }
476 /* ECMA-262 3rd Edition 12.14 */
477 Finally
478 : kFINALLY Block { $$ = $2; }
480 /* ECMA-262 3rd Edition 11.14 */
481 Expression_opt
482 : /* empty */ { $$ = NULL; }
483 | Expression { $$ = $1; }
485 /* ECMA-262 3rd Edition 11.14 */
486 Expression
487 : AssignmentExpression { $$ = $1; }
488 | Expression ',' AssignmentExpression
489 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
491 /* ECMA-262 3rd Edition 11.14 */
492 ExpressionNoIn_opt
493 : /* empty */ { $$ = NULL; }
494 | ExpressionNoIn { $$ = $1; }
496 /* ECMA-262 3rd Edition 11.14 */
497 ExpressionNoIn
498 : AssignmentExpressionNoIn
499 { $$ = $1; }
500 | ExpressionNoIn ',' AssignmentExpressionNoIn
501 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
503 /* ECMA-262 3rd Edition 11.13 */
504 AssignmentExpression
505 : ConditionalExpression { $$ = $1; }
506 | LeftHandSideExpression '=' AssignmentExpression
507 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
508 | LeftHandSideExpression tAssignOper AssignmentExpression
509 { $$ = new_binary_expression(ctx, $2, $1, $3); }
511 /* ECMA-262 3rd Edition 11.13 */
512 AssignmentExpressionNoIn
513 : ConditionalExpressionNoIn
514 { $$ = $1; }
515 | LeftHandSideExpression '=' AssignmentExpressionNoIn
516 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
517 | LeftHandSideExpression tAssignOper AssignmentExpressionNoIn
518 { $$ = new_binary_expression(ctx, $2, $1, $3); }
520 /* ECMA-262 3rd Edition 11.12 */
521 ConditionalExpression
522 : LogicalORExpression { $$ = $1; }
523 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
524 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
526 /* ECMA-262 3rd Edition 11.12 */
527 ConditionalExpressionNoIn
528 : LogicalORExpressionNoIn
529 { $$ = $1; }
530 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
531 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
533 /* ECMA-262 3rd Edition 11.11 */
534 LogicalORExpression
535 : LogicalANDExpression { $$ = $1; }
536 | LogicalORExpression tOROR LogicalANDExpression
537 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
539 /* ECMA-262 3rd Edition 11.11 */
540 LogicalORExpressionNoIn
541 : LogicalANDExpressionNoIn
542 { $$ = $1; }
543 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
544 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
546 /* ECMA-262 3rd Edition 11.11 */
547 LogicalANDExpression
548 : BitwiseORExpression { $$ = $1; }
549 | LogicalANDExpression tANDAND BitwiseORExpression
550 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
552 /* ECMA-262 3rd Edition 11.11 */
553 LogicalANDExpressionNoIn
554 : BitwiseORExpressionNoIn
555 { $$ = $1; }
556 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
557 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
559 /* ECMA-262 3rd Edition 11.10 */
560 BitwiseORExpression
561 : BitwiseXORExpression { $$ = $1; }
562 | BitwiseORExpression '|' BitwiseXORExpression
563 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
565 /* ECMA-262 3rd Edition 11.10 */
566 BitwiseORExpressionNoIn
567 : BitwiseXORExpressionNoIn
568 { $$ = $1; }
569 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
570 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
572 /* ECMA-262 3rd Edition 11.10 */
573 BitwiseXORExpression
574 : BitwiseANDExpression { $$ = $1; }
575 | BitwiseXORExpression '^' BitwiseANDExpression
576 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
578 /* ECMA-262 3rd Edition 11.10 */
579 BitwiseXORExpressionNoIn
580 : BitwiseANDExpressionNoIn
581 { $$ = $1; }
582 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
583 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
585 /* ECMA-262 3rd Edition 11.10 */
586 BitwiseANDExpression
587 : EqualityExpression { $$ = $1; }
588 | BitwiseANDExpression '&' EqualityExpression
589 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
591 /* ECMA-262 3rd Edition 11.10 */
592 BitwiseANDExpressionNoIn
593 : EqualityExpressionNoIn
594 { $$ = $1; }
595 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
596 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
598 /* ECMA-262 3rd Edition 11.9 */
599 EqualityExpression
600 : RelationalExpression { $$ = $1; }
601 | EqualityExpression tEqOper RelationalExpression
602 { $$ = new_binary_expression(ctx, $2, $1, $3); }
604 /* ECMA-262 3rd Edition 11.9 */
605 EqualityExpressionNoIn
606 : RelationalExpressionNoIn { $$ = $1; }
607 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
608 { $$ = new_binary_expression(ctx, $2, $1, $3); }
610 /* ECMA-262 3rd Edition 11.8 */
611 RelationalExpression
612 : ShiftExpression { $$ = $1; }
613 | RelationalExpression tRelOper ShiftExpression
614 { $$ = new_binary_expression(ctx, $2, $1, $3); }
615 | RelationalExpression kINSTANCEOF ShiftExpression
616 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
617 | RelationalExpression kIN ShiftExpression
618 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
620 /* ECMA-262 3rd Edition 11.8 */
621 RelationalExpressionNoIn
622 : ShiftExpression { $$ = $1; }
623 | RelationalExpressionNoIn tRelOper ShiftExpression
624 { $$ = new_binary_expression(ctx, $2, $1, $3); }
625 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
626 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
628 /* ECMA-262 3rd Edition 11.7 */
629 ShiftExpression
630 : AdditiveExpression { $$ = $1; }
631 | ShiftExpression tShiftOper AdditiveExpression
632 { $$ = new_binary_expression(ctx, $2, $1, $3); }
634 /* ECMA-262 3rd Edition 11.6 */
635 AdditiveExpression
636 : MultiplicativeExpression
637 { $$ = $1; }
638 | AdditiveExpression '+' MultiplicativeExpression
639 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
640 | AdditiveExpression '-' MultiplicativeExpression
641 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
643 /* ECMA-262 3rd Edition 11.5 */
644 MultiplicativeExpression
645 : UnaryExpression { $$ = $1; }
646 | MultiplicativeExpression '*' UnaryExpression
647 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
648 | MultiplicativeExpression '/' UnaryExpression
649 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
650 | MultiplicativeExpression '%' UnaryExpression
651 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
653 /* ECMA-262 3rd Edition 11.4 */
654 UnaryExpression
655 : PostfixExpression { $$ = $1; }
656 | kDELETE UnaryExpression
657 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
658 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
659 | kTYPEOF UnaryExpression
660 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
661 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
662 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
663 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
664 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
665 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
666 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
668 /* ECMA-262 3rd Edition 11.2 */
669 PostfixExpression
670 : LeftHandSideExpression
671 { $$ = $1; }
672 | LeftHandSideExpression /* NONL */ tINC
673 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
674 | LeftHandSideExpression /* NONL */ tDEC
675 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
678 /* ECMA-262 3rd Edition 11.2 */
679 LeftHandSideExpression
680 : NewExpression { $$ = $1; }
681 | CallExpression { $$ = $1; }
683 /* ECMA-262 3rd Edition 11.2 */
684 NewExpression
685 : MemberExpression { $$ = $1; }
686 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
688 /* ECMA-262 3rd Edition 11.2 */
689 MemberExpression
690 : PrimaryExpression { $$ = $1; }
691 | FunctionExpression { $$ = $1; }
692 | MemberExpression '[' Expression ']'
693 { $$ = new_array_expression(ctx, $1, $3); }
694 | MemberExpression '.' tIdentifier
695 { $$ = new_member_expression(ctx, $1, $3); }
696 | kNEW MemberExpression Arguments
697 { $$ = new_new_expression(ctx, $2, $3); }
699 /* ECMA-262 3rd Edition 11.2 */
700 CallExpression
701 : MemberExpression Arguments
702 { $$ = new_call_expression(ctx, $1, $2); }
703 | CallExpression Arguments
704 { $$ = new_call_expression(ctx, $1, $2); }
705 | CallExpression '[' Expression ']'
706 { $$ = new_array_expression(ctx, $1, $3); }
707 | CallExpression '.' tIdentifier
708 { $$ = new_member_expression(ctx, $1, $3); }
710 /* ECMA-262 3rd Edition 11.2 */
711 Arguments
712 : '(' ')' { $$ = NULL; }
713 | '(' ArgumentList ')' { $$ = $2; }
715 /* ECMA-262 3rd Edition 11.2 */
716 ArgumentList
717 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
718 | ArgumentList ',' AssignmentExpression
719 { $$ = argument_list_add(ctx, $1, $3); }
721 /* ECMA-262 3rd Edition 11.1 */
722 PrimaryExpression
723 : kTHIS { $$ = new_this_expression(ctx); }
724 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
725 | Literal { $$ = new_literal_expression(ctx, $1); }
726 | ArrayLiteral { $$ = $1; }
727 | ObjectLiteral { $$ = $1; }
728 | '(' Expression ')' { $$ = $2; }
730 /* ECMA-262 3rd Edition 11.1.4 */
731 ArrayLiteral
732 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
733 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
734 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
735 | '[' ElementList ',' Elision_opt ']'
736 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
738 /* ECMA-262 3rd Edition 11.1.4 */
739 ElementList
740 : Elision_opt AssignmentExpression
741 { $$ = new_element_list(ctx, $1, $2); }
742 | ElementList ',' Elision_opt AssignmentExpression
743 { $$ = element_list_add(ctx, $1, $3, $4); }
745 /* ECMA-262 3rd Edition 11.1.4 */
746 Elision
747 : ',' { $$ = 1; }
748 | Elision ',' { $$ = $1 + 1; }
750 /* ECMA-262 3rd Edition 11.1.4 */
751 Elision_opt
752 : /* empty */ { $$ = 0; }
753 | Elision { $$ = $1; }
755 /* ECMA-262 3rd Edition 11.1.5 */
756 ObjectLiteral
757 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
758 | '{' PropertyNameAndValueList '}'
759 { $$ = new_prop_and_value_expression(ctx, $2); }
761 /* ECMA-262 3rd Edition 11.1.5 */
762 PropertyNameAndValueList
763 : PropertyName ':' AssignmentExpression
764 { $$ = new_property_list(ctx, $1, $3); }
765 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
766 { $$ = property_list_add(ctx, $1, $3, $5); }
768 /* ECMA-262 3rd Edition 11.1.5 */
769 PropertyName
770 : tIdentifier { $$ = new_string_literal(ctx, $1); }
771 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
772 | tNumericLiteral { $$ = $1; }
774 /* ECMA-262 3rd Edition 7.6 */
775 Identifier_opt
776 : /* empty*/ { $$ = NULL; }
777 | tIdentifier { $$ = $1; }
779 /* ECMA-262 3rd Edition 7.8 */
780 Literal
781 : kNULL { $$ = new_null_literal(ctx); }
782 | kUNDEFINED { $$ = new_undefined_literal(ctx); }
783 | BooleanLiteral { $$ = $1; }
784 | tNumericLiteral { $$ = $1; }
785 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
786 | '/' { $$ = parse_regexp(ctx);
787 if(!$$) YYABORT; }
789 /* ECMA-262 3rd Edition 7.8.2 */
790 BooleanLiteral
791 : kTRUE { $$ = new_boolean_literal(ctx, TRUE); }
792 | kFALSE { $$ = new_boolean_literal(ctx, FALSE); }
794 semicolon_opt
795 : ';'
796 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
800 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
802 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
805 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
807 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
809 ret->vt = VT_BSTR;
810 ret->u.wstr = str;
812 return ret;
815 static literal_t *new_null_literal(parser_ctx_t *ctx)
817 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
819 ret->vt = VT_NULL;
821 return ret;
824 static literal_t *new_undefined_literal(parser_ctx_t *ctx)
826 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
828 ret->vt = VT_EMPTY;
830 return ret;
833 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
835 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
837 ret->vt = VT_BOOL;
838 ret->u.bval = bval;
840 return ret;
843 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
845 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
847 ret->name = name;
848 ret->value = value;
849 ret->next = NULL;
851 return ret;
854 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
856 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
858 ret->head = ret->tail = new_prop_val(ctx, name, value);
860 return ret;
863 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
865 list->tail = list->tail->next = new_prop_val(ctx, name, value);
867 return list;
870 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
872 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
874 ret->elision = elision;
875 ret->expr = expr;
876 ret->next = NULL;
878 return ret;
881 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
883 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
885 ret->head = ret->tail = new_array_element(ctx, elision, expr);
887 return ret;
890 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
892 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
894 return list;
897 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
899 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
901 ret->expr = expr;
902 ret->next = NULL;
904 return ret;
907 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
909 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
911 ret->head = ret->tail = new_argument(ctx, expr);
913 return ret;
916 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
918 list->tail = list->tail->next = new_argument(ctx, expr);
920 return list;
923 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
925 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
927 ret->identifier = identifier;
928 ret->statement = statement;
930 return ret;
933 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
935 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
937 ret->expr = expr;
938 ret->stat = stat_list ? stat_list->head : NULL;
939 ret->next = NULL;
941 return ret;
944 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
946 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
948 ret->head = ret->tail = case_clausule;
950 return ret;
953 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
955 list->tail = list->tail->next = case_clausule;
957 return list;
960 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
961 case_clausule_t *default_clausule, case_list_t *case_list2)
963 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
964 statement_t *stat = NULL;
966 if(case_list1) {
967 ret = case_list1->head;
968 iter = case_list1->tail;
971 if(default_clausule) {
972 if(ret)
973 iter = iter->next = default_clausule;
974 else
975 ret = iter = default_clausule;
978 if(case_list2) {
979 if(ret)
980 iter->next = case_list2->head;
981 else
982 ret = case_list2->head;
985 if(!ret)
986 return NULL;
988 for(iter = ret; iter; iter = iter->next) {
989 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
990 if(!iter2)
991 break;
993 while(iter != iter2) {
994 iter->stat = iter2->stat;
995 iter = iter->next;
998 if(stat) {
999 while(stat->next)
1000 stat = stat->next;
1001 stat->next = iter->stat;
1002 }else {
1003 stat = iter->stat;
1007 return ret;
1010 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1012 block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1014 ret->stat.eval = block_statement_eval;
1015 ret->stat.next = NULL;
1016 ret->stat_list = list ? list->head : NULL;
1018 return &ret->stat;
1021 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1023 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1024 var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1026 ret->identifier = identifier;
1027 ret->expr = expr;
1028 ret->next = NULL;
1030 var_list->identifier = identifier;
1031 var_list->next = NULL;
1033 if(ctx->func_stack->var_tail)
1034 ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1035 else
1036 ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1038 return ret;
1041 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1043 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1045 ret->head = ret->tail = decl;
1047 return ret;
1050 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1052 list->tail = list->tail->next = decl;
1054 return list;
1057 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1059 var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1061 ret->stat.eval = var_statement_eval;
1062 ret->stat.next = NULL;
1063 ret->variable_list = variable_list->head;
1065 return &ret->stat;
1068 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1070 statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1072 ret->eval = empty_statement_eval;
1073 ret->next = NULL;
1075 return ret;
1078 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1080 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1082 ret->stat.eval = expression_statement_eval;
1083 ret->stat.next = NULL;
1084 ret->expr = expr;
1086 return &ret->stat;
1089 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1091 if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1093 ret->stat.eval = if_statement_eval;
1094 ret->stat.next = NULL;
1095 ret->expr = expr;
1096 ret->if_stat = if_stat;
1097 ret->else_stat = else_stat;
1099 return &ret->stat;
1102 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1104 while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1106 ret->stat.eval = while_statement_eval;
1107 ret->stat.next = NULL;
1108 ret->do_while = dowhile;
1109 ret->expr = expr;
1110 ret->statement = stat;
1112 return &ret->stat;
1115 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1116 expression_t *expr, expression_t *end_expr, statement_t *statement)
1118 for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1120 ret->stat.eval = for_statement_eval;
1121 ret->stat.next = NULL;
1122 ret->variable_list = variable_list ? variable_list->head : NULL;
1123 ret->begin_expr = begin_expr;
1124 ret->expr = expr;
1125 ret->end_expr = end_expr;
1126 ret->statement = statement;
1128 return &ret->stat;
1131 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1132 expression_t *in_expr, statement_t *statement)
1134 forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1136 ret->stat.eval = forin_statement_eval;
1137 ret->stat.next = NULL;
1138 ret->variable = variable;
1139 ret->expr = expr;
1140 ret->in_expr = in_expr;
1141 ret->statement = statement;
1143 return &ret->stat;
1146 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1148 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1150 ret->stat.eval = continue_statement_eval;
1151 ret->stat.next = NULL;
1152 ret->identifier = identifier;
1154 return &ret->stat;
1157 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1159 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1161 ret->stat.eval = break_statement_eval;
1162 ret->stat.next = NULL;
1163 ret->identifier = identifier;
1165 return &ret->stat;
1168 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1170 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1172 ret->stat.eval = return_statement_eval;
1173 ret->stat.next = NULL;
1174 ret->expr = expr;
1176 return &ret->stat;
1179 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1181 with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1183 ret->stat.eval = with_statement_eval;
1184 ret->stat.next = NULL;
1185 ret->expr = expr;
1186 ret->statement = statement;
1188 return &ret->stat;
1191 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1193 labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1195 ret->stat.eval = labelled_statement_eval;
1196 ret->stat.next = NULL;
1197 ret->identifier = identifier;
1198 ret->statement = statement;
1200 return &ret->stat;
1203 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1205 switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1207 ret->stat.eval = switch_statement_eval;
1208 ret->stat.next = NULL;
1209 ret->expr = expr;
1210 ret->case_list = case_list;
1212 return &ret->stat;
1215 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1217 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1219 ret->stat.eval = throw_statement_eval;
1220 ret->stat.next = NULL;
1221 ret->expr = expr;
1223 return &ret->stat;
1226 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1227 catch_block_t *catch_block, statement_t *finally_statement)
1229 try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1231 ret->stat.eval = try_statement_eval;
1232 ret->stat.next = NULL;
1233 ret->try_statement = try_statement;
1234 ret->catch_block = catch_block;
1235 ret->finally_statement = finally_statement;
1237 return &ret->stat;
1240 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1242 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1244 ret->identifier = identifier;
1245 ret->next = NULL;
1247 return ret;
1250 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1252 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1254 ret->head = ret->tail = new_parameter(ctx, identifier);
1256 return ret;
1259 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1261 list->tail = list->tail->next = new_parameter(ctx, identifier);
1263 return list;
1266 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1267 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1269 function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1271 ret->expr.eval = function_expression_eval;
1272 ret->identifier = identifier;
1273 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1274 ret->source_elements = source_elements;
1275 ret->src_str = src_str;
1276 ret->src_len = src_len;
1278 if(ret->identifier) {
1279 function_declaration_t *decl = parser_alloc(ctx, sizeof(function_declaration_t));
1281 decl->expr = ret;
1282 decl->next = NULL;
1284 if(ctx->func_stack->func_tail)
1285 ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
1286 else
1287 ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1290 return &ret->expr;
1293 static const expression_eval_t expression_eval_table[] = {
1294 comma_expression_eval,
1295 logical_or_expression_eval,
1296 logical_and_expression_eval,
1297 binary_or_expression_eval,
1298 binary_xor_expression_eval,
1299 binary_and_expression_eval,
1300 instanceof_expression_eval,
1301 in_expression_eval,
1302 add_expression_eval,
1303 sub_expression_eval,
1304 mul_expression_eval,
1305 div_expression_eval,
1306 mod_expression_eval,
1307 delete_expression_eval,
1308 void_expression_eval,
1309 typeof_expression_eval,
1310 minus_expression_eval,
1311 plus_expression_eval,
1312 post_increment_expression_eval,
1313 post_decrement_expression_eval,
1314 pre_increment_expression_eval,
1315 pre_decrement_expression_eval,
1316 equal_expression_eval,
1317 equal2_expression_eval,
1318 not_equal_expression_eval,
1319 not_equal2_expression_eval,
1320 less_expression_eval,
1321 lesseq_expression_eval,
1322 greater_expression_eval,
1323 greatereq_expression_eval,
1324 binary_negation_expression_eval,
1325 logical_negation_expression_eval,
1326 left_shift_expression_eval,
1327 right_shift_expression_eval,
1328 right2_shift_expression_eval,
1329 assign_expression_eval,
1330 assign_lshift_expression_eval,
1331 assign_rshift_expression_eval,
1332 assign_rrshift_expression_eval,
1333 assign_add_expression_eval,
1334 assign_sub_expression_eval,
1335 assign_mul_expression_eval,
1336 assign_div_expression_eval,
1337 assign_mod_expression_eval,
1338 assign_and_expression_eval,
1339 assign_or_expression_eval,
1340 assign_xor_expression_eval,
1343 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1344 expression_t *expression1, expression_t *expression2)
1346 binary_expression_t *ret = parser_alloc(ctx, sizeof(binary_expression_t));
1348 ret->expr.eval = expression_eval_table[type];
1349 ret->expression1 = expression1;
1350 ret->expression2 = expression2;
1352 return &ret->expr;
1355 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1357 unary_expression_t *ret = parser_alloc(ctx, sizeof(unary_expression_t));
1359 ret->expr.eval = expression_eval_table[type];
1360 ret->expression = expression;
1362 return &ret->expr;
1365 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1366 expression_t *true_expression, expression_t *false_expression)
1368 conditional_expression_t *ret = parser_alloc(ctx, sizeof(conditional_expression_t));
1370 ret->expr.eval = conditional_expression_eval;
1371 ret->expression = expression;
1372 ret->true_expression = true_expression;
1373 ret->false_expression = false_expression;
1375 return &ret->expr;
1378 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1380 array_expression_t *ret = parser_alloc(ctx, sizeof(array_expression_t));
1382 ret->expr.eval = array_expression_eval;
1383 ret->member_expr = member_expr;
1384 ret->expression = expression;
1386 return &ret->expr;
1389 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1391 member_expression_t *ret = parser_alloc(ctx, sizeof(member_expression_t));
1393 ret->expr.eval = member_expression_eval;
1394 ret->expression = expression;
1395 ret->identifier = identifier;
1397 return &ret->expr;
1400 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1402 call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1404 ret->expr.eval = new_expression_eval;
1405 ret->expression = expression;
1406 ret->argument_list = argument_list ? argument_list->head : NULL;
1408 return &ret->expr;
1411 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1413 call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1415 ret->expr.eval = call_expression_eval;
1416 ret->expression = expression;
1417 ret->argument_list = argument_list ? argument_list->head : NULL;
1419 return &ret->expr;
1422 static expression_t *new_this_expression(parser_ctx_t *ctx)
1424 expression_t *ret = parser_alloc(ctx, sizeof(expression_t));
1426 ret->eval = this_expression_eval;
1428 return ret;
1431 static int parser_error(const char *str)
1433 return 0;
1436 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1438 identifier_expression_t *ret = parser_alloc(ctx, sizeof(identifier_expression_t));
1440 ret->expr.eval = identifier_expression_eval;
1441 ret->identifier = identifier;
1443 return &ret->expr;
1446 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1448 array_literal_expression_t *ret = parser_alloc(ctx, sizeof(array_literal_expression_t));
1450 ret->expr.eval = array_literal_expression_eval;
1451 ret->element_list = element_list ? element_list->head : NULL;
1452 ret->length = length;
1454 return &ret->expr;
1457 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1459 property_value_expression_t *ret = parser_alloc(ctx, sizeof(property_value_expression_t));
1461 ret->expr.eval = property_value_expression_eval;
1462 ret->property_list = property_list ? property_list->head : NULL;
1464 return &ret->expr;
1467 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1469 literal_expression_t *ret = parser_alloc(ctx, sizeof(literal_expression_t));
1471 ret->expr.eval = literal_expression_eval;
1472 ret->literal = literal;
1474 return &ret->expr;
1477 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1479 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1481 memset(ret, 0, sizeof(*ret));
1483 return ret;
1486 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1488 if(source_elements->statement_tail)
1489 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1490 else
1491 source_elements->statement = source_elements->statement_tail = statement;
1493 return source_elements;
1496 statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1498 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1500 ret->head = ret->tail = statement;
1502 return ret;
1505 statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1507 list->tail = list->tail->next = statement;
1509 return list;
1512 static void push_func(parser_ctx_t *ctx)
1514 func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1516 new_func->func_head = new_func->func_tail = NULL;
1517 new_func->var_head = new_func->var_tail = NULL;
1519 new_func->next = ctx->func_stack;
1520 ctx->func_stack = new_func;
1523 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1525 source->functions = ctx->func_stack->func_head;
1526 source->variables = ctx->func_stack->var_head;
1527 pop_func(ctx);
1529 return source;
1532 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1534 source->functions = ctx->func_stack->func_head;
1535 source->variables = ctx->func_stack->var_head;
1536 pop_func(ctx);
1538 ctx->source = source;
1539 ctx->hres = S_OK;
1542 void parser_release(parser_ctx_t *ctx)
1544 obj_literal_t *iter;
1546 if(--ctx->ref)
1547 return;
1549 for(iter = ctx->obj_literals; iter; iter = iter->next)
1550 jsdisp_release(iter->obj);
1552 jsheap_free(&ctx->heap);
1553 heap_free(ctx);
1556 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, parser_ctx_t **ret)
1558 parser_ctx_t *parser_ctx;
1559 jsheap_t *mark;
1560 HRESULT hres;
1562 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1563 if(!parser_ctx)
1564 return E_OUTOFMEMORY;
1566 parser_ctx->ref = 1;
1567 parser_ctx->hres = E_FAIL;
1569 parser_ctx->begin = parser_ctx->ptr = code;
1570 parser_ctx->end = code + strlenW(code);
1572 script_addref(ctx);
1573 parser_ctx->script = ctx;
1575 mark = jsheap_mark(&ctx->tmp_heap);
1576 jsheap_init(&parser_ctx->heap);
1578 push_func(parser_ctx);
1580 parser_parse(parser_ctx);
1581 jsheap_clear(mark);
1582 if(FAILED(parser_ctx->hres)) {
1583 hres = parser_ctx->hres;
1584 parser_release(parser_ctx);
1585 return hres;
1588 *ret = parser_ctx;
1589 return S_OK;