msvcp90: Use streambuf sgetc/snextc in std::getline(istream<> &).
[wine.git] / dlls / jscript / parser.y
blob4c69ed9a9a1a73477b3609e9db1bbac01883b1ea
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 #define YYLEX_PARAM ctx
29 #define YYPARSE_PARAM ctx
31 static int parser_error(const char*);
32 static void set_error(parser_ctx_t*,UINT);
33 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
34 static BOOL allow_auto_semicolon(parser_ctx_t*);
35 static void program_parsed(parser_ctx_t*,source_elements_t*);
37 typedef struct _statement_list_t {
38 statement_t *head;
39 statement_t *tail;
40 } statement_list_t;
42 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
43 static literal_t *new_null_literal(parser_ctx_t*);
45 typedef struct _property_list_t {
46 prop_val_t *head;
47 prop_val_t *tail;
48 } property_list_t;
50 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
51 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
53 typedef struct _element_list_t {
54 array_element_t *head;
55 array_element_t *tail;
56 } element_list_t;
58 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
59 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
61 typedef struct _argument_list_t {
62 argument_t *head;
63 argument_t *tail;
64 } argument_list_t;
66 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
67 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
69 typedef struct _case_list_t {
70 case_clausule_t *head;
71 case_clausule_t *tail;
72 } case_list_t;
74 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
75 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
76 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
77 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
78 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
80 typedef struct _variable_list_t {
81 variable_declaration_t *head;
82 variable_declaration_t *tail;
83 } variable_list_t;
85 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
86 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
87 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
89 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
90 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
91 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
92 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
93 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
94 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
95 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
96 expression_t*,statement_t*);
97 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
98 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
99 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
100 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
101 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
102 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
103 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
104 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
105 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
107 struct statement_list_t {
108 statement_t *head;
109 statement_t *tail;
112 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
113 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
115 typedef struct _parameter_list_t {
116 parameter_t *head;
117 parameter_t *tail;
118 } parameter_list_t;
120 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
121 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
123 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
124 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
125 source_elements_t*,const WCHAR*,DWORD);
126 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
127 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
128 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
129 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
130 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
131 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
132 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
133 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
134 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
135 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
137 static source_elements_t *new_source_elements(parser_ctx_t*);
138 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
142 %pure_parser
143 %start Program
145 %union {
146 int ival;
147 const WCHAR *srcptr;
148 LPCWSTR wstr;
149 literal_t *literal;
150 struct _argument_list_t *argument_list;
151 case_clausule_t *case_clausule;
152 struct _case_list_t *case_list;
153 catch_block_t *catch_block;
154 struct _element_list_t *element_list;
155 expression_t *expr;
156 const WCHAR *identifier;
157 struct _parameter_list_t *parameter_list;
158 struct _property_list_t *property_list;
159 source_elements_t *source_elements;
160 statement_t *statement;
161 struct _statement_list_t *statement_list;
162 struct _variable_list_t *variable_list;
163 variable_declaration_t *variable_declaration;
166 /* keywords */
167 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
168 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
169 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
171 %token <srcptr> kFUNCTION '}'
173 /* tokens */
174 %token <identifier> tIdentifier
175 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
176 %token <literal> tNumericLiteral tBooleanLiteral
177 %token <wstr> tStringLiteral
178 %token tEOF
180 %type <source_elements> SourceElements
181 %type <source_elements> FunctionBody
182 %type <statement> Statement
183 %type <statement> Block
184 %type <statement> VariableStatement
185 %type <statement> EmptyStatement
186 %type <statement> ExpressionStatement
187 %type <statement> IfStatement
188 %type <statement> IterationStatement
189 %type <statement> ContinueStatement
190 %type <statement> BreakStatement
191 %type <statement> ReturnStatement
192 %type <statement> WithStatement
193 %type <statement> LabelledStatement
194 %type <statement> SwitchStatement
195 %type <statement> ThrowStatement
196 %type <statement> TryStatement
197 %type <statement> Finally
198 %type <statement_list> StatementList StatementList_opt
199 %type <parameter_list> FormalParameterList FormalParameterList_opt
200 %type <expr> Expression Expression_opt Expression_err
201 %type <expr> ExpressionNoIn ExpressionNoIn_opt
202 %type <expr> FunctionExpression
203 %type <expr> AssignmentExpression AssignmentExpressionNoIn
204 %type <expr> ConditionalExpression ConditionalExpressionNoIn
205 %type <expr> LeftHandSideExpression
206 %type <expr> LogicalORExpression LogicalORExpressionNoIn
207 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
208 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
209 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
210 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
211 %type <expr> EqualityExpression EqualityExpressionNoIn
212 %type <expr> RelationalExpression RelationalExpressionNoIn
213 %type <expr> ShiftExpression
214 %type <expr> AdditiveExpression
215 %type <expr> MultiplicativeExpression
216 %type <expr> Initialiser_opt Initialiser
217 %type <expr> InitialiserNoIn_opt InitialiserNoIn
218 %type <expr> UnaryExpression
219 %type <expr> PostfixExpression
220 %type <expr> NewExpression
221 %type <expr> CallExpression
222 %type <expr> MemberExpression
223 %type <expr> PrimaryExpression
224 %type <identifier> Identifier_opt
225 %type <variable_list> VariableDeclarationList
226 %type <variable_list> VariableDeclarationListNoIn
227 %type <variable_declaration> VariableDeclaration
228 %type <variable_declaration> VariableDeclarationNoIn
229 %type <case_list> CaseClausules CaseClausules_opt
230 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
231 %type <catch_block> Catch
232 %type <argument_list> Arguments
233 %type <argument_list> ArgumentList
234 %type <literal> Literal
235 %type <expr> ArrayLiteral
236 %type <expr> ObjectLiteral
237 %type <ival> Elision Elision_opt
238 %type <element_list> ElementList
239 %type <property_list> PropertyNameAndValueList
240 %type <literal> PropertyName
241 %type <literal> BooleanLiteral
242 %type <srcptr> KFunction
243 %type <ival> AssignOper
245 %nonassoc LOWER_THAN_ELSE
246 %nonassoc kELSE
250 /* ECMA-262 3rd Edition 14 */
251 Program
252 : SourceElements HtmlComment tEOF
253 { program_parsed(ctx, $1); }
255 HtmlComment
256 : tHTMLCOMMENT {}
257 | /* empty */ {}
259 /* ECMA-262 3rd Edition 14 */
260 SourceElements
261 : /* empty */ { $$ = new_source_elements(ctx); }
262 | SourceElements Statement
263 { $$ = source_elements_add_statement($1, $2); }
265 /* ECMA-262 3rd Edition 13 */
266 FunctionExpression
267 : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
268 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
270 KFunction
271 : kFUNCTION { $$ = $1; }
273 /* ECMA-262 3rd Edition 13 */
274 FunctionBody
275 : SourceElements { $$ = $1; }
277 /* ECMA-262 3rd Edition 13 */
278 FormalParameterList
279 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
280 | FormalParameterList ',' tIdentifier
281 { $$ = parameter_list_add(ctx, $1, $3); }
283 /* ECMA-262 3rd Edition 13 */
284 FormalParameterList_opt
285 : /* empty */ { $$ = NULL; }
286 | FormalParameterList { $$ = $1; }
288 /* ECMA-262 3rd Edition 12 */
289 Statement
290 : Block { $$ = $1; }
291 | VariableStatement { $$ = $1; }
292 | EmptyStatement { $$ = $1; }
293 | FunctionExpression { $$ = new_expression_statement(ctx, $1); }
294 | ExpressionStatement { $$ = $1; }
295 | IfStatement { $$ = $1; }
296 | IterationStatement { $$ = $1; }
297 | ContinueStatement { $$ = $1; }
298 | BreakStatement { $$ = $1; }
299 | ReturnStatement { $$ = $1; }
300 | WithStatement { $$ = $1; }
301 | LabelledStatement { $$ = $1; }
302 | SwitchStatement { $$ = $1; }
303 | ThrowStatement { $$ = $1; }
304 | TryStatement { $$ = $1; }
306 /* ECMA-262 3rd Edition 12.2 */
307 StatementList
308 : Statement { $$ = new_statement_list(ctx, $1); }
309 | StatementList Statement
310 { $$ = statement_list_add($1, $2); }
312 /* ECMA-262 3rd Edition 12.2 */
313 StatementList_opt
314 : /* empty */ { $$ = NULL; }
315 | StatementList { $$ = $1; }
317 /* ECMA-262 3rd Edition 12.1 */
318 Block
319 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
320 | '{' '}' { $$ = new_block_statement(ctx, NULL); }
322 /* ECMA-262 3rd Edition 12.2 */
323 VariableStatement
324 : kVAR VariableDeclarationList semicolon_opt
325 { $$ = new_var_statement(ctx, $2); }
327 /* ECMA-262 3rd Edition 12.2 */
328 VariableDeclarationList
329 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
330 | VariableDeclarationList ',' VariableDeclaration
331 { $$ = variable_list_add(ctx, $1, $3); }
333 /* ECMA-262 3rd Edition 12.2 */
334 VariableDeclarationListNoIn
335 : VariableDeclarationNoIn
336 { $$ = new_variable_list(ctx, $1); }
337 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
338 { $$ = variable_list_add(ctx, $1, $3); }
340 /* ECMA-262 3rd Edition 12.2 */
341 VariableDeclaration
342 : tIdentifier Initialiser_opt
343 { $$ = new_variable_declaration(ctx, $1, $2); }
345 /* ECMA-262 3rd Edition 12.2 */
346 VariableDeclarationNoIn
347 : tIdentifier InitialiserNoIn_opt
348 { $$ = new_variable_declaration(ctx, $1, $2); }
350 /* ECMA-262 3rd Edition 12.2 */
351 Initialiser_opt
352 : /* empty */ { $$ = NULL; }
353 | Initialiser { $$ = $1; }
355 /* ECMA-262 3rd Edition 12.2 */
356 Initialiser
357 : '=' AssignmentExpression
358 { $$ = $2; }
360 /* ECMA-262 3rd Edition 12.2 */
361 InitialiserNoIn_opt
362 : /* empty */ { $$ = NULL; }
363 | InitialiserNoIn { $$ = $1; }
365 /* ECMA-262 3rd Edition 12.2 */
366 InitialiserNoIn
367 : '=' AssignmentExpressionNoIn
368 { $$ = $2; }
370 /* ECMA-262 3rd Edition 12.3 */
371 EmptyStatement
372 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0); }
374 /* ECMA-262 3rd Edition 12.4 */
375 ExpressionStatement
376 : Expression semicolon_opt
377 { $$ = new_expression_statement(ctx, $1); }
379 /* ECMA-262 3rd Edition 12.5 */
380 IfStatement
381 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
382 { $$ = new_if_statement(ctx, $3, $5, $7); }
383 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
384 { $$ = new_if_statement(ctx, $3, $5, NULL); }
386 /* ECMA-262 3rd Edition 12.6 */
387 IterationStatement
388 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
389 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
390 | kWHILE left_bracket Expression_err right_bracket Statement
391 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
392 | kFOR left_bracket ExpressionNoIn_opt
393 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
394 semicolon Expression_opt
395 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
396 semicolon Expression_opt right_bracket Statement
397 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
398 | kFOR left_bracket kVAR VariableDeclarationListNoIn
399 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
400 semicolon Expression_opt
401 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
402 semicolon Expression_opt right_bracket Statement
403 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
404 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
405 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
406 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
407 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
409 /* ECMA-262 3rd Edition 12.7 */
410 ContinueStatement
411 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
412 { $$ = new_continue_statement(ctx, $2); }
414 /* ECMA-262 3rd Edition 12.8 */
415 BreakStatement
416 : kBREAK /* NONL */ Identifier_opt semicolon_opt
417 { $$ = new_break_statement(ctx, $2); }
419 /* ECMA-262 3rd Edition 12.9 */
420 ReturnStatement
421 : kRETURN /* NONL */ Expression_opt semicolon_opt
422 { $$ = new_return_statement(ctx, $2); }
424 /* ECMA-262 3rd Edition 12.10 */
425 WithStatement
426 : kWITH left_bracket Expression right_bracket Statement
427 { $$ = new_with_statement(ctx, $3, $5); }
429 /* ECMA-262 3rd Edition 12.12 */
430 LabelledStatement
431 : tIdentifier ':' Statement
432 { $$ = new_labelled_statement(ctx, $1, $3); }
434 /* ECMA-262 3rd Edition 12.11 */
435 SwitchStatement
436 : kSWITCH left_bracket Expression right_bracket CaseBlock
437 { $$ = new_switch_statement(ctx, $3, $5); }
439 /* ECMA-262 3rd Edition 12.11 */
440 CaseBlock
441 : '{' CaseClausules_opt '}'
442 { $$ = new_case_block(ctx, $2, NULL, NULL); }
443 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
444 { $$ = new_case_block(ctx, $2, $3, $4); }
446 /* ECMA-262 3rd Edition 12.11 */
447 CaseClausules_opt
448 : /* empty */ { $$ = NULL; }
449 | CaseClausules { $$ = $1; }
451 /* ECMA-262 3rd Edition 12.11 */
452 CaseClausules
453 : CaseClausule { $$ = new_case_list(ctx, $1); }
454 | CaseClausules CaseClausule
455 { $$ = case_list_add(ctx, $1, $2); }
457 /* ECMA-262 3rd Edition 12.11 */
458 CaseClausule
459 : kCASE Expression ':' StatementList_opt
460 { $$ = new_case_clausule(ctx, $2, $4); }
462 /* ECMA-262 3rd Edition 12.11 */
463 DefaultClausule
464 : kDEFAULT ':' StatementList_opt
465 { $$ = new_case_clausule(ctx, NULL, $3); }
467 /* ECMA-262 3rd Edition 12.13 */
468 ThrowStatement
469 : kTHROW /* NONL */ Expression semicolon_opt
470 { $$ = new_throw_statement(ctx, $2); }
472 /* ECMA-262 3rd Edition 12.14 */
473 TryStatement
474 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
475 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
476 | kTRY Block Catch Finally
477 { $$ = new_try_statement(ctx, $2, $3, $4); }
479 /* ECMA-262 3rd Edition 12.14 */
480 Catch
481 : kCATCH left_bracket tIdentifier right_bracket Block
482 { $$ = new_catch_block(ctx, $3, $5); }
484 /* ECMA-262 3rd Edition 12.14 */
485 Finally
486 : kFINALLY Block { $$ = $2; }
488 /* ECMA-262 3rd Edition 11.14 */
489 Expression_opt
490 : /* empty */ { $$ = NULL; }
491 | Expression { $$ = $1; }
493 Expression_err
494 : Expression { $$ = $1; }
495 | error { set_error(ctx, JS_E_SYNTAX); YYABORT; }
497 /* ECMA-262 3rd Edition 11.14 */
498 Expression
499 : AssignmentExpression { $$ = $1; }
500 | Expression ',' AssignmentExpression
501 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
503 /* ECMA-262 3rd Edition 11.14 */
504 ExpressionNoIn_opt
505 : /* empty */ { $$ = NULL; }
506 | ExpressionNoIn { $$ = $1; }
508 /* ECMA-262 3rd Edition 11.14 */
509 ExpressionNoIn
510 : AssignmentExpressionNoIn
511 { $$ = $1; }
512 | ExpressionNoIn ',' AssignmentExpressionNoIn
513 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
515 AssignOper
516 : tAssignOper { $$ = $1; }
517 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
519 /* ECMA-262 3rd Edition 11.13 */
520 AssignmentExpression
521 : ConditionalExpression { $$ = $1; }
522 | LeftHandSideExpression '=' AssignmentExpression
523 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
524 | LeftHandSideExpression AssignOper AssignmentExpression
525 { $$ = new_binary_expression(ctx, $2, $1, $3); }
527 /* ECMA-262 3rd Edition 11.13 */
528 AssignmentExpressionNoIn
529 : ConditionalExpressionNoIn
530 { $$ = $1; }
531 | LeftHandSideExpression '=' AssignmentExpressionNoIn
532 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
533 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
534 { $$ = new_binary_expression(ctx, $2, $1, $3); }
536 /* ECMA-262 3rd Edition 11.12 */
537 ConditionalExpression
538 : LogicalORExpression { $$ = $1; }
539 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
540 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
542 /* ECMA-262 3rd Edition 11.12 */
543 ConditionalExpressionNoIn
544 : LogicalORExpressionNoIn
545 { $$ = $1; }
546 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
547 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
549 /* ECMA-262 3rd Edition 11.11 */
550 LogicalORExpression
551 : LogicalANDExpression { $$ = $1; }
552 | LogicalORExpression tOROR LogicalANDExpression
553 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
555 /* ECMA-262 3rd Edition 11.11 */
556 LogicalORExpressionNoIn
557 : LogicalANDExpressionNoIn
558 { $$ = $1; }
559 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
560 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
562 /* ECMA-262 3rd Edition 11.11 */
563 LogicalANDExpression
564 : BitwiseORExpression { $$ = $1; }
565 | LogicalANDExpression tANDAND BitwiseORExpression
566 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
568 /* ECMA-262 3rd Edition 11.11 */
569 LogicalANDExpressionNoIn
570 : BitwiseORExpressionNoIn
571 { $$ = $1; }
572 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
573 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
575 /* ECMA-262 3rd Edition 11.10 */
576 BitwiseORExpression
577 : BitwiseXORExpression { $$ = $1; }
578 | BitwiseORExpression '|' BitwiseXORExpression
579 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
581 /* ECMA-262 3rd Edition 11.10 */
582 BitwiseORExpressionNoIn
583 : BitwiseXORExpressionNoIn
584 { $$ = $1; }
585 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
586 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
588 /* ECMA-262 3rd Edition 11.10 */
589 BitwiseXORExpression
590 : BitwiseANDExpression { $$ = $1; }
591 | BitwiseXORExpression '^' BitwiseANDExpression
592 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
594 /* ECMA-262 3rd Edition 11.10 */
595 BitwiseXORExpressionNoIn
596 : BitwiseANDExpressionNoIn
597 { $$ = $1; }
598 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
599 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
601 /* ECMA-262 3rd Edition 11.10 */
602 BitwiseANDExpression
603 : EqualityExpression { $$ = $1; }
604 | BitwiseANDExpression '&' EqualityExpression
605 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
607 /* ECMA-262 3rd Edition 11.10 */
608 BitwiseANDExpressionNoIn
609 : EqualityExpressionNoIn
610 { $$ = $1; }
611 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
612 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
614 /* ECMA-262 3rd Edition 11.9 */
615 EqualityExpression
616 : RelationalExpression { $$ = $1; }
617 | EqualityExpression tEqOper RelationalExpression
618 { $$ = new_binary_expression(ctx, $2, $1, $3); }
620 /* ECMA-262 3rd Edition 11.9 */
621 EqualityExpressionNoIn
622 : RelationalExpressionNoIn { $$ = $1; }
623 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
624 { $$ = new_binary_expression(ctx, $2, $1, $3); }
626 /* ECMA-262 3rd Edition 11.8 */
627 RelationalExpression
628 : ShiftExpression { $$ = $1; }
629 | RelationalExpression tRelOper ShiftExpression
630 { $$ = new_binary_expression(ctx, $2, $1, $3); }
631 | RelationalExpression kINSTANCEOF ShiftExpression
632 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
633 | RelationalExpression kIN ShiftExpression
634 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
636 /* ECMA-262 3rd Edition 11.8 */
637 RelationalExpressionNoIn
638 : ShiftExpression { $$ = $1; }
639 | RelationalExpressionNoIn tRelOper ShiftExpression
640 { $$ = new_binary_expression(ctx, $2, $1, $3); }
641 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
642 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
644 /* ECMA-262 3rd Edition 11.7 */
645 ShiftExpression
646 : AdditiveExpression { $$ = $1; }
647 | ShiftExpression tShiftOper AdditiveExpression
648 { $$ = new_binary_expression(ctx, $2, $1, $3); }
650 /* ECMA-262 3rd Edition 11.6 */
651 AdditiveExpression
652 : MultiplicativeExpression
653 { $$ = $1; }
654 | AdditiveExpression '+' MultiplicativeExpression
655 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
656 | AdditiveExpression '-' MultiplicativeExpression
657 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
659 /* ECMA-262 3rd Edition 11.5 */
660 MultiplicativeExpression
661 : UnaryExpression { $$ = $1; }
662 | MultiplicativeExpression '*' UnaryExpression
663 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
664 | MultiplicativeExpression '/' UnaryExpression
665 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
666 | MultiplicativeExpression '%' UnaryExpression
667 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
669 /* ECMA-262 3rd Edition 11.4 */
670 UnaryExpression
671 : PostfixExpression { $$ = $1; }
672 | kDELETE UnaryExpression
673 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
674 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
675 | kTYPEOF UnaryExpression
676 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
677 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
678 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
679 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
680 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
681 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
682 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
684 /* ECMA-262 3rd Edition 11.2 */
685 PostfixExpression
686 : LeftHandSideExpression
687 { $$ = $1; }
688 | LeftHandSideExpression /* NONL */ tINC
689 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
690 | LeftHandSideExpression /* NONL */ tDEC
691 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
694 /* ECMA-262 3rd Edition 11.2 */
695 LeftHandSideExpression
696 : NewExpression { $$ = $1; }
697 | CallExpression { $$ = $1; }
699 /* ECMA-262 3rd Edition 11.2 */
700 NewExpression
701 : MemberExpression { $$ = $1; }
702 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
704 /* ECMA-262 3rd Edition 11.2 */
705 MemberExpression
706 : PrimaryExpression { $$ = $1; }
707 | FunctionExpression { $$ = $1; }
708 | MemberExpression '[' Expression ']'
709 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
710 | MemberExpression '.' tIdentifier
711 { $$ = new_member_expression(ctx, $1, $3); }
712 | kNEW MemberExpression Arguments
713 { $$ = new_new_expression(ctx, $2, $3); }
715 /* ECMA-262 3rd Edition 11.2 */
716 CallExpression
717 : MemberExpression Arguments
718 { $$ = new_call_expression(ctx, $1, $2); }
719 | CallExpression Arguments
720 { $$ = new_call_expression(ctx, $1, $2); }
721 | CallExpression '[' Expression ']'
722 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
723 | CallExpression '.' tIdentifier
724 { $$ = new_member_expression(ctx, $1, $3); }
726 /* ECMA-262 3rd Edition 11.2 */
727 Arguments
728 : '(' ')' { $$ = NULL; }
729 | '(' ArgumentList ')' { $$ = $2; }
731 /* ECMA-262 3rd Edition 11.2 */
732 ArgumentList
733 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
734 | ArgumentList ',' AssignmentExpression
735 { $$ = argument_list_add(ctx, $1, $3); }
737 /* ECMA-262 3rd Edition 11.1 */
738 PrimaryExpression
739 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
740 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
741 | Literal { $$ = new_literal_expression(ctx, $1); }
742 | ArrayLiteral { $$ = $1; }
743 | ObjectLiteral { $$ = $1; }
744 | '(' Expression ')' { $$ = $2; }
746 /* ECMA-262 3rd Edition 11.1.4 */
747 ArrayLiteral
748 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
749 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
750 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
751 | '[' ElementList ',' Elision_opt ']'
752 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
754 /* ECMA-262 3rd Edition 11.1.4 */
755 ElementList
756 : Elision_opt AssignmentExpression
757 { $$ = new_element_list(ctx, $1, $2); }
758 | ElementList ',' Elision_opt AssignmentExpression
759 { $$ = element_list_add(ctx, $1, $3, $4); }
761 /* ECMA-262 3rd Edition 11.1.4 */
762 Elision
763 : ',' { $$ = 1; }
764 | Elision ',' { $$ = $1 + 1; }
766 /* ECMA-262 3rd Edition 11.1.4 */
767 Elision_opt
768 : /* empty */ { $$ = 0; }
769 | Elision { $$ = $1; }
771 /* ECMA-262 3rd Edition 11.1.5 */
772 ObjectLiteral
773 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
774 | '{' PropertyNameAndValueList '}'
775 { $$ = new_prop_and_value_expression(ctx, $2); }
777 /* ECMA-262 3rd Edition 11.1.5 */
778 PropertyNameAndValueList
779 : PropertyName ':' AssignmentExpression
780 { $$ = new_property_list(ctx, $1, $3); }
781 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
782 { $$ = property_list_add(ctx, $1, $3, $5); }
784 /* ECMA-262 3rd Edition 11.1.5 */
785 PropertyName
786 : tIdentifier { $$ = new_string_literal(ctx, $1); }
787 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
788 | tNumericLiteral { $$ = $1; }
790 /* ECMA-262 3rd Edition 7.6 */
791 Identifier_opt
792 : /* empty*/ { $$ = NULL; }
793 | tIdentifier { $$ = $1; }
795 /* ECMA-262 3rd Edition 7.8 */
796 Literal
797 : kNULL { $$ = new_null_literal(ctx); }
798 | BooleanLiteral { $$ = $1; }
799 | tNumericLiteral { $$ = $1; }
800 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
801 | '/' { $$ = parse_regexp(ctx);
802 if(!$$) YYABORT; }
803 | kDIVEQ { $$ = parse_regexp(ctx);
804 if(!$$) YYABORT; }
806 /* ECMA-262 3rd Edition 7.8.2 */
807 BooleanLiteral
808 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
809 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
810 | tBooleanLiteral { $$ = $1; }
812 semicolon_opt
813 : ';'
814 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
816 left_bracket
817 : '('
818 | error { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
820 right_bracket
821 : ')'
822 | error { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
824 semicolon
825 : ';'
826 | error { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; }
830 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
832 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
835 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
837 statement_t *stat;
839 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
840 if(!stat)
841 return NULL;
843 stat->type = type;
844 stat->next = NULL;
846 return stat;
849 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
851 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
853 ret->type = LT_STRING;
854 ret->u.wstr = str;
856 return ret;
859 static literal_t *new_null_literal(parser_ctx_t *ctx)
861 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
863 ret->type = LT_NULL;
865 return ret;
868 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
870 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
872 ret->name = name;
873 ret->value = value;
874 ret->next = NULL;
876 return ret;
879 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
881 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
883 ret->head = ret->tail = new_prop_val(ctx, name, value);
885 return ret;
888 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
890 list->tail = list->tail->next = new_prop_val(ctx, name, value);
892 return list;
895 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
897 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
899 ret->elision = elision;
900 ret->expr = expr;
901 ret->next = NULL;
903 return ret;
906 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
908 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
910 ret->head = ret->tail = new_array_element(ctx, elision, expr);
912 return ret;
915 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
917 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
919 return list;
922 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
924 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
926 ret->expr = expr;
927 ret->next = NULL;
929 return ret;
932 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
934 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
936 ret->head = ret->tail = new_argument(ctx, expr);
938 return ret;
941 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
943 list->tail = list->tail->next = new_argument(ctx, expr);
945 return list;
948 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
950 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
952 ret->identifier = identifier;
953 ret->statement = statement;
955 return ret;
958 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
960 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
962 ret->expr = expr;
963 ret->stat = stat_list ? stat_list->head : NULL;
964 ret->next = NULL;
966 return ret;
969 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
971 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
973 ret->head = ret->tail = case_clausule;
975 return ret;
978 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
980 list->tail = list->tail->next = case_clausule;
982 return list;
985 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
986 case_clausule_t *default_clausule, case_list_t *case_list2)
988 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
989 statement_t *stat = NULL;
991 if(case_list1) {
992 ret = case_list1->head;
993 iter = case_list1->tail;
996 if(default_clausule) {
997 if(ret)
998 iter = iter->next = default_clausule;
999 else
1000 ret = iter = default_clausule;
1003 if(case_list2) {
1004 if(ret)
1005 iter->next = case_list2->head;
1006 else
1007 ret = case_list2->head;
1010 if(!ret)
1011 return NULL;
1013 for(iter = ret; iter; iter = iter->next) {
1014 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1015 if(!iter2)
1016 break;
1018 while(iter != iter2) {
1019 iter->stat = iter2->stat;
1020 iter = iter->next;
1023 if(stat) {
1024 while(stat->next)
1025 stat = stat->next;
1026 stat->next = iter->stat;
1027 }else {
1028 stat = iter->stat;
1032 return ret;
1035 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1037 block_statement_t *ret;
1039 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret));
1040 if(!ret)
1041 return NULL;
1043 ret->stat_list = list ? list->head : NULL;
1045 return &ret->stat;
1048 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1050 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1052 ret->identifier = identifier;
1053 ret->expr = expr;
1054 ret->next = NULL;
1055 ret->global_next = NULL;
1057 return ret;
1060 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1062 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1064 ret->head = ret->tail = decl;
1066 return ret;
1069 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1071 list->tail = list->tail->next = decl;
1073 return list;
1076 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1078 var_statement_t *ret;
1080 ret = new_statement(ctx, STAT_VAR, sizeof(*ret));
1081 if(!ret)
1082 return NULL;
1084 ret->variable_list = variable_list->head;
1086 return &ret->stat;
1089 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1091 expression_statement_t *ret;
1093 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret));
1094 if(!ret)
1095 return NULL;
1097 ret->expr = expr;
1099 return &ret->stat;
1102 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1104 if_statement_t *ret;
1106 ret = new_statement(ctx, STAT_IF, sizeof(*ret));
1107 if(!ret)
1108 return NULL;
1110 ret->expr = expr;
1111 ret->if_stat = if_stat;
1112 ret->else_stat = else_stat;
1114 return &ret->stat;
1117 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1119 while_statement_t *ret;
1121 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret));
1122 if(!ret)
1123 return NULL;
1125 ret->do_while = dowhile;
1126 ret->expr = expr;
1127 ret->statement = stat;
1129 return &ret->stat;
1132 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1133 expression_t *expr, expression_t *end_expr, statement_t *statement)
1135 for_statement_t *ret;
1137 ret = new_statement(ctx, STAT_FOR, sizeof(*ret));
1138 if(!ret)
1139 return NULL;
1141 ret->variable_list = variable_list ? variable_list->head : NULL;
1142 ret->begin_expr = begin_expr;
1143 ret->expr = expr;
1144 ret->end_expr = end_expr;
1145 ret->statement = statement;
1147 return &ret->stat;
1150 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1151 expression_t *in_expr, statement_t *statement)
1153 forin_statement_t *ret;
1155 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret));
1156 if(!ret)
1157 return NULL;
1159 ret->variable = variable;
1160 ret->expr = expr;
1161 ret->in_expr = in_expr;
1162 ret->statement = statement;
1164 return &ret->stat;
1167 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1169 branch_statement_t *ret;
1171 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret));
1172 if(!ret)
1173 return NULL;
1175 ret->identifier = identifier;
1177 return &ret->stat;
1180 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1182 branch_statement_t *ret;
1184 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret));
1185 if(!ret)
1186 return NULL;
1188 ret->identifier = identifier;
1190 return &ret->stat;
1193 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1195 expression_statement_t *ret;
1197 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret));
1198 if(!ret)
1199 return NULL;
1201 ret->expr = expr;
1203 return &ret->stat;
1206 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1208 with_statement_t *ret;
1210 ret = new_statement(ctx, STAT_WITH, sizeof(*ret));
1211 if(!ret)
1212 return NULL;
1214 ret->expr = expr;
1215 ret->statement = statement;
1217 return &ret->stat;
1220 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1222 labelled_statement_t *ret;
1224 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret));
1225 if(!ret)
1226 return NULL;
1228 ret->identifier = identifier;
1229 ret->statement = statement;
1231 return &ret->stat;
1234 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1236 switch_statement_t *ret;
1238 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret));
1239 if(!ret)
1240 return NULL;
1242 ret->expr = expr;
1243 ret->case_list = case_list;
1245 return &ret->stat;
1248 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1250 expression_statement_t *ret;
1252 ret = new_statement(ctx, STAT_THROW, sizeof(*ret));
1253 if(!ret)
1254 return NULL;
1256 ret->expr = expr;
1258 return &ret->stat;
1261 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1262 catch_block_t *catch_block, statement_t *finally_statement)
1264 try_statement_t *ret;
1266 ret = new_statement(ctx, STAT_TRY, sizeof(*ret));
1267 if(!ret)
1268 return NULL;
1270 ret->try_statement = try_statement;
1271 ret->catch_block = catch_block;
1272 ret->finally_statement = finally_statement;
1274 return &ret->stat;
1277 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1279 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1281 ret->identifier = identifier;
1282 ret->next = NULL;
1284 return ret;
1287 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1289 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1291 ret->head = ret->tail = new_parameter(ctx, identifier);
1293 return ret;
1296 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1298 list->tail = list->tail->next = new_parameter(ctx, identifier);
1300 return list;
1303 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1304 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1306 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1308 ret->identifier = identifier;
1309 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1310 ret->source_elements = source_elements;
1311 ret->src_str = src_str;
1312 ret->src_len = src_len;
1313 ret->next = NULL;
1315 return &ret->expr;
1318 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1320 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1322 ret->type = type;
1324 return ret;
1327 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1328 expression_t *expression1, expression_t *expression2)
1330 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1332 ret->expression1 = expression1;
1333 ret->expression2 = expression2;
1335 return &ret->expr;
1338 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1340 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1342 ret->expression = expression;
1344 return &ret->expr;
1347 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1348 expression_t *true_expression, expression_t *false_expression)
1350 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1352 ret->expression = expression;
1353 ret->true_expression = true_expression;
1354 ret->false_expression = false_expression;
1356 return &ret->expr;
1359 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1361 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1363 ret->expression = expression;
1364 ret->identifier = identifier;
1366 return &ret->expr;
1369 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1371 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1373 ret->expression = expression;
1374 ret->argument_list = argument_list ? argument_list->head : NULL;
1376 return &ret->expr;
1379 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1381 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1383 ret->expression = expression;
1384 ret->argument_list = argument_list ? argument_list->head : NULL;
1386 return &ret->expr;
1389 static int parser_error(const char *str)
1391 return 0;
1394 static void set_error(parser_ctx_t *ctx, UINT error)
1396 ctx->hres = error;
1399 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1401 if(obj || *(ctx->ptr-1)==next) return TRUE;
1403 set_error(ctx, JS_E_SYNTAX);
1404 return FALSE;
1408 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1410 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1412 ret->identifier = identifier;
1414 return &ret->expr;
1417 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1419 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1421 ret->element_list = element_list ? element_list->head : NULL;
1422 ret->length = length;
1424 return &ret->expr;
1427 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1429 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1431 ret->property_list = property_list ? property_list->head : NULL;
1433 return &ret->expr;
1436 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1438 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1440 ret->literal = literal;
1442 return &ret->expr;
1445 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1447 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1449 memset(ret, 0, sizeof(*ret));
1451 return ret;
1454 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1456 if(source_elements->statement_tail)
1457 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1458 else
1459 source_elements->statement = source_elements->statement_tail = statement;
1461 return source_elements;
1464 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1466 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1468 ret->head = ret->tail = statement;
1470 return ret;
1473 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1475 list->tail = list->tail->next = statement;
1477 return list;
1480 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1482 ctx->source = source;
1483 if(!ctx->lexer_error)
1484 ctx->hres = S_OK;
1487 void parser_release(parser_ctx_t *ctx)
1489 script_release(ctx->script);
1490 jsheap_free(&ctx->heap);
1491 heap_free(ctx);
1494 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval,
1495 parser_ctx_t **ret)
1497 parser_ctx_t *parser_ctx;
1498 jsheap_t *mark;
1499 HRESULT hres;
1501 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1503 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1504 if(!parser_ctx)
1505 return E_OUTOFMEMORY;
1507 parser_ctx->hres = JS_E_SYNTAX;
1508 parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1510 parser_ctx->begin = parser_ctx->ptr = code;
1511 parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1513 script_addref(ctx);
1514 parser_ctx->script = ctx;
1516 mark = jsheap_mark(&ctx->tmp_heap);
1517 jsheap_init(&parser_ctx->heap);
1519 parser_parse(parser_ctx);
1520 jsheap_clear(mark);
1521 hres = parser_ctx->hres;
1522 if(FAILED(hres)) {
1523 WARN("parser failed around %s\n",
1524 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20));
1525 parser_release(parser_ctx);
1526 return hres;
1529 *ret = parser_ctx;
1530 return S_OK;