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