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