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