jscript: Added parser support for regular expressions.
[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         | '/'                   { $$ = parse_regexp(ctx);
779                                   if(!$$) YYABORT; }
780
781 /* ECMA-262 3rd Edition    7.8.2 */
782 BooleanLiteral
783         : kTRUE                 { $$ = new_boolean_literal(ctx, TRUE); }
784         | kFALSE                { $$ = new_boolean_literal(ctx, FALSE); }
785
786 semicolon_opt
787         : ';'
788         | error                 { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
789
790 %%
791
792 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
793 {
794     return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
795 }
796
797 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
798 {
799     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
800
801     ret->vt = VT_BSTR;
802     ret->u.wstr = str;
803
804     return ret;
805 }
806
807 static literal_t *new_null_literal(parser_ctx_t *ctx)
808 {
809     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
810
811     ret->vt = VT_NULL;
812
813     return ret;
814 }
815
816 static literal_t *new_undefined_literal(parser_ctx_t *ctx)
817 {
818     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
819
820     ret->vt = VT_EMPTY;
821
822     return ret;
823 }
824
825 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
826 {
827     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
828
829     ret->vt = VT_BOOL;
830     ret->u.bval = bval;
831
832     return ret;
833 }
834
835 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
836 {
837     prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
838
839     ret->name = name;
840     ret->value = value;
841     ret->next = NULL;
842
843     return ret;
844 }
845
846 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
847 {
848     property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
849
850     ret->head = ret->tail = new_prop_val(ctx, name, value);
851
852     return ret;
853 }
854
855 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
856 {
857     list->tail = list->tail->next = new_prop_val(ctx, name, value);
858
859     return list;
860 }
861
862 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
863 {
864     array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
865
866     ret->elision = elision;
867     ret->expr = expr;
868     ret->next = NULL;
869
870     return ret;
871 }
872
873 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
874 {
875     element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
876
877     ret->head = ret->tail = new_array_element(ctx, elision, expr);
878
879     return ret;
880 }
881
882 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
883 {
884     list->tail = list->tail->next = new_array_element(ctx, elision, expr);
885
886     return list;
887 }
888
889 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
890 {
891     argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
892
893     ret->expr = expr;
894     ret->next = NULL;
895
896     return ret;
897 }
898
899 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
900 {
901     argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
902
903     ret->head = ret->tail = new_argument(ctx, expr);
904
905     return ret;
906 }
907
908 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
909 {
910     list->tail = list->tail->next = new_argument(ctx, expr);
911
912     return list;
913 }
914
915 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
916 {
917     catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
918
919     ret->identifier = identifier;
920     ret->statement = statement;
921
922     return ret;
923 }
924
925 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
926 {
927     case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
928
929     ret->expr = expr;
930     ret->stat = stat_list ? stat_list->head : NULL;
931     ret->next = NULL;
932
933     return ret;
934 }
935
936 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
937 {
938     case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
939
940     ret->head = ret->tail = case_clausule;
941
942     return ret;
943 }
944
945 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
946 {
947     list->tail = list->tail->next = case_clausule;
948
949     return list;
950 }
951
952 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
953         case_clausule_t *default_clausule, case_list_t *case_list2)
954 {
955     case_clausule_t *ret = NULL, *iter = NULL, *iter2;
956     statement_t *stat = NULL;
957
958     if(case_list1) {
959         ret = case_list1->head;
960         iter = case_list1->tail;
961     }
962
963     if(default_clausule) {
964         if(ret)
965             iter = iter->next = default_clausule;
966         else
967             ret = iter = default_clausule;
968     }
969
970     if(case_list2) {
971         if(ret)
972             iter->next = case_list2->head;
973         else
974             ret = case_list2->head;
975     }
976
977     if(!ret)
978         return NULL;
979
980     for(iter = ret->next; iter->next; iter = iter->next) {
981         for(iter2 = iter; iter2 && !iter2->expr; iter2 = iter2->next);
982         if(!iter2)
983             break;
984
985         while(iter != iter2) {
986             iter->stat = iter2->stat;
987             iter2 = iter2->next;
988         }
989
990         if(stat) {
991             while(stat->next)
992                 stat = stat->next;
993             stat->next = iter->stat;
994         }else {
995             stat = iter->stat;
996         }
997     }
998
999     return ret;
1000 }
1001
1002 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1003 {
1004     block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1005
1006     ret->stat.eval = block_statement_eval;
1007     ret->stat.next = NULL;
1008     ret->stat_list = list ? list->head : NULL;
1009
1010     return &ret->stat;
1011 }
1012
1013 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1014 {
1015     variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1016
1017     ret->identifier = identifier;
1018     ret->expr = expr;
1019     ret->next = NULL;
1020
1021     return ret;
1022 }
1023
1024 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1025 {
1026     variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1027
1028     ret->head = ret->tail = decl;
1029
1030     return ret;
1031 }
1032
1033 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1034 {
1035     list->tail = list->tail->next = decl;
1036
1037     return list;
1038 }
1039
1040 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1041 {
1042     var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1043
1044     ret->stat.eval = var_statement_eval;
1045     ret->stat.next = NULL;
1046     ret->variable_list = variable_list->head;
1047
1048     return &ret->stat;
1049 }
1050
1051 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1052 {
1053     statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1054
1055     ret->eval = empty_statement_eval;
1056     ret->next = NULL;
1057
1058     return ret;
1059 }
1060
1061 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1062 {
1063     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1064
1065     ret->stat.eval = expression_statement_eval;
1066     ret->stat.next = NULL;
1067     ret->expr = expr;
1068
1069     return &ret->stat;
1070 }
1071
1072 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1073 {
1074     if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1075
1076     ret->stat.eval = if_statement_eval;
1077     ret->stat.next = NULL;
1078     ret->expr = expr;
1079     ret->if_stat = if_stat;
1080     ret->else_stat = else_stat;
1081
1082     return &ret->stat;
1083 }
1084
1085 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1086 {
1087     while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1088
1089     ret->stat.eval = dowhile ? dowhile_statement_eval : while_statement_eval;
1090     ret->stat.next = NULL;
1091     ret->expr = expr;
1092     ret->statement = stat;
1093
1094     return &ret->stat;
1095 }
1096
1097 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1098         expression_t *expr, expression_t *end_expr, statement_t *statement)
1099 {
1100     for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1101
1102     ret->stat.eval = for_statement_eval;
1103     ret->stat.next = NULL;
1104     ret->variable_list = variable_list ? variable_list->head : NULL;
1105     ret->begin_expr = begin_expr;
1106     ret->expr = expr;
1107     ret->end_expr = end_expr;
1108     ret->statement = statement;
1109
1110     return &ret->stat;
1111 }
1112
1113 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1114         expression_t *in_expr, statement_t *statement)
1115 {
1116     forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1117
1118     ret->stat.eval = forin_statement_eval;
1119     ret->stat.next = NULL;
1120     ret->variable = variable;
1121     ret->expr = expr;
1122     ret->in_expr = in_expr;
1123     ret->statement = statement;
1124
1125     return &ret->stat;
1126 }
1127
1128 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1129 {
1130     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1131
1132     ret->stat.eval = continue_statement_eval;
1133     ret->stat.next = NULL;
1134     ret->identifier = identifier;
1135
1136     return &ret->stat;
1137 }
1138
1139 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1140 {
1141     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1142
1143     ret->stat.eval = break_statement_eval;
1144     ret->stat.next = NULL;
1145     ret->identifier = identifier;
1146
1147     return &ret->stat;
1148 }
1149
1150 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1151 {
1152     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1153
1154     ret->stat.eval = return_statement_eval;
1155     ret->stat.next = NULL;
1156     ret->expr = expr;
1157
1158     return &ret->stat;
1159 }
1160
1161 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1162 {
1163     with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1164
1165     ret->stat.eval = with_statement_eval;
1166     ret->stat.next = NULL;
1167     ret->expr = expr;
1168     ret->statement = statement;
1169
1170     return &ret->stat;
1171 }
1172
1173 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1174 {
1175     labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1176
1177     ret->stat.eval = labelled_statement_eval;
1178     ret->stat.next = NULL;
1179     ret->identifier = identifier;
1180     ret->statement = statement;
1181
1182     return &ret->stat;
1183 }
1184
1185 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1186 {
1187     switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1188
1189     ret->stat.eval = switch_statement_eval;
1190     ret->stat.next = NULL;
1191     ret->expr = expr;
1192     ret->case_list = case_list;
1193
1194     return &ret->stat;
1195 }
1196
1197 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1198 {
1199     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1200
1201     ret->stat.eval = throw_statement_eval;
1202     ret->stat.next = NULL;
1203     ret->expr = expr;
1204
1205     return &ret->stat;
1206 }
1207
1208 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1209        catch_block_t *catch_block, statement_t *finally_statement)
1210 {
1211     try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1212
1213     ret->stat.eval = try_statement_eval;
1214     ret->stat.next = NULL;
1215     ret->try_statement = try_statement;
1216     ret->catch_block = catch_block;
1217     ret->finally_statement = finally_statement;
1218
1219     return &ret->stat;
1220 }
1221
1222 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1223 {
1224     parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1225
1226     ret->identifier = identifier;
1227     ret->next = NULL;
1228
1229     return ret;
1230 }
1231
1232 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1233 {
1234     parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1235
1236     ret->head = ret->tail = new_parameter(ctx, identifier);
1237
1238     return ret;
1239 }
1240
1241 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1242 {
1243     list->tail = list->tail->next = new_parameter(ctx, identifier);
1244
1245     return list;
1246 }
1247
1248 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1249        parameter_list_t *parameter_list, source_elements_t *source_elements)
1250 {
1251     function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1252
1253     ret->expr.eval = function_expression_eval;
1254     ret->identifier = identifier;
1255     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1256     ret->source_elements = source_elements;
1257
1258     return &ret->expr;
1259 }
1260
1261 static const expression_eval_t expression_eval_table[] = {
1262    comma_expression_eval,
1263    logical_or_expression_eval,
1264    logical_and_expression_eval,
1265    binary_or_expression_eval,
1266    binary_xor_expression_eval,
1267    binary_and_expression_eval,
1268    instanceof_expression_eval,
1269    in_expression_eval,
1270    add_expression_eval,
1271    sub_expression_eval,
1272    mul_expression_eval,
1273    div_expression_eval,
1274    mod_expression_eval,
1275    delete_expression_eval,
1276    void_expression_eval,
1277    typeof_expression_eval,
1278    minus_expression_eval,
1279    plus_expression_eval,
1280    post_increment_expression_eval,
1281    post_decrement_expression_eval,
1282    pre_increment_expression_eval,
1283    pre_decrement_expression_eval,
1284    new_expression_eval,
1285    equal_expression_eval,
1286    equal2_expression_eval,
1287    not_equal_expression_eval,
1288    not_equal2_expression_eval,
1289    less_expression_eval,
1290    lesseq_expression_eval,
1291    greater_expression_eval,
1292    greatereq_expression_eval,
1293    binary_negation_expression_eval,
1294    logical_negation_expression_eval,
1295    left_shift_expression_eval,
1296    right_shift_expression_eval,
1297    right2_shift_expression_eval,
1298    assign_expression_eval,
1299    assign_lshift_expression_eval,
1300    assign_rshift_expression_eval,
1301    assign_rrshift_expression_eval,
1302    assign_add_expression_eval,
1303    assign_sub_expression_eval,
1304    assign_mul_expression_eval,
1305    assign_div_expression_eval,
1306    assign_mod_expression_eval,
1307    assign_and_expression_eval,
1308    assign_or_expression_eval,
1309    assign_xor_expression_eval,
1310 };
1311
1312 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1313        expression_t *expression1, expression_t *expression2)
1314 {
1315     binary_expression_t *ret = parser_alloc(ctx, sizeof(binary_expression_t));
1316
1317     ret->expr.eval = expression_eval_table[type];
1318     ret->expression1 = expression1;
1319     ret->expression2 = expression2;
1320
1321     return &ret->expr;
1322 }
1323
1324 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1325 {
1326     unary_expression_t *ret = parser_alloc(ctx, sizeof(unary_expression_t));
1327
1328     ret->expr.eval = expression_eval_table[type];
1329     ret->expression = expression;
1330
1331     return &ret->expr;
1332 }
1333
1334 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1335        expression_t *true_expression, expression_t *false_expression)
1336 {
1337     conditional_expression_t *ret = parser_alloc(ctx, sizeof(conditional_expression_t));
1338
1339     ret->expr.eval = conditional_expression_eval;
1340     ret->expression = expression;
1341     ret->true_expression = true_expression;
1342     ret->false_expression = false_expression;
1343
1344     return &ret->expr;
1345 }
1346
1347 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1348 {
1349     array_expression_t *ret = parser_alloc(ctx, sizeof(array_expression_t));
1350
1351     ret->expr.eval = array_expression_eval;
1352     ret->member_expr = member_expr;
1353     ret->expression = expression;
1354
1355     return &ret->expr;
1356 }
1357
1358 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1359 {
1360     member_expression_t *ret = parser_alloc(ctx, sizeof(member_expression_t));
1361
1362     ret->expr.eval = member_expression_eval;
1363     ret->expression = expression;
1364     ret->identifier = identifier;
1365
1366     return &ret->expr;
1367 }
1368
1369 static expression_t *new_member_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1370 {
1371     call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1372
1373     ret->expr.eval = member_new_expression_eval;
1374     ret->expression = expression;
1375     ret->argument_list = argument_list ? argument_list->head : NULL;
1376
1377     return &ret->expr;
1378 }
1379
1380 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1381 {
1382     call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1383
1384     ret->expr.eval = call_expression_eval;
1385     ret->expression = expression;
1386     ret->argument_list = argument_list ? argument_list->head : NULL;
1387
1388     return &ret->expr;
1389 }
1390
1391 static expression_t *new_this_expression(parser_ctx_t *ctx)
1392 {
1393     expression_t *ret = parser_alloc(ctx, sizeof(expression_t));
1394
1395     ret->eval = this_expression_eval;
1396
1397     return ret;
1398 }
1399
1400 static int parser_error(const char *str)
1401 {
1402     return 0;
1403 }
1404
1405 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1406 {
1407     identifier_expression_t *ret = parser_alloc(ctx, sizeof(identifier_expression_t));
1408
1409     ret->expr.eval = identifier_expression_eval;
1410     ret->identifier = identifier;
1411
1412     return &ret->expr;
1413 }
1414
1415 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1416 {
1417     array_literal_expression_t *ret = parser_alloc(ctx, sizeof(array_literal_expression_t));
1418
1419     ret->expr.eval = array_literal_expression_eval;
1420     ret->element_list = element_list ? element_list->head : NULL;
1421     ret->length = length;
1422
1423     return &ret->expr;
1424 }
1425
1426 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1427 {
1428     property_value_expression_t *ret = parser_alloc(ctx, sizeof(property_value_expression_t));
1429
1430     ret->expr.eval = property_value_expression_eval;
1431     ret->property_list = property_list ? property_list->head : NULL;
1432
1433     return &ret->expr;
1434 }
1435
1436 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1437 {
1438     literal_expression_t *ret = parser_alloc(ctx, sizeof(literal_expression_t));
1439
1440     ret->expr.eval = literal_expression_eval;
1441     ret->literal = literal;
1442
1443     return &ret->expr;
1444 }
1445
1446 static function_declaration_t *new_function_declaration(parser_ctx_t *ctx, const WCHAR *identifier,
1447        parameter_list_t *parameter_list, source_elements_t *source_elements)
1448 {
1449     function_declaration_t *ret = parser_alloc(ctx, sizeof(function_declaration_t));
1450
1451     ret->identifier = identifier;
1452     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1453     ret->source_elements = source_elements;
1454     ret->next = NULL;
1455
1456     return ret;
1457 }
1458
1459 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1460 {
1461     source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1462
1463     memset(ret, 0, sizeof(*ret));
1464
1465     return ret;
1466 }
1467
1468 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1469 {
1470     if(source_elements->statement_tail)
1471         source_elements->statement_tail = source_elements->statement_tail->next = statement;
1472     else
1473         source_elements->statement = source_elements->statement_tail = statement;
1474
1475     return source_elements;
1476 }
1477
1478 static source_elements_t *source_elements_add_function(source_elements_t *source_elements,
1479        function_declaration_t *function_declaration)
1480 {
1481     if(source_elements->functions_tail)
1482         source_elements->functions_tail = source_elements->functions_tail->next = function_declaration;
1483     else
1484         source_elements->functions = source_elements->functions_tail = function_declaration;
1485
1486     return source_elements;
1487 }
1488
1489 statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1490 {
1491     statement_list_t *ret =  parser_alloc_tmp(ctx, sizeof(statement_list_t));
1492
1493     ret->head = ret->tail = statement;
1494
1495     return ret;
1496 }
1497
1498 statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1499 {
1500     list->tail = list->tail->next = statement;
1501
1502     return list;
1503 }
1504
1505 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1506 {
1507     ctx->source = source;
1508     ctx->hres = S_OK;
1509 }
1510
1511 void parser_release(parser_ctx_t *ctx)
1512 {
1513     obj_literal_t *iter;
1514
1515     if(--ctx->ref)
1516         return;
1517
1518     for(iter = ctx->obj_literals; iter; iter = iter->next)
1519         jsdisp_release(iter->obj);
1520
1521     jsheap_free(&ctx->heap);
1522     heap_free(ctx);
1523 }
1524
1525 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, parser_ctx_t **ret)
1526 {
1527     parser_ctx_t *parser_ctx;
1528     HRESULT hres;
1529
1530     parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1531     if(!parser_ctx)
1532         return E_OUTOFMEMORY;
1533
1534     parser_ctx->ref = 1;
1535     parser_ctx->hres = E_FAIL;
1536
1537     parser_ctx->begin = parser_ctx->ptr = code;
1538     parser_ctx->end = code + strlenW(code);
1539
1540     script_addref(ctx);
1541     parser_ctx->script = ctx;
1542
1543     jsheap_init(&parser_ctx->tmp_heap);
1544     jsheap_init(&parser_ctx->heap);
1545
1546     parser_parse(parser_ctx);
1547     jsheap_free(&parser_ctx->tmp_heap);
1548     if(FAILED(parser_ctx->hres)) {
1549         hres = parser_ctx->hres;
1550         parser_release(parser_ctx);
1551         return hres;
1552     }
1553
1554     *ret = parser_ctx;
1555     return S_OK;
1556 }