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