shlwapi: Avoid a not necessary intermediate variable.
[wine/multimedia.git] / dlls / jscript / parser.y
blob327366a7f8e31a10e426e286b42334a05c366c2e
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 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 static int parser_error(parser_ctx_t*,const char*);
29 static void set_error(parser_ctx_t*,UINT);
30 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
31 static BOOL allow_auto_semicolon(parser_ctx_t*);
32 static void program_parsed(parser_ctx_t*,source_elements_t*);
34 typedef struct _statement_list_t {
35 statement_t *head;
36 statement_t *tail;
37 } statement_list_t;
39 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
40 static literal_t *new_null_literal(parser_ctx_t*);
42 typedef struct _property_list_t {
43 prop_val_t *head;
44 prop_val_t *tail;
45 } property_list_t;
47 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
48 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
50 typedef struct _element_list_t {
51 array_element_t *head;
52 array_element_t *tail;
53 } element_list_t;
55 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
56 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
58 typedef struct _argument_list_t {
59 argument_t *head;
60 argument_t *tail;
61 } argument_list_t;
63 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
64 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
66 typedef struct _case_list_t {
67 case_clausule_t *head;
68 case_clausule_t *tail;
69 } case_list_t;
71 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
72 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
73 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
74 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
75 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
77 typedef struct _variable_list_t {
78 variable_declaration_t *head;
79 variable_declaration_t *tail;
80 } variable_list_t;
82 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
83 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
84 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
86 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
87 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
88 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
89 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
90 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
91 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
92 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
93 expression_t*,statement_t*);
94 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
95 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
96 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
97 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
98 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
99 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
100 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
101 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
102 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
104 struct statement_list_t {
105 statement_t *head;
106 statement_t *tail;
109 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
110 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
112 typedef struct _parameter_list_t {
113 parameter_t *head;
114 parameter_t *tail;
115 } parameter_list_t;
117 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
118 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
120 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
121 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
122 source_elements_t*,const WCHAR*,DWORD);
123 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
124 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
125 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
126 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
127 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
128 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
129 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
130 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
131 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
132 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
134 static source_elements_t *new_source_elements(parser_ctx_t*);
135 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
139 %lex-param { parser_ctx_t *ctx }
140 %parse-param { parser_ctx_t *ctx }
141 %pure-parser
142 %start Program
144 %union {
145 int ival;
146 const WCHAR *srcptr;
147 LPCWSTR wstr;
148 literal_t *literal;
149 struct _argument_list_t *argument_list;
150 case_clausule_t *case_clausule;
151 struct _case_list_t *case_list;
152 catch_block_t *catch_block;
153 struct _element_list_t *element_list;
154 expression_t *expr;
155 const WCHAR *identifier;
156 struct _parameter_list_t *parameter_list;
157 struct _property_list_t *property_list;
158 source_elements_t *source_elements;
159 statement_t *statement;
160 struct _statement_list_t *statement_list;
161 struct _variable_list_t *variable_list;
162 variable_declaration_t *variable_declaration;
165 /* keywords */
166 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
167 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
168 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
170 %token <srcptr> kFUNCTION '}'
172 /* tokens */
173 %token <identifier> tIdentifier
174 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
175 %token <literal> tNumericLiteral tBooleanLiteral
176 %token <wstr> tStringLiteral
177 %token tEOF
179 %type <source_elements> SourceElements
180 %type <source_elements> FunctionBody
181 %type <statement> Statement
182 %type <statement> Block
183 %type <statement> VariableStatement
184 %type <statement> EmptyStatement
185 %type <statement> ExpressionStatement
186 %type <statement> IfStatement
187 %type <statement> IterationStatement
188 %type <statement> ContinueStatement
189 %type <statement> BreakStatement
190 %type <statement> ReturnStatement
191 %type <statement> WithStatement
192 %type <statement> LabelledStatement
193 %type <statement> SwitchStatement
194 %type <statement> ThrowStatement
195 %type <statement> TryStatement
196 %type <statement> Finally
197 %type <statement_list> StatementList StatementList_opt
198 %type <parameter_list> FormalParameterList FormalParameterList_opt
199 %type <expr> Expression Expression_opt Expression_err
200 %type <expr> ExpressionNoIn ExpressionNoIn_opt
201 %type <expr> FunctionExpression
202 %type <expr> AssignmentExpression AssignmentExpressionNoIn
203 %type <expr> ConditionalExpression ConditionalExpressionNoIn
204 %type <expr> LeftHandSideExpression
205 %type <expr> LogicalORExpression LogicalORExpressionNoIn
206 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
207 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
208 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
209 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
210 %type <expr> EqualityExpression EqualityExpressionNoIn
211 %type <expr> RelationalExpression RelationalExpressionNoIn
212 %type <expr> ShiftExpression
213 %type <expr> AdditiveExpression
214 %type <expr> MultiplicativeExpression
215 %type <expr> Initialiser_opt Initialiser
216 %type <expr> InitialiserNoIn_opt InitialiserNoIn
217 %type <expr> UnaryExpression
218 %type <expr> PostfixExpression
219 %type <expr> NewExpression
220 %type <expr> CallExpression
221 %type <expr> MemberExpression
222 %type <expr> PrimaryExpression
223 %type <identifier> Identifier_opt
224 %type <variable_list> VariableDeclarationList
225 %type <variable_list> VariableDeclarationListNoIn
226 %type <variable_declaration> VariableDeclaration
227 %type <variable_declaration> VariableDeclarationNoIn
228 %type <case_list> CaseClausules CaseClausules_opt
229 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
230 %type <catch_block> Catch
231 %type <argument_list> Arguments
232 %type <argument_list> ArgumentList
233 %type <literal> Literal
234 %type <expr> ArrayLiteral
235 %type <expr> ObjectLiteral
236 %type <ival> Elision Elision_opt
237 %type <element_list> ElementList
238 %type <property_list> PropertyNameAndValueList
239 %type <literal> PropertyName
240 %type <literal> BooleanLiteral
241 %type <srcptr> KFunction
242 %type <ival> AssignOper
244 %nonassoc LOWER_THAN_ELSE
245 %nonassoc kELSE
249 /* ECMA-262 3rd Edition 14 */
250 Program
251 : SourceElements HtmlComment tEOF
252 { program_parsed(ctx, $1); }
254 HtmlComment
255 : tHTMLCOMMENT {}
256 | /* empty */ {}
258 /* ECMA-262 3rd Edition 14 */
259 SourceElements
260 : /* empty */ { $$ = new_source_elements(ctx); }
261 | SourceElements Statement
262 { $$ = source_elements_add_statement($1, $2); }
264 /* ECMA-262 3rd Edition 13 */
265 FunctionExpression
266 : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
267 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
269 KFunction
270 : kFUNCTION { $$ = $1; }
272 /* ECMA-262 3rd Edition 13 */
273 FunctionBody
274 : SourceElements { $$ = $1; }
276 /* ECMA-262 3rd Edition 13 */
277 FormalParameterList
278 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
279 | FormalParameterList ',' tIdentifier
280 { $$ = parameter_list_add(ctx, $1, $3); }
282 /* ECMA-262 3rd Edition 13 */
283 FormalParameterList_opt
284 : /* empty */ { $$ = NULL; }
285 | FormalParameterList { $$ = $1; }
287 /* ECMA-262 3rd Edition 12 */
288 Statement
289 : Block { $$ = $1; }
290 | VariableStatement { $$ = $1; }
291 | EmptyStatement { $$ = $1; }
292 | FunctionExpression { $$ = new_expression_statement(ctx, $1); }
293 | ExpressionStatement { $$ = $1; }
294 | IfStatement { $$ = $1; }
295 | IterationStatement { $$ = $1; }
296 | ContinueStatement { $$ = $1; }
297 | BreakStatement { $$ = $1; }
298 | ReturnStatement { $$ = $1; }
299 | WithStatement { $$ = $1; }
300 | LabelledStatement { $$ = $1; }
301 | SwitchStatement { $$ = $1; }
302 | ThrowStatement { $$ = $1; }
303 | TryStatement { $$ = $1; }
305 /* ECMA-262 3rd Edition 12.2 */
306 StatementList
307 : Statement { $$ = new_statement_list(ctx, $1); }
308 | StatementList Statement
309 { $$ = statement_list_add($1, $2); }
311 /* ECMA-262 3rd Edition 12.2 */
312 StatementList_opt
313 : /* empty */ { $$ = NULL; }
314 | StatementList { $$ = $1; }
316 /* ECMA-262 3rd Edition 12.1 */
317 Block
318 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
319 | '{' '}' { $$ = new_block_statement(ctx, NULL); }
321 /* ECMA-262 3rd Edition 12.2 */
322 VariableStatement
323 : kVAR VariableDeclarationList semicolon_opt
324 { $$ = new_var_statement(ctx, $2); }
326 /* ECMA-262 3rd Edition 12.2 */
327 VariableDeclarationList
328 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
329 | VariableDeclarationList ',' VariableDeclaration
330 { $$ = variable_list_add(ctx, $1, $3); }
332 /* ECMA-262 3rd Edition 12.2 */
333 VariableDeclarationListNoIn
334 : VariableDeclarationNoIn
335 { $$ = new_variable_list(ctx, $1); }
336 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
337 { $$ = variable_list_add(ctx, $1, $3); }
339 /* ECMA-262 3rd Edition 12.2 */
340 VariableDeclaration
341 : tIdentifier Initialiser_opt
342 { $$ = new_variable_declaration(ctx, $1, $2); }
344 /* ECMA-262 3rd Edition 12.2 */
345 VariableDeclarationNoIn
346 : tIdentifier InitialiserNoIn_opt
347 { $$ = new_variable_declaration(ctx, $1, $2); }
349 /* ECMA-262 3rd Edition 12.2 */
350 Initialiser_opt
351 : /* empty */ { $$ = NULL; }
352 | Initialiser { $$ = $1; }
354 /* ECMA-262 3rd Edition 12.2 */
355 Initialiser
356 : '=' AssignmentExpression
357 { $$ = $2; }
359 /* ECMA-262 3rd Edition 12.2 */
360 InitialiserNoIn_opt
361 : /* empty */ { $$ = NULL; }
362 | InitialiserNoIn { $$ = $1; }
364 /* ECMA-262 3rd Edition 12.2 */
365 InitialiserNoIn
366 : '=' AssignmentExpressionNoIn
367 { $$ = $2; }
369 /* ECMA-262 3rd Edition 12.3 */
370 EmptyStatement
371 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0); }
373 /* ECMA-262 3rd Edition 12.4 */
374 ExpressionStatement
375 : Expression semicolon_opt
376 { $$ = new_expression_statement(ctx, $1); }
378 /* ECMA-262 3rd Edition 12.5 */
379 IfStatement
380 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
381 { $$ = new_if_statement(ctx, $3, $5, $7); }
382 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
383 { $$ = new_if_statement(ctx, $3, $5, NULL); }
385 /* ECMA-262 3rd Edition 12.6 */
386 IterationStatement
387 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
388 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
389 | kWHILE left_bracket Expression_err right_bracket Statement
390 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
391 | kFOR left_bracket ExpressionNoIn_opt
392 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
393 semicolon Expression_opt
394 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
395 semicolon Expression_opt right_bracket Statement
396 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
397 | kFOR left_bracket kVAR VariableDeclarationListNoIn
398 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
399 semicolon Expression_opt
400 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
401 semicolon Expression_opt right_bracket Statement
402 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
403 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
404 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
405 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
406 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
408 /* ECMA-262 3rd Edition 12.7 */
409 ContinueStatement
410 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
411 { $$ = new_continue_statement(ctx, $2); }
413 /* ECMA-262 3rd Edition 12.8 */
414 BreakStatement
415 : kBREAK /* NONL */ Identifier_opt semicolon_opt
416 { $$ = new_break_statement(ctx, $2); }
418 /* ECMA-262 3rd Edition 12.9 */
419 ReturnStatement
420 : kRETURN /* NONL */ Expression_opt semicolon_opt
421 { $$ = new_return_statement(ctx, $2); }
423 /* ECMA-262 3rd Edition 12.10 */
424 WithStatement
425 : kWITH left_bracket Expression right_bracket Statement
426 { $$ = new_with_statement(ctx, $3, $5); }
428 /* ECMA-262 3rd Edition 12.12 */
429 LabelledStatement
430 : tIdentifier ':' Statement
431 { $$ = new_labelled_statement(ctx, $1, $3); }
433 /* ECMA-262 3rd Edition 12.11 */
434 SwitchStatement
435 : kSWITCH left_bracket Expression right_bracket CaseBlock
436 { $$ = new_switch_statement(ctx, $3, $5); }
438 /* ECMA-262 3rd Edition 12.11 */
439 CaseBlock
440 : '{' CaseClausules_opt '}'
441 { $$ = new_case_block(ctx, $2, NULL, NULL); }
442 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
443 { $$ = new_case_block(ctx, $2, $3, $4); }
445 /* ECMA-262 3rd Edition 12.11 */
446 CaseClausules_opt
447 : /* empty */ { $$ = NULL; }
448 | CaseClausules { $$ = $1; }
450 /* ECMA-262 3rd Edition 12.11 */
451 CaseClausules
452 : CaseClausule { $$ = new_case_list(ctx, $1); }
453 | CaseClausules CaseClausule
454 { $$ = case_list_add(ctx, $1, $2); }
456 /* ECMA-262 3rd Edition 12.11 */
457 CaseClausule
458 : kCASE Expression ':' StatementList_opt
459 { $$ = new_case_clausule(ctx, $2, $4); }
461 /* ECMA-262 3rd Edition 12.11 */
462 DefaultClausule
463 : kDEFAULT ':' StatementList_opt
464 { $$ = new_case_clausule(ctx, NULL, $3); }
466 /* ECMA-262 3rd Edition 12.13 */
467 ThrowStatement
468 : kTHROW /* NONL */ Expression semicolon_opt
469 { $$ = new_throw_statement(ctx, $2); }
471 /* ECMA-262 3rd Edition 12.14 */
472 TryStatement
473 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
474 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
475 | kTRY Block Catch Finally
476 { $$ = new_try_statement(ctx, $2, $3, $4); }
478 /* ECMA-262 3rd Edition 12.14 */
479 Catch
480 : kCATCH left_bracket tIdentifier right_bracket Block
481 { $$ = new_catch_block(ctx, $3, $5); }
483 /* ECMA-262 3rd Edition 12.14 */
484 Finally
485 : kFINALLY Block { $$ = $2; }
487 /* ECMA-262 3rd Edition 11.14 */
488 Expression_opt
489 : /* empty */ { $$ = NULL; }
490 | Expression { $$ = $1; }
492 Expression_err
493 : Expression { $$ = $1; }
494 | error { set_error(ctx, JS_E_SYNTAX); YYABORT; }
496 /* ECMA-262 3rd Edition 11.14 */
497 Expression
498 : AssignmentExpression { $$ = $1; }
499 | Expression ',' AssignmentExpression
500 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
502 /* ECMA-262 3rd Edition 11.14 */
503 ExpressionNoIn_opt
504 : /* empty */ { $$ = NULL; }
505 | ExpressionNoIn { $$ = $1; }
507 /* ECMA-262 3rd Edition 11.14 */
508 ExpressionNoIn
509 : AssignmentExpressionNoIn
510 { $$ = $1; }
511 | ExpressionNoIn ',' AssignmentExpressionNoIn
512 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
514 AssignOper
515 : tAssignOper { $$ = $1; }
516 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
518 /* ECMA-262 3rd Edition 11.13 */
519 AssignmentExpression
520 : ConditionalExpression { $$ = $1; }
521 | LeftHandSideExpression '=' AssignmentExpression
522 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
523 | LeftHandSideExpression AssignOper AssignmentExpression
524 { $$ = new_binary_expression(ctx, $2, $1, $3); }
526 /* ECMA-262 3rd Edition 11.13 */
527 AssignmentExpressionNoIn
528 : ConditionalExpressionNoIn
529 { $$ = $1; }
530 | LeftHandSideExpression '=' AssignmentExpressionNoIn
531 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
532 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
533 { $$ = new_binary_expression(ctx, $2, $1, $3); }
535 /* ECMA-262 3rd Edition 11.12 */
536 ConditionalExpression
537 : LogicalORExpression { $$ = $1; }
538 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
539 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
541 /* ECMA-262 3rd Edition 11.12 */
542 ConditionalExpressionNoIn
543 : LogicalORExpressionNoIn
544 { $$ = $1; }
545 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
546 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
548 /* ECMA-262 3rd Edition 11.11 */
549 LogicalORExpression
550 : LogicalANDExpression { $$ = $1; }
551 | LogicalORExpression tOROR LogicalANDExpression
552 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
554 /* ECMA-262 3rd Edition 11.11 */
555 LogicalORExpressionNoIn
556 : LogicalANDExpressionNoIn
557 { $$ = $1; }
558 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
559 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
561 /* ECMA-262 3rd Edition 11.11 */
562 LogicalANDExpression
563 : BitwiseORExpression { $$ = $1; }
564 | LogicalANDExpression tANDAND BitwiseORExpression
565 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
567 /* ECMA-262 3rd Edition 11.11 */
568 LogicalANDExpressionNoIn
569 : BitwiseORExpressionNoIn
570 { $$ = $1; }
571 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
572 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
574 /* ECMA-262 3rd Edition 11.10 */
575 BitwiseORExpression
576 : BitwiseXORExpression { $$ = $1; }
577 | BitwiseORExpression '|' BitwiseXORExpression
578 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
580 /* ECMA-262 3rd Edition 11.10 */
581 BitwiseORExpressionNoIn
582 : BitwiseXORExpressionNoIn
583 { $$ = $1; }
584 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
585 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
587 /* ECMA-262 3rd Edition 11.10 */
588 BitwiseXORExpression
589 : BitwiseANDExpression { $$ = $1; }
590 | BitwiseXORExpression '^' BitwiseANDExpression
591 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
593 /* ECMA-262 3rd Edition 11.10 */
594 BitwiseXORExpressionNoIn
595 : BitwiseANDExpressionNoIn
596 { $$ = $1; }
597 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
598 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
600 /* ECMA-262 3rd Edition 11.10 */
601 BitwiseANDExpression
602 : EqualityExpression { $$ = $1; }
603 | BitwiseANDExpression '&' EqualityExpression
604 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
606 /* ECMA-262 3rd Edition 11.10 */
607 BitwiseANDExpressionNoIn
608 : EqualityExpressionNoIn
609 { $$ = $1; }
610 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
611 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
613 /* ECMA-262 3rd Edition 11.9 */
614 EqualityExpression
615 : RelationalExpression { $$ = $1; }
616 | EqualityExpression tEqOper RelationalExpression
617 { $$ = new_binary_expression(ctx, $2, $1, $3); }
619 /* ECMA-262 3rd Edition 11.9 */
620 EqualityExpressionNoIn
621 : RelationalExpressionNoIn { $$ = $1; }
622 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
623 { $$ = new_binary_expression(ctx, $2, $1, $3); }
625 /* ECMA-262 3rd Edition 11.8 */
626 RelationalExpression
627 : ShiftExpression { $$ = $1; }
628 | RelationalExpression tRelOper ShiftExpression
629 { $$ = new_binary_expression(ctx, $2, $1, $3); }
630 | RelationalExpression kINSTANCEOF ShiftExpression
631 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
632 | RelationalExpression kIN ShiftExpression
633 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
635 /* ECMA-262 3rd Edition 11.8 */
636 RelationalExpressionNoIn
637 : ShiftExpression { $$ = $1; }
638 | RelationalExpressionNoIn tRelOper ShiftExpression
639 { $$ = new_binary_expression(ctx, $2, $1, $3); }
640 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
641 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
643 /* ECMA-262 3rd Edition 11.7 */
644 ShiftExpression
645 : AdditiveExpression { $$ = $1; }
646 | ShiftExpression tShiftOper AdditiveExpression
647 { $$ = new_binary_expression(ctx, $2, $1, $3); }
649 /* ECMA-262 3rd Edition 11.6 */
650 AdditiveExpression
651 : MultiplicativeExpression
652 { $$ = $1; }
653 | AdditiveExpression '+' MultiplicativeExpression
654 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
655 | AdditiveExpression '-' MultiplicativeExpression
656 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
658 /* ECMA-262 3rd Edition 11.5 */
659 MultiplicativeExpression
660 : UnaryExpression { $$ = $1; }
661 | MultiplicativeExpression '*' UnaryExpression
662 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
663 | MultiplicativeExpression '/' UnaryExpression
664 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
665 | MultiplicativeExpression '%' UnaryExpression
666 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
668 /* ECMA-262 3rd Edition 11.4 */
669 UnaryExpression
670 : PostfixExpression { $$ = $1; }
671 | kDELETE UnaryExpression
672 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
673 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
674 | kTYPEOF UnaryExpression
675 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
676 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
677 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
678 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
679 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
680 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
681 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
683 /* ECMA-262 3rd Edition 11.2 */
684 PostfixExpression
685 : LeftHandSideExpression
686 { $$ = $1; }
687 | LeftHandSideExpression /* NONL */ tINC
688 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
689 | LeftHandSideExpression /* NONL */ tDEC
690 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
693 /* ECMA-262 3rd Edition 11.2 */
694 LeftHandSideExpression
695 : NewExpression { $$ = $1; }
696 | CallExpression { $$ = $1; }
698 /* ECMA-262 3rd Edition 11.2 */
699 NewExpression
700 : MemberExpression { $$ = $1; }
701 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
703 /* ECMA-262 3rd Edition 11.2 */
704 MemberExpression
705 : PrimaryExpression { $$ = $1; }
706 | FunctionExpression { $$ = $1; }
707 | MemberExpression '[' Expression ']'
708 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
709 | MemberExpression '.' tIdentifier
710 { $$ = new_member_expression(ctx, $1, $3); }
711 | kNEW MemberExpression Arguments
712 { $$ = new_new_expression(ctx, $2, $3); }
714 /* ECMA-262 3rd Edition 11.2 */
715 CallExpression
716 : MemberExpression Arguments
717 { $$ = new_call_expression(ctx, $1, $2); }
718 | CallExpression Arguments
719 { $$ = new_call_expression(ctx, $1, $2); }
720 | CallExpression '[' Expression ']'
721 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
722 | CallExpression '.' tIdentifier
723 { $$ = new_member_expression(ctx, $1, $3); }
725 /* ECMA-262 3rd Edition 11.2 */
726 Arguments
727 : '(' ')' { $$ = NULL; }
728 | '(' ArgumentList ')' { $$ = $2; }
730 /* ECMA-262 3rd Edition 11.2 */
731 ArgumentList
732 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
733 | ArgumentList ',' AssignmentExpression
734 { $$ = argument_list_add(ctx, $1, $3); }
736 /* ECMA-262 3rd Edition 11.1 */
737 PrimaryExpression
738 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
739 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
740 | Literal { $$ = new_literal_expression(ctx, $1); }
741 | ArrayLiteral { $$ = $1; }
742 | ObjectLiteral { $$ = $1; }
743 | '(' Expression ')' { $$ = $2; }
745 /* ECMA-262 3rd Edition 11.1.4 */
746 ArrayLiteral
747 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
748 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
749 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
750 | '[' ElementList ',' Elision_opt ']'
751 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
753 /* ECMA-262 3rd Edition 11.1.4 */
754 ElementList
755 : Elision_opt AssignmentExpression
756 { $$ = new_element_list(ctx, $1, $2); }
757 | ElementList ',' Elision_opt AssignmentExpression
758 { $$ = element_list_add(ctx, $1, $3, $4); }
760 /* ECMA-262 3rd Edition 11.1.4 */
761 Elision
762 : ',' { $$ = 1; }
763 | Elision ',' { $$ = $1 + 1; }
765 /* ECMA-262 3rd Edition 11.1.4 */
766 Elision_opt
767 : /* empty */ { $$ = 0; }
768 | Elision { $$ = $1; }
770 /* ECMA-262 3rd Edition 11.1.5 */
771 ObjectLiteral
772 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
773 | '{' PropertyNameAndValueList '}'
774 { $$ = new_prop_and_value_expression(ctx, $2); }
776 /* ECMA-262 3rd Edition 11.1.5 */
777 PropertyNameAndValueList
778 : PropertyName ':' AssignmentExpression
779 { $$ = new_property_list(ctx, $1, $3); }
780 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
781 { $$ = property_list_add(ctx, $1, $3, $5); }
783 /* ECMA-262 3rd Edition 11.1.5 */
784 PropertyName
785 : tIdentifier { $$ = new_string_literal(ctx, $1); }
786 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
787 | tNumericLiteral { $$ = $1; }
789 /* ECMA-262 3rd Edition 7.6 */
790 Identifier_opt
791 : /* empty*/ { $$ = NULL; }
792 | tIdentifier { $$ = $1; }
794 /* ECMA-262 3rd Edition 7.8 */
795 Literal
796 : kNULL { $$ = new_null_literal(ctx); }
797 | BooleanLiteral { $$ = $1; }
798 | tNumericLiteral { $$ = $1; }
799 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
800 | '/' { $$ = parse_regexp(ctx);
801 if(!$$) YYABORT; }
802 | kDIVEQ { $$ = parse_regexp(ctx);
803 if(!$$) YYABORT; }
805 /* ECMA-262 3rd Edition 7.8.2 */
806 BooleanLiteral
807 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
808 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
809 | tBooleanLiteral { $$ = $1; }
811 semicolon_opt
812 : ';'
813 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
815 left_bracket
816 : '('
817 | error { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
819 right_bracket
820 : ')'
821 | error { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
823 semicolon
824 : ';'
825 | error { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; }
829 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
831 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
834 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
836 statement_t *stat;
838 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
839 if(!stat)
840 return NULL;
842 stat->type = type;
843 stat->next = NULL;
845 return stat;
848 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
850 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
852 ret->type = LT_STRING;
853 ret->u.wstr = str;
855 return ret;
858 static literal_t *new_null_literal(parser_ctx_t *ctx)
860 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
862 ret->type = LT_NULL;
864 return ret;
867 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
869 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
871 ret->name = name;
872 ret->value = value;
873 ret->next = NULL;
875 return ret;
878 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
880 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
882 ret->head = ret->tail = new_prop_val(ctx, name, value);
884 return ret;
887 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
889 list->tail = list->tail->next = new_prop_val(ctx, name, value);
891 return list;
894 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
896 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
898 ret->elision = elision;
899 ret->expr = expr;
900 ret->next = NULL;
902 return ret;
905 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
907 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
909 ret->head = ret->tail = new_array_element(ctx, elision, expr);
911 return ret;
914 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
916 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
918 return list;
921 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
923 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
925 ret->expr = expr;
926 ret->next = NULL;
928 return ret;
931 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
933 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
935 ret->head = ret->tail = new_argument(ctx, expr);
937 return ret;
940 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
942 list->tail = list->tail->next = new_argument(ctx, expr);
944 return list;
947 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
949 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
951 ret->identifier = identifier;
952 ret->statement = statement;
954 return ret;
957 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
959 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
961 ret->expr = expr;
962 ret->stat = stat_list ? stat_list->head : NULL;
963 ret->next = NULL;
965 return ret;
968 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
970 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
972 ret->head = ret->tail = case_clausule;
974 return ret;
977 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
979 list->tail = list->tail->next = case_clausule;
981 return list;
984 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
985 case_clausule_t *default_clausule, case_list_t *case_list2)
987 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
988 statement_t *stat = NULL;
990 if(case_list1) {
991 ret = case_list1->head;
992 iter = case_list1->tail;
995 if(default_clausule) {
996 if(ret)
997 iter = iter->next = default_clausule;
998 else
999 ret = iter = default_clausule;
1002 if(case_list2) {
1003 if(ret)
1004 iter->next = case_list2->head;
1005 else
1006 ret = case_list2->head;
1009 if(!ret)
1010 return NULL;
1012 for(iter = ret; iter; iter = iter->next) {
1013 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1014 if(!iter2)
1015 break;
1017 while(iter != iter2) {
1018 iter->stat = iter2->stat;
1019 iter = iter->next;
1022 if(stat) {
1023 while(stat->next)
1024 stat = stat->next;
1025 stat->next = iter->stat;
1026 }else {
1027 stat = iter->stat;
1031 return ret;
1034 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1036 block_statement_t *ret;
1038 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret));
1039 if(!ret)
1040 return NULL;
1042 ret->stat_list = list ? list->head : NULL;
1044 return &ret->stat;
1047 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1049 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1051 ret->identifier = identifier;
1052 ret->expr = expr;
1053 ret->next = NULL;
1054 ret->global_next = NULL;
1056 return ret;
1059 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1061 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1063 ret->head = ret->tail = decl;
1065 return ret;
1068 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1070 list->tail = list->tail->next = decl;
1072 return list;
1075 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1077 var_statement_t *ret;
1079 ret = new_statement(ctx, STAT_VAR, sizeof(*ret));
1080 if(!ret)
1081 return NULL;
1083 ret->variable_list = variable_list->head;
1085 return &ret->stat;
1088 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1090 expression_statement_t *ret;
1092 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret));
1093 if(!ret)
1094 return NULL;
1096 ret->expr = expr;
1098 return &ret->stat;
1101 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1103 if_statement_t *ret;
1105 ret = new_statement(ctx, STAT_IF, sizeof(*ret));
1106 if(!ret)
1107 return NULL;
1109 ret->expr = expr;
1110 ret->if_stat = if_stat;
1111 ret->else_stat = else_stat;
1113 return &ret->stat;
1116 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1118 while_statement_t *ret;
1120 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret));
1121 if(!ret)
1122 return NULL;
1124 ret->do_while = dowhile;
1125 ret->expr = expr;
1126 ret->statement = stat;
1128 return &ret->stat;
1131 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1132 expression_t *expr, expression_t *end_expr, statement_t *statement)
1134 for_statement_t *ret;
1136 ret = new_statement(ctx, STAT_FOR, sizeof(*ret));
1137 if(!ret)
1138 return NULL;
1140 ret->variable_list = variable_list ? variable_list->head : NULL;
1141 ret->begin_expr = begin_expr;
1142 ret->expr = expr;
1143 ret->end_expr = end_expr;
1144 ret->statement = statement;
1146 return &ret->stat;
1149 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1150 expression_t *in_expr, statement_t *statement)
1152 forin_statement_t *ret;
1154 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret));
1155 if(!ret)
1156 return NULL;
1158 ret->variable = variable;
1159 ret->expr = expr;
1160 ret->in_expr = in_expr;
1161 ret->statement = statement;
1163 return &ret->stat;
1166 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1168 branch_statement_t *ret;
1170 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret));
1171 if(!ret)
1172 return NULL;
1174 ret->identifier = identifier;
1176 return &ret->stat;
1179 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1181 branch_statement_t *ret;
1183 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret));
1184 if(!ret)
1185 return NULL;
1187 ret->identifier = identifier;
1189 return &ret->stat;
1192 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1194 expression_statement_t *ret;
1196 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret));
1197 if(!ret)
1198 return NULL;
1200 ret->expr = expr;
1202 return &ret->stat;
1205 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1207 with_statement_t *ret;
1209 ret = new_statement(ctx, STAT_WITH, sizeof(*ret));
1210 if(!ret)
1211 return NULL;
1213 ret->expr = expr;
1214 ret->statement = statement;
1216 return &ret->stat;
1219 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1221 labelled_statement_t *ret;
1223 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret));
1224 if(!ret)
1225 return NULL;
1227 ret->identifier = identifier;
1228 ret->statement = statement;
1230 return &ret->stat;
1233 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1235 switch_statement_t *ret;
1237 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret));
1238 if(!ret)
1239 return NULL;
1241 ret->expr = expr;
1242 ret->case_list = case_list;
1244 return &ret->stat;
1247 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1249 expression_statement_t *ret;
1251 ret = new_statement(ctx, STAT_THROW, sizeof(*ret));
1252 if(!ret)
1253 return NULL;
1255 ret->expr = expr;
1257 return &ret->stat;
1260 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1261 catch_block_t *catch_block, statement_t *finally_statement)
1263 try_statement_t *ret;
1265 ret = new_statement(ctx, STAT_TRY, sizeof(*ret));
1266 if(!ret)
1267 return NULL;
1269 ret->try_statement = try_statement;
1270 ret->catch_block = catch_block;
1271 ret->finally_statement = finally_statement;
1273 return &ret->stat;
1276 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1278 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1280 ret->identifier = identifier;
1281 ret->next = NULL;
1283 return ret;
1286 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1288 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1290 ret->head = ret->tail = new_parameter(ctx, identifier);
1292 return ret;
1295 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1297 list->tail = list->tail->next = new_parameter(ctx, identifier);
1299 return list;
1302 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1303 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1305 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
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 ret->next = NULL;
1314 return &ret->expr;
1317 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1319 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1321 ret->type = type;
1323 return ret;
1326 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1327 expression_t *expression1, expression_t *expression2)
1329 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1331 ret->expression1 = expression1;
1332 ret->expression2 = expression2;
1334 return &ret->expr;
1337 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1339 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1341 ret->expression = expression;
1343 return &ret->expr;
1346 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1347 expression_t *true_expression, expression_t *false_expression)
1349 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1351 ret->expression = expression;
1352 ret->true_expression = true_expression;
1353 ret->false_expression = false_expression;
1355 return &ret->expr;
1358 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1360 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1362 ret->expression = expression;
1363 ret->identifier = identifier;
1365 return &ret->expr;
1368 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1370 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1372 ret->expression = expression;
1373 ret->argument_list = argument_list ? argument_list->head : NULL;
1375 return &ret->expr;
1378 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1380 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1382 ret->expression = expression;
1383 ret->argument_list = argument_list ? argument_list->head : NULL;
1385 return &ret->expr;
1388 static int parser_error(parser_ctx_t *ctx, const char *str)
1390 return 0;
1393 static void set_error(parser_ctx_t *ctx, UINT error)
1395 ctx->hres = error;
1398 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1400 if(obj || *(ctx->ptr-1)==next) return TRUE;
1402 set_error(ctx, JS_E_SYNTAX);
1403 return FALSE;
1407 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1409 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1411 ret->identifier = identifier;
1413 return &ret->expr;
1416 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1418 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1420 ret->element_list = element_list ? element_list->head : NULL;
1421 ret->length = length;
1423 return &ret->expr;
1426 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1428 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1430 ret->property_list = property_list ? property_list->head : NULL;
1432 return &ret->expr;
1435 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1437 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1439 ret->literal = literal;
1441 return &ret->expr;
1444 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1446 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1448 memset(ret, 0, sizeof(*ret));
1450 return ret;
1453 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1455 if(source_elements->statement_tail)
1456 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1457 else
1458 source_elements->statement = source_elements->statement_tail = statement;
1460 return source_elements;
1463 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1465 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1467 ret->head = ret->tail = statement;
1469 return ret;
1472 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1474 list->tail = list->tail->next = statement;
1476 return list;
1479 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1481 ctx->source = source;
1482 if(!ctx->lexer_error)
1483 ctx->hres = S_OK;
1486 void parser_release(parser_ctx_t *ctx)
1488 script_release(ctx->script);
1489 heap_pool_free(&ctx->heap);
1490 heap_free(ctx);
1493 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval,
1494 parser_ctx_t **ret)
1496 parser_ctx_t *parser_ctx;
1497 heap_pool_t *mark;
1498 HRESULT hres;
1500 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1502 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1503 if(!parser_ctx)
1504 return E_OUTOFMEMORY;
1506 parser_ctx->hres = JS_E_SYNTAX;
1507 parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1509 parser_ctx->begin = parser_ctx->ptr = code;
1510 parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1512 script_addref(ctx);
1513 parser_ctx->script = ctx;
1515 mark = heap_pool_mark(&ctx->tmp_heap);
1516 heap_pool_init(&parser_ctx->heap);
1518 parser_parse(parser_ctx);
1519 heap_pool_clear(mark);
1520 hres = parser_ctx->hres;
1521 if(FAILED(hres)) {
1522 WARN("parser failed around %s\n",
1523 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20));
1524 parser_release(parser_ctx);
1525 return hres;
1528 *ret = parser_ctx;
1529 return S_OK;