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
24 #define YYLEX_PARAM ctx
25 #define YYPARSE_PARAM ctx
27 static int parser_error(const char*);
28 static void set_error(parser_ctx_t*,UINT);
29 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
30 static BOOL allow_auto_semicolon(parser_ctx_t*);
31 static void program_parsed(parser_ctx_t*,source_elements_t*);
32 static source_elements_t *function_body_parsed(parser_ctx_t*,source_elements_t*);
34 typedef struct _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 {
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;
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 {
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;
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;
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 statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
87 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
88 static statement_t *new_empty_statement(parser_ctx_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 {
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 {
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 push_func(parser_ctx_t*);
121 static inline void pop_func(parser_ctx_t *ctx)
123 ctx->func_stack = ctx->func_stack->next;
126 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
127 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
128 source_elements_t*,const WCHAR*,DWORD);
129 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
130 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
131 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
132 static expression_t *new_array_expression(parser_ctx_t*,expression_t*,expression_t*);
133 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
134 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
135 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
136 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
137 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
138 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
139 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
141 static source_elements_t *new_source_elements(parser_ctx_t*);
142 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
154 struct _argument_list_t *argument_list;
155 case_clausule_t *case_clausule;
156 struct _case_list_t *case_list;
157 catch_block_t *catch_block;
158 struct _element_list_t *element_list;
160 const WCHAR *identifier;
161 struct _parameter_list_t *parameter_list;
162 struct _property_list_t *property_list;
163 source_elements_t *source_elements;
164 statement_t *statement;
165 struct _statement_list_t *statement_list;
166 struct _variable_list_t *variable_list;
167 variable_declaration_t *variable_declaration;
171 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
172 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
173 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
175 %token <srcptr> kFUNCTION '}'
178 %token <identifier> tIdentifier
179 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
180 %token <literal> tNumericLiteral tBooleanLiteral
181 %token <wstr> tStringLiteral
184 %type <source_elements> SourceElements
185 %type <source_elements> FunctionBody
186 %type <statement> Statement
187 %type <statement> Block
188 %type <statement> VariableStatement
189 %type <statement> EmptyStatement
190 %type <statement> ExpressionStatement
191 %type <statement> IfStatement
192 %type <statement> IterationStatement
193 %type <statement> ContinueStatement
194 %type <statement> BreakStatement
195 %type <statement> ReturnStatement
196 %type <statement> WithStatement
197 %type <statement> LabelledStatement
198 %type <statement> SwitchStatement
199 %type <statement> ThrowStatement
200 %type <statement> TryStatement
201 %type <statement> Finally
202 %type <statement_list> StatementList StatementList_opt
203 %type <parameter_list> FormalParameterList FormalParameterList_opt
204 %type <expr> Expression Expression_opt Expression_err
205 %type <expr> ExpressionNoIn ExpressionNoIn_opt
206 %type <expr> FunctionExpression
207 %type <expr> AssignmentExpression AssignmentExpressionNoIn
208 %type <expr> ConditionalExpression ConditionalExpressionNoIn
209 %type <expr> LeftHandSideExpression
210 %type <expr> LogicalORExpression LogicalORExpressionNoIn
211 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
212 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
213 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
214 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
215 %type <expr> EqualityExpression EqualityExpressionNoIn
216 %type <expr> RelationalExpression RelationalExpressionNoIn
217 %type <expr> ShiftExpression
218 %type <expr> AdditiveExpression
219 %type <expr> MultiplicativeExpression
220 %type <expr> Initialiser_opt Initialiser
221 %type <expr> InitialiserNoIn_opt InitialiserNoIn
222 %type <expr> UnaryExpression
223 %type <expr> PostfixExpression
224 %type <expr> NewExpression
225 %type <expr> CallExpression
226 %type <expr> MemberExpression
227 %type <expr> PrimaryExpression
228 %type <identifier> Identifier_opt
229 %type <variable_list> VariableDeclarationList
230 %type <variable_list> VariableDeclarationListNoIn
231 %type <variable_declaration> VariableDeclaration
232 %type <variable_declaration> VariableDeclarationNoIn
233 %type <case_list> CaseClausules CaseClausules_opt
234 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
235 %type <catch_block> Catch
236 %type <argument_list> Arguments
237 %type <argument_list> ArgumentList
238 %type <literal> Literal
239 %type <expr> ArrayLiteral
240 %type <expr> ObjectLiteral
241 %type <ival> Elision Elision_opt
242 %type <element_list> ElementList
243 %type <property_list> PropertyNameAndValueList
244 %type <literal> PropertyName
245 %type <literal> BooleanLiteral
246 %type <srcptr> KFunction
247 %type <ival> AssignOper
249 %nonassoc LOWER_THAN_ELSE
254 /* ECMA-262 3rd Edition 14 */
256 : SourceElements HtmlComment tEOF
257 { program_parsed(ctx, $1); }
263 /* ECMA-262 3rd Edition 14 */
265 : /* empty */ { $$ = new_source_elements(ctx); }
266 | SourceElements Statement
267 { $$ = source_elements_add_statement($1, $2); }
269 /* ECMA-262 3rd Edition 13 */
271 : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
272 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
275 : kFUNCTION { push_func(ctx); $$ = $1; }
277 /* ECMA-262 3rd Edition 13 */
279 : SourceElements { $$ = function_body_parsed(ctx, $1); }
281 /* ECMA-262 3rd Edition 13 */
283 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
284 | FormalParameterList ',' tIdentifier
285 { $$ = parameter_list_add(ctx, $1, $3); }
287 /* ECMA-262 3rd Edition 13 */
288 FormalParameterList_opt
289 : /* empty */ { $$ = NULL; }
290 | FormalParameterList { $$ = $1; }
292 /* ECMA-262 3rd Edition 12 */
295 | VariableStatement { $$ = $1; }
296 | EmptyStatement { $$ = $1; }
297 | FunctionExpression { $$ = new_empty_statement(ctx); } /* FIXME: return NULL */
298 | ExpressionStatement { $$ = $1; }
299 | IfStatement { $$ = $1; }
300 | IterationStatement { $$ = $1; }
301 | ContinueStatement { $$ = $1; }
302 | BreakStatement { $$ = $1; }
303 | ReturnStatement { $$ = $1; }
304 | WithStatement { $$ = $1; }
305 | LabelledStatement { $$ = $1; }
306 | SwitchStatement { $$ = $1; }
307 | ThrowStatement { $$ = $1; }
308 | TryStatement { $$ = $1; }
310 /* ECMA-262 3rd Edition 12.2 */
312 : Statement { $$ = new_statement_list(ctx, $1); }
313 | StatementList Statement
314 { $$ = statement_list_add($1, $2); }
316 /* ECMA-262 3rd Edition 12.2 */
318 : /* empty */ { $$ = NULL; }
319 | StatementList { $$ = $1; }
321 /* ECMA-262 3rd Edition 12.1 */
323 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
324 | '{' '}' { $$ = new_block_statement(ctx, NULL); }
326 /* ECMA-262 3rd Edition 12.2 */
328 : kVAR VariableDeclarationList semicolon_opt
329 { $$ = new_var_statement(ctx, $2); }
331 /* ECMA-262 3rd Edition 12.2 */
332 VariableDeclarationList
333 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
334 | VariableDeclarationList ',' VariableDeclaration
335 { $$ = variable_list_add(ctx, $1, $3); }
337 /* ECMA-262 3rd Edition 12.2 */
338 VariableDeclarationListNoIn
339 : VariableDeclarationNoIn
340 { $$ = new_variable_list(ctx, $1); }
341 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
342 { $$ = variable_list_add(ctx, $1, $3); }
344 /* ECMA-262 3rd Edition 12.2 */
346 : tIdentifier Initialiser_opt
347 { $$ = new_variable_declaration(ctx, $1, $2); }
349 /* ECMA-262 3rd Edition 12.2 */
350 VariableDeclarationNoIn
351 : tIdentifier InitialiserNoIn_opt
352 { $$ = new_variable_declaration(ctx, $1, $2); }
354 /* ECMA-262 3rd Edition 12.2 */
356 : /* empty */ { $$ = NULL; }
357 | Initialiser { $$ = $1; }
359 /* ECMA-262 3rd Edition 12.2 */
361 : '=' AssignmentExpression
364 /* ECMA-262 3rd Edition 12.2 */
366 : /* empty */ { $$ = NULL; }
367 | InitialiserNoIn { $$ = $1; }
369 /* ECMA-262 3rd Edition 12.2 */
371 : '=' AssignmentExpressionNoIn
374 /* ECMA-262 3rd Edition 12.3 */
376 : ';' { $$ = new_empty_statement(ctx); }
378 /* ECMA-262 3rd Edition 12.4 */
380 : Expression semicolon_opt
381 { $$ = new_expression_statement(ctx, $1); }
383 /* ECMA-262 3rd Edition 12.5 */
385 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
386 { $$ = new_if_statement(ctx, $3, $5, $7); }
387 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
388 { $$ = new_if_statement(ctx, $3, $5, NULL); }
390 /* ECMA-262 3rd Edition 12.6 */
392 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
393 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
394 | kWHILE left_bracket Expression_err right_bracket Statement
395 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
396 | kFOR left_bracket ExpressionNoIn_opt
397 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
398 semicolon Expression_opt
399 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
400 semicolon Expression_opt right_bracket Statement
401 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
402 | kFOR left_bracket kVAR VariableDeclarationListNoIn
403 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
404 semicolon Expression_opt
405 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
406 semicolon Expression_opt right_bracket Statement
407 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
408 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
409 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
410 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
411 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
413 /* ECMA-262 3rd Edition 12.7 */
415 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
416 { $$ = new_continue_statement(ctx, $2); }
418 /* ECMA-262 3rd Edition 12.8 */
420 : kBREAK /* NONL */ Identifier_opt semicolon_opt
421 { $$ = new_break_statement(ctx, $2); }
423 /* ECMA-262 3rd Edition 12.9 */
425 : kRETURN /* NONL */ Expression_opt semicolon_opt
426 { $$ = new_return_statement(ctx, $2); }
428 /* ECMA-262 3rd Edition 12.10 */
430 : kWITH left_bracket Expression right_bracket Statement
431 { $$ = new_with_statement(ctx, $3, $5); }
433 /* ECMA-262 3rd Edition 12.12 */
435 : tIdentifier ':' Statement
436 { $$ = new_labelled_statement(ctx, $1, $3); }
438 /* ECMA-262 3rd Edition 12.11 */
440 : kSWITCH left_bracket Expression right_bracket CaseBlock
441 { $$ = new_switch_statement(ctx, $3, $5); }
443 /* ECMA-262 3rd Edition 12.11 */
445 : '{' CaseClausules_opt '}'
446 { $$ = new_case_block(ctx, $2, NULL, NULL); }
447 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
448 { $$ = new_case_block(ctx, $2, $3, $4); }
450 /* ECMA-262 3rd Edition 12.11 */
452 : /* empty */ { $$ = NULL; }
453 | CaseClausules { $$ = $1; }
455 /* ECMA-262 3rd Edition 12.11 */
457 : CaseClausule { $$ = new_case_list(ctx, $1); }
458 | CaseClausules CaseClausule
459 { $$ = case_list_add(ctx, $1, $2); }
461 /* ECMA-262 3rd Edition 12.11 */
463 : kCASE Expression ':' StatementList_opt
464 { $$ = new_case_clausule(ctx, $2, $4); }
466 /* ECMA-262 3rd Edition 12.11 */
468 : kDEFAULT ':' StatementList_opt
469 { $$ = new_case_clausule(ctx, NULL, $3); }
471 /* ECMA-262 3rd Edition 12.13 */
473 : kTHROW /* NONL */ Expression semicolon_opt
474 { $$ = new_throw_statement(ctx, $2); }
476 /* ECMA-262 3rd Edition 12.14 */
478 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
479 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
480 | kTRY Block Catch Finally
481 { $$ = new_try_statement(ctx, $2, $3, $4); }
483 /* ECMA-262 3rd Edition 12.14 */
485 : kCATCH left_bracket tIdentifier right_bracket Block
486 { $$ = new_catch_block(ctx, $3, $5); }
488 /* ECMA-262 3rd Edition 12.14 */
490 : kFINALLY Block { $$ = $2; }
492 /* ECMA-262 3rd Edition 11.14 */
494 : /* empty */ { $$ = NULL; }
495 | Expression { $$ = $1; }
498 : Expression { $$ = $1; }
499 | error { set_error(ctx, JS_E_SYNTAX); YYABORT; }
501 /* ECMA-262 3rd Edition 11.14 */
503 : AssignmentExpression { $$ = $1; }
504 | Expression ',' AssignmentExpression
505 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
507 /* ECMA-262 3rd Edition 11.14 */
509 : /* empty */ { $$ = NULL; }
510 | ExpressionNoIn { $$ = $1; }
512 /* ECMA-262 3rd Edition 11.14 */
514 : AssignmentExpressionNoIn
516 | ExpressionNoIn ',' AssignmentExpressionNoIn
517 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
520 : tAssignOper { $$ = $1; }
521 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
523 /* ECMA-262 3rd Edition 11.13 */
525 : ConditionalExpression { $$ = $1; }
526 | LeftHandSideExpression '=' AssignmentExpression
527 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
528 | LeftHandSideExpression AssignOper AssignmentExpression
529 { $$ = new_binary_expression(ctx, $2, $1, $3); }
531 /* ECMA-262 3rd Edition 11.13 */
532 AssignmentExpressionNoIn
533 : ConditionalExpressionNoIn
535 | LeftHandSideExpression '=' AssignmentExpressionNoIn
536 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
537 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
538 { $$ = new_binary_expression(ctx, $2, $1, $3); }
540 /* ECMA-262 3rd Edition 11.12 */
541 ConditionalExpression
542 : LogicalORExpression { $$ = $1; }
543 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
544 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
546 /* ECMA-262 3rd Edition 11.12 */
547 ConditionalExpressionNoIn
548 : LogicalORExpressionNoIn
550 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
551 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
553 /* ECMA-262 3rd Edition 11.11 */
555 : LogicalANDExpression { $$ = $1; }
556 | LogicalORExpression tOROR LogicalANDExpression
557 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
559 /* ECMA-262 3rd Edition 11.11 */
560 LogicalORExpressionNoIn
561 : LogicalANDExpressionNoIn
563 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
564 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
566 /* ECMA-262 3rd Edition 11.11 */
568 : BitwiseORExpression { $$ = $1; }
569 | LogicalANDExpression tANDAND BitwiseORExpression
570 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
572 /* ECMA-262 3rd Edition 11.11 */
573 LogicalANDExpressionNoIn
574 : BitwiseORExpressionNoIn
576 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
577 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
579 /* ECMA-262 3rd Edition 11.10 */
581 : BitwiseXORExpression { $$ = $1; }
582 | BitwiseORExpression '|' BitwiseXORExpression
583 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
585 /* ECMA-262 3rd Edition 11.10 */
586 BitwiseORExpressionNoIn
587 : BitwiseXORExpressionNoIn
589 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
590 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
592 /* ECMA-262 3rd Edition 11.10 */
594 : BitwiseANDExpression { $$ = $1; }
595 | BitwiseXORExpression '^' BitwiseANDExpression
596 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
598 /* ECMA-262 3rd Edition 11.10 */
599 BitwiseXORExpressionNoIn
600 : BitwiseANDExpressionNoIn
602 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
603 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
605 /* ECMA-262 3rd Edition 11.10 */
607 : EqualityExpression { $$ = $1; }
608 | BitwiseANDExpression '&' EqualityExpression
609 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
611 /* ECMA-262 3rd Edition 11.10 */
612 BitwiseANDExpressionNoIn
613 : EqualityExpressionNoIn
615 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
616 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
618 /* ECMA-262 3rd Edition 11.9 */
620 : RelationalExpression { $$ = $1; }
621 | EqualityExpression tEqOper RelationalExpression
622 { $$ = new_binary_expression(ctx, $2, $1, $3); }
624 /* ECMA-262 3rd Edition 11.9 */
625 EqualityExpressionNoIn
626 : RelationalExpressionNoIn { $$ = $1; }
627 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
628 { $$ = new_binary_expression(ctx, $2, $1, $3); }
630 /* ECMA-262 3rd Edition 11.8 */
632 : ShiftExpression { $$ = $1; }
633 | RelationalExpression tRelOper ShiftExpression
634 { $$ = new_binary_expression(ctx, $2, $1, $3); }
635 | RelationalExpression kINSTANCEOF ShiftExpression
636 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
637 | RelationalExpression kIN ShiftExpression
638 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
640 /* ECMA-262 3rd Edition 11.8 */
641 RelationalExpressionNoIn
642 : ShiftExpression { $$ = $1; }
643 | RelationalExpressionNoIn tRelOper ShiftExpression
644 { $$ = new_binary_expression(ctx, $2, $1, $3); }
645 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
646 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
648 /* ECMA-262 3rd Edition 11.7 */
650 : AdditiveExpression { $$ = $1; }
651 | ShiftExpression tShiftOper AdditiveExpression
652 { $$ = new_binary_expression(ctx, $2, $1, $3); }
654 /* ECMA-262 3rd Edition 11.6 */
656 : MultiplicativeExpression
658 | AdditiveExpression '+' MultiplicativeExpression
659 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
660 | AdditiveExpression '-' MultiplicativeExpression
661 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
663 /* ECMA-262 3rd Edition 11.5 */
664 MultiplicativeExpression
665 : UnaryExpression { $$ = $1; }
666 | MultiplicativeExpression '*' UnaryExpression
667 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
668 | MultiplicativeExpression '/' UnaryExpression
669 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
670 | MultiplicativeExpression '%' UnaryExpression
671 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
673 /* ECMA-262 3rd Edition 11.4 */
675 : PostfixExpression { $$ = $1; }
676 | kDELETE UnaryExpression
677 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
678 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
679 | kTYPEOF UnaryExpression
680 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
681 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
682 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
683 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
684 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
685 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
686 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
688 /* ECMA-262 3rd Edition 11.2 */
690 : LeftHandSideExpression
692 | LeftHandSideExpression /* NONL */ tINC
693 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
694 | LeftHandSideExpression /* NONL */ tDEC
695 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
698 /* ECMA-262 3rd Edition 11.2 */
699 LeftHandSideExpression
700 : NewExpression { $$ = $1; }
701 | CallExpression { $$ = $1; }
703 /* ECMA-262 3rd Edition 11.2 */
705 : MemberExpression { $$ = $1; }
706 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
708 /* ECMA-262 3rd Edition 11.2 */
710 : PrimaryExpression { $$ = $1; }
711 | FunctionExpression { $$ = $1; }
712 | MemberExpression '[' Expression ']'
713 { $$ = new_array_expression(ctx, $1, $3); }
714 | MemberExpression '.' tIdentifier
715 { $$ = new_member_expression(ctx, $1, $3); }
716 | kNEW MemberExpression Arguments
717 { $$ = new_new_expression(ctx, $2, $3); }
719 /* ECMA-262 3rd Edition 11.2 */
721 : MemberExpression Arguments
722 { $$ = new_call_expression(ctx, $1, $2); }
723 | CallExpression Arguments
724 { $$ = new_call_expression(ctx, $1, $2); }
725 | CallExpression '[' Expression ']'
726 { $$ = new_array_expression(ctx, $1, $3); }
727 | CallExpression '.' tIdentifier
728 { $$ = new_member_expression(ctx, $1, $3); }
730 /* ECMA-262 3rd Edition 11.2 */
732 : '(' ')' { $$ = NULL; }
733 | '(' ArgumentList ')' { $$ = $2; }
735 /* ECMA-262 3rd Edition 11.2 */
737 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
738 | ArgumentList ',' AssignmentExpression
739 { $$ = argument_list_add(ctx, $1, $3); }
741 /* ECMA-262 3rd Edition 11.1 */
743 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
744 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
745 | Literal { $$ = new_literal_expression(ctx, $1); }
746 | ArrayLiteral { $$ = $1; }
747 | ObjectLiteral { $$ = $1; }
748 | '(' Expression ')' { $$ = $2; }
750 /* ECMA-262 3rd Edition 11.1.4 */
752 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
753 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
754 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
755 | '[' ElementList ',' Elision_opt ']'
756 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
758 /* ECMA-262 3rd Edition 11.1.4 */
760 : Elision_opt AssignmentExpression
761 { $$ = new_element_list(ctx, $1, $2); }
762 | ElementList ',' Elision_opt AssignmentExpression
763 { $$ = element_list_add(ctx, $1, $3, $4); }
765 /* ECMA-262 3rd Edition 11.1.4 */
768 | Elision ',' { $$ = $1 + 1; }
770 /* ECMA-262 3rd Edition 11.1.4 */
772 : /* empty */ { $$ = 0; }
773 | Elision { $$ = $1; }
775 /* ECMA-262 3rd Edition 11.1.5 */
777 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
778 | '{' PropertyNameAndValueList '}'
779 { $$ = new_prop_and_value_expression(ctx, $2); }
781 /* ECMA-262 3rd Edition 11.1.5 */
782 PropertyNameAndValueList
783 : PropertyName ':' AssignmentExpression
784 { $$ = new_property_list(ctx, $1, $3); }
785 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
786 { $$ = property_list_add(ctx, $1, $3, $5); }
788 /* ECMA-262 3rd Edition 11.1.5 */
790 : tIdentifier { $$ = new_string_literal(ctx, $1); }
791 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
792 | tNumericLiteral { $$ = $1; }
794 /* ECMA-262 3rd Edition 7.6 */
796 : /* empty*/ { $$ = NULL; }
797 | tIdentifier { $$ = $1; }
799 /* ECMA-262 3rd Edition 7.8 */
801 : kNULL { $$ = new_null_literal(ctx); }
802 | BooleanLiteral { $$ = $1; }
803 | tNumericLiteral { $$ = $1; }
804 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
805 | '/' { $$ = parse_regexp(ctx);
807 | kDIVEQ { $$ = parse_regexp(ctx);
810 /* ECMA-262 3rd Edition 7.8.2 */
812 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
813 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
814 | tBooleanLiteral { $$ = $1; }
818 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
822 | error { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
826 | error { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
830 | error { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; }
834 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
836 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
839 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
841 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
843 ret->type = LT_STRING;
849 static literal_t *new_null_literal(parser_ctx_t *ctx)
851 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
858 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
860 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
869 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
871 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
873 ret->head = ret->tail = new_prop_val(ctx, name, value);
878 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
880 list->tail = list->tail->next = new_prop_val(ctx, name, value);
885 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
887 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
889 ret->elision = elision;
896 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
898 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
900 ret->head = ret->tail = new_array_element(ctx, elision, expr);
905 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
907 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
912 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
914 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
922 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
924 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
926 ret->head = ret->tail = new_argument(ctx, expr);
931 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
933 list->tail = list->tail->next = new_argument(ctx, expr);
938 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
940 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
942 ret->identifier = identifier;
943 ret->statement = statement;
948 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
950 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
953 ret->stat = stat_list ? stat_list->head : NULL;
959 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
961 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
963 ret->head = ret->tail = case_clausule;
968 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
970 list->tail = list->tail->next = case_clausule;
975 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
976 case_clausule_t *default_clausule, case_list_t *case_list2)
978 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
979 statement_t *stat = NULL;
982 ret = case_list1->head;
983 iter = case_list1->tail;
986 if(default_clausule) {
988 iter = iter->next = default_clausule;
990 ret = iter = default_clausule;
995 iter->next = case_list2->head;
997 ret = case_list2->head;
1003 for(iter = ret; iter; iter = iter->next) {
1004 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1008 while(iter != iter2) {
1009 iter->stat = iter2->stat;
1016 stat->next = iter->stat;
1025 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1027 block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1029 ret->stat.eval = block_statement_eval;
1030 ret->stat.next = NULL;
1031 ret->stat_list = list ? list->head : NULL;
1036 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1038 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1039 var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1041 ret->identifier = identifier;
1045 var_list->identifier = identifier;
1046 var_list->next = NULL;
1048 if(ctx->func_stack->var_tail)
1049 ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1051 ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1056 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1058 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1060 ret->head = ret->tail = decl;
1065 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1067 list->tail = list->tail->next = decl;
1072 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1074 var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1076 ret->stat.eval = var_statement_eval;
1077 ret->stat.next = NULL;
1078 ret->variable_list = variable_list->head;
1083 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1085 statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1087 ret->eval = empty_statement_eval;
1093 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1095 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1097 ret->stat.eval = expression_statement_eval;
1098 ret->stat.next = NULL;
1104 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1106 if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1108 ret->stat.eval = if_statement_eval;
1109 ret->stat.next = NULL;
1111 ret->if_stat = if_stat;
1112 ret->else_stat = else_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 = parser_alloc(ctx, sizeof(while_statement_t));
1121 ret->stat.eval = while_statement_eval;
1122 ret->stat.next = NULL;
1123 ret->do_while = dowhile;
1125 ret->statement = stat;
1130 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1131 expression_t *expr, expression_t *end_expr, statement_t *statement)
1133 for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1135 ret->stat.eval = for_statement_eval;
1136 ret->stat.next = NULL;
1137 ret->variable_list = variable_list ? variable_list->head : NULL;
1138 ret->begin_expr = begin_expr;
1140 ret->end_expr = end_expr;
1141 ret->statement = statement;
1146 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1147 expression_t *in_expr, statement_t *statement)
1149 forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1151 ret->stat.eval = forin_statement_eval;
1152 ret->stat.next = NULL;
1153 ret->variable = variable;
1155 ret->in_expr = in_expr;
1156 ret->statement = statement;
1161 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1163 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1165 ret->stat.eval = continue_statement_eval;
1166 ret->stat.next = NULL;
1167 ret->identifier = identifier;
1172 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1174 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1176 ret->stat.eval = break_statement_eval;
1177 ret->stat.next = NULL;
1178 ret->identifier = identifier;
1183 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1185 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1187 ret->stat.eval = return_statement_eval;
1188 ret->stat.next = NULL;
1194 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1196 with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1198 ret->stat.eval = with_statement_eval;
1199 ret->stat.next = NULL;
1201 ret->statement = statement;
1206 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1208 labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1210 ret->stat.eval = labelled_statement_eval;
1211 ret->stat.next = NULL;
1212 ret->identifier = identifier;
1213 ret->statement = statement;
1218 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1220 switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1222 ret->stat.eval = switch_statement_eval;
1223 ret->stat.next = NULL;
1225 ret->case_list = case_list;
1230 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1232 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1234 ret->stat.eval = throw_statement_eval;
1235 ret->stat.next = NULL;
1241 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1242 catch_block_t *catch_block, statement_t *finally_statement)
1244 try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1246 ret->stat.eval = try_statement_eval;
1247 ret->stat.next = NULL;
1248 ret->try_statement = try_statement;
1249 ret->catch_block = catch_block;
1250 ret->finally_statement = finally_statement;
1255 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1257 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1259 ret->identifier = identifier;
1265 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1267 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1269 ret->head = ret->tail = new_parameter(ctx, identifier);
1274 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1276 list->tail = list->tail->next = new_parameter(ctx, identifier);
1281 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1282 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1284 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1286 ret->identifier = identifier;
1287 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1288 ret->source_elements = source_elements;
1289 ret->src_str = src_str;
1290 ret->src_len = src_len;
1292 if(ret->identifier) {
1293 function_declaration_t *decl = parser_alloc(ctx, sizeof(function_declaration_t));
1298 if(ctx->func_stack->func_tail)
1299 ctx->func_stack->func_tail = ctx->func_stack->func_tail->next = decl;
1301 ctx->func_stack->func_head = ctx->func_stack->func_tail = decl;
1307 static const expression_eval_t expression_eval_table[] = {
1308 comma_expression_eval,
1309 logical_or_expression_eval,
1310 logical_and_expression_eval,
1311 binary_or_expression_eval,
1312 binary_xor_expression_eval,
1313 binary_and_expression_eval,
1314 instanceof_expression_eval,
1316 add_expression_eval,
1317 sub_expression_eval,
1318 mul_expression_eval,
1319 div_expression_eval,
1320 mod_expression_eval,
1321 delete_expression_eval,
1322 void_expression_eval,
1323 typeof_expression_eval,
1324 minus_expression_eval,
1325 plus_expression_eval,
1326 post_increment_expression_eval,
1327 post_decrement_expression_eval,
1328 pre_increment_expression_eval,
1329 pre_decrement_expression_eval,
1330 equal_expression_eval,
1331 equal2_expression_eval,
1332 not_equal_expression_eval,
1333 not_equal2_expression_eval,
1334 less_expression_eval,
1335 lesseq_expression_eval,
1336 greater_expression_eval,
1337 greatereq_expression_eval,
1338 binary_negation_expression_eval,
1339 logical_negation_expression_eval,
1340 left_shift_expression_eval,
1341 right_shift_expression_eval,
1342 right2_shift_expression_eval,
1343 assign_expression_eval,
1344 assign_lshift_expression_eval,
1345 assign_rshift_expression_eval,
1346 assign_rrshift_expression_eval,
1347 assign_add_expression_eval,
1348 assign_sub_expression_eval,
1349 assign_mul_expression_eval,
1350 assign_div_expression_eval,
1351 assign_mod_expression_eval,
1352 assign_and_expression_eval,
1353 assign_or_expression_eval,
1354 assign_xor_expression_eval,
1355 conditional_expression_eval,
1356 array_expression_eval,
1357 member_expression_eval,
1358 new_expression_eval,
1359 call_expression_eval,
1360 this_expression_eval,
1361 function_expression_eval,
1362 identifier_expression_eval,
1363 array_literal_expression_eval,
1364 property_value_expression_eval,
1365 literal_expression_eval
1368 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1370 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1372 ret->eval = expression_eval_table[type];
1377 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1378 expression_t *expression1, expression_t *expression2)
1380 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1382 ret->expression1 = expression1;
1383 ret->expression2 = expression2;
1388 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1390 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1392 ret->expression = expression;
1397 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1398 expression_t *true_expression, expression_t *false_expression)
1400 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1402 ret->expression = expression;
1403 ret->true_expression = true_expression;
1404 ret->false_expression = false_expression;
1409 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1411 array_expression_t *ret = new_expression(ctx, EXPR_ARRAY, sizeof(*ret));
1413 ret->member_expr = member_expr;
1414 ret->expression = expression;
1419 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1421 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1423 ret->expression = expression;
1424 ret->identifier = identifier;
1429 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1431 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1433 ret->expression = expression;
1434 ret->argument_list = argument_list ? argument_list->head : NULL;
1439 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1441 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1443 ret->expression = expression;
1444 ret->argument_list = argument_list ? argument_list->head : NULL;
1449 static int parser_error(const char *str)
1454 static void set_error(parser_ctx_t *ctx, UINT error)
1459 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1461 if(obj || *(ctx->ptr-1)==next) return TRUE;
1463 set_error(ctx, JS_E_SYNTAX);
1468 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1470 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1472 ret->identifier = identifier;
1477 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1479 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1481 ret->element_list = element_list ? element_list->head : NULL;
1482 ret->length = length;
1487 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1489 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1491 ret->property_list = property_list ? property_list->head : NULL;
1496 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1498 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1500 ret->literal = literal;
1505 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1507 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1509 memset(ret, 0, sizeof(*ret));
1514 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1516 if(source_elements->statement_tail)
1517 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1519 source_elements->statement = source_elements->statement_tail = statement;
1521 return source_elements;
1524 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1526 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1528 ret->head = ret->tail = statement;
1533 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1535 list->tail = list->tail->next = statement;
1540 static void push_func(parser_ctx_t *ctx)
1542 func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1544 new_func->func_head = new_func->func_tail = NULL;
1545 new_func->var_head = new_func->var_tail = NULL;
1547 new_func->next = ctx->func_stack;
1548 ctx->func_stack = new_func;
1551 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1553 source->functions = ctx->func_stack->func_head;
1554 source->variables = ctx->func_stack->var_head;
1560 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1562 source->functions = ctx->func_stack->func_head;
1563 source->variables = ctx->func_stack->var_head;
1566 ctx->source = source;
1567 if(!ctx->lexer_error)
1571 void parser_release(parser_ctx_t *ctx)
1576 script_release(ctx->script);
1577 heap_free(ctx->begin);
1578 jsheap_free(&ctx->heap);
1582 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter,
1585 parser_ctx_t *parser_ctx;
1589 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1591 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1593 return E_OUTOFMEMORY;
1595 parser_ctx->ref = 1;
1596 parser_ctx->hres = JS_E_SYNTAX;
1597 parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1599 parser_ctx->begin = heap_strdupW(code);
1600 if(!parser_ctx->begin) {
1601 heap_free(parser_ctx);
1602 return E_OUTOFMEMORY;
1605 parser_ctx->ptr = parser_ctx->begin;
1606 parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1609 parser_ctx->script = ctx;
1611 mark = jsheap_mark(&ctx->tmp_heap);
1612 jsheap_init(&parser_ctx->heap);
1614 push_func(parser_ctx);
1616 parser_parse(parser_ctx);
1618 if(FAILED(parser_ctx->hres)) {
1619 hres = parser_ctx->hres;
1620 parser_release(parser_ctx);