wined3d: Don't record dirty areas in a readonly lock.
[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
184 %type <source_elements> SourceElements
185 %type <source_elements> FunctionBody
186 %type <statement> Statement
187 %type <statement> Block
188 %type <statement> VariableStatement
189 %type <statement> EmptyStatement
190 %type <statement> ExpressionStatement
191 %type <statement> IfStatement
192 %type <statement> IterationStatement
193 %type <statement> ContinueStatement
194 %type <statement> BreakStatement
195 %type <statement> ReturnStatement
196 %type <statement> WithStatement
197 %type <statement> LabelledStatement
198 %type <statement> SwitchStatement
199 %type <statement> ThrowStatement
200 %type <statement> TryStatement
201 %type <statement> Finally
202 %type <statement_list> StatementList StatementList_opt
203 %type <parameter_list> FormalParameterList FormalParameterList_opt
204 %type <expr> Expression Expression_opt Expression_err
205 %type <expr> ExpressionNoIn ExpressionNoIn_opt
206 %type <expr> FunctionExpression
207 %type <expr> AssignmentExpression AssignmentExpressionNoIn
208 %type <expr> ConditionalExpression ConditionalExpressionNoIn
209 %type <expr> LeftHandSideExpression
210 %type <expr> LogicalORExpression LogicalORExpressionNoIn
211 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
212 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
213 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
214 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
215 %type <expr> EqualityExpression EqualityExpressionNoIn
216 %type <expr> RelationalExpression RelationalExpressionNoIn
217 %type <expr> ShiftExpression
218 %type <expr> AdditiveExpression
219 %type <expr> MultiplicativeExpression
220 %type <expr> Initialiser_opt Initialiser
221 %type <expr> InitialiserNoIn_opt InitialiserNoIn
222 %type <expr> UnaryExpression
223 %type <expr> PostfixExpression
224 %type <expr> NewExpression
225 %type <expr> CallExpression
226 %type <expr> MemberExpression
227 %type <expr> PrimaryExpression
228 %type <identifier> Identifier_opt
229 %type <variable_list> VariableDeclarationList
230 %type <variable_list> VariableDeclarationListNoIn
231 %type <variable_declaration> VariableDeclaration
232 %type <variable_declaration> VariableDeclarationNoIn
233 %type <case_list> CaseClausules CaseClausules_opt
234 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
235 %type <catch_block> Catch
236 %type <argument_list> Arguments
237 %type <argument_list> ArgumentList
238 %type <literal> Literal
239 %type <expr> ArrayLiteral
240 %type <expr> ObjectLiteral
241 %type <ival> Elision Elision_opt
242 %type <element_list> ElementList
243 %type <property_list> PropertyNameAndValueList
244 %type <literal> PropertyName
245 %type <literal> BooleanLiteral
246 %type <srcptr> KFunction
247 %type <ival> AssignOper
248
249 %nonassoc LOWER_THAN_ELSE
250 %nonassoc kELSE
251
252 %%
253
254 /* ECMA-262 3rd Edition    14 */
255 Program
256        : SourceElements HtmlComment
257                                 { program_parsed(ctx, $1); }
258
259 HtmlComment
260         : tHTMLCOMMENT          {}
261         | /* empty */           {}
262
263 /* ECMA-262 3rd Edition    14 */
264 SourceElements
265         : /* empty */           { $$ = new_source_elements(ctx); }
266         | SourceElements Statement
267                                 { $$ = source_elements_add_statement($1, $2); }
268
269 /* ECMA-262 3rd Edition    13 */
270 FunctionExpression
271         : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
272                                 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
273
274 KFunction
275         : kFUNCTION             { push_func(ctx); $$ = $1; }
276
277 /* ECMA-262 3rd Edition    13 */
278 FunctionBody
279         : SourceElements        { $$ = function_body_parsed(ctx, $1); }
280
281 /* ECMA-262 3rd Edition    13 */
282 FormalParameterList
283         : tIdentifier           { $$ = new_parameter_list(ctx, $1); }
284         | FormalParameterList ',' tIdentifier
285                                 { $$ = parameter_list_add(ctx, $1, $3); }
286
287 /* ECMA-262 3rd Edition    13 */
288 FormalParameterList_opt
289         : /* empty */           { $$ = NULL; }
290         | FormalParameterList   { $$ = $1; }
291
292 /* ECMA-262 3rd Edition    12 */
293 Statement
294         : Block                 { $$ = $1; }
295         | VariableStatement     { $$ = $1; }
296         | EmptyStatement        { $$ = $1; }
297         | FunctionExpression    { $$ = new_empty_statement(ctx); } /* FIXME: return NULL */
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         | BooleanLiteral        { $$ = $1; }
803         | tNumericLiteral       { $$ = $1; }
804         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
805         | '/'                   { $$ = parse_regexp(ctx);
806                                   if(!$$) YYABORT; }
807         | kDIVEQ                { $$ = parse_regexp(ctx);
808                                   if(!$$) YYABORT; }
809
810 /* ECMA-262 3rd Edition    7.8.2 */
811 BooleanLiteral
812         : kTRUE                 { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
813         | kFALSE                { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
814
815 semicolon_opt
816         : ';'
817         | error                 { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
818
819 left_bracket
820         : '('
821         | error                 { set_error(ctx, IDS_LBRACKET); YYABORT; }
822
823 right_bracket
824         : ')'
825         | error                 { set_error(ctx, IDS_RBRACKET); YYABORT; }
826
827 semicolon
828         : ';'
829         | error                 { set_error(ctx, IDS_SEMICOLON); YYABORT; }
830
831 %%
832
833 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
834 {
835     return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
836 }
837
838 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
839 {
840     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
841
842     ret->type = LT_STRING;
843     ret->u.wstr = str;
844
845     return ret;
846 }
847
848 static literal_t *new_null_literal(parser_ctx_t *ctx)
849 {
850     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
851
852     ret->type = LT_NULL;
853
854     return ret;
855 }
856
857 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
858 {
859     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
860
861     ret->type = LT_BOOL;
862     ret->u.bval = bval;
863
864     return ret;
865 }
866
867 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
868 {
869     prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
870
871     ret->name = name;
872     ret->value = value;
873     ret->next = NULL;
874
875     return ret;
876 }
877
878 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
879 {
880     property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
881
882     ret->head = ret->tail = new_prop_val(ctx, name, value);
883
884     return ret;
885 }
886
887 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
888 {
889     list->tail = list->tail->next = new_prop_val(ctx, name, value);
890
891     return list;
892 }
893
894 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
895 {
896     array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
897
898     ret->elision = elision;
899     ret->expr = expr;
900     ret->next = NULL;
901
902     return ret;
903 }
904
905 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
906 {
907     element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
908
909     ret->head = ret->tail = new_array_element(ctx, elision, expr);
910
911     return ret;
912 }
913
914 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
915 {
916     list->tail = list->tail->next = new_array_element(ctx, elision, expr);
917
918     return list;
919 }
920
921 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
922 {
923     argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
924
925     ret->expr = expr;
926     ret->next = NULL;
927
928     return ret;
929 }
930
931 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
932 {
933     argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
934
935     ret->head = ret->tail = new_argument(ctx, expr);
936
937     return ret;
938 }
939
940 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
941 {
942     list->tail = list->tail->next = new_argument(ctx, expr);
943
944     return list;
945 }
946
947 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
948 {
949     catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
950
951     ret->identifier = identifier;
952     ret->statement = statement;
953
954     return ret;
955 }
956
957 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
958 {
959     case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
960
961     ret->expr = expr;
962     ret->stat = stat_list ? stat_list->head : NULL;
963     ret->next = NULL;
964
965     return ret;
966 }
967
968 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
969 {
970     case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
971
972     ret->head = ret->tail = case_clausule;
973
974     return ret;
975 }
976
977 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
978 {
979     list->tail = list->tail->next = case_clausule;
980
981     return list;
982 }
983
984 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
985         case_clausule_t *default_clausule, case_list_t *case_list2)
986 {
987     case_clausule_t *ret = NULL, *iter = NULL, *iter2;
988     statement_t *stat = NULL;
989
990     if(case_list1) {
991         ret = case_list1->head;
992         iter = case_list1->tail;
993     }
994
995     if(default_clausule) {
996         if(ret)
997             iter = iter->next = default_clausule;
998         else
999             ret = iter = default_clausule;
1000     }
1001
1002     if(case_list2) {
1003         if(ret)
1004             iter->next = case_list2->head;
1005         else
1006             ret = case_list2->head;
1007     }
1008
1009     if(!ret)
1010         return NULL;
1011
1012     for(iter = ret; iter; iter = iter->next) {
1013         for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1014         if(!iter2)
1015             break;
1016
1017         while(iter != iter2) {
1018             iter->stat = iter2->stat;
1019             iter = iter->next;
1020         }
1021
1022         if(stat) {
1023             while(stat->next)
1024                 stat = stat->next;
1025             stat->next = iter->stat;
1026         }else {
1027             stat = iter->stat;
1028         }
1029     }
1030
1031     return ret;
1032 }
1033
1034 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1035 {
1036     block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1037
1038     ret->stat.eval = block_statement_eval;
1039     ret->stat.next = NULL;
1040     ret->stat_list = list ? list->head : NULL;
1041
1042     return &ret->stat;
1043 }
1044
1045 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1046 {
1047     variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1048     var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1049
1050     ret->identifier = identifier;
1051     ret->expr = expr;
1052     ret->next = NULL;
1053
1054     var_list->identifier = identifier;
1055     var_list->next = NULL;
1056
1057     if(ctx->func_stack->var_tail)
1058         ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1059     else
1060         ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1061
1062     return ret;
1063 }
1064
1065 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1066 {
1067     variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1068
1069     ret->head = ret->tail = decl;
1070
1071     return ret;
1072 }
1073
1074 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1075 {
1076     list->tail = list->tail->next = decl;
1077
1078     return list;
1079 }
1080
1081 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1082 {
1083     var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1084
1085     ret->stat.eval = var_statement_eval;
1086     ret->stat.next = NULL;
1087     ret->variable_list = variable_list->head;
1088
1089     return &ret->stat;
1090 }
1091
1092 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1093 {
1094     statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1095
1096     ret->eval = empty_statement_eval;
1097     ret->next = NULL;
1098
1099     return ret;
1100 }
1101
1102 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1103 {
1104     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1105
1106     ret->stat.eval = expression_statement_eval;
1107     ret->stat.next = NULL;
1108     ret->expr = expr;
1109
1110     return &ret->stat;
1111 }
1112
1113 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1114 {
1115     if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1116
1117     ret->stat.eval = if_statement_eval;
1118     ret->stat.next = NULL;
1119     ret->expr = expr;
1120     ret->if_stat = if_stat;
1121     ret->else_stat = else_stat;
1122
1123     return &ret->stat;
1124 }
1125
1126 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1127 {
1128     while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1129
1130     ret->stat.eval = while_statement_eval;
1131     ret->stat.next = NULL;
1132     ret->do_while = dowhile;
1133     ret->expr = expr;
1134     ret->statement = stat;
1135
1136     return &ret->stat;
1137 }
1138
1139 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1140         expression_t *expr, expression_t *end_expr, statement_t *statement)
1141 {
1142     for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1143
1144     ret->stat.eval = for_statement_eval;
1145     ret->stat.next = NULL;
1146     ret->variable_list = variable_list ? variable_list->head : NULL;
1147     ret->begin_expr = begin_expr;
1148     ret->expr = expr;
1149     ret->end_expr = end_expr;
1150     ret->statement = statement;
1151
1152     return &ret->stat;
1153 }
1154
1155 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1156         expression_t *in_expr, statement_t *statement)
1157 {
1158     forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1159
1160     ret->stat.eval = forin_statement_eval;
1161     ret->stat.next = NULL;
1162     ret->variable = variable;
1163     ret->expr = expr;
1164     ret->in_expr = in_expr;
1165     ret->statement = statement;
1166
1167     return &ret->stat;
1168 }
1169
1170 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1171 {
1172     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1173
1174     ret->stat.eval = continue_statement_eval;
1175     ret->stat.next = NULL;
1176     ret->identifier = identifier;
1177
1178     return &ret->stat;
1179 }
1180
1181 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1182 {
1183     branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1184
1185     ret->stat.eval = break_statement_eval;
1186     ret->stat.next = NULL;
1187     ret->identifier = identifier;
1188
1189     return &ret->stat;
1190 }
1191
1192 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1193 {
1194     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1195
1196     ret->stat.eval = return_statement_eval;
1197     ret->stat.next = NULL;
1198     ret->expr = expr;
1199
1200     return &ret->stat;
1201 }
1202
1203 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1204 {
1205     with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1206
1207     ret->stat.eval = with_statement_eval;
1208     ret->stat.next = NULL;
1209     ret->expr = expr;
1210     ret->statement = statement;
1211
1212     return &ret->stat;
1213 }
1214
1215 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1216 {
1217     labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1218
1219     ret->stat.eval = labelled_statement_eval;
1220     ret->stat.next = NULL;
1221     ret->identifier = identifier;
1222     ret->statement = statement;
1223
1224     return &ret->stat;
1225 }
1226
1227 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1228 {
1229     switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1230
1231     ret->stat.eval = switch_statement_eval;
1232     ret->stat.next = NULL;
1233     ret->expr = expr;
1234     ret->case_list = case_list;
1235
1236     return &ret->stat;
1237 }
1238
1239 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1240 {
1241     expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1242
1243     ret->stat.eval = throw_statement_eval;
1244     ret->stat.next = NULL;
1245     ret->expr = expr;
1246
1247     return &ret->stat;
1248 }
1249
1250 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1251        catch_block_t *catch_block, statement_t *finally_statement)
1252 {
1253     try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1254
1255     ret->stat.eval = try_statement_eval;
1256     ret->stat.next = NULL;
1257     ret->try_statement = try_statement;
1258     ret->catch_block = catch_block;
1259     ret->finally_statement = finally_statement;
1260
1261     return &ret->stat;
1262 }
1263
1264 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1265 {
1266     parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1267
1268     ret->identifier = identifier;
1269     ret->next = NULL;
1270
1271     return ret;
1272 }
1273
1274 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1275 {
1276     parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1277
1278     ret->head = ret->tail = new_parameter(ctx, identifier);
1279
1280     return ret;
1281 }
1282
1283 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1284 {
1285     list->tail = list->tail->next = new_parameter(ctx, identifier);
1286
1287     return list;
1288 }
1289
1290 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1291        parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1292 {
1293     function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1294
1295     ret->expr.eval = function_expression_eval;
1296     ret->identifier = identifier;
1297     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1298     ret->source_elements = source_elements;
1299     ret->src_str = src_str;
1300     ret->src_len = src_len;
1301
1302     if(ret->identifier) {
1303         function_declaration_t *decl = parser_alloc(ctx, sizeof(function_declaration_t));
1304
1305         decl->expr = ret;
1306         decl->next = NULL;
1307
1308         if(ctx->func_stack->func_tail)
1309             ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
1310         else
1311             ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1312     }
1313
1314     return &ret->expr;
1315 }
1316
1317 static const expression_eval_t expression_eval_table[] = {
1318    comma_expression_eval,
1319    logical_or_expression_eval,
1320    logical_and_expression_eval,
1321    binary_or_expression_eval,
1322    binary_xor_expression_eval,
1323    binary_and_expression_eval,
1324    instanceof_expression_eval,
1325    in_expression_eval,
1326    add_expression_eval,
1327    sub_expression_eval,
1328    mul_expression_eval,
1329    div_expression_eval,
1330    mod_expression_eval,
1331    delete_expression_eval,
1332    void_expression_eval,
1333    typeof_expression_eval,
1334    minus_expression_eval,
1335    plus_expression_eval,
1336    post_increment_expression_eval,
1337    post_decrement_expression_eval,
1338    pre_increment_expression_eval,
1339    pre_decrement_expression_eval,
1340    equal_expression_eval,
1341    equal2_expression_eval,
1342    not_equal_expression_eval,
1343    not_equal2_expression_eval,
1344    less_expression_eval,
1345    lesseq_expression_eval,
1346    greater_expression_eval,
1347    greatereq_expression_eval,
1348    binary_negation_expression_eval,
1349    logical_negation_expression_eval,
1350    left_shift_expression_eval,
1351    right_shift_expression_eval,
1352    right2_shift_expression_eval,
1353    assign_expression_eval,
1354    assign_lshift_expression_eval,
1355    assign_rshift_expression_eval,
1356    assign_rrshift_expression_eval,
1357    assign_add_expression_eval,
1358    assign_sub_expression_eval,
1359    assign_mul_expression_eval,
1360    assign_div_expression_eval,
1361    assign_mod_expression_eval,
1362    assign_and_expression_eval,
1363    assign_or_expression_eval,
1364    assign_xor_expression_eval,
1365 };
1366
1367 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1368        expression_t *expression1, expression_t *expression2)
1369 {
1370     binary_expression_t *ret = parser_alloc(ctx, sizeof(binary_expression_t));
1371
1372     ret->expr.eval = expression_eval_table[type];
1373     ret->expression1 = expression1;
1374     ret->expression2 = expression2;
1375
1376     return &ret->expr;
1377 }
1378
1379 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1380 {
1381     unary_expression_t *ret = parser_alloc(ctx, sizeof(unary_expression_t));
1382
1383     ret->expr.eval = expression_eval_table[type];
1384     ret->expression = expression;
1385
1386     return &ret->expr;
1387 }
1388
1389 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1390        expression_t *true_expression, expression_t *false_expression)
1391 {
1392     conditional_expression_t *ret = parser_alloc(ctx, sizeof(conditional_expression_t));
1393
1394     ret->expr.eval = conditional_expression_eval;
1395     ret->expression = expression;
1396     ret->true_expression = true_expression;
1397     ret->false_expression = false_expression;
1398
1399     return &ret->expr;
1400 }
1401
1402 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1403 {
1404     array_expression_t *ret = parser_alloc(ctx, sizeof(array_expression_t));
1405
1406     ret->expr.eval = array_expression_eval;
1407     ret->member_expr = member_expr;
1408     ret->expression = expression;
1409
1410     return &ret->expr;
1411 }
1412
1413 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1414 {
1415     member_expression_t *ret = parser_alloc(ctx, sizeof(member_expression_t));
1416
1417     ret->expr.eval = member_expression_eval;
1418     ret->expression = expression;
1419     ret->identifier = identifier;
1420
1421     return &ret->expr;
1422 }
1423
1424 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1425 {
1426     call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1427
1428     ret->expr.eval = new_expression_eval;
1429     ret->expression = expression;
1430     ret->argument_list = argument_list ? argument_list->head : NULL;
1431
1432     return &ret->expr;
1433 }
1434
1435 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1436 {
1437     call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1438
1439     ret->expr.eval = call_expression_eval;
1440     ret->expression = expression;
1441     ret->argument_list = argument_list ? argument_list->head : NULL;
1442
1443     return &ret->expr;
1444 }
1445
1446 static expression_t *new_this_expression(parser_ctx_t *ctx)
1447 {
1448     expression_t *ret = parser_alloc(ctx, sizeof(expression_t));
1449
1450     ret->eval = this_expression_eval;
1451
1452     return ret;
1453 }
1454
1455 static int parser_error(const char *str)
1456 {
1457     return 0;
1458 }
1459
1460 static void set_error(parser_ctx_t *ctx, UINT error)
1461 {
1462     ctx->hres = JSCRIPT_ERROR|error;
1463 }
1464
1465 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1466 {
1467     if(obj || *(ctx->ptr-1)==next) return TRUE;
1468
1469     set_error(ctx, IDS_SYNTAX_ERROR);
1470     return FALSE;
1471 }
1472
1473
1474 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1475 {
1476     identifier_expression_t *ret = parser_alloc(ctx, sizeof(identifier_expression_t));
1477
1478     ret->expr.eval = identifier_expression_eval;
1479     ret->identifier = identifier;
1480
1481     return &ret->expr;
1482 }
1483
1484 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1485 {
1486     array_literal_expression_t *ret = parser_alloc(ctx, sizeof(array_literal_expression_t));
1487
1488     ret->expr.eval = array_literal_expression_eval;
1489     ret->element_list = element_list ? element_list->head : NULL;
1490     ret->length = length;
1491
1492     return &ret->expr;
1493 }
1494
1495 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1496 {
1497     property_value_expression_t *ret = parser_alloc(ctx, sizeof(property_value_expression_t));
1498
1499     ret->expr.eval = property_value_expression_eval;
1500     ret->property_list = property_list ? property_list->head : NULL;
1501
1502     return &ret->expr;
1503 }
1504
1505 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1506 {
1507     literal_expression_t *ret = parser_alloc(ctx, sizeof(literal_expression_t));
1508
1509     ret->expr.eval = literal_expression_eval;
1510     ret->literal = literal;
1511
1512     return &ret->expr;
1513 }
1514
1515 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1516 {
1517     source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1518
1519     memset(ret, 0, sizeof(*ret));
1520
1521     return ret;
1522 }
1523
1524 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1525 {
1526     if(source_elements->statement_tail)
1527         source_elements->statement_tail = source_elements->statement_tail->next = statement;
1528     else
1529         source_elements->statement = source_elements->statement_tail = statement;
1530
1531     return source_elements;
1532 }
1533
1534 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1535 {
1536     statement_list_t *ret =  parser_alloc_tmp(ctx, sizeof(statement_list_t));
1537
1538     ret->head = ret->tail = statement;
1539
1540     return ret;
1541 }
1542
1543 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1544 {
1545     list->tail = list->tail->next = statement;
1546
1547     return list;
1548 }
1549
1550 static void push_func(parser_ctx_t *ctx)
1551 {
1552     func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1553
1554     new_func->func_head = new_func->func_tail = NULL;
1555     new_func->var_head = new_func->var_tail = NULL;
1556
1557     new_func->next = ctx->func_stack;
1558     ctx->func_stack = new_func;
1559 }
1560
1561 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1562 {
1563     source->functions = ctx->func_stack->func_head;
1564     source->variables = ctx->func_stack->var_head;
1565     pop_func(ctx);
1566
1567     return source;
1568 }
1569
1570 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1571 {
1572     source->functions = ctx->func_stack->func_head;
1573     source->variables = ctx->func_stack->var_head;
1574     pop_func(ctx);
1575
1576     ctx->source = source;
1577     if(!ctx->lexer_error)
1578         ctx->hres = S_OK;
1579 }
1580
1581 void parser_release(parser_ctx_t *ctx)
1582 {
1583     if(--ctx->ref)
1584         return;
1585
1586     script_release(ctx->script);
1587     heap_free(ctx->begin);
1588     jsheap_free(&ctx->heap);
1589     heap_free(ctx);
1590 }
1591
1592 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter,
1593         parser_ctx_t **ret)
1594 {
1595     parser_ctx_t *parser_ctx;
1596     jsheap_t *mark;
1597     HRESULT hres;
1598
1599     const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1600
1601     parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1602     if(!parser_ctx)
1603         return E_OUTOFMEMORY;
1604
1605     parser_ctx->ref = 1;
1606     parser_ctx->hres = JSCRIPT_ERROR|IDS_SYNTAX_ERROR;
1607     parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1608
1609     parser_ctx->begin = heap_strdupW(code);
1610     if(!parser_ctx->begin) {
1611         heap_free(parser_ctx);
1612         return E_OUTOFMEMORY;
1613     }
1614
1615     parser_ctx->ptr = parser_ctx->begin;
1616     parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1617
1618     script_addref(ctx);
1619     parser_ctx->script = ctx;
1620
1621     mark = jsheap_mark(&ctx->tmp_heap);
1622     jsheap_init(&parser_ctx->heap);
1623
1624     push_func(parser_ctx);
1625
1626     parser_parse(parser_ctx);
1627     jsheap_clear(mark);
1628     if(FAILED(parser_ctx->hres)) {
1629         hres = parser_ctx->hres;
1630         parser_release(parser_ctx);
1631         return hres;
1632     }
1633
1634     *ret = parser_ctx;
1635     return S_OK;
1636 }