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