msxml3/tests: Really test load() on files with leading spaces.
[wine/multimedia.git] / dlls / jscript / parser.y
blobd108ab0c35a07fd699aa0bf872013524cd8e2702
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
21 #include "jscript.h"
22 #include "engine.h"
24 #define YYLEX_PARAM ctx
25 #define YYPARSE_PARAM ctx
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*);
33 typedef struct _statement_list_t {
34 statement_t *head;
35 statement_t *tail;
36 } statement_list_t;
38 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
39 static literal_t *new_null_literal(parser_ctx_t*);
41 typedef struct _property_list_t {
42 prop_val_t *head;
43 prop_val_t *tail;
44 } property_list_t;
46 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
47 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
49 typedef struct _element_list_t {
50 array_element_t *head;
51 array_element_t *tail;
52 } element_list_t;
54 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
55 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
57 typedef struct _argument_list_t {
58 argument_t *head;
59 argument_t *tail;
60 } argument_list_t;
62 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
63 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
65 typedef struct _case_list_t {
66 case_clausule_t *head;
67 case_clausule_t *tail;
68 } case_list_t;
70 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
71 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
72 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
73 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
74 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
76 typedef struct _variable_list_t {
77 variable_declaration_t *head;
78 variable_declaration_t *tail;
79 } variable_list_t;
81 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
82 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
83 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
85 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
86 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
87 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
88 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
89 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
90 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
91 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
92 expression_t*,statement_t*);
93 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
94 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
95 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
96 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
97 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
98 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
99 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
100 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
101 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
103 struct statement_list_t {
104 statement_t *head;
105 statement_t *tail;
108 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
109 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
111 typedef struct _parameter_list_t {
112 parameter_t *head;
113 parameter_t *tail;
114 } parameter_list_t;
116 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
117 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
119 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
120 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
121 source_elements_t*,const WCHAR*,DWORD);
122 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
123 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
124 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
125 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
126 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
127 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
128 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
129 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
130 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
131 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
133 static source_elements_t *new_source_elements(parser_ctx_t*);
134 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
138 %pure_parser
139 %start Program
141 %union {
142 int ival;
143 const WCHAR *srcptr;
144 LPCWSTR wstr;
145 literal_t *literal;
146 struct _argument_list_t *argument_list;
147 case_clausule_t *case_clausule;
148 struct _case_list_t *case_list;
149 catch_block_t *catch_block;
150 struct _element_list_t *element_list;
151 expression_t *expr;
152 const WCHAR *identifier;
153 struct _parameter_list_t *parameter_list;
154 struct _property_list_t *property_list;
155 source_elements_t *source_elements;
156 statement_t *statement;
157 struct _statement_list_t *statement_list;
158 struct _variable_list_t *variable_list;
159 variable_declaration_t *variable_declaration;
162 /* keywords */
163 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
164 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
165 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
167 %token <srcptr> kFUNCTION '}'
169 /* tokens */
170 %token <identifier> tIdentifier
171 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
172 %token <literal> tNumericLiteral tBooleanLiteral
173 %token <wstr> tStringLiteral
174 %token tEOF
176 %type <source_elements> SourceElements
177 %type <source_elements> FunctionBody
178 %type <statement> Statement
179 %type <statement> Block
180 %type <statement> VariableStatement
181 %type <statement> EmptyStatement
182 %type <statement> ExpressionStatement
183 %type <statement> IfStatement
184 %type <statement> IterationStatement
185 %type <statement> ContinueStatement
186 %type <statement> BreakStatement
187 %type <statement> ReturnStatement
188 %type <statement> WithStatement
189 %type <statement> LabelledStatement
190 %type <statement> SwitchStatement
191 %type <statement> ThrowStatement
192 %type <statement> TryStatement
193 %type <statement> Finally
194 %type <statement_list> StatementList StatementList_opt
195 %type <parameter_list> FormalParameterList FormalParameterList_opt
196 %type <expr> Expression Expression_opt Expression_err
197 %type <expr> ExpressionNoIn ExpressionNoIn_opt
198 %type <expr> FunctionExpression
199 %type <expr> AssignmentExpression AssignmentExpressionNoIn
200 %type <expr> ConditionalExpression ConditionalExpressionNoIn
201 %type <expr> LeftHandSideExpression
202 %type <expr> LogicalORExpression LogicalORExpressionNoIn
203 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
204 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
205 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
206 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
207 %type <expr> EqualityExpression EqualityExpressionNoIn
208 %type <expr> RelationalExpression RelationalExpressionNoIn
209 %type <expr> ShiftExpression
210 %type <expr> AdditiveExpression
211 %type <expr> MultiplicativeExpression
212 %type <expr> Initialiser_opt Initialiser
213 %type <expr> InitialiserNoIn_opt InitialiserNoIn
214 %type <expr> UnaryExpression
215 %type <expr> PostfixExpression
216 %type <expr> NewExpression
217 %type <expr> CallExpression
218 %type <expr> MemberExpression
219 %type <expr> PrimaryExpression
220 %type <identifier> Identifier_opt
221 %type <variable_list> VariableDeclarationList
222 %type <variable_list> VariableDeclarationListNoIn
223 %type <variable_declaration> VariableDeclaration
224 %type <variable_declaration> VariableDeclarationNoIn
225 %type <case_list> CaseClausules CaseClausules_opt
226 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
227 %type <catch_block> Catch
228 %type <argument_list> Arguments
229 %type <argument_list> ArgumentList
230 %type <literal> Literal
231 %type <expr> ArrayLiteral
232 %type <expr> ObjectLiteral
233 %type <ival> Elision Elision_opt
234 %type <element_list> ElementList
235 %type <property_list> PropertyNameAndValueList
236 %type <literal> PropertyName
237 %type <literal> BooleanLiteral
238 %type <srcptr> KFunction
239 %type <ival> AssignOper
241 %nonassoc LOWER_THAN_ELSE
242 %nonassoc kELSE
246 /* ECMA-262 3rd Edition 14 */
247 Program
248 : SourceElements HtmlComment tEOF
249 { program_parsed(ctx, $1); }
251 HtmlComment
252 : tHTMLCOMMENT {}
253 | /* empty */ {}
255 /* ECMA-262 3rd Edition 14 */
256 SourceElements
257 : /* empty */ { $$ = new_source_elements(ctx); }
258 | SourceElements Statement
259 { $$ = source_elements_add_statement($1, $2); }
261 /* ECMA-262 3rd Edition 13 */
262 FunctionExpression
263 : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
264 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
266 KFunction
267 : kFUNCTION { $$ = $1; }
269 /* ECMA-262 3rd Edition 13 */
270 FunctionBody
271 : SourceElements { $$ = $1; }
273 /* ECMA-262 3rd Edition 13 */
274 FormalParameterList
275 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
276 | FormalParameterList ',' tIdentifier
277 { $$ = parameter_list_add(ctx, $1, $3); }
279 /* ECMA-262 3rd Edition 13 */
280 FormalParameterList_opt
281 : /* empty */ { $$ = NULL; }
282 | FormalParameterList { $$ = $1; }
284 /* ECMA-262 3rd Edition 12 */
285 Statement
286 : Block { $$ = $1; }
287 | VariableStatement { $$ = $1; }
288 | EmptyStatement { $$ = $1; }
289 | FunctionExpression { $$ = new_expression_statement(ctx, $1); }
290 | ExpressionStatement { $$ = $1; }
291 | IfStatement { $$ = $1; }
292 | IterationStatement { $$ = $1; }
293 | ContinueStatement { $$ = $1; }
294 | BreakStatement { $$ = $1; }
295 | ReturnStatement { $$ = $1; }
296 | WithStatement { $$ = $1; }
297 | LabelledStatement { $$ = $1; }
298 | SwitchStatement { $$ = $1; }
299 | ThrowStatement { $$ = $1; }
300 | TryStatement { $$ = $1; }
302 /* ECMA-262 3rd Edition 12.2 */
303 StatementList
304 : Statement { $$ = new_statement_list(ctx, $1); }
305 | StatementList Statement
306 { $$ = statement_list_add($1, $2); }
308 /* ECMA-262 3rd Edition 12.2 */
309 StatementList_opt
310 : /* empty */ { $$ = NULL; }
311 | StatementList { $$ = $1; }
313 /* ECMA-262 3rd Edition 12.1 */
314 Block
315 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
316 | '{' '}' { $$ = new_block_statement(ctx, NULL); }
318 /* ECMA-262 3rd Edition 12.2 */
319 VariableStatement
320 : kVAR VariableDeclarationList semicolon_opt
321 { $$ = new_var_statement(ctx, $2); }
323 /* ECMA-262 3rd Edition 12.2 */
324 VariableDeclarationList
325 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
326 | VariableDeclarationList ',' VariableDeclaration
327 { $$ = variable_list_add(ctx, $1, $3); }
329 /* ECMA-262 3rd Edition 12.2 */
330 VariableDeclarationListNoIn
331 : VariableDeclarationNoIn
332 { $$ = new_variable_list(ctx, $1); }
333 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
334 { $$ = variable_list_add(ctx, $1, $3); }
336 /* ECMA-262 3rd Edition 12.2 */
337 VariableDeclaration
338 : tIdentifier Initialiser_opt
339 { $$ = new_variable_declaration(ctx, $1, $2); }
341 /* ECMA-262 3rd Edition 12.2 */
342 VariableDeclarationNoIn
343 : tIdentifier InitialiserNoIn_opt
344 { $$ = new_variable_declaration(ctx, $1, $2); }
346 /* ECMA-262 3rd Edition 12.2 */
347 Initialiser_opt
348 : /* empty */ { $$ = NULL; }
349 | Initialiser { $$ = $1; }
351 /* ECMA-262 3rd Edition 12.2 */
352 Initialiser
353 : '=' AssignmentExpression
354 { $$ = $2; }
356 /* ECMA-262 3rd Edition 12.2 */
357 InitialiserNoIn_opt
358 : /* empty */ { $$ = NULL; }
359 | InitialiserNoIn { $$ = $1; }
361 /* ECMA-262 3rd Edition 12.2 */
362 InitialiserNoIn
363 : '=' AssignmentExpressionNoIn
364 { $$ = $2; }
366 /* ECMA-262 3rd Edition 12.3 */
367 EmptyStatement
368 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0); }
370 /* ECMA-262 3rd Edition 12.4 */
371 ExpressionStatement
372 : Expression semicolon_opt
373 { $$ = new_expression_statement(ctx, $1); }
375 /* ECMA-262 3rd Edition 12.5 */
376 IfStatement
377 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
378 { $$ = new_if_statement(ctx, $3, $5, $7); }
379 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
380 { $$ = new_if_statement(ctx, $3, $5, NULL); }
382 /* ECMA-262 3rd Edition 12.6 */
383 IterationStatement
384 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
385 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
386 | kWHILE left_bracket Expression_err right_bracket Statement
387 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
388 | kFOR left_bracket ExpressionNoIn_opt
389 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
390 semicolon Expression_opt
391 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
392 semicolon Expression_opt right_bracket Statement
393 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
394 | kFOR left_bracket kVAR VariableDeclarationListNoIn
395 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
396 semicolon Expression_opt
397 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
398 semicolon Expression_opt right_bracket Statement
399 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
400 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
401 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
402 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
403 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
405 /* ECMA-262 3rd Edition 12.7 */
406 ContinueStatement
407 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
408 { $$ = new_continue_statement(ctx, $2); }
410 /* ECMA-262 3rd Edition 12.8 */
411 BreakStatement
412 : kBREAK /* NONL */ Identifier_opt semicolon_opt
413 { $$ = new_break_statement(ctx, $2); }
415 /* ECMA-262 3rd Edition 12.9 */
416 ReturnStatement
417 : kRETURN /* NONL */ Expression_opt semicolon_opt
418 { $$ = new_return_statement(ctx, $2); }
420 /* ECMA-262 3rd Edition 12.10 */
421 WithStatement
422 : kWITH left_bracket Expression right_bracket Statement
423 { $$ = new_with_statement(ctx, $3, $5); }
425 /* ECMA-262 3rd Edition 12.12 */
426 LabelledStatement
427 : tIdentifier ':' Statement
428 { $$ = new_labelled_statement(ctx, $1, $3); }
430 /* ECMA-262 3rd Edition 12.11 */
431 SwitchStatement
432 : kSWITCH left_bracket Expression right_bracket CaseBlock
433 { $$ = new_switch_statement(ctx, $3, $5); }
435 /* ECMA-262 3rd Edition 12.11 */
436 CaseBlock
437 : '{' CaseClausules_opt '}'
438 { $$ = new_case_block(ctx, $2, NULL, NULL); }
439 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
440 { $$ = new_case_block(ctx, $2, $3, $4); }
442 /* ECMA-262 3rd Edition 12.11 */
443 CaseClausules_opt
444 : /* empty */ { $$ = NULL; }
445 | CaseClausules { $$ = $1; }
447 /* ECMA-262 3rd Edition 12.11 */
448 CaseClausules
449 : CaseClausule { $$ = new_case_list(ctx, $1); }
450 | CaseClausules CaseClausule
451 { $$ = case_list_add(ctx, $1, $2); }
453 /* ECMA-262 3rd Edition 12.11 */
454 CaseClausule
455 : kCASE Expression ':' StatementList_opt
456 { $$ = new_case_clausule(ctx, $2, $4); }
458 /* ECMA-262 3rd Edition 12.11 */
459 DefaultClausule
460 : kDEFAULT ':' StatementList_opt
461 { $$ = new_case_clausule(ctx, NULL, $3); }
463 /* ECMA-262 3rd Edition 12.13 */
464 ThrowStatement
465 : kTHROW /* NONL */ Expression semicolon_opt
466 { $$ = new_throw_statement(ctx, $2); }
468 /* ECMA-262 3rd Edition 12.14 */
469 TryStatement
470 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
471 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
472 | kTRY Block Catch Finally
473 { $$ = new_try_statement(ctx, $2, $3, $4); }
475 /* ECMA-262 3rd Edition 12.14 */
476 Catch
477 : kCATCH left_bracket tIdentifier right_bracket Block
478 { $$ = new_catch_block(ctx, $3, $5); }
480 /* ECMA-262 3rd Edition 12.14 */
481 Finally
482 : kFINALLY Block { $$ = $2; }
484 /* ECMA-262 3rd Edition 11.14 */
485 Expression_opt
486 : /* empty */ { $$ = NULL; }
487 | Expression { $$ = $1; }
489 Expression_err
490 : Expression { $$ = $1; }
491 | error { set_error(ctx, JS_E_SYNTAX); YYABORT; }
493 /* ECMA-262 3rd Edition 11.14 */
494 Expression
495 : AssignmentExpression { $$ = $1; }
496 | Expression ',' AssignmentExpression
497 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
499 /* ECMA-262 3rd Edition 11.14 */
500 ExpressionNoIn_opt
501 : /* empty */ { $$ = NULL; }
502 | ExpressionNoIn { $$ = $1; }
504 /* ECMA-262 3rd Edition 11.14 */
505 ExpressionNoIn
506 : AssignmentExpressionNoIn
507 { $$ = $1; }
508 | ExpressionNoIn ',' AssignmentExpressionNoIn
509 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
511 AssignOper
512 : tAssignOper { $$ = $1; }
513 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
515 /* ECMA-262 3rd Edition 11.13 */
516 AssignmentExpression
517 : ConditionalExpression { $$ = $1; }
518 | LeftHandSideExpression '=' AssignmentExpression
519 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
520 | LeftHandSideExpression AssignOper AssignmentExpression
521 { $$ = new_binary_expression(ctx, $2, $1, $3); }
523 /* ECMA-262 3rd Edition 11.13 */
524 AssignmentExpressionNoIn
525 : ConditionalExpressionNoIn
526 { $$ = $1; }
527 | LeftHandSideExpression '=' AssignmentExpressionNoIn
528 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
529 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
530 { $$ = new_binary_expression(ctx, $2, $1, $3); }
532 /* ECMA-262 3rd Edition 11.12 */
533 ConditionalExpression
534 : LogicalORExpression { $$ = $1; }
535 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
536 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
538 /* ECMA-262 3rd Edition 11.12 */
539 ConditionalExpressionNoIn
540 : LogicalORExpressionNoIn
541 { $$ = $1; }
542 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
543 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
545 /* ECMA-262 3rd Edition 11.11 */
546 LogicalORExpression
547 : LogicalANDExpression { $$ = $1; }
548 | LogicalORExpression tOROR LogicalANDExpression
549 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
551 /* ECMA-262 3rd Edition 11.11 */
552 LogicalORExpressionNoIn
553 : LogicalANDExpressionNoIn
554 { $$ = $1; }
555 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
556 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
558 /* ECMA-262 3rd Edition 11.11 */
559 LogicalANDExpression
560 : BitwiseORExpression { $$ = $1; }
561 | LogicalANDExpression tANDAND BitwiseORExpression
562 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
564 /* ECMA-262 3rd Edition 11.11 */
565 LogicalANDExpressionNoIn
566 : BitwiseORExpressionNoIn
567 { $$ = $1; }
568 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
569 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
571 /* ECMA-262 3rd Edition 11.10 */
572 BitwiseORExpression
573 : BitwiseXORExpression { $$ = $1; }
574 | BitwiseORExpression '|' BitwiseXORExpression
575 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
577 /* ECMA-262 3rd Edition 11.10 */
578 BitwiseORExpressionNoIn
579 : BitwiseXORExpressionNoIn
580 { $$ = $1; }
581 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
582 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
584 /* ECMA-262 3rd Edition 11.10 */
585 BitwiseXORExpression
586 : BitwiseANDExpression { $$ = $1; }
587 | BitwiseXORExpression '^' BitwiseANDExpression
588 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
590 /* ECMA-262 3rd Edition 11.10 */
591 BitwiseXORExpressionNoIn
592 : BitwiseANDExpressionNoIn
593 { $$ = $1; }
594 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
595 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
597 /* ECMA-262 3rd Edition 11.10 */
598 BitwiseANDExpression
599 : EqualityExpression { $$ = $1; }
600 | BitwiseANDExpression '&' EqualityExpression
601 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
603 /* ECMA-262 3rd Edition 11.10 */
604 BitwiseANDExpressionNoIn
605 : EqualityExpressionNoIn
606 { $$ = $1; }
607 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
608 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
610 /* ECMA-262 3rd Edition 11.9 */
611 EqualityExpression
612 : RelationalExpression { $$ = $1; }
613 | EqualityExpression tEqOper RelationalExpression
614 { $$ = new_binary_expression(ctx, $2, $1, $3); }
616 /* ECMA-262 3rd Edition 11.9 */
617 EqualityExpressionNoIn
618 : RelationalExpressionNoIn { $$ = $1; }
619 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
620 { $$ = new_binary_expression(ctx, $2, $1, $3); }
622 /* ECMA-262 3rd Edition 11.8 */
623 RelationalExpression
624 : ShiftExpression { $$ = $1; }
625 | RelationalExpression tRelOper ShiftExpression
626 { $$ = new_binary_expression(ctx, $2, $1, $3); }
627 | RelationalExpression kINSTANCEOF ShiftExpression
628 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
629 | RelationalExpression kIN ShiftExpression
630 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
632 /* ECMA-262 3rd Edition 11.8 */
633 RelationalExpressionNoIn
634 : ShiftExpression { $$ = $1; }
635 | RelationalExpressionNoIn tRelOper ShiftExpression
636 { $$ = new_binary_expression(ctx, $2, $1, $3); }
637 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
638 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
640 /* ECMA-262 3rd Edition 11.7 */
641 ShiftExpression
642 : AdditiveExpression { $$ = $1; }
643 | ShiftExpression tShiftOper AdditiveExpression
644 { $$ = new_binary_expression(ctx, $2, $1, $3); }
646 /* ECMA-262 3rd Edition 11.6 */
647 AdditiveExpression
648 : MultiplicativeExpression
649 { $$ = $1; }
650 | AdditiveExpression '+' MultiplicativeExpression
651 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
652 | AdditiveExpression '-' MultiplicativeExpression
653 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
655 /* ECMA-262 3rd Edition 11.5 */
656 MultiplicativeExpression
657 : UnaryExpression { $$ = $1; }
658 | MultiplicativeExpression '*' UnaryExpression
659 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
660 | MultiplicativeExpression '/' UnaryExpression
661 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
662 | MultiplicativeExpression '%' UnaryExpression
663 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
665 /* ECMA-262 3rd Edition 11.4 */
666 UnaryExpression
667 : PostfixExpression { $$ = $1; }
668 | kDELETE UnaryExpression
669 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
670 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
671 | kTYPEOF UnaryExpression
672 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
673 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
674 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
675 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
676 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
677 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
678 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
680 /* ECMA-262 3rd Edition 11.2 */
681 PostfixExpression
682 : LeftHandSideExpression
683 { $$ = $1; }
684 | LeftHandSideExpression /* NONL */ tINC
685 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
686 | LeftHandSideExpression /* NONL */ tDEC
687 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
690 /* ECMA-262 3rd Edition 11.2 */
691 LeftHandSideExpression
692 : NewExpression { $$ = $1; }
693 | CallExpression { $$ = $1; }
695 /* ECMA-262 3rd Edition 11.2 */
696 NewExpression
697 : MemberExpression { $$ = $1; }
698 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
700 /* ECMA-262 3rd Edition 11.2 */
701 MemberExpression
702 : PrimaryExpression { $$ = $1; }
703 | FunctionExpression { $$ = $1; }
704 | MemberExpression '[' Expression ']'
705 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
706 | MemberExpression '.' tIdentifier
707 { $$ = new_member_expression(ctx, $1, $3); }
708 | kNEW MemberExpression Arguments
709 { $$ = new_new_expression(ctx, $2, $3); }
711 /* ECMA-262 3rd Edition 11.2 */
712 CallExpression
713 : MemberExpression Arguments
714 { $$ = new_call_expression(ctx, $1, $2); }
715 | CallExpression Arguments
716 { $$ = new_call_expression(ctx, $1, $2); }
717 | CallExpression '[' Expression ']'
718 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
719 | CallExpression '.' tIdentifier
720 { $$ = new_member_expression(ctx, $1, $3); }
722 /* ECMA-262 3rd Edition 11.2 */
723 Arguments
724 : '(' ')' { $$ = NULL; }
725 | '(' ArgumentList ')' { $$ = $2; }
727 /* ECMA-262 3rd Edition 11.2 */
728 ArgumentList
729 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
730 | ArgumentList ',' AssignmentExpression
731 { $$ = argument_list_add(ctx, $1, $3); }
733 /* ECMA-262 3rd Edition 11.1 */
734 PrimaryExpression
735 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
736 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
737 | Literal { $$ = new_literal_expression(ctx, $1); }
738 | ArrayLiteral { $$ = $1; }
739 | ObjectLiteral { $$ = $1; }
740 | '(' Expression ')' { $$ = $2; }
742 /* ECMA-262 3rd Edition 11.1.4 */
743 ArrayLiteral
744 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
745 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
746 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
747 | '[' ElementList ',' Elision_opt ']'
748 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
750 /* ECMA-262 3rd Edition 11.1.4 */
751 ElementList
752 : Elision_opt AssignmentExpression
753 { $$ = new_element_list(ctx, $1, $2); }
754 | ElementList ',' Elision_opt AssignmentExpression
755 { $$ = element_list_add(ctx, $1, $3, $4); }
757 /* ECMA-262 3rd Edition 11.1.4 */
758 Elision
759 : ',' { $$ = 1; }
760 | Elision ',' { $$ = $1 + 1; }
762 /* ECMA-262 3rd Edition 11.1.4 */
763 Elision_opt
764 : /* empty */ { $$ = 0; }
765 | Elision { $$ = $1; }
767 /* ECMA-262 3rd Edition 11.1.5 */
768 ObjectLiteral
769 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
770 | '{' PropertyNameAndValueList '}'
771 { $$ = new_prop_and_value_expression(ctx, $2); }
773 /* ECMA-262 3rd Edition 11.1.5 */
774 PropertyNameAndValueList
775 : PropertyName ':' AssignmentExpression
776 { $$ = new_property_list(ctx, $1, $3); }
777 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
778 { $$ = property_list_add(ctx, $1, $3, $5); }
780 /* ECMA-262 3rd Edition 11.1.5 */
781 PropertyName
782 : tIdentifier { $$ = new_string_literal(ctx, $1); }
783 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
784 | tNumericLiteral { $$ = $1; }
786 /* ECMA-262 3rd Edition 7.6 */
787 Identifier_opt
788 : /* empty*/ { $$ = NULL; }
789 | tIdentifier { $$ = $1; }
791 /* ECMA-262 3rd Edition 7.8 */
792 Literal
793 : kNULL { $$ = new_null_literal(ctx); }
794 | BooleanLiteral { $$ = $1; }
795 | tNumericLiteral { $$ = $1; }
796 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
797 | '/' { $$ = parse_regexp(ctx);
798 if(!$$) YYABORT; }
799 | kDIVEQ { $$ = parse_regexp(ctx);
800 if(!$$) YYABORT; }
802 /* ECMA-262 3rd Edition 7.8.2 */
803 BooleanLiteral
804 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
805 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
806 | tBooleanLiteral { $$ = $1; }
808 semicolon_opt
809 : ';'
810 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
812 left_bracket
813 : '('
814 | error { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
816 right_bracket
817 : ')'
818 | error { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
820 semicolon
821 : ';'
822 | error { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; }
826 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
828 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
831 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
833 statement_t *stat;
835 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
836 if(!stat)
837 return NULL;
839 stat->type = type;
840 stat->next = NULL;
842 return stat;
845 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
847 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
849 ret->type = LT_STRING;
850 ret->u.wstr = str;
852 return ret;
855 static literal_t *new_null_literal(parser_ctx_t *ctx)
857 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
859 ret->type = LT_NULL;
861 return ret;
864 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
866 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
868 ret->name = name;
869 ret->value = value;
870 ret->next = NULL;
872 return ret;
875 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
877 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
879 ret->head = ret->tail = new_prop_val(ctx, name, value);
881 return ret;
884 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
886 list->tail = list->tail->next = new_prop_val(ctx, name, value);
888 return list;
891 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
893 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
895 ret->elision = elision;
896 ret->expr = expr;
897 ret->next = NULL;
899 return ret;
902 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
904 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
906 ret->head = ret->tail = new_array_element(ctx, elision, expr);
908 return ret;
911 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
913 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
915 return list;
918 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
920 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
922 ret->expr = expr;
923 ret->next = NULL;
925 return ret;
928 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
930 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
932 ret->head = ret->tail = new_argument(ctx, expr);
934 return ret;
937 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
939 list->tail = list->tail->next = new_argument(ctx, expr);
941 return list;
944 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
946 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
948 ret->identifier = identifier;
949 ret->statement = statement;
951 return ret;
954 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
956 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
958 ret->expr = expr;
959 ret->stat = stat_list ? stat_list->head : NULL;
960 ret->next = NULL;
962 return ret;
965 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
967 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
969 ret->head = ret->tail = case_clausule;
971 return ret;
974 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
976 list->tail = list->tail->next = case_clausule;
978 return list;
981 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
982 case_clausule_t *default_clausule, case_list_t *case_list2)
984 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
985 statement_t *stat = NULL;
987 if(case_list1) {
988 ret = case_list1->head;
989 iter = case_list1->tail;
992 if(default_clausule) {
993 if(ret)
994 iter = iter->next = default_clausule;
995 else
996 ret = iter = default_clausule;
999 if(case_list2) {
1000 if(ret)
1001 iter->next = case_list2->head;
1002 else
1003 ret = case_list2->head;
1006 if(!ret)
1007 return NULL;
1009 for(iter = ret; iter; iter = iter->next) {
1010 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1011 if(!iter2)
1012 break;
1014 while(iter != iter2) {
1015 iter->stat = iter2->stat;
1016 iter = iter->next;
1019 if(stat) {
1020 while(stat->next)
1021 stat = stat->next;
1022 stat->next = iter->stat;
1023 }else {
1024 stat = iter->stat;
1028 return ret;
1031 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1033 block_statement_t *ret;
1035 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret));
1036 if(!ret)
1037 return NULL;
1039 ret->stat_list = list ? list->head : NULL;
1041 return &ret->stat;
1044 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1046 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1048 ret->identifier = identifier;
1049 ret->expr = expr;
1050 ret->next = NULL;
1051 ret->global_next = NULL;
1053 return ret;
1056 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1058 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1060 ret->head = ret->tail = decl;
1062 return ret;
1065 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1067 list->tail = list->tail->next = decl;
1069 return list;
1072 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1074 var_statement_t *ret;
1076 ret = new_statement(ctx, STAT_VAR, sizeof(*ret));
1077 if(!ret)
1078 return NULL;
1080 ret->variable_list = variable_list->head;
1082 return &ret->stat;
1085 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1087 expression_statement_t *ret;
1089 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret));
1090 if(!ret)
1091 return NULL;
1093 ret->expr = expr;
1095 return &ret->stat;
1098 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1100 if_statement_t *ret;
1102 ret = new_statement(ctx, STAT_IF, sizeof(*ret));
1103 if(!ret)
1104 return NULL;
1106 ret->expr = expr;
1107 ret->if_stat = if_stat;
1108 ret->else_stat = else_stat;
1110 return &ret->stat;
1113 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1115 while_statement_t *ret;
1117 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret));
1118 if(!ret)
1119 return NULL;
1121 ret->do_while = dowhile;
1122 ret->expr = expr;
1123 ret->statement = stat;
1125 return &ret->stat;
1128 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1129 expression_t *expr, expression_t *end_expr, statement_t *statement)
1131 for_statement_t *ret;
1133 ret = new_statement(ctx, STAT_FOR, sizeof(*ret));
1134 if(!ret)
1135 return NULL;
1137 ret->variable_list = variable_list ? variable_list->head : NULL;
1138 ret->begin_expr = begin_expr;
1139 ret->expr = expr;
1140 ret->end_expr = end_expr;
1141 ret->statement = statement;
1143 return &ret->stat;
1146 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1147 expression_t *in_expr, statement_t *statement)
1149 forin_statement_t *ret;
1151 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret));
1152 if(!ret)
1153 return NULL;
1155 ret->variable = variable;
1156 ret->expr = expr;
1157 ret->in_expr = in_expr;
1158 ret->statement = statement;
1160 return &ret->stat;
1163 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1165 branch_statement_t *ret;
1167 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret));
1168 if(!ret)
1169 return NULL;
1171 ret->identifier = identifier;
1173 return &ret->stat;
1176 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1178 branch_statement_t *ret;
1180 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret));
1181 if(!ret)
1182 return NULL;
1184 ret->identifier = identifier;
1186 return &ret->stat;
1189 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1191 expression_statement_t *ret;
1193 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret));
1194 if(!ret)
1195 return NULL;
1197 ret->expr = expr;
1199 return &ret->stat;
1202 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1204 with_statement_t *ret;
1206 ret = new_statement(ctx, STAT_WITH, sizeof(*ret));
1207 if(!ret)
1208 return NULL;
1210 ret->expr = expr;
1211 ret->statement = statement;
1213 return &ret->stat;
1216 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1218 labelled_statement_t *ret;
1220 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret));
1221 if(!ret)
1222 return NULL;
1224 ret->identifier = identifier;
1225 ret->statement = statement;
1227 return &ret->stat;
1230 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1232 switch_statement_t *ret;
1234 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret));
1235 if(!ret)
1236 return NULL;
1238 ret->expr = expr;
1239 ret->case_list = case_list;
1241 return &ret->stat;
1244 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1246 expression_statement_t *ret;
1248 ret = new_statement(ctx, STAT_THROW, sizeof(*ret));
1249 if(!ret)
1250 return NULL;
1252 ret->expr = expr;
1254 return &ret->stat;
1257 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1258 catch_block_t *catch_block, statement_t *finally_statement)
1260 try_statement_t *ret;
1262 ret = new_statement(ctx, STAT_TRY, sizeof(*ret));
1263 if(!ret)
1264 return NULL;
1266 ret->try_statement = try_statement;
1267 ret->catch_block = catch_block;
1268 ret->finally_statement = finally_statement;
1270 return &ret->stat;
1273 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1275 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1277 ret->identifier = identifier;
1278 ret->next = NULL;
1280 return ret;
1283 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1285 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1287 ret->head = ret->tail = new_parameter(ctx, identifier);
1289 return ret;
1292 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1294 list->tail = list->tail->next = new_parameter(ctx, identifier);
1296 return list;
1299 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1300 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1302 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1304 ret->identifier = identifier;
1305 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1306 ret->source_elements = source_elements;
1307 ret->src_str = src_str;
1308 ret->src_len = src_len;
1309 ret->next = NULL;
1311 return &ret->expr;
1314 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1316 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1318 ret->type = type;
1320 return ret;
1323 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1324 expression_t *expression1, expression_t *expression2)
1326 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1328 ret->expression1 = expression1;
1329 ret->expression2 = expression2;
1331 return &ret->expr;
1334 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1336 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1338 ret->expression = expression;
1340 return &ret->expr;
1343 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1344 expression_t *true_expression, expression_t *false_expression)
1346 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1348 ret->expression = expression;
1349 ret->true_expression = true_expression;
1350 ret->false_expression = false_expression;
1352 return &ret->expr;
1355 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1357 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1359 ret->expression = expression;
1360 ret->identifier = identifier;
1362 return &ret->expr;
1365 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1367 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1369 ret->expression = expression;
1370 ret->argument_list = argument_list ? argument_list->head : NULL;
1372 return &ret->expr;
1375 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1377 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1379 ret->expression = expression;
1380 ret->argument_list = argument_list ? argument_list->head : NULL;
1382 return &ret->expr;
1385 static int parser_error(const char *str)
1387 return 0;
1390 static void set_error(parser_ctx_t *ctx, UINT error)
1392 ctx->hres = error;
1395 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1397 if(obj || *(ctx->ptr-1)==next) return TRUE;
1399 set_error(ctx, JS_E_SYNTAX);
1400 return FALSE;
1404 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1406 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1408 ret->identifier = identifier;
1410 return &ret->expr;
1413 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1415 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1417 ret->element_list = element_list ? element_list->head : NULL;
1418 ret->length = length;
1420 return &ret->expr;
1423 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1425 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1427 ret->property_list = property_list ? property_list->head : NULL;
1429 return &ret->expr;
1432 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1434 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1436 ret->literal = literal;
1438 return &ret->expr;
1441 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1443 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1445 memset(ret, 0, sizeof(*ret));
1447 return ret;
1450 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1452 if(source_elements->statement_tail)
1453 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1454 else
1455 source_elements->statement = source_elements->statement_tail = statement;
1457 return source_elements;
1460 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1462 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1464 ret->head = ret->tail = statement;
1466 return ret;
1469 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1471 list->tail = list->tail->next = statement;
1473 return list;
1476 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1478 ctx->source = source;
1479 if(!ctx->lexer_error)
1480 ctx->hres = S_OK;
1483 void parser_release(parser_ctx_t *ctx)
1485 script_release(ctx->script);
1486 jsheap_free(&ctx->heap);
1487 heap_free(ctx);
1490 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval,
1491 parser_ctx_t **ret)
1493 parser_ctx_t *parser_ctx;
1494 jsheap_t *mark;
1495 HRESULT hres;
1497 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1499 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1500 if(!parser_ctx)
1501 return E_OUTOFMEMORY;
1503 parser_ctx->hres = JS_E_SYNTAX;
1504 parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1506 parser_ctx->begin = parser_ctx->ptr = code;
1507 parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1509 script_addref(ctx);
1510 parser_ctx->script = ctx;
1512 mark = jsheap_mark(&ctx->tmp_heap);
1513 jsheap_init(&parser_ctx->heap);
1515 parser_parse(parser_ctx);
1516 jsheap_clear(mark);
1517 hres = parser_ctx->hres;
1518 if(FAILED(hres)) {
1519 parser_release(parser_ctx);
1520 return hres;
1523 *ret = parser_ctx;
1524 return S_OK;