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