msxml: Partial implementation of ISAXXMLReader_putProperty.
[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 %%
257
258 /* ECMA-262 3rd Edition    14 */
259 Program
260        : SourceElements         { program_parsed(ctx, $1); }
261
262 /* ECMA-262 3rd Edition    14 */
263 SourceElements
264         : /* empty */           { $$ = new_source_elements(ctx); }
265         | SourceElements Statement
266                                 { $$ = source_elements_add_statement($1, $2); }
267         | SourceElements FunctionDeclaration
268                                 { $$ = source_elements_add_function($1, $2); }
269
270 /* ECMA-262 3rd Edition    13 */
271 FunctionDeclaration
272         : KFunction tIdentifier '(' FormalParameterList_opt ')' '{' FunctionBody '}'
273                                 { $$ = new_function_declaration(ctx, $2, $4, $7, $1, $8-$1+1); }
274
275 /* ECMA-262 3rd Edition    13 */
276 FunctionExpression
277         : KFunction Identifier_opt '(' FormalParameterList_opt ')' '{' FunctionBody '}'
278                                 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
279
280 KFunction
281         : kFUNCTION             { push_func(ctx); $$ = $1; }
282
283 /* ECMA-262 3rd Edition    13 */
284 FunctionBody
285         : SourceElements        { $$ = function_body_parsed(ctx, $1); }
286
287 /* ECMA-262 3rd Edition    13 */
288 FormalParameterList
289         : tIdentifier           { $$ = new_parameter_list(ctx, $1); }
290         | FormalParameterList ',' tIdentifier
291                                 { $$ = parameter_list_add(ctx, $1, $3); }
292
293 /* ECMA-262 3rd Edition    13 */
294 FormalParameterList_opt
295         : /* empty */           { $$ = NULL; }
296         | FormalParameterList   { $$ = $1; }
297
298 /* ECMA-262 3rd Edition    12 */
299 Statement
300         : Block                 { $$ = $1; }
301         | VariableStatement     { $$ = $1; }
302         | EmptyStatement        { $$ = $1; }
303         | ExpressionStatement   { $$ = $1; }
304         | IfStatement           { $$ = $1; }
305         | IterationStatement    { $$ = $1; }
306         | ContinueStatement     { $$ = $1; }
307         | BreakStatement        { $$ = $1; }
308         | ReturnStatement       { $$ = $1; }
309         | WithStatement         { $$ = $1; }
310         | LabelledStatement     { $$ = $1; }
311         | SwitchStatement       { $$ = $1; }
312         | ThrowStatement        { $$ = $1; }
313         | TryStatement          { $$ = $1; }
314
315 /* ECMA-262 3rd Edition    12.2 */
316 StatementList
317         : Statement             { $$ = new_statement_list(ctx, $1); }
318         | StatementList Statement
319                                 { $$ = statement_list_add($1, $2); }
320
321 /* ECMA-262 3rd Edition    12.2 */
322 StatementList_opt
323         : /* empty */           { $$ = NULL; }
324         | StatementList         { $$ = $1; }
325
326 /* ECMA-262 3rd Edition    12.1 */
327 Block
328         : '{' StatementList_opt '}'
329                                 { $$ = new_block_statement(ctx, $2); }
330
331 /* ECMA-262 3rd Edition    12.2 */
332 VariableStatement
333         : kVAR VariableDeclarationList semicolon_opt
334                                 { $$ = new_var_statement(ctx, $2); }
335
336 /* ECMA-262 3rd Edition    12.2 */
337 VariableDeclarationList
338         : VariableDeclaration   { $$ = new_variable_list(ctx, $1); }
339         | VariableDeclarationList ',' VariableDeclaration
340                                 { $$ = variable_list_add(ctx, $1, $3); }
341
342 /* ECMA-262 3rd Edition    12.2 */
343 VariableDeclarationListNoIn
344         : VariableDeclarationNoIn
345                                 { $$ = new_variable_list(ctx, $1); }
346         | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
347                                 { $$ = variable_list_add(ctx, $1, $3); }
348
349 /* ECMA-262 3rd Edition    12.2 */
350 VariableDeclaration
351         : tIdentifier Initialiser_opt
352                                 { $$ = new_variable_declaration(ctx, $1, $2); }
353
354 /* ECMA-262 3rd Edition    12.2 */
355 VariableDeclarationNoIn
356         : tIdentifier InitialiserNoIn_opt
357                                 { $$ = new_variable_declaration(ctx, $1, $2); }
358
359 /* ECMA-262 3rd Edition    12.2 */
360 Initialiser_opt
361         : /* empty */           { $$ = NULL; }
362         | Initialiser           { $$ = $1; }
363
364 /* ECMA-262 3rd Edition    12.2 */
365 Initialiser
366         : '=' AssignmentExpression
367                                 { $$ = $2; }
368
369 /* ECMA-262 3rd Edition    12.2 */
370 InitialiserNoIn_opt
371         : /* empty */           { $$ = NULL; }
372         | InitialiserNoIn       { $$ = $1; }
373
374 /* ECMA-262 3rd Edition    12.2 */
375 InitialiserNoIn
376         : '=' AssignmentExpressionNoIn
377                                 { $$ = $2; }
378
379 /* ECMA-262 3rd Edition    12.3 */
380 EmptyStatement
381         : ';'                   { $$ = new_empty_statement(ctx); }
382
383 /* ECMA-262 3rd Edition    12.4 */
384 ExpressionStatement
385         : Expression semicolon_opt
386                                 { $$ = new_expression_statement(ctx, $1); }
387
388 /* ECMA-262 3rd Edition    12.5 */
389 IfStatement
390         : kIF '(' Expression ')' Statement kELSE Statement
391                                 { $$ = new_if_statement(ctx, $3, $5, $7); }
392         | kIF '(' Expression ')' Statement
393                                 { $$ = new_if_statement(ctx, $3, $5, NULL); }
394
395 /* ECMA-262 3rd Edition    12.6 */
396 IterationStatement
397         : kDO Statement kWHILE '(' Expression ')' ';'
398                                 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
399         | kWHILE '(' Expression ')' Statement
400                                 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
401         | kFOR '(' ExpressionNoIn_opt ';' Expression_opt ';' Expression_opt ')' Statement
402                                 { $$ = new_for_statement(ctx, NULL, $3, $5, $7, $9); }
403         | kFOR '(' kVAR VariableDeclarationListNoIn ';' Expression_opt ';' Expression_opt ')' Statement
404                                 { $$ = new_for_statement(ctx, $4, NULL, $6, $8, $10); }
405         | kFOR '(' LeftHandSideExpression kIN Expression ')' Statement
406                                 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
407         | kFOR '(' kVAR VariableDeclarationNoIn kIN Expression ')' Statement
408                                 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
409
410 /* ECMA-262 3rd Edition    12.7 */
411 ContinueStatement
412         : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
413                                 { $$ = new_continue_statement(ctx, $2); }
414
415 /* ECMA-262 3rd Edition    12.8 */
416 BreakStatement
417         : kBREAK /* NONL */ Identifier_opt semicolon_opt
418                                 { $$ = new_break_statement(ctx, $2); }
419
420 /* ECMA-262 3rd Edition    12.9 */
421 ReturnStatement
422         : kRETURN /* NONL */ Expression_opt semicolon_opt
423                                 { $$ = new_return_statement(ctx, $2); }
424
425 /* ECMA-262 3rd Edition    12.10 */
426 WithStatement
427         : kWITH '(' Expression ')' Statement
428                                 { $$ = new_with_statement(ctx, $3, $5); }
429
430 /* ECMA-262 3rd Edition    12.12 */
431 LabelledStatement
432         : tIdentifier ':' Statement
433                                 { $$ = new_labelled_statement(ctx, $1, $3); }
434
435 /* ECMA-262 3rd Edition    12.11 */
436 SwitchStatement
437         : kSWITCH '(' Expression ')' CaseBlock
438                                  { $$ = new_switch_statement(ctx, $3, $5); }
439
440 /* ECMA-262 3rd Edition    12.11 */
441 CaseBlock
442         : '{' CaseClausules_opt '}'
443                                  { $$ = new_case_block(ctx, $2, NULL, NULL); }
444         | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
445                                  { $$ = new_case_block(ctx, $2, $3, $4); }
446
447 /* ECMA-262 3rd Edition    12.11 */
448 CaseClausules_opt
449         : /* empty */            { $$ = NULL; }
450         | CaseClausules          { $$ = $1; }
451
452 /* ECMA-262 3rd Edition    12.11 */
453 CaseClausules
454         : CaseClausule           { $$ = new_case_list(ctx, $1); }
455         | CaseClausules CaseClausule
456                                  { $$ = case_list_add(ctx, $1, $2); }
457
458 /* ECMA-262 3rd Edition    12.11 */
459 CaseClausule
460         : kCASE Expression ':' StatementList_opt
461                                  { $$ = new_case_clausule(ctx, $2, $4); }
462
463 /* ECMA-262 3rd Edition    12.11 */
464 DefaultClausule
465         : kDEFAULT ':' StatementList_opt
466                                  { $$ = new_case_clausule(ctx, NULL, $3); }
467
468 /* ECMA-262 3rd Edition    12.13 */
469 ThrowStatement
470         : kTHROW /* NONL */ Expression semicolon_opt
471                                 { $$ = new_throw_statement(ctx, $2); }
472
473 /* ECMA-262 3rd Edition    12.14 */
474 TryStatement
475         : kTRY Block Catch      { $$ = new_try_statement(ctx, $2, $3, NULL); }
476         | kTRY Block Finally    { $$ = new_try_statement(ctx, $2, NULL, $3); }
477         | kTRY Block Catch Finally
478                                 { $$ = new_try_statement(ctx, $2, $3, $4); }
479
480 /* ECMA-262 3rd Edition    12.14 */
481 Catch
482         : kCATCH '(' tIdentifier ')' Block
483                                  { $$ = new_catch_block(ctx, $3, $5); }
484
485 /* ECMA-262 3rd Edition    12.14 */
486 Finally
487         : kFINALLY Block        { $$ = $2; }
488
489 /* ECMA-262 3rd Edition    11.14 */
490 Expression_opt
491         : /* empty */           { $$ = NULL; }
492         | Expression            { $$ = $1; }
493
494 /* ECMA-262 3rd Edition    11.14 */
495 Expression
496         : AssignmentExpression  { $$ = $1; }
497         | Expression ',' AssignmentExpression
498                                 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
499
500 /* ECMA-262 3rd Edition    11.14 */
501 ExpressionNoIn_opt
502         : /* empty */           { $$ = NULL; }
503         | ExpressionNoIn        { $$ = $1; }
504
505 /* ECMA-262 3rd Edition    11.14 */
506 ExpressionNoIn
507         : AssignmentExpressionNoIn
508                                 { $$ = $1; }
509         | ExpressionNoIn ',' AssignmentExpressionNoIn
510                                 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
511
512 /* ECMA-262 3rd Edition    11.13 */
513 AssignmentExpression
514         : ConditionalExpression { $$ = $1; }
515         | LeftHandSideExpression '=' AssignmentExpression
516                                 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
517         | LeftHandSideExpression tAssignOper AssignmentExpression
518                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
519
520 /* ECMA-262 3rd Edition    11.13 */
521 AssignmentExpressionNoIn
522         : ConditionalExpressionNoIn
523                                 { $$ = $1; }
524         | LeftHandSideExpression '=' AssignmentExpressionNoIn
525                                 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
526         | LeftHandSideExpression tAssignOper AssignmentExpressionNoIn
527                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
528
529 /* ECMA-262 3rd Edition    11.12 */
530 ConditionalExpression
531         : LogicalORExpression   { $$ = $1; }
532         | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
533                                 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
534
535 /* ECMA-262 3rd Edition    11.12 */
536 ConditionalExpressionNoIn
537         : LogicalORExpressionNoIn
538                                 { $$ = $1; }
539         | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
540                                 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
541
542 /* ECMA-262 3rd Edition    11.11 */
543 LogicalORExpression
544         : LogicalANDExpression  { $$ = $1; }
545         | LogicalORExpression tOROR LogicalANDExpression
546                                 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
547
548 /* ECMA-262 3rd Edition    11.11 */
549 LogicalORExpressionNoIn
550         : LogicalANDExpressionNoIn
551                                 { $$ = $1; }
552         | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
553                                 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
554
555 /* ECMA-262 3rd Edition    11.11 */
556 LogicalANDExpression
557         : BitwiseORExpression   { $$ = $1; }
558         | LogicalANDExpression tANDAND BitwiseORExpression
559                                 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
560
561 /* ECMA-262 3rd Edition    11.11 */
562 LogicalANDExpressionNoIn
563         : BitwiseORExpressionNoIn
564                                 { $$ = $1; }
565         | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
566                                 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
567
568 /* ECMA-262 3rd Edition    11.10 */
569 BitwiseORExpression
570         : BitwiseXORExpression   { $$ = $1; }
571         | BitwiseORExpression '|' BitwiseXORExpression
572                                 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
573
574 /* ECMA-262 3rd Edition    11.10 */
575 BitwiseORExpressionNoIn
576         : BitwiseXORExpressionNoIn
577                                 { $$ = $1; }
578         | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
579                                 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
580
581 /* ECMA-262 3rd Edition    11.10 */
582 BitwiseXORExpression
583         : BitwiseANDExpression  { $$ = $1; }
584         | BitwiseXORExpression '^' BitwiseANDExpression
585                                 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
586
587 /* ECMA-262 3rd Edition    11.10 */
588 BitwiseXORExpressionNoIn
589         : BitwiseANDExpressionNoIn
590                                 { $$ = $1; }
591         | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
592                                 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
593
594 /* ECMA-262 3rd Edition    11.10 */
595 BitwiseANDExpression
596         : EqualityExpression    { $$ = $1; }
597         | BitwiseANDExpression '&' EqualityExpression
598                                 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
599
600 /* ECMA-262 3rd Edition    11.10 */
601 BitwiseANDExpressionNoIn
602         : EqualityExpressionNoIn
603                                 { $$ = $1; }
604         | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
605                                 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
606
607 /* ECMA-262 3rd Edition    11.9 */
608 EqualityExpression
609         : RelationalExpression  { $$ = $1; }
610         | EqualityExpression tEqOper RelationalExpression
611                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
612
613 /* ECMA-262 3rd Edition    11.9 */
614 EqualityExpressionNoIn
615         : RelationalExpressionNoIn  { $$ = $1; }
616         | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
617                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
618
619 /* ECMA-262 3rd Edition    11.8 */
620 RelationalExpression
621         : ShiftExpression       { $$ = $1; }
622         | RelationalExpression tRelOper ShiftExpression
623                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
624         | RelationalExpression kINSTANCEOF ShiftExpression
625                                 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
626         | RelationalExpression kIN ShiftExpression
627                                 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
628
629 /* ECMA-262 3rd Edition    11.8 */
630 RelationalExpressionNoIn
631         : ShiftExpression       { $$ = $1; }
632         | RelationalExpressionNoIn tRelOper ShiftExpression
633                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
634         | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
635                                 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
636
637 /* ECMA-262 3rd Edition    11.7 */
638 ShiftExpression
639         : AdditiveExpression    { $$ = $1; }
640         | ShiftExpression tShiftOper AdditiveExpression
641                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
642
643 /* ECMA-262 3rd Edition    11.6 */
644 AdditiveExpression
645         : MultiplicativeExpression
646                                 { $$ = $1; }
647         | AdditiveExpression '+' MultiplicativeExpression
648                                 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
649         | AdditiveExpression '-' MultiplicativeExpression
650                                 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
651
652 /* ECMA-262 3rd Edition    11.5 */
653 MultiplicativeExpression
654         : UnaryExpression       { $$ = $1; }
655         | MultiplicativeExpression '*' UnaryExpression
656                                 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
657         | MultiplicativeExpression '/' UnaryExpression
658                                 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
659         | MultiplicativeExpression '%' UnaryExpression
660                                 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
661
662 /* ECMA-262 3rd Edition    11.4 */
663 UnaryExpression
664         : PostfixExpression     { $$ = $1; }
665         | kDELETE UnaryExpression
666                                 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
667         | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
668         | kTYPEOF UnaryExpression
669                                 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
670         | tINC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
671         | tDEC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
672         | '+' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
673         | '-' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
674         | '~' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
675         | '!' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
676
677 /* ECMA-262 3rd Edition    11.2 */
678 PostfixExpression
679         : LeftHandSideExpression
680                                 { $$ = $1; }
681         | LeftHandSideExpression /* NONL */ tINC
682                                 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
683         | LeftHandSideExpression /* NONL */ tDEC
684                                 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
685
686
687 /* ECMA-262 3rd Edition    11.2 */
688 LeftHandSideExpression
689         : NewExpression         { $$ = $1; }
690         | CallExpression        { $$ = $1; }
691
692 /* ECMA-262 3rd Edition    11.2 */
693 NewExpression
694         : MemberExpression      { $$ = $1; }
695         | kNEW NewExpression    { $$ = new_new_expression(ctx, $2, NULL); }
696
697 /* ECMA-262 3rd Edition    11.2 */
698 MemberExpression
699         : PrimaryExpression     { $$ = $1; }
700         | FunctionExpression    { $$ = $1; }
701         | MemberExpression '[' Expression ']'
702                                 { $$ = new_array_expression(ctx, $1, $3); }
703         | MemberExpression '.' tIdentifier
704                                 { $$ = new_member_expression(ctx, $1, $3); }
705         | kNEW MemberExpression Arguments
706                                 { $$ = new_new_expression(ctx, $2, $3); }
707
708 /* ECMA-262 3rd Edition    11.2 */
709 CallExpression
710         : MemberExpression Arguments
711                                 { $$ = new_call_expression(ctx, $1, $2); }
712         | CallExpression Arguments
713                                 { $$ = new_call_expression(ctx, $1, $2); }
714         | CallExpression '[' Expression ']'
715                                 { $$ = new_array_expression(ctx, $1, $3); }
716         | CallExpression '.' tIdentifier
717                                 { $$ = new_member_expression(ctx, $1, $3); }
718
719 /* ECMA-262 3rd Edition    11.2 */
720 Arguments
721         : '(' ')'               { $$ = NULL; }
722         | '(' ArgumentList ')'  { $$ = $2; }
723
724 /* ECMA-262 3rd Edition    11.2 */
725 ArgumentList
726         : AssignmentExpression  { $$ = new_argument_list(ctx, $1); }
727         | ArgumentList ',' AssignmentExpression
728                                 { $$ = argument_list_add(ctx, $1, $3); }
729
730 /* ECMA-262 3rd Edition    11.1 */
731 PrimaryExpression
732         : kTHIS                 { $$ = new_this_expression(ctx); }
733         | tIdentifier           { $$ = new_identifier_expression(ctx, $1); }
734         | Literal               { $$ = new_literal_expression(ctx, $1); }
735         | ArrayLiteral          { $$ = $1; }
736         | ObjectLiteral         { $$ = $1; }
737         | '(' Expression ')'    { $$ = $2; }
738
739 /* ECMA-262 3rd Edition    11.1.4 */
740 ArrayLiteral
741         : '[' Elision_opt ']'   { $$ = new_array_literal_expression(ctx, NULL, $2); }
742         | '[' ElementList ']'   { $$ = new_array_literal_expression(ctx, $2, 0); }
743         | '[' ElementList ',' Elision_opt ']'
744                                 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
745
746 /* ECMA-262 3rd Edition    11.1.4 */
747 ElementList
748         : Elision_opt AssignmentExpression
749                                 { $$ = new_element_list(ctx, $1, $2); }
750         | ElementList ',' Elision_opt AssignmentExpression
751                                 { $$ = element_list_add(ctx, $1, $3, $4); }
752
753 /* ECMA-262 3rd Edition    11.1.4 */
754 Elision
755         : ','                   { $$ = 1; }
756         | Elision ','           { $$ = $1 + 1; }
757
758 /* ECMA-262 3rd Edition    11.1.4 */
759 Elision_opt
760         : /* empty */           { $$ = 0; }
761         | Elision               { $$ = $1; }
762
763 /* ECMA-262 3rd Edition    11.1.5 */
764 ObjectLiteral
765         : '{' '}'               { $$ = new_prop_and_value_expression(ctx, NULL); }
766         | '{' PropertyNameAndValueList '}'
767                                 { $$ = new_prop_and_value_expression(ctx, $2); }
768
769 /* ECMA-262 3rd Edition    11.1.5 */
770 PropertyNameAndValueList
771         : PropertyName ':' AssignmentExpression
772                                 { $$ = new_property_list(ctx, $1, $3); }
773         | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
774                                 { $$ = property_list_add(ctx, $1, $3, $5); }
775
776 /* ECMA-262 3rd Edition    11.1.5 */
777 PropertyName
778         : tIdentifier           { $$ = new_string_literal(ctx, $1); }
779         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
780         | tNumericLiteral       { $$ = $1; }
781
782 /* ECMA-262 3rd Edition    7.6 */
783 Identifier_opt
784         : /* empty*/            { $$ = NULL; }
785         | tIdentifier           { $$ = $1; }
786
787 /* ECMA-262 3rd Edition    7.8 */
788 Literal
789         : kNULL                 { $$ = new_null_literal(ctx); }
790         | kUNDEFINED            { $$ = new_undefined_literal(ctx); }
791         | BooleanLiteral        { $$ = $1; }
792         | tNumericLiteral       { $$ = $1; }
793         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
794         | '/'                   { $$ = parse_regexp(ctx);
795                                   if(!$$) YYABORT; }
796
797 /* ECMA-262 3rd Edition    7.8.2 */
798 BooleanLiteral
799         : kTRUE                 { $$ = new_boolean_literal(ctx, TRUE); }
800         | kFALSE                { $$ = new_boolean_literal(ctx, FALSE); }
801
802 semicolon_opt
803         : ';'
804         | error                 { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
805
806 %%
807
808 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
809 {
810     return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
811 }
812
813 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
814 {
815     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
816
817     ret->vt = VT_BSTR;
818     ret->u.wstr = str;
819
820     return ret;
821 }
822
823 static literal_t *new_null_literal(parser_ctx_t *ctx)
824 {
825     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
826
827     ret->vt = VT_NULL;
828
829     return ret;
830 }
831
832 static literal_t *new_undefined_literal(parser_ctx_t *ctx)
833 {
834     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
835
836     ret->vt = VT_EMPTY;
837
838     return ret;
839 }
840
841 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
842 {
843     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
844
845     ret->vt = VT_BOOL;
846     ret->u.bval = bval;
847
848     return ret;
849 }
850
851 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
852 {
853     prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
854
855     ret->name = name;
856     ret->value = value;
857     ret->next = NULL;
858
859     return ret;
860 }
861
862 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
863 {
864     property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
865
866     ret->head = ret->tail = new_prop_val(ctx, name, value);
867
868     return ret;
869 }
870
871 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
872 {
873     list->tail = list->tail->next = new_prop_val(ctx, name, value);
874
875     return list;
876 }
877
878 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
879 {
880     array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
881
882     ret->elision = elision;
883     ret->expr = expr;
884     ret->next = NULL;
885
886     return ret;
887 }
888
889 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
890 {
891     element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
892
893     ret->head = ret->tail = new_array_element(ctx, elision, expr);
894
895     return ret;
896 }
897
898 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
899 {
900     list->tail = list->tail->next = new_array_element(ctx, elision, expr);
901
902     return list;
903 }
904
905 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
906 {
907     argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
908
909     ret->expr = expr;
910     ret->next = NULL;
911
912     return ret;
913 }
914
915 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
916 {
917     argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
918
919     ret->head = ret->tail = new_argument(ctx, expr);
920
921     return ret;
922 }
923
924 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
925 {
926     list->tail = list->tail->next = new_argument(ctx, expr);
927
928     return list;
929 }
930
931 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
932 {
933     catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
934
935     ret->identifier = identifier;
936     ret->statement = statement;
937
938     return ret;
939 }
940
941 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
942 {
943     case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
944
945     ret->expr = expr;
946     ret->stat = stat_list ? stat_list->head : NULL;
947     ret->next = NULL;
948
949     return ret;
950 }
951
952 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
953 {
954     case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
955
956     ret->head = ret->tail = case_clausule;
957
958     return ret;
959 }
960
961 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
962 {
963     list->tail = list->tail->next = case_clausule;
964
965     return list;
966 }
967
968 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
969         case_clausule_t *default_clausule, case_list_t *case_list2)
970 {
971     case_clausule_t *ret = NULL, *iter = NULL, *iter2;
972     statement_t *stat = NULL;
973
974     if(case_list1) {
975         ret = case_list1->head;
976         iter = case_list1->tail;
977     }
978
979     if(default_clausule) {
980         if(ret)
981             iter = iter->next = default_clausule;
982         else
983             ret = iter = default_clausule;
984     }
985
986     if(case_list2) {
987         if(ret)
988             iter->next = case_list2->head;
989         else
990             ret = case_list2->head;
991     }
992
993     if(!ret)
994         return NULL;
995
996     for(iter = ret; iter; iter = iter->next) {
997         for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
998         if(!iter2)
999             break;
1000
1001         while(iter != iter2) {
1002             iter->stat = iter2->stat;
1003             iter = iter->next;
1004         }
1005
1006         if(stat) {
1007             while(stat->next)
1008                 stat = stat->next;
1009             stat->next = iter->stat;
1010         }else {
1011             stat = iter->stat;
1012         }
1013     }
1014
1015     return ret;
1016 }
1017
1018 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1019 {
1020     block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1021
1022     ret->stat.eval = block_statement_eval;
1023     ret->stat.next = NULL;
1024     ret->stat_list = list ? list->head : NULL;
1025
1026     return &ret->stat;
1027 }
1028
1029 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1030 {
1031     variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1032     var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1033
1034     ret->identifier = identifier;
1035     ret->expr = expr;
1036     ret->next = NULL;
1037
1038     var_list->identifier = identifier;
1039     var_list->next = NULL;
1040
1041     if(ctx->func_stack->var_tail)
1042         ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1043     else
1044         ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1045
1046     return ret;
1047 }
1048
1049 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1050 {
1051     variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1052
1053     ret->head = ret->tail = decl;
1054
1055     return ret;
1056 }
1057
1058 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1059 {
1060     list->tail = list->tail->next = decl;
1061
1062     return list;
1063 }
1064
1065 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1066 {
1067     var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1068
1069     ret->stat.eval = var_statement_eval;
1070     ret->stat.next = NULL;
1071     ret->variable_list = variable_list->head;
1072
1073     return &ret->stat;
1074 }
1075
1076 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1077 {
1078     statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1079
1080     ret->eval = empty_statement_eval;
1081     ret->next = NULL;
1082
1083     return ret;
1084 }
1085
1086 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1087 {
1088     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1089
1090     ret->stat.eval = expression_statement_eval;
1091     ret->stat.next = NULL;
1092     ret->expr = expr;
1093
1094     return &ret->stat;
1095 }
1096
1097 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1098 {
1099     if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1100
1101     ret->stat.eval = if_statement_eval;
1102     ret->stat.next = NULL;
1103     ret->expr = expr;
1104     ret->if_stat = if_stat;
1105     ret->else_stat = else_stat;
1106
1107     return &ret->stat;
1108 }
1109
1110 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1111 {
1112     while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1113
1114     ret->stat.eval = while_statement_eval;
1115     ret->stat.next = NULL;
1116     ret->do_while = dowhile;
1117     ret->expr = expr;
1118     ret->statement = stat;
1119
1120     return &ret->stat;
1121 }
1122
1123 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1124         expression_t *expr, expression_t *end_expr, statement_t *statement)
1125 {
1126     for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1127
1128     ret->stat.eval = for_statement_eval;
1129     ret->stat.next = NULL;
1130     ret->variable_list = variable_list ? variable_list->head : NULL;
1131     ret->begin_expr = begin_expr;
1132     ret->expr = expr;
1133     ret->end_expr = end_expr;
1134     ret->statement = statement;
1135
1136     return &ret->stat;
1137 }
1138
1139 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1140         expression_t *in_expr, statement_t *statement)
1141 {
1142     forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1143
1144     ret->stat.eval = forin_statement_eval;
1145     ret->stat.next = NULL;
1146     ret->variable = variable;
1147     ret->expr = expr;
1148     ret->in_expr = in_expr;
1149     ret->statement = statement;
1150
1151     return &ret->stat;
1152 }
1153
1154 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1155 {
1156     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1157
1158     ret->stat.eval = continue_statement_eval;
1159     ret->stat.next = NULL;
1160     ret->identifier = identifier;
1161
1162     return &ret->stat;
1163 }
1164
1165 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1166 {
1167     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1168
1169     ret->stat.eval = break_statement_eval;
1170     ret->stat.next = NULL;
1171     ret->identifier = identifier;
1172
1173     return &ret->stat;
1174 }
1175
1176 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1177 {
1178     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1179
1180     ret->stat.eval = return_statement_eval;
1181     ret->stat.next = NULL;
1182     ret->expr = expr;
1183
1184     return &ret->stat;
1185 }
1186
1187 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1188 {
1189     with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1190
1191     ret->stat.eval = with_statement_eval;
1192     ret->stat.next = NULL;
1193     ret->expr = expr;
1194     ret->statement = statement;
1195
1196     return &ret->stat;
1197 }
1198
1199 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1200 {
1201     labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1202
1203     ret->stat.eval = labelled_statement_eval;
1204     ret->stat.next = NULL;
1205     ret->identifier = identifier;
1206     ret->statement = statement;
1207
1208     return &ret->stat;
1209 }
1210
1211 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1212 {
1213     switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1214
1215     ret->stat.eval = switch_statement_eval;
1216     ret->stat.next = NULL;
1217     ret->expr = expr;
1218     ret->case_list = case_list;
1219
1220     return &ret->stat;
1221 }
1222
1223 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1224 {
1225     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1226
1227     ret->stat.eval = throw_statement_eval;
1228     ret->stat.next = NULL;
1229     ret->expr = expr;
1230
1231     return &ret->stat;
1232 }
1233
1234 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1235        catch_block_t *catch_block, statement_t *finally_statement)
1236 {
1237     try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1238
1239     ret->stat.eval = try_statement_eval;
1240     ret->stat.next = NULL;
1241     ret->try_statement = try_statement;
1242     ret->catch_block = catch_block;
1243     ret->finally_statement = finally_statement;
1244
1245     return &ret->stat;
1246 }
1247
1248 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1249 {
1250     parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1251
1252     ret->identifier = identifier;
1253     ret->next = NULL;
1254
1255     return ret;
1256 }
1257
1258 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1259 {
1260     parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1261
1262     ret->head = ret->tail = new_parameter(ctx, identifier);
1263
1264     return ret;
1265 }
1266
1267 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1268 {
1269     list->tail = list->tail->next = new_parameter(ctx, identifier);
1270
1271     return list;
1272 }
1273
1274 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1275        parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1276 {
1277     function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1278
1279     ret->expr.eval = function_expression_eval;
1280     ret->identifier = identifier;
1281     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1282     ret->source_elements = source_elements;
1283     ret->src_str = src_str;
1284     ret->src_len = src_len;
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 function_declaration_t *new_function_declaration(parser_ctx_t *ctx, const WCHAR *identifier,
1474        parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1475 {
1476     function_declaration_t *ret = parser_alloc(ctx, sizeof(function_declaration_t));
1477
1478     ret->identifier = identifier;
1479     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1480     ret->source_elements = source_elements;
1481     ret->src_str = src_str;
1482     ret->src_len = src_len;
1483     ret->next = NULL;
1484
1485     return ret;
1486 }
1487
1488 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1489 {
1490     source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1491
1492     memset(ret, 0, sizeof(*ret));
1493
1494     return ret;
1495 }
1496
1497 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1498 {
1499     if(source_elements->statement_tail)
1500         source_elements->statement_tail = source_elements->statement_tail->next = statement;
1501     else
1502         source_elements->statement = source_elements->statement_tail = statement;
1503
1504     return source_elements;
1505 }
1506
1507 static source_elements_t *source_elements_add_function(source_elements_t *source_elements,
1508        function_declaration_t *function_declaration)
1509 {
1510     if(source_elements->functions_tail)
1511         source_elements->functions_tail = source_elements->functions_tail->next = function_declaration;
1512     else
1513         source_elements->functions = source_elements->functions_tail = function_declaration;
1514
1515     return source_elements;
1516 }
1517
1518 statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1519 {
1520     statement_list_t *ret =  parser_alloc_tmp(ctx, sizeof(statement_list_t));
1521
1522     ret->head = ret->tail = statement;
1523
1524     return ret;
1525 }
1526
1527 statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1528 {
1529     list->tail = list->tail->next = statement;
1530
1531     return list;
1532 }
1533
1534 static void push_func(parser_ctx_t *ctx)
1535 {
1536     func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1537
1538     new_func->var_head = new_func->var_tail = NULL;
1539
1540     new_func->next = ctx->func_stack;
1541     ctx->func_stack = new_func;
1542 }
1543
1544 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1545 {
1546     source->variables = ctx->func_stack->var_head;
1547     pop_func(ctx);
1548
1549     return source;
1550 }
1551
1552 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1553 {
1554     source->variables = ctx->func_stack->var_head;
1555     pop_func(ctx);
1556
1557     ctx->source = source;
1558     ctx->hres = S_OK;
1559 }
1560
1561 void parser_release(parser_ctx_t *ctx)
1562 {
1563     obj_literal_t *iter;
1564
1565     if(--ctx->ref)
1566         return;
1567
1568     for(iter = ctx->obj_literals; iter; iter = iter->next)
1569         jsdisp_release(iter->obj);
1570
1571     jsheap_free(&ctx->heap);
1572     heap_free(ctx);
1573 }
1574
1575 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, parser_ctx_t **ret)
1576 {
1577     parser_ctx_t *parser_ctx;
1578     jsheap_t *mark;
1579     HRESULT hres;
1580
1581     parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1582     if(!parser_ctx)
1583         return E_OUTOFMEMORY;
1584
1585     parser_ctx->ref = 1;
1586     parser_ctx->hres = E_FAIL;
1587
1588     parser_ctx->begin = parser_ctx->ptr = code;
1589     parser_ctx->end = code + strlenW(code);
1590
1591     script_addref(ctx);
1592     parser_ctx->script = ctx;
1593
1594     mark = jsheap_mark(&ctx->tmp_heap);
1595     jsheap_init(&parser_ctx->heap);
1596
1597     push_func(parser_ctx);
1598
1599     parser_parse(parser_ctx);
1600     jsheap_clear(mark);
1601     if(FAILED(parser_ctx->hres)) {
1602         hres = parser_ctx->hres;
1603         parser_release(parser_ctx);
1604         return hres;
1605     }
1606
1607     *ret = parser_ctx;
1608     return S_OK;
1609 }