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