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