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