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