jscript: Added JScriptEncode object tests.
[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
42 typedef struct _property_list_t {
43     prop_val_t *head;
44     prop_val_t *tail;
45 } property_list_t;
46
47 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
48 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
49
50 typedef struct _element_list_t {
51     array_element_t *head;
52     array_element_t *tail;
53 } element_list_t;
54
55 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
56 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
57
58 typedef struct _argument_list_t {
59     argument_t *head;
60     argument_t *tail;
61 } argument_list_t;
62
63 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
64 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
65
66 typedef struct _case_list_t {
67     case_clausule_t *head;
68     case_clausule_t *tail;
69 } case_list_t;
70
71 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
72 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
73 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
74 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
75 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
76
77 typedef struct _variable_list_t {
78     variable_declaration_t *head;
79     variable_declaration_t *tail;
80 } variable_list_t;
81
82 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
83 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
84 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
85
86 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
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_expression_statement(parser_ctx_t*,expression_t*);
90 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
91 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
92 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
93         expression_t*,statement_t*);
94 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
95 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
96 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
97 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
98 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
99 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
100 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
101 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
102 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
103
104 struct statement_list_t {
105    statement_t *head;
106    statement_t *tail;
107 };
108
109 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
110 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
111
112 typedef struct _parameter_list_t {
113     parameter_t *head;
114     parameter_t *tail;
115 } parameter_list_t;
116
117 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
118 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
119
120 static void push_func(parser_ctx_t*);
121 static inline void pop_func(parser_ctx_t *ctx)
122 {
123     ctx->func_stack = ctx->func_stack->next;
124 }
125
126 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
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_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
133 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
134 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
135 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
136 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
137 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
138 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
139
140 static source_elements_t *new_source_elements(parser_ctx_t*);
141 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
142
143 %}
144
145 %pure_parser
146 %start Program
147
148 %union {
149     int                     ival;
150     const WCHAR             *srcptr;
151     LPCWSTR                 wstr;
152     literal_t               *literal;
153     struct _argument_list_t *argument_list;
154     case_clausule_t         *case_clausule;
155     struct _case_list_t     *case_list;
156     catch_block_t           *catch_block;
157     struct _element_list_t  *element_list;
158     expression_t            *expr;
159     const WCHAR            *identifier;
160     struct _parameter_list_t *parameter_list;
161     struct _property_list_t *property_list;
162     source_elements_t       *source_elements;
163     statement_t             *statement;
164     struct _statement_list_t *statement_list;
165     struct _variable_list_t *variable_list;
166     variable_declaration_t  *variable_declaration;
167 }
168
169 /* keywords */
170 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
171 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
172 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
173
174 %token <srcptr> kFUNCTION '}'
175
176 /* tokens */
177 %token <identifier> tIdentifier
178 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
179 %token <literal> tNumericLiteral tBooleanLiteral
180 %token <wstr> tStringLiteral
181 %token tEOF
182
183 %type <source_elements> SourceElements
184 %type <source_elements> FunctionBody
185 %type <statement> Statement
186 %type <statement> Block
187 %type <statement> VariableStatement
188 %type <statement> EmptyStatement
189 %type <statement> ExpressionStatement
190 %type <statement> IfStatement
191 %type <statement> IterationStatement
192 %type <statement> ContinueStatement
193 %type <statement> BreakStatement
194 %type <statement> ReturnStatement
195 %type <statement> WithStatement
196 %type <statement> LabelledStatement
197 %type <statement> SwitchStatement
198 %type <statement> ThrowStatement
199 %type <statement> TryStatement
200 %type <statement> Finally
201 %type <statement_list> StatementList StatementList_opt
202 %type <parameter_list> FormalParameterList FormalParameterList_opt
203 %type <expr> Expression Expression_opt Expression_err
204 %type <expr> ExpressionNoIn ExpressionNoIn_opt
205 %type <expr> FunctionExpression
206 %type <expr> AssignmentExpression AssignmentExpressionNoIn
207 %type <expr> ConditionalExpression ConditionalExpressionNoIn
208 %type <expr> LeftHandSideExpression
209 %type <expr> LogicalORExpression LogicalORExpressionNoIn
210 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
211 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
212 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
213 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
214 %type <expr> EqualityExpression EqualityExpressionNoIn
215 %type <expr> RelationalExpression RelationalExpressionNoIn
216 %type <expr> ShiftExpression
217 %type <expr> AdditiveExpression
218 %type <expr> MultiplicativeExpression
219 %type <expr> Initialiser_opt Initialiser
220 %type <expr> InitialiserNoIn_opt InitialiserNoIn
221 %type <expr> UnaryExpression
222 %type <expr> PostfixExpression
223 %type <expr> NewExpression
224 %type <expr> CallExpression
225 %type <expr> MemberExpression
226 %type <expr> PrimaryExpression
227 %type <identifier> Identifier_opt
228 %type <variable_list> VariableDeclarationList
229 %type <variable_list> VariableDeclarationListNoIn
230 %type <variable_declaration> VariableDeclaration
231 %type <variable_declaration> VariableDeclarationNoIn
232 %type <case_list> CaseClausules CaseClausules_opt
233 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
234 %type <catch_block> Catch
235 %type <argument_list> Arguments
236 %type <argument_list> ArgumentList
237 %type <literal> Literal
238 %type <expr> ArrayLiteral
239 %type <expr> ObjectLiteral
240 %type <ival> Elision Elision_opt
241 %type <element_list> ElementList
242 %type <property_list> PropertyNameAndValueList
243 %type <literal> PropertyName
244 %type <literal> BooleanLiteral
245 %type <srcptr> KFunction
246 %type <ival> AssignOper
247
248 %nonassoc LOWER_THAN_ELSE
249 %nonassoc kELSE
250
251 %%
252
253 /* ECMA-262 3rd Edition    14 */
254 Program
255        : SourceElements HtmlComment tEOF
256                                 { program_parsed(ctx, $1); }
257
258 HtmlComment
259         : tHTMLCOMMENT          {}
260         | /* empty */           {}
261
262 /* ECMA-262 3rd Edition    14 */
263 SourceElements
264         : /* empty */           { $$ = new_source_elements(ctx); }
265         | SourceElements Statement
266                                 { $$ = source_elements_add_statement($1, $2); }
267
268 /* ECMA-262 3rd Edition    13 */
269 FunctionExpression
270         : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
271                                 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
272
273 KFunction
274         : kFUNCTION             { push_func(ctx); $$ = $1; }
275
276 /* ECMA-262 3rd Edition    13 */
277 FunctionBody
278         : SourceElements        { $$ = function_body_parsed(ctx, $1); }
279
280 /* ECMA-262 3rd Edition    13 */
281 FormalParameterList
282         : tIdentifier           { $$ = new_parameter_list(ctx, $1); }
283         | FormalParameterList ',' tIdentifier
284                                 { $$ = parameter_list_add(ctx, $1, $3); }
285
286 /* ECMA-262 3rd Edition    13 */
287 FormalParameterList_opt
288         : /* empty */           { $$ = NULL; }
289         | FormalParameterList   { $$ = $1; }
290
291 /* ECMA-262 3rd Edition    12 */
292 Statement
293         : Block                 { $$ = $1; }
294         | VariableStatement     { $$ = $1; }
295         | EmptyStatement        { $$ = $1; }
296         | FunctionExpression    { $$ = new_statement(ctx, STAT_EMPTY, 0); }
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_statement(ctx, STAT_EMPTY, 0); }
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, JS_E_SYNTAX); 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 AssignOper
519         : tAssignOper           { $$ = $1; }
520         | kDIVEQ                { $$ = EXPR_ASSIGNDIV; }
521
522 /* ECMA-262 3rd Edition    11.13 */
523 AssignmentExpression
524         : ConditionalExpression { $$ = $1; }
525         | LeftHandSideExpression '=' AssignmentExpression
526                                 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
527         | LeftHandSideExpression AssignOper AssignmentExpression
528                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
529
530 /* ECMA-262 3rd Edition    11.13 */
531 AssignmentExpressionNoIn
532         : ConditionalExpressionNoIn
533                                 { $$ = $1; }
534         | LeftHandSideExpression '=' AssignmentExpressionNoIn
535                                 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
536         | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
537                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
538
539 /* ECMA-262 3rd Edition    11.12 */
540 ConditionalExpression
541         : LogicalORExpression   { $$ = $1; }
542         | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
543                                 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
544
545 /* ECMA-262 3rd Edition    11.12 */
546 ConditionalExpressionNoIn
547         : LogicalORExpressionNoIn
548                                 { $$ = $1; }
549         | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
550                                 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
551
552 /* ECMA-262 3rd Edition    11.11 */
553 LogicalORExpression
554         : LogicalANDExpression  { $$ = $1; }
555         | LogicalORExpression tOROR LogicalANDExpression
556                                 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
557
558 /* ECMA-262 3rd Edition    11.11 */
559 LogicalORExpressionNoIn
560         : LogicalANDExpressionNoIn
561                                 { $$ = $1; }
562         | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
563                                 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
564
565 /* ECMA-262 3rd Edition    11.11 */
566 LogicalANDExpression
567         : BitwiseORExpression   { $$ = $1; }
568         | LogicalANDExpression tANDAND BitwiseORExpression
569                                 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
570
571 /* ECMA-262 3rd Edition    11.11 */
572 LogicalANDExpressionNoIn
573         : BitwiseORExpressionNoIn
574                                 { $$ = $1; }
575         | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
576                                 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
577
578 /* ECMA-262 3rd Edition    11.10 */
579 BitwiseORExpression
580         : BitwiseXORExpression   { $$ = $1; }
581         | BitwiseORExpression '|' BitwiseXORExpression
582                                 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
583
584 /* ECMA-262 3rd Edition    11.10 */
585 BitwiseORExpressionNoIn
586         : BitwiseXORExpressionNoIn
587                                 { $$ = $1; }
588         | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
589                                 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
590
591 /* ECMA-262 3rd Edition    11.10 */
592 BitwiseXORExpression
593         : BitwiseANDExpression  { $$ = $1; }
594         | BitwiseXORExpression '^' BitwiseANDExpression
595                                 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
596
597 /* ECMA-262 3rd Edition    11.10 */
598 BitwiseXORExpressionNoIn
599         : BitwiseANDExpressionNoIn
600                                 { $$ = $1; }
601         | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
602                                 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
603
604 /* ECMA-262 3rd Edition    11.10 */
605 BitwiseANDExpression
606         : EqualityExpression    { $$ = $1; }
607         | BitwiseANDExpression '&' EqualityExpression
608                                 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
609
610 /* ECMA-262 3rd Edition    11.10 */
611 BitwiseANDExpressionNoIn
612         : EqualityExpressionNoIn
613                                 { $$ = $1; }
614         | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
615                                 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
616
617 /* ECMA-262 3rd Edition    11.9 */
618 EqualityExpression
619         : RelationalExpression  { $$ = $1; }
620         | EqualityExpression tEqOper RelationalExpression
621                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
622
623 /* ECMA-262 3rd Edition    11.9 */
624 EqualityExpressionNoIn
625         : RelationalExpressionNoIn  { $$ = $1; }
626         | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
627                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
628
629 /* ECMA-262 3rd Edition    11.8 */
630 RelationalExpression
631         : ShiftExpression       { $$ = $1; }
632         | RelationalExpression tRelOper ShiftExpression
633                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
634         | RelationalExpression kINSTANCEOF ShiftExpression
635                                 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
636         | RelationalExpression kIN ShiftExpression
637                                 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
638
639 /* ECMA-262 3rd Edition    11.8 */
640 RelationalExpressionNoIn
641         : ShiftExpression       { $$ = $1; }
642         | RelationalExpressionNoIn tRelOper ShiftExpression
643                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
644         | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
645                                 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
646
647 /* ECMA-262 3rd Edition    11.7 */
648 ShiftExpression
649         : AdditiveExpression    { $$ = $1; }
650         | ShiftExpression tShiftOper AdditiveExpression
651                                 { $$ = new_binary_expression(ctx, $2, $1, $3); }
652
653 /* ECMA-262 3rd Edition    11.6 */
654 AdditiveExpression
655         : MultiplicativeExpression
656                                 { $$ = $1; }
657         | AdditiveExpression '+' MultiplicativeExpression
658                                 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
659         | AdditiveExpression '-' MultiplicativeExpression
660                                 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
661
662 /* ECMA-262 3rd Edition    11.5 */
663 MultiplicativeExpression
664         : UnaryExpression       { $$ = $1; }
665         | MultiplicativeExpression '*' UnaryExpression
666                                 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
667         | MultiplicativeExpression '/' UnaryExpression
668                                 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
669         | MultiplicativeExpression '%' UnaryExpression
670                                 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
671
672 /* ECMA-262 3rd Edition    11.4 */
673 UnaryExpression
674         : PostfixExpression     { $$ = $1; }
675         | kDELETE UnaryExpression
676                                 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
677         | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
678         | kTYPEOF UnaryExpression
679                                 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
680         | tINC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
681         | tDEC UnaryExpression  { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
682         | '+' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
683         | '-' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
684         | '~' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
685         | '!' UnaryExpression   { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
686
687 /* ECMA-262 3rd Edition    11.2 */
688 PostfixExpression
689         : LeftHandSideExpression
690                                 { $$ = $1; }
691         | LeftHandSideExpression /* NONL */ tINC
692                                 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
693         | LeftHandSideExpression /* NONL */ tDEC
694                                 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
695
696
697 /* ECMA-262 3rd Edition    11.2 */
698 LeftHandSideExpression
699         : NewExpression         { $$ = $1; }
700         | CallExpression        { $$ = $1; }
701
702 /* ECMA-262 3rd Edition    11.2 */
703 NewExpression
704         : MemberExpression      { $$ = $1; }
705         | kNEW NewExpression    { $$ = new_new_expression(ctx, $2, NULL); }
706
707 /* ECMA-262 3rd Edition    11.2 */
708 MemberExpression
709         : PrimaryExpression     { $$ = $1; }
710         | FunctionExpression    { $$ = $1; }
711         | MemberExpression '[' Expression ']'
712                                 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
713         | MemberExpression '.' tIdentifier
714                                 { $$ = new_member_expression(ctx, $1, $3); }
715         | kNEW MemberExpression Arguments
716                                 { $$ = new_new_expression(ctx, $2, $3); }
717
718 /* ECMA-262 3rd Edition    11.2 */
719 CallExpression
720         : MemberExpression Arguments
721                                 { $$ = new_call_expression(ctx, $1, $2); }
722         | CallExpression Arguments
723                                 { $$ = new_call_expression(ctx, $1, $2); }
724         | CallExpression '[' Expression ']'
725                                 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
726         | CallExpression '.' tIdentifier
727                                 { $$ = new_member_expression(ctx, $1, $3); }
728
729 /* ECMA-262 3rd Edition    11.2 */
730 Arguments
731         : '(' ')'               { $$ = NULL; }
732         | '(' ArgumentList ')'  { $$ = $2; }
733
734 /* ECMA-262 3rd Edition    11.2 */
735 ArgumentList
736         : AssignmentExpression  { $$ = new_argument_list(ctx, $1); }
737         | ArgumentList ',' AssignmentExpression
738                                 { $$ = argument_list_add(ctx, $1, $3); }
739
740 /* ECMA-262 3rd Edition    11.1 */
741 PrimaryExpression
742         : kTHIS                 { $$ = new_expression(ctx, EXPR_THIS, 0); }
743         | tIdentifier           { $$ = new_identifier_expression(ctx, $1); }
744         | Literal               { $$ = new_literal_expression(ctx, $1); }
745         | ArrayLiteral          { $$ = $1; }
746         | ObjectLiteral         { $$ = $1; }
747         | '(' Expression ')'    { $$ = $2; }
748
749 /* ECMA-262 3rd Edition    11.1.4 */
750 ArrayLiteral
751         : '[' ']'               { $$ = new_array_literal_expression(ctx, NULL, 0); }
752         | '[' Elision ']'       { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
753         | '[' ElementList ']'   { $$ = new_array_literal_expression(ctx, $2, 0); }
754         | '[' ElementList ',' Elision_opt ']'
755                                 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
756
757 /* ECMA-262 3rd Edition    11.1.4 */
758 ElementList
759         : Elision_opt AssignmentExpression
760                                 { $$ = new_element_list(ctx, $1, $2); }
761         | ElementList ',' Elision_opt AssignmentExpression
762                                 { $$ = element_list_add(ctx, $1, $3, $4); }
763
764 /* ECMA-262 3rd Edition    11.1.4 */
765 Elision
766         : ','                   { $$ = 1; }
767         | Elision ','           { $$ = $1 + 1; }
768
769 /* ECMA-262 3rd Edition    11.1.4 */
770 Elision_opt
771         : /* empty */           { $$ = 0; }
772         | Elision               { $$ = $1; }
773
774 /* ECMA-262 3rd Edition    11.1.5 */
775 ObjectLiteral
776         : '{' '}'               { $$ = new_prop_and_value_expression(ctx, NULL); }
777         | '{' PropertyNameAndValueList '}'
778                                 { $$ = new_prop_and_value_expression(ctx, $2); }
779
780 /* ECMA-262 3rd Edition    11.1.5 */
781 PropertyNameAndValueList
782         : PropertyName ':' AssignmentExpression
783                                 { $$ = new_property_list(ctx, $1, $3); }
784         | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
785                                 { $$ = property_list_add(ctx, $1, $3, $5); }
786
787 /* ECMA-262 3rd Edition    11.1.5 */
788 PropertyName
789         : tIdentifier           { $$ = new_string_literal(ctx, $1); }
790         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
791         | tNumericLiteral       { $$ = $1; }
792
793 /* ECMA-262 3rd Edition    7.6 */
794 Identifier_opt
795         : /* empty*/            { $$ = NULL; }
796         | tIdentifier           { $$ = $1; }
797
798 /* ECMA-262 3rd Edition    7.8 */
799 Literal
800         : kNULL                 { $$ = new_null_literal(ctx); }
801         | BooleanLiteral        { $$ = $1; }
802         | tNumericLiteral       { $$ = $1; }
803         | tStringLiteral        { $$ = new_string_literal(ctx, $1); }
804         | '/'                   { $$ = parse_regexp(ctx);
805                                   if(!$$) YYABORT; }
806         | kDIVEQ                { $$ = parse_regexp(ctx);
807                                   if(!$$) YYABORT; }
808
809 /* ECMA-262 3rd Edition    7.8.2 */
810 BooleanLiteral
811         : kTRUE                 { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
812         | kFALSE                { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
813         | tBooleanLiteral       { $$ = $1; }
814
815 semicolon_opt
816         : ';'
817         | error                 { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
818
819 left_bracket
820         : '('
821         | error                 { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
822
823 right_bracket
824         : ')'
825         | error                 { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
826
827 semicolon
828         : ';'
829         | error                 { set_error(ctx, JS_E_MISSING_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 void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
839 {
840     statement_t *stat;
841
842     stat = parser_alloc(ctx, size ? size : sizeof(*stat));
843     if(!stat)
844         return NULL;
845
846     stat->type = type;
847     stat->next = NULL;
848
849     return stat;
850 }
851
852 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
853 {
854     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
855
856     ret->type = LT_STRING;
857     ret->u.wstr = str;
858
859     return ret;
860 }
861
862 static literal_t *new_null_literal(parser_ctx_t *ctx)
863 {
864     literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
865
866     ret->type = LT_NULL;
867
868     return ret;
869 }
870
871 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
872 {
873     prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
874
875     ret->name = name;
876     ret->value = value;
877     ret->next = NULL;
878
879     return ret;
880 }
881
882 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
883 {
884     property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
885
886     ret->head = ret->tail = new_prop_val(ctx, name, value);
887
888     return ret;
889 }
890
891 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
892 {
893     list->tail = list->tail->next = new_prop_val(ctx, name, value);
894
895     return list;
896 }
897
898 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
899 {
900     array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
901
902     ret->elision = elision;
903     ret->expr = expr;
904     ret->next = NULL;
905
906     return ret;
907 }
908
909 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
910 {
911     element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
912
913     ret->head = ret->tail = new_array_element(ctx, elision, expr);
914
915     return ret;
916 }
917
918 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
919 {
920     list->tail = list->tail->next = new_array_element(ctx, elision, expr);
921
922     return list;
923 }
924
925 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
926 {
927     argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
928
929     ret->expr = expr;
930     ret->next = NULL;
931
932     return ret;
933 }
934
935 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
936 {
937     argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
938
939     ret->head = ret->tail = new_argument(ctx, expr);
940
941     return ret;
942 }
943
944 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
945 {
946     list->tail = list->tail->next = new_argument(ctx, expr);
947
948     return list;
949 }
950
951 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
952 {
953     catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
954
955     ret->identifier = identifier;
956     ret->statement = statement;
957
958     return ret;
959 }
960
961 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
962 {
963     case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
964
965     ret->expr = expr;
966     ret->stat = stat_list ? stat_list->head : NULL;
967     ret->next = NULL;
968
969     return ret;
970 }
971
972 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
973 {
974     case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
975
976     ret->head = ret->tail = case_clausule;
977
978     return ret;
979 }
980
981 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
982 {
983     list->tail = list->tail->next = case_clausule;
984
985     return list;
986 }
987
988 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
989         case_clausule_t *default_clausule, case_list_t *case_list2)
990 {
991     case_clausule_t *ret = NULL, *iter = NULL, *iter2;
992     statement_t *stat = NULL;
993
994     if(case_list1) {
995         ret = case_list1->head;
996         iter = case_list1->tail;
997     }
998
999     if(default_clausule) {
1000         if(ret)
1001             iter = iter->next = default_clausule;
1002         else
1003             ret = iter = default_clausule;
1004     }
1005
1006     if(case_list2) {
1007         if(ret)
1008             iter->next = case_list2->head;
1009         else
1010             ret = case_list2->head;
1011     }
1012
1013     if(!ret)
1014         return NULL;
1015
1016     for(iter = ret; iter; iter = iter->next) {
1017         for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1018         if(!iter2)
1019             break;
1020
1021         while(iter != iter2) {
1022             iter->stat = iter2->stat;
1023             iter = iter->next;
1024         }
1025
1026         if(stat) {
1027             while(stat->next)
1028                 stat = stat->next;
1029             stat->next = iter->stat;
1030         }else {
1031             stat = iter->stat;
1032         }
1033     }
1034
1035     return ret;
1036 }
1037
1038 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1039 {
1040     block_statement_t *ret;
1041
1042     ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret));
1043     if(!ret)
1044         return NULL;
1045
1046     ret->stat_list = list ? list->head : NULL;
1047
1048     return &ret->stat;
1049 }
1050
1051 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1052 {
1053     variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1054     var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1055
1056     ret->identifier = identifier;
1057     ret->expr = expr;
1058     ret->next = NULL;
1059
1060     var_list->identifier = identifier;
1061     var_list->next = NULL;
1062
1063     if(ctx->func_stack->var_tail)
1064         ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1065     else
1066         ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1067
1068     return ret;
1069 }
1070
1071 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1072 {
1073     variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1074
1075     ret->head = ret->tail = decl;
1076
1077     return ret;
1078 }
1079
1080 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1081 {
1082     list->tail = list->tail->next = decl;
1083
1084     return list;
1085 }
1086
1087 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1088 {
1089     var_statement_t *ret;
1090
1091     ret = new_statement(ctx, STAT_VAR, sizeof(*ret));
1092     if(!ret)
1093         return NULL;
1094
1095     ret->variable_list = variable_list->head;
1096
1097     return &ret->stat;
1098 }
1099
1100 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1101 {
1102     expression_statement_t *ret;
1103
1104     ret = new_statement(ctx, STAT_EXPR, sizeof(*ret));
1105     if(!ret)
1106         return NULL;
1107
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;
1116
1117     ret = new_statement(ctx, STAT_IF, sizeof(*ret));
1118     if(!ret)
1119         return NULL;
1120
1121     ret->expr = expr;
1122     ret->if_stat = if_stat;
1123     ret->else_stat = else_stat;
1124
1125     return &ret->stat;
1126 }
1127
1128 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1129 {
1130     while_statement_t *ret;
1131
1132     ret = new_statement(ctx, STAT_WHILE, sizeof(*ret));
1133     if(!ret)
1134         return NULL;
1135
1136     ret->do_while = dowhile;
1137     ret->expr = expr;
1138     ret->statement = stat;
1139
1140     return &ret->stat;
1141 }
1142
1143 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1144         expression_t *expr, expression_t *end_expr, statement_t *statement)
1145 {
1146     for_statement_t *ret;
1147
1148     ret = new_statement(ctx, STAT_FOR, sizeof(*ret));
1149     if(!ret)
1150         return NULL;
1151
1152     ret->variable_list = variable_list ? variable_list->head : NULL;
1153     ret->begin_expr = begin_expr;
1154     ret->expr = expr;
1155     ret->end_expr = end_expr;
1156     ret->statement = statement;
1157
1158     return &ret->stat;
1159 }
1160
1161 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1162         expression_t *in_expr, statement_t *statement)
1163 {
1164     forin_statement_t *ret;
1165
1166     ret = new_statement(ctx, STAT_FORIN, sizeof(*ret));
1167     if(!ret)
1168         return NULL;
1169
1170     ret->variable = variable;
1171     ret->expr = expr;
1172     ret->in_expr = in_expr;
1173     ret->statement = statement;
1174
1175     return &ret->stat;
1176 }
1177
1178 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1179 {
1180     branch_statement_t *ret;
1181
1182     ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret));
1183     if(!ret)
1184         return NULL;
1185
1186     ret->identifier = identifier;
1187
1188     return &ret->stat;
1189 }
1190
1191 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1192 {
1193     branch_statement_t *ret;
1194
1195     ret = new_statement(ctx, STAT_BREAK, sizeof(*ret));
1196     if(!ret)
1197         return NULL;
1198
1199     ret->identifier = identifier;
1200
1201     return &ret->stat;
1202 }
1203
1204 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1205 {
1206     expression_statement_t *ret;
1207
1208     ret = new_statement(ctx, STAT_RETURN, sizeof(*ret));
1209     if(!ret)
1210         return NULL;
1211
1212     ret->expr = expr;
1213
1214     return &ret->stat;
1215 }
1216
1217 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1218 {
1219     with_statement_t *ret;
1220
1221     ret = new_statement(ctx, STAT_WITH, sizeof(*ret));
1222     if(!ret)
1223         return NULL;
1224
1225     ret->expr = expr;
1226     ret->statement = statement;
1227
1228     return &ret->stat;
1229 }
1230
1231 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1232 {
1233     labelled_statement_t *ret;
1234
1235     ret = new_statement(ctx, STAT_LABEL, sizeof(*ret));
1236     if(!ret)
1237         return NULL;
1238
1239     ret->identifier = identifier;
1240     ret->statement = statement;
1241
1242     return &ret->stat;
1243 }
1244
1245 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1246 {
1247     switch_statement_t *ret;
1248
1249     ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret));
1250     if(!ret)
1251         return NULL;
1252
1253     ret->expr = expr;
1254     ret->case_list = case_list;
1255
1256     return &ret->stat;
1257 }
1258
1259 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1260 {
1261     expression_statement_t *ret;
1262
1263     ret = new_statement(ctx, STAT_THROW, sizeof(*ret));
1264     if(!ret)
1265         return NULL;
1266
1267     ret->expr = expr;
1268
1269     return &ret->stat;
1270 }
1271
1272 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1273        catch_block_t *catch_block, statement_t *finally_statement)
1274 {
1275     try_statement_t *ret;
1276
1277     ret = new_statement(ctx, STAT_TRY, sizeof(*ret));
1278     if(!ret)
1279         return NULL;
1280
1281     ret->try_statement = try_statement;
1282     ret->catch_block = catch_block;
1283     ret->finally_statement = finally_statement;
1284
1285     return &ret->stat;
1286 }
1287
1288 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1289 {
1290     parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1291
1292     ret->identifier = identifier;
1293     ret->next = NULL;
1294
1295     return ret;
1296 }
1297
1298 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1299 {
1300     parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1301
1302     ret->head = ret->tail = new_parameter(ctx, identifier);
1303
1304     return ret;
1305 }
1306
1307 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1308 {
1309     list->tail = list->tail->next = new_parameter(ctx, identifier);
1310
1311     return list;
1312 }
1313
1314 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1315        parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1316 {
1317     function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1318     function_declaration_t *decl;
1319
1320     ret->identifier = identifier;
1321     ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1322     ret->source_elements = source_elements;
1323     ret->src_str = src_str;
1324     ret->src_len = src_len;
1325
1326     decl = parser_alloc(ctx, sizeof(function_declaration_t));
1327
1328     decl->expr = ret;
1329     decl->next = NULL;
1330
1331     if(ctx->func_stack->func_tail)
1332         ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
1333     else
1334         ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1335
1336     return &ret->expr;
1337 }
1338
1339 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1340 {
1341     expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1342
1343     ret->type = type;
1344
1345     return ret;
1346 }
1347
1348 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1349        expression_t *expression1, expression_t *expression2)
1350 {
1351     binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1352
1353     ret->expression1 = expression1;
1354     ret->expression2 = expression2;
1355
1356     return &ret->expr;
1357 }
1358
1359 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1360 {
1361     unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1362
1363     ret->expression = expression;
1364
1365     return &ret->expr;
1366 }
1367
1368 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1369        expression_t *true_expression, expression_t *false_expression)
1370 {
1371     conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1372
1373     ret->expression = expression;
1374     ret->true_expression = true_expression;
1375     ret->false_expression = false_expression;
1376
1377     return &ret->expr;
1378 }
1379
1380 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1381 {
1382     member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1383
1384     ret->expression = expression;
1385     ret->identifier = identifier;
1386
1387     return &ret->expr;
1388 }
1389
1390 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1391 {
1392     call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1393
1394     ret->expression = expression;
1395     ret->argument_list = argument_list ? argument_list->head : NULL;
1396
1397     return &ret->expr;
1398 }
1399
1400 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1401 {
1402     call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1403
1404     ret->expression = expression;
1405     ret->argument_list = argument_list ? argument_list->head : NULL;
1406
1407     return &ret->expr;
1408 }
1409
1410 static int parser_error(const char *str)
1411 {
1412     return 0;
1413 }
1414
1415 static void set_error(parser_ctx_t *ctx, UINT error)
1416 {
1417     ctx->hres = error;
1418 }
1419
1420 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1421 {
1422     if(obj || *(ctx->ptr-1)==next) return TRUE;
1423
1424     set_error(ctx, JS_E_SYNTAX);
1425     return FALSE;
1426 }
1427
1428
1429 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1430 {
1431     identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1432
1433     ret->identifier = identifier;
1434
1435     return &ret->expr;
1436 }
1437
1438 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1439 {
1440     array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1441
1442     ret->element_list = element_list ? element_list->head : NULL;
1443     ret->length = length;
1444
1445     return &ret->expr;
1446 }
1447
1448 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1449 {
1450     property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1451
1452     ret->property_list = property_list ? property_list->head : NULL;
1453
1454     return &ret->expr;
1455 }
1456
1457 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1458 {
1459     literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1460
1461     ret->literal = literal;
1462
1463     return &ret->expr;
1464 }
1465
1466 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1467 {
1468     source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1469
1470     memset(ret, 0, sizeof(*ret));
1471     ret->instr_off = 0;
1472
1473     return ret;
1474 }
1475
1476 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1477 {
1478     if(source_elements->statement_tail)
1479         source_elements->statement_tail = source_elements->statement_tail->next = statement;
1480     else
1481         source_elements->statement = source_elements->statement_tail = statement;
1482
1483     return source_elements;
1484 }
1485
1486 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1487 {
1488     statement_list_t *ret =  parser_alloc_tmp(ctx, sizeof(statement_list_t));
1489
1490     ret->head = ret->tail = statement;
1491
1492     return ret;
1493 }
1494
1495 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1496 {
1497     list->tail = list->tail->next = statement;
1498
1499     return list;
1500 }
1501
1502 static void push_func(parser_ctx_t *ctx)
1503 {
1504     func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1505
1506     new_func->func_head = new_func->func_tail = NULL;
1507     new_func->var_head = new_func->var_tail = NULL;
1508
1509     new_func->next = ctx->func_stack;
1510     ctx->func_stack = new_func;
1511 }
1512
1513 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1514 {
1515     source->functions = ctx->func_stack->func_head;
1516     source->variables = ctx->func_stack->var_head;
1517     pop_func(ctx);
1518
1519     return source;
1520 }
1521
1522 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1523 {
1524     source->functions = ctx->func_stack->func_head;
1525     source->variables = ctx->func_stack->var_head;
1526     pop_func(ctx);
1527
1528     ctx->source = source;
1529     if(!ctx->lexer_error)
1530         ctx->hres = S_OK;
1531 }
1532
1533 void parser_release(parser_ctx_t *ctx)
1534 {
1535     script_release(ctx->script);
1536     jsheap_free(&ctx->heap);
1537     heap_free(ctx);
1538 }
1539
1540 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval,
1541         parser_ctx_t **ret)
1542 {
1543     parser_ctx_t *parser_ctx;
1544     jsheap_t *mark;
1545     HRESULT hres;
1546
1547     const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1548
1549     parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1550     if(!parser_ctx)
1551         return E_OUTOFMEMORY;
1552
1553     parser_ctx->hres = JS_E_SYNTAX;
1554     parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1555
1556     parser_ctx->begin = parser_ctx->ptr = code;
1557     parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1558
1559     script_addref(ctx);
1560     parser_ctx->script = ctx;
1561
1562     mark = jsheap_mark(&ctx->tmp_heap);
1563     jsheap_init(&parser_ctx->heap);
1564
1565     push_func(parser_ctx);
1566
1567     parser_parse(parser_ctx);
1568     jsheap_clear(mark);
1569     hres = parser_ctx->hres;
1570     if(FAILED(hres)) {
1571         parser_release(parser_ctx);
1572         return hres;
1573     }
1574
1575     *ret = parser_ctx;
1576     return S_OK;
1577 }