quartz/tests: If CoCreateInstance() fails, it's safe to return, as no cleanup is...
[wine] / dlls / jscript / parser.y
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 %{
20
21 #include "jscript.h"
22 #include "engine.h"
23
24 #define YYLEX_PARAM ctx
25 #define YYPARSE_PARAM ctx
26
27 static int parser_error(const char*);
28 static BOOL allow_auto_semicolon(parser_ctx_t*);
29 static void program_parsed(parser_ctx_t*,source_elements_t*);
30 static source_elements_t *function_body_parsed(parser_ctx_t*,source_elements_t*);
31
32 typedef struct _statement_list_t {
33     statement_t *head;
34     statement_t *tail;
35 } statement_list_t;
36
37 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
38 static literal_t *new_null_literal(parser_ctx_t*);
39 static literal_t *new_undefined_literal(parser_ctx_t*);
40 static literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL);
41
42 typedef struct _property_list_t {
43     prop_val_t *head;
44     prop_val_t *tail;
45 } property_list_t;
46
47 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
48 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
49
50 typedef struct _element_list_t {
51     array_element_t *head;
52     array_element_t *tail;
53 } element_list_t;
54
55 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
56 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
57
58 typedef struct _argument_list_t {
59     argument_t *head;
60     argument_t *tail;
61 } argument_list_t;
62
63 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
64 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
65
66 typedef struct _case_list_t {
67     case_clausule_t *head;
68     case_clausule_t *tail;
69 } case_list_t;
70
71 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
72 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
73 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
74 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
75 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
76
77 typedef struct _variable_list_t {
78     variable_declaration_t *head;
79     variable_declaration_t *tail;
80 } variable_list_t;
81
82 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
83 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
84 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
85
86 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
87 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
88 static statement_t *new_empty_statement(parser_ctx_t*);
89 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
90 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
91 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
92 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
93         expression_t*,statement_t*);
94 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
95 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
96 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
97 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
98 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
99 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
100 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
101 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
102 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
103
104 struct statement_list_t {
105    statement_t *head;
106    statement_t *tail;
107 };
108
109 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
110 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
111
112 typedef struct _parameter_list_t {
113     parameter_t *head;
114     parameter_t *tail;
115 } parameter_list_t;
116
117 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
118 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
119
120 static void push_func(parser_ctx_t*);
121 static inline void pop_func(parser_ctx_t *ctx)
122 {
123     ctx->func_stack = ctx->func_stack->next;
124 }
125
126 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
127         source_elements_t*,const WCHAR*,DWORD);
128 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
129 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
130 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
131 static expression_t *new_array_expression(parser_ctx_t*,expression_t*,expression_t*);
132 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
133 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
134 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
135 static expression_t *new_this_expression(parser_ctx_t*);
136 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
137 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
138 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
139 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
140
141 static source_elements_t *new_source_elements(parser_ctx_t*);
142 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
143
144 %}
145
146 %pure_parser
147 %start Program
148
149 %union {
150     int                     ival;
151     const WCHAR             *srcptr;
152     LPCWSTR                 wstr;
153     literal_t               *literal;
154     struct _argument_list_t *argument_list;
155     case_clausule_t         *case_clausule;
156     struct _case_list_t     *case_list;
157     catch_block_t           *catch_block;
158     struct _element_list_t  *element_list;
159     expression_t            *expr;
160     const WCHAR            *identifier;
161     struct _parameter_list_t *parameter_list;
162     struct _property_list_t *property_list;
163     source_elements_t       *source_elements;
164     statement_t             *statement;
165     struct _statement_list_t *statement_list;
166     struct _variable_list_t *variable_list;
167     variable_declaration_t  *variable_declaration;
168 }
169
170 /* keywords */
171 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
172 %token kINSTANCEOF kNEW kNULL kUNDEFINED kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
173 %token tANDAND tOROR tINC tDEC
174
175 %token <srcptr> kFUNCTION '}'
176
177 /* tokens */
178 %token <identifier> tIdentifier
179 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
180 %token <literal> tNumericLiteral
181 %token <wstr> tStringLiteral
182
183 %type <source_elements> SourceElements
184 %type <source_elements> FunctionBody
185 %type <statement> Statement
186 %type <statement> Block
187 %type <statement> VariableStatement
188 %type <statement> EmptyStatement
189 %type <statement> ExpressionStatement
190 %type <statement> IfStatement
191 %type <statement> IterationStatement
192 %type <statement> ContinueStatement
193 %type <statement> BreakStatement
194 %type <statement> ReturnStatement
195 %type <statement> WithStatement
196 %type <statement> LabelledStatement
197 %type <statement> SwitchStatement
198 %type <statement> ThrowStatement
199 %type <statement> TryStatement
200 %type <statement> Finally
201 %type <statement_list> StatementList StatementList_opt
202 %type <parameter_list> FormalParameterList FormalParameterList_opt
203 %type <expr> Expression Expression_opt
204 %type <expr> ExpressionNoIn ExpressionNoIn_opt
205 %type <expr> FunctionExpression
206 %type <expr> AssignmentExpression AssignmentExpressionNoIn
207 %type <expr> ConditionalExpression ConditionalExpressionNoIn
208 %type <expr> LeftHandSideExpression
209 %type <expr> LogicalORExpression LogicalORExpressionNoIn
210 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
211 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
212 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
213 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
214 %type <expr> EqualityExpression EqualityExpressionNoIn
215 %type <expr> RelationalExpression RelationalExpressionNoIn
216 %type <expr> ShiftExpression
217 %type <expr> AdditiveExpression
218 %type <expr> MultiplicativeExpression
219 %type <expr> Initialiser_opt Initialiser
220 %type <expr> InitialiserNoIn_opt InitialiserNoIn
221 %type <expr> UnaryExpression
222 %type <expr> PostfixExpression
223 %type <expr> NewExpression
224 %type <expr> CallExpression
225 %type <expr> MemberExpression
226 %type <expr> PrimaryExpression
227 %type <identifier> Identifier_opt
228 %type <variable_list> VariableDeclarationList
229 %type <variable_list> VariableDeclarationListNoIn
230 %type <variable_declaration> VariableDeclaration
231 %type <variable_declaration> VariableDeclarationNoIn
232 %type <case_list> CaseClausules CaseClausules_opt
233 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
234 %type <catch_block> Catch
235 %type <argument_list> Arguments
236 %type <argument_list> ArgumentList
237 %type <literal> Literal
238 %type <expr> ArrayLiteral
239 %type <expr> ObjectLiteral
240 %type <ival> Elision Elision_opt
241 %type <element_list> ElementList
242 %type <property_list> PropertyNameAndValueList
243 %type <literal> PropertyName
244 %type <literal> BooleanLiteral
245 %type <srcptr> KFunction
246
247 %nonassoc LOWER_THAN_ELSE
248 %nonassoc kELSE
249
250 %%
251
252 /* ECMA-262 3rd Edition    14 */
253 Program
254        : SourceElements         { program_parsed(ctx, $1); }
255
256 /* ECMA-262 3rd Edition    14 */
257 SourceElements
258         : /* empty */           { $$ = new_source_elements(ctx); }
259         | SourceElements Statement
260                                 { $$ = source_elements_add_statement($1, $2); }
261
262 /* ECMA-262 3rd Edition    13 */
263 FunctionExpression
264         : KFunction Identifier_opt '(' FormalParameterList_opt ')' '{' FunctionBody '}'
265                                 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
266
267 KFunction
268         : kFUNCTION             { push_func(ctx); $$ = $1; }
269
270 /* ECMA-262 3rd Edition    13 */
271 FunctionBody
272         : SourceElements        { $$ = function_body_parsed(ctx, $1); }
273
274 /* ECMA-262 3rd Edition    13 */
275 FormalParameterList
276         : tIdentifier           { $$ = new_parameter_list(ctx, $1); }
277         | FormalParameterList ',' tIdentifier
278                                 { $$ = parameter_list_add(ctx, $1, $3); }
279
280 /* ECMA-262 3rd Edition    13 */
281 FormalParameterList_opt
282         : /* empty */           { $$ = NULL; }
283         | FormalParameterList   { $$ = $1; }
284
285 /* ECMA-262 3rd Edition    12 */
286 Statement
287         : Block                 { $$ = $1; }
288         | VariableStatement     { $$ = $1; }
289         | EmptyStatement        { $$ = $1; }
290         | ExpressionStatement   { $$ = $1; }
291         | IfStatement           { $$ = $1; }
292         | IterationStatement    { $$ = $1; }
293         | ContinueStatement     { $$ = $1; }
294         | BreakStatement        { $$ = $1; }
295         | ReturnStatement       { $$ = $1; }
296         | WithStatement         { $$ = $1; }
297         | LabelledStatement     { $$ = $1; }
298         | SwitchStatement       { $$ = $1; }
299         | ThrowStatement        { $$ = $1; }
300         | TryStatement          { $$ = $1; }
301
302 /* ECMA-262 3rd Edition    12.2 */
303 StatementList
304         : Statement             { $$ = new_statement_list(ctx, $1); }
305         | StatementList Statement
306                                 { $$ = statement_list_add($1, $2); }
307
308 /* ECMA-262 3rd Edition    12.2 */
309 StatementList_opt
310         : /* empty */           { $$ = NULL; }
311         | StatementList         { $$ = $1; }
312
313 /* ECMA-262 3rd Edition    12.1 */
314 Block
315         : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
316         | '{' '}'               { $$ = new_block_statement(ctx, NULL); }
317
318 /* ECMA-262 3rd Edition    12.2 */
319 VariableStatement
320         : kVAR VariableDeclarationList semicolon_opt
321                                 { $$ = new_var_statement(ctx, $2); }
322
323 /* ECMA-262 3rd Edition    12.2 */
324 VariableDeclarationList
325         : VariableDeclaration   { $$ = new_variable_list(ctx, $1); }
326         | VariableDeclarationList ',' VariableDeclaration
327                                 { $$ = variable_list_add(ctx, $1, $3); }
328
329 /* ECMA-262 3rd Edition    12.2 */
330 VariableDeclarationListNoIn
331         : VariableDeclarationNoIn
332                                 { $$ = new_variable_list(ctx, $1); }
333         | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
334                                 { $$ = variable_list_add(ctx, $1, $3); }
335
336 /* ECMA-262 3rd Edition    12.2 */
337 VariableDeclaration
338         : tIdentifier Initialiser_opt
339                                 { $$ = new_variable_declaration(ctx, $1, $2); }
340
341 /* ECMA-262 3rd Edition    12.2 */
342 VariableDeclarationNoIn
343         : tIdentifier InitialiserNoIn_opt
344                                 { $$ = new_variable_declaration(ctx, $1, $2); }
345
346 /* ECMA-262 3rd Edition    12.2 */
347 Initialiser_opt
348         : /* empty */           { $$ = NULL; }
349         | Initialiser           { $$ = $1; }
350
351 /* ECMA-262 3rd Edition    12.2 */
352 Initialiser
353         : '=' AssignmentExpression
354                                 { $$ = $2; }
355
356 /* ECMA-262 3rd Edition    12.2 */
357 InitialiserNoIn_opt
358         : /* empty */           { $$ = NULL; }
359         | InitialiserNoIn       { $$ = $1; }
360
361 /* ECMA-262 3rd Edition    12.2 */
362 InitialiserNoIn
363         : '=' AssignmentExpressionNoIn
364                                 { $$ = $2; }
365
366 /* ECMA-262 3rd Edition    12.3 */
367 EmptyStatement
368         : ';'                   { $$ = new_empty_statement(ctx); }
369
370 /* ECMA-262 3rd Edition    12.4 */
371 ExpressionStatement
372         : Expression semicolon_opt
373                                 { $$ = new_expression_statement(ctx, $1); }
374
375 /* ECMA-262 3rd Edition    12.5 */
376 IfStatement
377         : kIF '(' Expression ')' Statement kELSE Statement
378                                 { $$ = new_if_statement(ctx, $3, $5, $7); }
379         | kIF '(' Expression ')' Statement %prec LOWER_THAN_ELSE
380                                 { $$ = new_if_statement(ctx, $3, $5, NULL); }
381
382 /* ECMA-262 3rd Edition    12.6 */
383 IterationStatement
384         : kDO Statement kWHILE '(' Expression ')' ';'
385                                 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
386         | kWHILE '(' Expression ')' Statement
387                                 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
388         | kFOR '(' ExpressionNoIn_opt ';' Expression_opt ';' Expression_opt ')' Statement
389                                 { $$ = new_for_statement(ctx, NULL, $3, $5, $7, $9); }
390         | kFOR '(' kVAR VariableDeclarationListNoIn ';' Expression_opt ';' Expression_opt ')' Statement
391                                 { $$ = new_for_statement(ctx, $4, NULL, $6, $8, $10); }
392         | kFOR '(' LeftHandSideExpression kIN Expression ')' Statement
393                                 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
394         | kFOR '(' kVAR VariableDeclarationNoIn kIN Expression ')' Statement
395                                 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
396
397 /* ECMA-262 3rd Edition    12.7 */
398 ContinueStatement
399         : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
400                                 { $$ = new_continue_statement(ctx, $2); }
401
402 /* ECMA-262 3rd Edition    12.8 */
403 BreakStatement
404         : kBREAK /* NONL */ Identifier_opt semicolon_opt
405                                 { $$ = new_break_statement(ctx, $2); }
406
407 /* ECMA-262 3rd Edition    12.9 */
408 ReturnStatement
409         : kRETURN /* NONL */ Expression_opt semicolon_opt
410                                 { $$ = new_return_statement(ctx, $2); }
411
412 /* ECMA-262 3rd Edition    12.10 */
413 WithStatement
414         : kWITH '(' Expression ')' Statement
415                                 { $$ = new_with_statement(ctx, $3, $5); }
416
417 /* ECMA-262 3rd Edition    12.12 */
418 LabelledStatement
419         : tIdentifier ':' Statement
420                                 { $$ = new_labelled_statement(ctx, $1, $3); }
421
422 /* ECMA-262 3rd Edition    12.11 */
423 SwitchStatement
424         : kSWITCH '(' Expression ')' CaseBlock
425                                  { $$ = new_switch_statement(ctx, $3, $5); }
426
427 /* ECMA-262 3rd Edition    12.11 */
428 CaseBlock
429         : '{' CaseClausules_opt '}'
430                                  { $$ = new_case_block(ctx, $2, NULL, NULL); }
431         | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
432                                  { $$ = new_case_block(ctx, $2, $3, $4); }
433
434 /* ECMA-262 3rd Edition    12.11 */
435 CaseClausules_opt
436         : /* empty */            { $$ = NULL; }
437         | CaseClausules          { $$ = $1; }
438
439 /* ECMA-262 3rd Edition    12.11 */
440 CaseClausules
441         : CaseClausule           { $$ = new_case_list(ctx, $1); }
442         | CaseClausules CaseClausule
443                                  { $$ = case_list_add(ctx, $1, $2); }
444
445 /* ECMA-262 3rd Edition    12.11 */
446 CaseClausule
447         : kCASE Expression ':' StatementList_opt
448                                  { $$ = new_case_clausule(ctx, $2, $4); }
449
450 /* ECMA-262 3rd Edition    12.11 */
451 DefaultClausule
452         : kDEFAULT ':' StatementList_opt
453                                  { $$ = new_case_clausule(ctx, NULL, $3); }
454
455 /* ECMA-262 3rd Edition    12.13 */
456 ThrowStatement
457         : kTHROW /* NONL */ Expression semicolon_opt
458                                 { $$ = new_throw_statement(ctx, $2); }
459
460 /* ECMA-262 3rd Edition    12.14 */
461 TryStatement
462         : kTRY Block Catch      { $$ = new_try_statement(ctx, $2, $3, NULL); }
463         | kTRY Block Finally    { $$ = new_try_statement(ctx, $2, NULL, $3); }
464         | kTRY Block Catch Finally
465                                 { $$ = new_try_statement(ctx, $2, $3, $4); }
466
467 /* ECMA-262 3rd Edition    12.14 */
468 Catch
469         : kCATCH '(' tIdentifier ')' Block
470                                  { $$ = new_catch_block(ctx, $3, $5); }
471
472 /* ECMA-262 3rd Edition    12.14 */
473 Finally
474         : kFINALLY Block        { $$ = $2; }
475
476 /* ECMA-262 3rd Edition    11.14 */
477 Expression_opt
478         : /* empty */           { $$ = NULL; }
479         | Expression            { $$ = $1; }
480
481 /* ECMA-262 3rd Edition    11.14 */
482 Expression
483         : AssignmentExpression  { $$ = $1; }
484         | Expression ',' AssignmentExpression
485                                 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
486
487 /* ECMA-262 3rd Edition    11.14 */
488 ExpressionNoIn_opt
489         : /* empty */           { $$ = NULL; }
490         | ExpressionNoIn        { $$ = $1; }
491
492 /* ECMA-262 3rd Edition    11.14 */
493 ExpressionNoIn
494         : AssignmentExpressionNoIn
495                                 { $$ = $1; }
496         | ExpressionNoIn ',' AssignmentExpressionNoIn
497                                 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
498
499 /* ECMA-262 3rd Edition    11.13 */
500 AssignmentExpression
501         : ConditionalExpression { $$ = $1; }
502         | LeftHandSideExpression '=' AssignmentExpression
503                                 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
504         | LeftHandSideExpression tAssignOper AssignmentExpression
505                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
506
507 /* ECMA-262 3rd Edition    11.13 */
508 AssignmentExpressionNoIn
509         : ConditionalExpressionNoIn
510                                 { $$ = $1; }
511         | LeftHandSideExpression '=' AssignmentExpressionNoIn
512                                 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
513         | LeftHandSideExpression tAssignOper AssignmentExpressionNoIn
514                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
515
516 /* ECMA-262 3rd Edition    11.12 */
517 ConditionalExpression
518         : LogicalORExpression   { $$ = $1; }
519         | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
520                                 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
521
522 /* ECMA-262 3rd Edition    11.12 */
523 ConditionalExpressionNoIn
524         : LogicalORExpressionNoIn
525                                 { $$ = $1; }
526         | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
527                                 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
528
529 /* ECMA-262 3rd Edition    11.11 */
530 LogicalORExpression
531         : LogicalANDExpression  { $$ = $1; }
532         | LogicalORExpression tOROR LogicalANDExpression
533                                 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
534
535 /* ECMA-262 3rd Edition    11.11 */
536 LogicalORExpressionNoIn
537         : LogicalANDExpressionNoIn
538                                 { $$ = $1; }
539         | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
540                                 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
541
542 /* ECMA-262 3rd Edition    11.11 */
543 LogicalANDExpression
544         : BitwiseORExpression   { $$ = $1; }
545         | LogicalANDExpression tANDAND BitwiseORExpression
546                                 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
547
548 /* ECMA-262 3rd Edition    11.11 */
549 LogicalANDExpressionNoIn
550         : BitwiseORExpressionNoIn
551                                 { $$ = $1; }
552         | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
553                                 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
554
555 /* ECMA-262 3rd Edition    11.10 */
556 BitwiseORExpression
557         : BitwiseXORExpression   { $$ = $1; }
558         | BitwiseORExpression '|' BitwiseXORExpression
559                                 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
560
561 /* ECMA-262 3rd Edition    11.10 */
562 BitwiseORExpressionNoIn
563         : BitwiseXORExpressionNoIn
564                                 { $$ = $1; }
565         | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
566                                 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
567
568 /* ECMA-262 3rd Edition    11.10 */
569 BitwiseXORExpression
570         : BitwiseANDExpression  { $$ = $1; }
571         | BitwiseXORExpression '^' BitwiseANDExpression
572                                 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
573
574 /* ECMA-262 3rd Edition    11.10 */
575 BitwiseXORExpressionNoIn
576         : BitwiseANDExpressionNoIn
577                                 { $$ = $1; }
578         | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
579                                 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
580
581 /* ECMA-262 3rd Edition    11.10 */
582 BitwiseANDExpression
583         : EqualityExpression    { $$ = $1; }
584         | BitwiseANDExpression '&' EqualityExpression
585                                 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
586
587 /* ECMA-262 3rd Edition    11.10 */
588 BitwiseANDExpressionNoIn
589         : EqualityExpressionNoIn
590                                 { $$ = $1; }
591         | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
592                                 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
593
594 /* ECMA-262 3rd Edition    11.9 */
595 EqualityExpression
596         : RelationalExpression  { $$ = $1; }
597         | EqualityExpression tEqOper RelationalExpression
598                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
599
600 /* ECMA-262 3rd Edition    11.9 */
601 EqualityExpressionNoIn
602         : RelationalExpressionNoIn  { $$ = $1; }
603         | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
604                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
605
606 /* ECMA-262 3rd Edition    11.8 */
607 RelationalExpression
608         : ShiftExpression       { $$ = $1; }
609         | RelationalExpression tRelOper ShiftExpression
610                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
611         | RelationalExpression kINSTANCEOF ShiftExpression
612                                 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
613         | RelationalExpression kIN ShiftExpression
614                                 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
615
616 /* ECMA-262 3rd Edition    11.8 */
617 RelationalExpressionNoIn
618         : ShiftExpression       { $$ = $1; }
619         | RelationalExpressionNoIn tRelOper ShiftExpression
620                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
621         | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
622                                 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
623
624 /* ECMA-262 3rd Edition    11.7 */
625 ShiftExpression
626         : AdditiveExpression    { $$ = $1; }
627         | ShiftExpression tShiftOper AdditiveExpression
628                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
629
630 /* ECMA-262 3rd Edition    11.6 */
631 AdditiveExpression
632         : MultiplicativeExpression
633                                 { $$ = $1; }
634         | AdditiveExpression '+' MultiplicativeExpression
635                                 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
636         | AdditiveExpression '-' MultiplicativeExpression
637                                 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
638
639 /* ECMA-262 3rd Edition    11.5 */
640 MultiplicativeExpression
641         : UnaryExpression       { $$ = $1; }
642         | MultiplicativeExpression '*' UnaryExpression
643                                 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
644         | MultiplicativeExpression '/' UnaryExpression
645                                 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
646         | MultiplicativeExpression '%' UnaryExpression
647                                 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
648
649 /* ECMA-262 3rd Edition    11.4 */
650 UnaryExpression
651         : PostfixExpression     { $$ = $1; }
652         | kDELETE UnaryExpression
653                                 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
654         | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
655         | kTYPEOF UnaryExpression
656                                 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
657         | tINC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
658         | tDEC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
659         | '+' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
660         | '-' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
661         | '~' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
662         | '!' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
663
664 /* ECMA-262 3rd Edition    11.2 */
665 PostfixExpression
666         : LeftHandSideExpression
667                                 { $$ = $1; }
668         | LeftHandSideExpression /* NONL */ tINC
669                                 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
670         | LeftHandSideExpression /* NONL */ tDEC
671                                 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
672
673
674 /* ECMA-262 3rd Edition    11.2 */
675 LeftHandSideExpression
676         : NewExpression         { $$ = $1; }
677         | CallExpression        { $$ = $1; }
678
679 /* ECMA-262 3rd Edition    11.2 */
680 NewExpression
681         : MemberExpression      { $$ = $1; }
682         | kNEW NewExpression    { $$ = new_new_expression(ctx, $2, NULL); }
683
684 /* ECMA-262 3rd Edition    11.2 */
685 MemberExpression
686         : PrimaryExpression     { $$ = $1; }
687         | FunctionExpression    { $$ = $1; }
688         | MemberExpression '[' Expression ']'
689                                 { $$ = new_array_expression(ctx, $1, $3); }
690         | MemberExpression '.' tIdentifier
691                                 { $$ = new_member_expression(ctx, $1, $3); }
692         | kNEW MemberExpression Arguments
693                                 { $$ = new_new_expression(ctx, $2, $3); }
694
695 /* ECMA-262 3rd Edition    11.2 */
696 CallExpression
697         : MemberExpression Arguments
698                                 { $$ = new_call_expression(ctx, $1, $2); }
699         | CallExpression Arguments
700                                 { $$ = new_call_expression(ctx, $1, $2); }
701         | CallExpression '[' Expression ']'
702                                 { $$ = new_array_expression(ctx, $1, $3); }
703         | CallExpression '.' tIdentifier
704                                 { $$ = new_member_expression(ctx, $1, $3); }
705
706 /* ECMA-262 3rd Edition    11.2 */
707 Arguments
708         : '(' ')'               { $$ = NULL; }
709         | '(' ArgumentList ')'  { $$ = $2; }
710
711 /* ECMA-262 3rd Edition    11.2 */
712 ArgumentList
713         : AssignmentExpression  { $$ = new_argument_list(ctx, $1); }
714         | ArgumentList ',' AssignmentExpression
715                                 { $$ = argument_list_add(ctx, $1, $3); }
716
717 /* ECMA-262 3rd Edition    11.1 */
718 PrimaryExpression
719         : kTHIS                 { $$ = new_this_expression(ctx); }
720         | tIdentifier           { $$ = new_identifier_expression(ctx, $1); }
721         | Literal               { $$ = new_literal_expression(ctx, $1); }
722         | ArrayLiteral          { $$ = $1; }
723         | ObjectLiteral         { $$ = $1; }
724         | '(' Expression ')'    { $$ = $2; }
725
726 /* ECMA-262 3rd Edition    11.1.4 */
727 ArrayLiteral
728         : '[' ']'               { $$ = new_array_literal_expression(ctx, NULL, 0); }
729         | '[' Elision ']'       { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
730         | '[' ElementList ']'   { $$ = new_array_literal_expression(ctx, $2, 0); }
731         | '[' ElementList ',' Elision_opt ']'
732                                 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
733
734 /* ECMA-262 3rd Edition    11.1.4 */
735 ElementList
736         : Elision_opt AssignmentExpression
737                                 { $$ = new_element_list(ctx, $1, $2); }
738         | ElementList ',' Elision_opt AssignmentExpression
739                                 { $$ = element_list_add(ctx, $1, $3, $4); }
740
741 /* ECMA-262 3rd Edition    11.1.4 */
742 Elision
743         : ','                   { $$ = 1; }
744         | Elision ','           { $$ = $1 + 1; }
745
746 /* ECMA-262 3rd Edition    11.1.4 */
747 Elision_opt
748         : /* empty */           { $$ = 0; }
749         | Elision               { $$ = $1; }
750
751 /* ECMA-262 3rd Edition    11.1.5 */
752 ObjectLiteral
753         : '{' '}'               { $$ = new_prop_and_value_expression(ctx, NULL); }
754         | '{' PropertyNameAndValueList '}'
755                                 { $$ = new_prop_and_value_expression(ctx, $2); }
756
757 /* ECMA-262 3rd Edition    11.1.5 */
758 PropertyNameAndValueList
759         : PropertyName ':' AssignmentExpression
760                                 { $$ = new_property_list(ctx, $1, $3); }
761         | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
762                                 { $$ = property_list_add(ctx, $1, $3, $5); }
763
764 /* ECMA-262 3rd Edition    11.1.5 */
765 PropertyName
766         : tIdentifier           { $$ = new_string_literal(ctx, $1); }
767         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
768         | tNumericLiteral       { $$ = $1; }
769
770 /* ECMA-262 3rd Edition    7.6 */
771 Identifier_opt
772         : /* empty*/            { $$ = NULL; }
773         | tIdentifier           { $$ = $1; }
774
775 /* ECMA-262 3rd Edition    7.8 */
776 Literal
777         : kNULL                 { $$ = new_null_literal(ctx); }
778         | kUNDEFINED            { $$ = new_undefined_literal(ctx); }
779         | BooleanLiteral        { $$ = $1; }
780         | tNumericLiteral       { $$ = $1; }
781         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
782         | '/'                   { $$ = parse_regexp(ctx);
783                                   if(!$$) YYABORT; }
784
785 /* ECMA-262 3rd Edition    7.8.2 */
786 BooleanLiteral
787         : kTRUE                 { $$ = new_boolean_literal(ctx, TRUE); }
788         | kFALSE                { $$ = new_boolean_literal(ctx, FALSE); }
789
790 semicolon_opt
791         : ';'
792         | error                 { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
793
794 %%
795
796 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
797 {
798     return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
799 }
800
801 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
802 {
803     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
804
805     ret->vt = VT_BSTR;
806     ret->u.wstr = str;
807
808     return ret;
809 }
810
811 static literal_t *new_null_literal(parser_ctx_t *ctx)
812 {
813     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
814
815     ret->vt = VT_NULL;
816
817     return ret;
818 }
819
820 static literal_t *new_undefined_literal(parser_ctx_t *ctx)
821 {
822     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
823
824     ret->vt = VT_EMPTY;
825
826     return ret;
827 }
828
829 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
830 {
831     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
832
833     ret->vt = VT_BOOL;
834     ret->u.bval = bval;
835
836     return ret;
837 }
838
839 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
840 {
841     prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
842
843     ret->name = name;
844     ret->value = value;
845     ret->next = NULL;
846
847     return ret;
848 }
849
850 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
851 {
852     property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
853
854     ret->head = ret->tail = new_prop_val(ctx, name, value);
855
856     return ret;
857 }
858
859 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
860 {
861     list->tail = list->tail->next = new_prop_val(ctx, name, value);
862
863     return list;
864 }
865
866 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
867 {
868     array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
869
870     ret->elision = elision;
871     ret->expr = expr;
872     ret->next = NULL;
873
874     return ret;
875 }
876
877 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
878 {
879     element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
880
881     ret->head = ret->tail = new_array_element(ctx, elision, expr);
882
883     return ret;
884 }
885
886 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
887 {
888     list->tail = list->tail->next = new_array_element(ctx, elision, expr);
889
890     return list;
891 }
892
893 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
894 {
895     argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
896
897     ret->expr = expr;
898     ret->next = NULL;
899
900     return ret;
901 }
902
903 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
904 {
905     argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
906
907     ret->head = ret->tail = new_argument(ctx, expr);
908
909     return ret;
910 }
911
912 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
913 {
914     list->tail = list->tail->next = new_argument(ctx, expr);
915
916     return list;
917 }
918
919 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
920 {
921     catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
922
923     ret->identifier = identifier;
924     ret->statement = statement;
925
926     return ret;
927 }
928
929 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
930 {
931     case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
932
933     ret->expr = expr;
934     ret->stat = stat_list ? stat_list->head : NULL;
935     ret->next = NULL;
936
937     return ret;
938 }
939
940 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
941 {
942     case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
943
944     ret->head = ret->tail = case_clausule;
945
946     return ret;
947 }
948
949 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
950 {
951     list->tail = list->tail->next = case_clausule;
952
953     return list;
954 }
955
956 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
957         case_clausule_t *default_clausule, case_list_t *case_list2)
958 {
959     case_clausule_t *ret = NULL, *iter = NULL, *iter2;
960     statement_t *stat = NULL;
961
962     if(case_list1) {
963         ret = case_list1->head;
964         iter = case_list1->tail;
965     }
966
967     if(default_clausule) {
968         if(ret)
969             iter = iter->next = default_clausule;
970         else
971             ret = iter = default_clausule;
972     }
973
974     if(case_list2) {
975         if(ret)
976             iter->next = case_list2->head;
977         else
978             ret = case_list2->head;
979     }
980
981     if(!ret)
982         return NULL;
983
984     for(iter = ret; iter; iter = iter->next) {
985         for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
986         if(!iter2)
987             break;
988
989         while(iter != iter2) {
990             iter->stat = iter2->stat;
991             iter = iter->next;
992         }
993
994         if(stat) {
995             while(stat->next)
996                 stat = stat->next;
997             stat->next = iter->stat;
998         }else {
999             stat = iter->stat;
1000         }
1001     }
1002
1003     return ret;
1004 }
1005
1006 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1007 {
1008     block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1009
1010     ret->stat.eval = block_statement_eval;
1011     ret->stat.next = NULL;
1012     ret->stat_list = list ? list->head : NULL;
1013
1014     return &ret->stat;
1015 }
1016
1017 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1018 {
1019     variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1020     var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1021
1022     ret->identifier = identifier;
1023     ret->expr = expr;
1024     ret->next = NULL;
1025
1026     var_list->identifier = identifier;
1027     var_list->next = NULL;
1028
1029     if(ctx->func_stack->var_tail)
1030         ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1031     else
1032         ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1033
1034     return ret;
1035 }
1036
1037 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1038 {
1039     variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1040
1041     ret->head = ret->tail = decl;
1042
1043     return ret;
1044 }
1045
1046 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1047 {
1048     list->tail = list->tail->next = decl;
1049
1050     return list;
1051 }
1052
1053 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1054 {
1055     var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1056
1057     ret->stat.eval = var_statement_eval;
1058     ret->stat.next = NULL;
1059     ret->variable_list = variable_list->head;
1060
1061     return &ret->stat;
1062 }
1063
1064 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1065 {
1066     statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1067
1068     ret->eval = empty_statement_eval;
1069     ret->next = NULL;
1070
1071     return ret;
1072 }
1073
1074 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1075 {
1076     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1077
1078     ret->stat.eval = expression_statement_eval;
1079     ret->stat.next = NULL;
1080     ret->expr = expr;
1081
1082     return &ret->stat;
1083 }
1084
1085 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1086 {
1087     if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1088
1089     ret->stat.eval = if_statement_eval;
1090     ret->stat.next = NULL;
1091     ret->expr = expr;
1092     ret->if_stat = if_stat;
1093     ret->else_stat = else_stat;
1094
1095     return &ret->stat;
1096 }
1097
1098 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1099 {
1100     while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1101
1102     ret->stat.eval = while_statement_eval;
1103     ret->stat.next = NULL;
1104     ret->do_while = dowhile;
1105     ret->expr = expr;
1106     ret->statement = stat;
1107
1108     return &ret->stat;
1109 }
1110
1111 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1112         expression_t *expr, expression_t *end_expr, statement_t *statement)
1113 {
1114     for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1115
1116     ret->stat.eval = for_statement_eval;
1117     ret->stat.next = NULL;
1118     ret->variable_list = variable_list ? variable_list->head : NULL;
1119     ret->begin_expr = begin_expr;
1120     ret->expr = expr;
1121     ret->end_expr = end_expr;
1122     ret->statement = statement;
1123
1124     return &ret->stat;
1125 }
1126
1127 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1128         expression_t *in_expr, statement_t *statement)
1129 {
1130     forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1131
1132     ret->stat.eval = forin_statement_eval;
1133     ret->stat.next = NULL;
1134     ret->variable = variable;
1135     ret->expr = expr;
1136     ret->in_expr = in_expr;
1137     ret->statement = statement;
1138
1139     return &ret->stat;
1140 }
1141
1142 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1143 {
1144     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1145
1146     ret->stat.eval = continue_statement_eval;
1147     ret->stat.next = NULL;
1148     ret->identifier = identifier;
1149
1150     return &ret->stat;
1151 }
1152
1153 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1154 {
1155     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1156
1157     ret->stat.eval = break_statement_eval;
1158     ret->stat.next = NULL;
1159     ret->identifier = identifier;
1160
1161     return &ret->stat;
1162 }
1163
1164 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1165 {
1166     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1167
1168     ret->stat.eval = return_statement_eval;
1169     ret->stat.next = NULL;
1170     ret->expr = expr;
1171
1172     return &ret->stat;
1173 }
1174
1175 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1176 {
1177     with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1178
1179     ret->stat.eval = with_statement_eval;
1180     ret->stat.next = NULL;
1181     ret->expr = expr;
1182     ret->statement = statement;
1183
1184     return &ret->stat;
1185 }
1186
1187 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1188 {
1189     labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1190
1191     ret->stat.eval = labelled_statement_eval;
1192     ret->stat.next = NULL;
1193     ret->identifier = identifier;
1194     ret->statement = statement;
1195
1196     return &ret->stat;
1197 }
1198
1199 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1200 {
1201     switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1202
1203     ret->stat.eval = switch_statement_eval;
1204     ret->stat.next = NULL;
1205     ret->expr = expr;
1206     ret->case_list = case_list;
1207
1208     return &ret->stat;
1209 }
1210
1211 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1212 {
1213     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1214
1215     ret->stat.eval = throw_statement_eval;
1216     ret->stat.next = NULL;
1217     ret->expr = expr;
1218
1219     return &ret->stat;
1220 }
1221
1222 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1223        catch_block_t *catch_block, statement_t *finally_statement)
1224 {
1225     try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1226
1227     ret->stat.eval = try_statement_eval;
1228     ret->stat.next = NULL;
1229     ret->try_statement = try_statement;
1230     ret->catch_block = catch_block;
1231     ret->finally_statement = finally_statement;
1232
1233     return &ret->stat;
1234 }
1235
1236 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1237 {
1238     parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1239
1240     ret->identifier = identifier;
1241     ret->next = NULL;
1242
1243     return ret;
1244 }
1245
1246 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1247 {
1248     parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1249
1250     ret->head = ret->tail = new_parameter(ctx, identifier);
1251
1252     return ret;
1253 }
1254
1255 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1256 {
1257     list->tail = list->tail->next = new_parameter(ctx, identifier);
1258
1259     return list;
1260 }
1261
1262 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1263        parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1264 {
1265     function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1266
1267     ret->expr.eval = function_expression_eval;
1268     ret->identifier = identifier;
1269     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1270     ret->source_elements = source_elements;
1271     ret->src_str = src_str;
1272     ret->src_len = src_len;
1273
1274     if(ret->identifier) {
1275         function_declaration_t *decl = parser_alloc(ctx, sizeof(function_declaration_t));
1276
1277         decl->expr = ret;
1278         decl->next = NULL;
1279
1280         if(ctx->func_stack->func_tail)
1281             ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
1282         else
1283             ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1284     }
1285
1286     return &ret->expr;
1287 }
1288
1289 static const expression_eval_t expression_eval_table[] = {
1290    comma_expression_eval,
1291    logical_or_expression_eval,
1292    logical_and_expression_eval,
1293    binary_or_expression_eval,
1294    binary_xor_expression_eval,
1295    binary_and_expression_eval,
1296    instanceof_expression_eval,
1297    in_expression_eval,
1298    add_expression_eval,
1299    sub_expression_eval,
1300    mul_expression_eval,
1301    div_expression_eval,
1302    mod_expression_eval,
1303    delete_expression_eval,
1304    void_expression_eval,
1305    typeof_expression_eval,
1306    minus_expression_eval,
1307    plus_expression_eval,
1308    post_increment_expression_eval,
1309    post_decrement_expression_eval,
1310    pre_increment_expression_eval,
1311    pre_decrement_expression_eval,
1312    equal_expression_eval,
1313    equal2_expression_eval,
1314    not_equal_expression_eval,
1315    not_equal2_expression_eval,
1316    less_expression_eval,
1317    lesseq_expression_eval,
1318    greater_expression_eval,
1319    greatereq_expression_eval,
1320    binary_negation_expression_eval,
1321    logical_negation_expression_eval,
1322    left_shift_expression_eval,
1323    right_shift_expression_eval,
1324    right2_shift_expression_eval,
1325    assign_expression_eval,
1326    assign_lshift_expression_eval,
1327    assign_rshift_expression_eval,
1328    assign_rrshift_expression_eval,
1329    assign_add_expression_eval,
1330    assign_sub_expression_eval,
1331    assign_mul_expression_eval,
1332    assign_div_expression_eval,
1333    assign_mod_expression_eval,
1334    assign_and_expression_eval,
1335    assign_or_expression_eval,
1336    assign_xor_expression_eval,
1337 };
1338
1339 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1340        expression_t *expression1, expression_t *expression2)
1341 {
1342     binary_expression_t *ret = parser_alloc(ctx, sizeof(binary_expression_t));
1343
1344     ret->expr.eval = expression_eval_table[type];
1345     ret->expression1 = expression1;
1346     ret->expression2 = expression2;
1347
1348     return &ret->expr;
1349 }
1350
1351 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1352 {
1353     unary_expression_t *ret = parser_alloc(ctx, sizeof(unary_expression_t));
1354
1355     ret->expr.eval = expression_eval_table[type];
1356     ret->expression = expression;
1357
1358     return &ret->expr;
1359 }
1360
1361 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1362        expression_t *true_expression, expression_t *false_expression)
1363 {
1364     conditional_expression_t *ret = parser_alloc(ctx, sizeof(conditional_expression_t));
1365
1366     ret->expr.eval = conditional_expression_eval;
1367     ret->expression = expression;
1368     ret->true_expression = true_expression;
1369     ret->false_expression = false_expression;
1370
1371     return &ret->expr;
1372 }
1373
1374 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1375 {
1376     array_expression_t *ret = parser_alloc(ctx, sizeof(array_expression_t));
1377
1378     ret->expr.eval = array_expression_eval;
1379     ret->member_expr = member_expr;
1380     ret->expression = expression;
1381
1382     return &ret->expr;
1383 }
1384
1385 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1386 {
1387     member_expression_t *ret = parser_alloc(ctx, sizeof(member_expression_t));
1388
1389     ret->expr.eval = member_expression_eval;
1390     ret->expression = expression;
1391     ret->identifier = identifier;
1392
1393     return &ret->expr;
1394 }
1395
1396 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1397 {
1398     call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1399
1400     ret->expr.eval = new_expression_eval;
1401     ret->expression = expression;
1402     ret->argument_list = argument_list ? argument_list->head : NULL;
1403
1404     return &ret->expr;
1405 }
1406
1407 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1408 {
1409     call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1410
1411     ret->expr.eval = call_expression_eval;
1412     ret->expression = expression;
1413     ret->argument_list = argument_list ? argument_list->head : NULL;
1414
1415     return &ret->expr;
1416 }
1417
1418 static expression_t *new_this_expression(parser_ctx_t *ctx)
1419 {
1420     expression_t *ret = parser_alloc(ctx, sizeof(expression_t));
1421
1422     ret->eval = this_expression_eval;
1423
1424     return ret;
1425 }
1426
1427 static int parser_error(const char *str)
1428 {
1429     return 0;
1430 }
1431
1432 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1433 {
1434     identifier_expression_t *ret = parser_alloc(ctx, sizeof(identifier_expression_t));
1435
1436     ret->expr.eval = identifier_expression_eval;
1437     ret->identifier = identifier;
1438
1439     return &ret->expr;
1440 }
1441
1442 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1443 {
1444     array_literal_expression_t *ret = parser_alloc(ctx, sizeof(array_literal_expression_t));
1445
1446     ret->expr.eval = array_literal_expression_eval;
1447     ret->element_list = element_list ? element_list->head : NULL;
1448     ret->length = length;
1449
1450     return &ret->expr;
1451 }
1452
1453 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1454 {
1455     property_value_expression_t *ret = parser_alloc(ctx, sizeof(property_value_expression_t));
1456
1457     ret->expr.eval = property_value_expression_eval;
1458     ret->property_list = property_list ? property_list->head : NULL;
1459
1460     return &ret->expr;
1461 }
1462
1463 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1464 {
1465     literal_expression_t *ret = parser_alloc(ctx, sizeof(literal_expression_t));
1466
1467     ret->expr.eval = literal_expression_eval;
1468     ret->literal = literal;
1469
1470     return &ret->expr;
1471 }
1472
1473 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1474 {
1475     source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1476
1477     memset(ret, 0, sizeof(*ret));
1478
1479     return ret;
1480 }
1481
1482 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1483 {
1484     if(source_elements->statement_tail)
1485         source_elements->statement_tail = source_elements->statement_tail->next = statement;
1486     else
1487         source_elements->statement = source_elements->statement_tail = statement;
1488
1489     return source_elements;
1490 }
1491
1492 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1493 {
1494     statement_list_t *ret =  parser_alloc_tmp(ctx, sizeof(statement_list_t));
1495
1496     ret->head = ret->tail = statement;
1497
1498     return ret;
1499 }
1500
1501 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1502 {
1503     list->tail = list->tail->next = statement;
1504
1505     return list;
1506 }
1507
1508 static void push_func(parser_ctx_t *ctx)
1509 {
1510     func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1511
1512     new_func->func_head = new_func->func_tail = NULL;
1513     new_func->var_head = new_func->var_tail = NULL;
1514
1515     new_func->next = ctx->func_stack;
1516     ctx->func_stack = new_func;
1517 }
1518
1519 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1520 {
1521     source->functions = ctx->func_stack->func_head;
1522     source->variables = ctx->func_stack->var_head;
1523     pop_func(ctx);
1524
1525     return source;
1526 }
1527
1528 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1529 {
1530     source->functions = ctx->func_stack->func_head;
1531     source->variables = ctx->func_stack->var_head;
1532     pop_func(ctx);
1533
1534     ctx->source = source;
1535     ctx->hres = S_OK;
1536 }
1537
1538 void parser_release(parser_ctx_t *ctx)
1539 {
1540     obj_literal_t *iter;
1541
1542     if(--ctx->ref)
1543         return;
1544
1545     for(iter = ctx->obj_literals; iter; iter = iter->next)
1546         jsdisp_release(iter->obj);
1547
1548     jsheap_free(&ctx->heap);
1549     heap_free(ctx);
1550 }
1551
1552 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, parser_ctx_t **ret)
1553 {
1554     parser_ctx_t *parser_ctx;
1555     jsheap_t *mark;
1556     HRESULT hres;
1557
1558     parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1559     if(!parser_ctx)
1560         return E_OUTOFMEMORY;
1561
1562     parser_ctx->ref = 1;
1563     parser_ctx->hres = E_FAIL;
1564
1565     parser_ctx->begin = parser_ctx->ptr = code;
1566     parser_ctx->end = code + strlenW(code);
1567
1568     script_addref(ctx);
1569     parser_ctx->script = ctx;
1570
1571     mark = jsheap_mark(&ctx->tmp_heap);
1572     jsheap_init(&parser_ctx->heap);
1573
1574     push_func(parser_ctx);
1575
1576     parser_parse(parser_ctx);
1577     jsheap_clear(mark);
1578     if(FAILED(parser_ctx->hres)) {
1579         hres = parser_ctx->hres;
1580         parser_release(parser_ctx);
1581         return hres;
1582     }
1583
1584     *ret = parser_ctx;
1585     return S_OK;
1586 }