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 #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 BOOL allow_auto_semicolon(parser_ctx_t*);
33 static void program_parsed(parser_ctx_t*,source_elements_t*);
34 static source_elements_t *function_body_parsed(parser_ctx_t*,source_elements_t*);
36 typedef struct _statement_list_t {
41 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
42 static literal_t *new_null_literal(parser_ctx_t*);
43 static literal_t *new_undefined_literal(parser_ctx_t*);
44 static literal_t *new_boolean_literal(parser_ctx_t*,VARIANT_BOOL);
46 typedef struct _property_list_t {
51 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
52 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
54 typedef struct _element_list_t {
55 array_element_t *head;
56 array_element_t *tail;
59 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
60 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
62 typedef struct _argument_list_t {
67 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
68 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
70 typedef struct _case_list_t {
71 case_clausule_t *head;
72 case_clausule_t *tail;
75 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
76 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
77 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
78 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
79 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
81 typedef struct _variable_list_t {
82 variable_declaration_t *head;
83 variable_declaration_t *tail;
86 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
87 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
88 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_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_empty_statement(parser_ctx_t*);
93 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
94 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
95 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
96 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
97 expression_t*,statement_t*);
98 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
99 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
100 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
101 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
102 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
103 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
104 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
105 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
106 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
108 struct statement_list_t {
113 statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
114 statement_list_t *statement_list_add(statement_list_t*,statement_t*);
116 typedef struct _parameter_list_t {
121 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
122 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
124 static void push_func(parser_ctx_t*);
125 static inline void pop_func(parser_ctx_t *ctx)
127 ctx->func_stack = ctx->func_stack->next;
130 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
131 source_elements_t*,const WCHAR*,DWORD);
132 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
133 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
134 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
135 static expression_t *new_array_expression(parser_ctx_t*,expression_t*,expression_t*);
136 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
137 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
138 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
139 static expression_t *new_this_expression(parser_ctx_t*);
140 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
141 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
142 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
143 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
145 static function_declaration_t *new_function_declaration(parser_ctx_t*,const WCHAR*,parameter_list_t*,
146 source_elements_t*,const WCHAR*,DWORD);
147 static source_elements_t *new_source_elements(parser_ctx_t*);
148 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
149 static source_elements_t *source_elements_add_function(source_elements_t*,function_declaration_t*);
161 struct _argument_list_t *argument_list;
162 case_clausule_t *case_clausule;
163 struct _case_list_t *case_list;
164 catch_block_t *catch_block;
165 struct _element_list_t *element_list;
167 const WCHAR *identifier;
168 function_declaration_t *function_declaration;
169 struct _parameter_list_t *parameter_list;
170 struct _property_list_t *property_list;
171 source_elements_t *source_elements;
172 statement_t *statement;
173 struct _statement_list_t *statement_list;
174 struct _variable_list_t *variable_list;
175 variable_declaration_t *variable_declaration;
179 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
180 %token kINSTANCEOF kNEW kNULL kUNDEFINED kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
181 %token tANDAND tOROR tINC tDEC
183 %token <srcptr> kFUNCTION '}'
186 %token <identifier> tIdentifier
187 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
188 %token <literal> tNumericLiteral
189 %token <wstr> tStringLiteral
191 %type <source_elements> SourceElements
192 %type <source_elements> FunctionBody
193 %type <statement> Statement
194 %type <statement> Block
195 %type <statement> VariableStatement
196 %type <statement> EmptyStatement
197 %type <statement> ExpressionStatement
198 %type <statement> IfStatement
199 %type <statement> IterationStatement
200 %type <statement> ContinueStatement
201 %type <statement> BreakStatement
202 %type <statement> ReturnStatement
203 %type <statement> WithStatement
204 %type <statement> LabelledStatement
205 %type <statement> SwitchStatement
206 %type <statement> ThrowStatement
207 %type <statement> TryStatement
208 %type <statement> Finally
209 %type <statement_list> StatementList StatementList_opt
210 %type <function_declaration> FunctionDeclaration
211 %type <parameter_list> FormalParameterList FormalParameterList_opt
212 %type <expr> Expression Expression_opt
213 %type <expr> ExpressionNoIn ExpressionNoIn_opt
214 %type <expr> FunctionExpression
215 %type <expr> AssignmentExpression AssignmentExpressionNoIn
216 %type <expr> ConditionalExpression ConditionalExpressionNoIn
217 %type <expr> LeftHandSideExpression
218 %type <expr> LogicalORExpression LogicalORExpressionNoIn
219 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
220 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
221 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
222 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
223 %type <expr> EqualityExpression EqualityExpressionNoIn
224 %type <expr> RelationalExpression RelationalExpressionNoIn
225 %type <expr> ShiftExpression
226 %type <expr> AdditiveExpression
227 %type <expr> MultiplicativeExpression
228 %type <expr> Initialiser_opt Initialiser
229 %type <expr> InitialiserNoIn_opt InitialiserNoIn
230 %type <expr> UnaryExpression
231 %type <expr> PostfixExpression
232 %type <expr> NewExpression
233 %type <expr> CallExpression
234 %type <expr> MemberExpression
235 %type <expr> PrimaryExpression
236 %type <identifier> Identifier_opt
237 %type <variable_list> VariableDeclarationList
238 %type <variable_list> VariableDeclarationListNoIn
239 %type <variable_declaration> VariableDeclaration
240 %type <variable_declaration> VariableDeclarationNoIn
241 %type <case_list> CaseClausules CaseClausules_opt
242 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
243 %type <catch_block> Catch
244 %type <argument_list> Arguments
245 %type <argument_list> ArgumentList
246 %type <literal> Literal
247 %type <expr> ArrayLiteral
248 %type <expr> ObjectLiteral
249 %type <ival> Elision Elision_opt
250 %type <element_list> ElementList
251 %type <property_list> PropertyNameAndValueList
252 %type <literal> PropertyName
253 %type <literal> BooleanLiteral
254 %type <srcptr> KFunction
258 /* ECMA-262 3rd Edition 14 */
260 : SourceElements { program_parsed(ctx, $1); }
262 /* ECMA-262 3rd Edition 14 */
264 : /* empty */ { $$ = new_source_elements(ctx); }
265 | SourceElements Statement
266 { $$ = source_elements_add_statement($1, $2); }
267 | SourceElements FunctionDeclaration
268 { $$ = source_elements_add_function($1, $2); }
270 /* ECMA-262 3rd Edition 13 */
272 : KFunction tIdentifier '(' FormalParameterList_opt ')' '{' FunctionBody '}'
273 { $$ = new_function_declaration(ctx, $2, $4, $7, $1, $8-$1+1); }
275 /* ECMA-262 3rd Edition 13 */
277 : KFunction Identifier_opt '(' FormalParameterList_opt ')' '{' FunctionBody '}'
278 { $$ = new_function_expression(ctx, $2, $4, $7, $1, $8-$1+1); }
281 : kFUNCTION { push_func(ctx); $$ = $1; }
283 /* ECMA-262 3rd Edition 13 */
285 : SourceElements { $$ = function_body_parsed(ctx, $1); }
287 /* ECMA-262 3rd Edition 13 */
289 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
290 | FormalParameterList ',' tIdentifier
291 { $$ = parameter_list_add(ctx, $1, $3); }
293 /* ECMA-262 3rd Edition 13 */
294 FormalParameterList_opt
295 : /* empty */ { $$ = NULL; }
296 | FormalParameterList { $$ = $1; }
298 /* ECMA-262 3rd Edition 12 */
301 | VariableStatement { $$ = $1; }
302 | EmptyStatement { $$ = $1; }
303 | ExpressionStatement { $$ = $1; }
304 | IfStatement { $$ = $1; }
305 | IterationStatement { $$ = $1; }
306 | ContinueStatement { $$ = $1; }
307 | BreakStatement { $$ = $1; }
308 | ReturnStatement { $$ = $1; }
309 | WithStatement { $$ = $1; }
310 | LabelledStatement { $$ = $1; }
311 | SwitchStatement { $$ = $1; }
312 | ThrowStatement { $$ = $1; }
313 | TryStatement { $$ = $1; }
315 /* ECMA-262 3rd Edition 12.2 */
317 : Statement { $$ = new_statement_list(ctx, $1); }
318 | StatementList Statement
319 { $$ = statement_list_add($1, $2); }
321 /* ECMA-262 3rd Edition 12.2 */
323 : /* empty */ { $$ = NULL; }
324 | StatementList { $$ = $1; }
326 /* ECMA-262 3rd Edition 12.1 */
328 : '{' StatementList_opt '}'
329 { $$ = new_block_statement(ctx, $2); }
331 /* ECMA-262 3rd Edition 12.2 */
333 : kVAR VariableDeclarationList semicolon_opt
334 { $$ = new_var_statement(ctx, $2); }
336 /* ECMA-262 3rd Edition 12.2 */
337 VariableDeclarationList
338 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
339 | VariableDeclarationList ',' VariableDeclaration
340 { $$ = variable_list_add(ctx, $1, $3); }
342 /* ECMA-262 3rd Edition 12.2 */
343 VariableDeclarationListNoIn
344 : VariableDeclarationNoIn
345 { $$ = new_variable_list(ctx, $1); }
346 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
347 { $$ = variable_list_add(ctx, $1, $3); }
349 /* ECMA-262 3rd Edition 12.2 */
351 : tIdentifier Initialiser_opt
352 { $$ = new_variable_declaration(ctx, $1, $2); }
354 /* ECMA-262 3rd Edition 12.2 */
355 VariableDeclarationNoIn
356 : tIdentifier InitialiserNoIn_opt
357 { $$ = new_variable_declaration(ctx, $1, $2); }
359 /* ECMA-262 3rd Edition 12.2 */
361 : /* empty */ { $$ = NULL; }
362 | Initialiser { $$ = $1; }
364 /* ECMA-262 3rd Edition 12.2 */
366 : '=' AssignmentExpression
369 /* ECMA-262 3rd Edition 12.2 */
371 : /* empty */ { $$ = NULL; }
372 | InitialiserNoIn { $$ = $1; }
374 /* ECMA-262 3rd Edition 12.2 */
376 : '=' AssignmentExpressionNoIn
379 /* ECMA-262 3rd Edition 12.3 */
381 : ';' { $$ = new_empty_statement(ctx); }
383 /* ECMA-262 3rd Edition 12.4 */
385 : Expression semicolon_opt
386 { $$ = new_expression_statement(ctx, $1); }
388 /* ECMA-262 3rd Edition 12.5 */
390 : kIF '(' Expression ')' Statement kELSE Statement
391 { $$ = new_if_statement(ctx, $3, $5, $7); }
392 | kIF '(' Expression ')' Statement
393 { $$ = new_if_statement(ctx, $3, $5, NULL); }
395 /* ECMA-262 3rd Edition 12.6 */
397 : kDO Statement kWHILE '(' Expression ')' ';'
398 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
399 | kWHILE '(' Expression ')' Statement
400 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
401 | kFOR '(' ExpressionNoIn_opt ';' Expression_opt ';' Expression_opt ')' Statement
402 { $$ = new_for_statement(ctx, NULL, $3, $5, $7, $9); }
403 | kFOR '(' kVAR VariableDeclarationListNoIn ';' Expression_opt ';' Expression_opt ')' Statement
404 { $$ = new_for_statement(ctx, $4, NULL, $6, $8, $10); }
405 | kFOR '(' LeftHandSideExpression kIN Expression ')' Statement
406 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
407 | kFOR '(' kVAR VariableDeclarationNoIn kIN Expression ')' Statement
408 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
410 /* ECMA-262 3rd Edition 12.7 */
412 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
413 { $$ = new_continue_statement(ctx, $2); }
415 /* ECMA-262 3rd Edition 12.8 */
417 : kBREAK /* NONL */ Identifier_opt semicolon_opt
418 { $$ = new_break_statement(ctx, $2); }
420 /* ECMA-262 3rd Edition 12.9 */
422 : kRETURN /* NONL */ Expression_opt semicolon_opt
423 { $$ = new_return_statement(ctx, $2); }
425 /* ECMA-262 3rd Edition 12.10 */
427 : kWITH '(' Expression ')' Statement
428 { $$ = new_with_statement(ctx, $3, $5); }
430 /* ECMA-262 3rd Edition 12.12 */
432 : tIdentifier ':' Statement
433 { $$ = new_labelled_statement(ctx, $1, $3); }
435 /* ECMA-262 3rd Edition 12.11 */
437 : kSWITCH '(' Expression ')' CaseBlock
438 { $$ = new_switch_statement(ctx, $3, $5); }
440 /* ECMA-262 3rd Edition 12.11 */
442 : '{' CaseClausules_opt '}'
443 { $$ = new_case_block(ctx, $2, NULL, NULL); }
444 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
445 { $$ = new_case_block(ctx, $2, $3, $4); }
447 /* ECMA-262 3rd Edition 12.11 */
449 : /* empty */ { $$ = NULL; }
450 | CaseClausules { $$ = $1; }
452 /* ECMA-262 3rd Edition 12.11 */
454 : CaseClausule { $$ = new_case_list(ctx, $1); }
455 | CaseClausules CaseClausule
456 { $$ = case_list_add(ctx, $1, $2); }
458 /* ECMA-262 3rd Edition 12.11 */
460 : kCASE Expression ':' StatementList_opt
461 { $$ = new_case_clausule(ctx, $2, $4); }
463 /* ECMA-262 3rd Edition 12.11 */
465 : kDEFAULT ':' StatementList_opt
466 { $$ = new_case_clausule(ctx, NULL, $3); }
468 /* ECMA-262 3rd Edition 12.13 */
470 : kTHROW /* NONL */ Expression semicolon_opt
471 { $$ = new_throw_statement(ctx, $2); }
473 /* ECMA-262 3rd Edition 12.14 */
475 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
476 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
477 | kTRY Block Catch Finally
478 { $$ = new_try_statement(ctx, $2, $3, $4); }
480 /* ECMA-262 3rd Edition 12.14 */
482 : kCATCH '(' tIdentifier ')' Block
483 { $$ = new_catch_block(ctx, $3, $5); }
485 /* ECMA-262 3rd Edition 12.14 */
487 : kFINALLY Block { $$ = $2; }
489 /* ECMA-262 3rd Edition 11.14 */
491 : /* empty */ { $$ = NULL; }
492 | Expression { $$ = $1; }
494 /* ECMA-262 3rd Edition 11.14 */
496 : AssignmentExpression { $$ = $1; }
497 | Expression ',' AssignmentExpression
498 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
500 /* ECMA-262 3rd Edition 11.14 */
502 : /* empty */ { $$ = NULL; }
503 | ExpressionNoIn { $$ = $1; }
505 /* ECMA-262 3rd Edition 11.14 */
507 : AssignmentExpressionNoIn
509 | ExpressionNoIn ',' AssignmentExpressionNoIn
510 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
512 /* ECMA-262 3rd Edition 11.13 */
514 : ConditionalExpression { $$ = $1; }
515 | LeftHandSideExpression '=' AssignmentExpression
516 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
517 | LeftHandSideExpression tAssignOper AssignmentExpression
518 { $$ = new_binary_expression(ctx, $2, $1, $3); }
520 /* ECMA-262 3rd Edition 11.13 */
521 AssignmentExpressionNoIn
522 : ConditionalExpressionNoIn
524 | LeftHandSideExpression '=' AssignmentExpressionNoIn
525 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
526 | LeftHandSideExpression tAssignOper AssignmentExpressionNoIn
527 { $$ = new_binary_expression(ctx, $2, $1, $3); }
529 /* ECMA-262 3rd Edition 11.12 */
530 ConditionalExpression
531 : LogicalORExpression { $$ = $1; }
532 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
533 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
535 /* ECMA-262 3rd Edition 11.12 */
536 ConditionalExpressionNoIn
537 : LogicalORExpressionNoIn
539 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
540 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
542 /* ECMA-262 3rd Edition 11.11 */
544 : LogicalANDExpression { $$ = $1; }
545 | LogicalORExpression tOROR LogicalANDExpression
546 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
548 /* ECMA-262 3rd Edition 11.11 */
549 LogicalORExpressionNoIn
550 : LogicalANDExpressionNoIn
552 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
553 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
555 /* ECMA-262 3rd Edition 11.11 */
557 : BitwiseORExpression { $$ = $1; }
558 | LogicalANDExpression tANDAND BitwiseORExpression
559 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
561 /* ECMA-262 3rd Edition 11.11 */
562 LogicalANDExpressionNoIn
563 : BitwiseORExpressionNoIn
565 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
566 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
568 /* ECMA-262 3rd Edition 11.10 */
570 : BitwiseXORExpression { $$ = $1; }
571 | BitwiseORExpression '|' BitwiseXORExpression
572 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
574 /* ECMA-262 3rd Edition 11.10 */
575 BitwiseORExpressionNoIn
576 : BitwiseXORExpressionNoIn
578 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
579 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
581 /* ECMA-262 3rd Edition 11.10 */
583 : BitwiseANDExpression { $$ = $1; }
584 | BitwiseXORExpression '^' BitwiseANDExpression
585 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
587 /* ECMA-262 3rd Edition 11.10 */
588 BitwiseXORExpressionNoIn
589 : BitwiseANDExpressionNoIn
591 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
592 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
594 /* ECMA-262 3rd Edition 11.10 */
596 : EqualityExpression { $$ = $1; }
597 | BitwiseANDExpression '&' EqualityExpression
598 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
600 /* ECMA-262 3rd Edition 11.10 */
601 BitwiseANDExpressionNoIn
602 : EqualityExpressionNoIn
604 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
605 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
607 /* ECMA-262 3rd Edition 11.9 */
609 : RelationalExpression { $$ = $1; }
610 | EqualityExpression tEqOper RelationalExpression
611 { $$ = new_binary_expression(ctx, $2, $1, $3); }
613 /* ECMA-262 3rd Edition 11.9 */
614 EqualityExpressionNoIn
615 : RelationalExpressionNoIn { $$ = $1; }
616 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
617 { $$ = new_binary_expression(ctx, $2, $1, $3); }
619 /* ECMA-262 3rd Edition 11.8 */
621 : ShiftExpression { $$ = $1; }
622 | RelationalExpression tRelOper ShiftExpression
623 { $$ = new_binary_expression(ctx, $2, $1, $3); }
624 | RelationalExpression kINSTANCEOF ShiftExpression
625 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
626 | RelationalExpression kIN ShiftExpression
627 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
629 /* ECMA-262 3rd Edition 11.8 */
630 RelationalExpressionNoIn
631 : ShiftExpression { $$ = $1; }
632 | RelationalExpressionNoIn tRelOper ShiftExpression
633 { $$ = new_binary_expression(ctx, $2, $1, $3); }
634 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
635 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
637 /* ECMA-262 3rd Edition 11.7 */
639 : AdditiveExpression { $$ = $1; }
640 | ShiftExpression tShiftOper AdditiveExpression
641 { $$ = new_binary_expression(ctx, $2, $1, $3); }
643 /* ECMA-262 3rd Edition 11.6 */
645 : MultiplicativeExpression
647 | AdditiveExpression '+' MultiplicativeExpression
648 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
649 | AdditiveExpression '-' MultiplicativeExpression
650 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
652 /* ECMA-262 3rd Edition 11.5 */
653 MultiplicativeExpression
654 : UnaryExpression { $$ = $1; }
655 | MultiplicativeExpression '*' UnaryExpression
656 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
657 | MultiplicativeExpression '/' UnaryExpression
658 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
659 | MultiplicativeExpression '%' UnaryExpression
660 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
662 /* ECMA-262 3rd Edition 11.4 */
664 : PostfixExpression { $$ = $1; }
665 | kDELETE UnaryExpression
666 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
667 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
668 | kTYPEOF UnaryExpression
669 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
670 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
671 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
672 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
673 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
674 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
675 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
677 /* ECMA-262 3rd Edition 11.2 */
679 : LeftHandSideExpression
681 | LeftHandSideExpression /* NONL */ tINC
682 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
683 | LeftHandSideExpression /* NONL */ tDEC
684 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
687 /* ECMA-262 3rd Edition 11.2 */
688 LeftHandSideExpression
689 : NewExpression { $$ = $1; }
690 | CallExpression { $$ = $1; }
692 /* ECMA-262 3rd Edition 11.2 */
694 : MemberExpression { $$ = $1; }
695 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
697 /* ECMA-262 3rd Edition 11.2 */
699 : PrimaryExpression { $$ = $1; }
700 | FunctionExpression { $$ = $1; }
701 | MemberExpression '[' Expression ']'
702 { $$ = new_array_expression(ctx, $1, $3); }
703 | MemberExpression '.' tIdentifier
704 { $$ = new_member_expression(ctx, $1, $3); }
705 | kNEW MemberExpression Arguments
706 { $$ = new_new_expression(ctx, $2, $3); }
708 /* ECMA-262 3rd Edition 11.2 */
710 : MemberExpression Arguments
711 { $$ = new_call_expression(ctx, $1, $2); }
712 | CallExpression Arguments
713 { $$ = new_call_expression(ctx, $1, $2); }
714 | CallExpression '[' Expression ']'
715 { $$ = new_array_expression(ctx, $1, $3); }
716 | CallExpression '.' tIdentifier
717 { $$ = new_member_expression(ctx, $1, $3); }
719 /* ECMA-262 3rd Edition 11.2 */
721 : '(' ')' { $$ = NULL; }
722 | '(' ArgumentList ')' { $$ = $2; }
724 /* ECMA-262 3rd Edition 11.2 */
726 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
727 | ArgumentList ',' AssignmentExpression
728 { $$ = argument_list_add(ctx, $1, $3); }
730 /* ECMA-262 3rd Edition 11.1 */
732 : kTHIS { $$ = new_this_expression(ctx); }
733 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
734 | Literal { $$ = new_literal_expression(ctx, $1); }
735 | ArrayLiteral { $$ = $1; }
736 | ObjectLiteral { $$ = $1; }
737 | '(' Expression ')' { $$ = $2; }
739 /* ECMA-262 3rd Edition 11.1.4 */
741 : '[' Elision_opt ']' { $$ = new_array_literal_expression(ctx, NULL, $2); }
742 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
743 | '[' ElementList ',' Elision_opt ']'
744 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
746 /* ECMA-262 3rd Edition 11.1.4 */
748 : Elision_opt AssignmentExpression
749 { $$ = new_element_list(ctx, $1, $2); }
750 | ElementList ',' Elision_opt AssignmentExpression
751 { $$ = element_list_add(ctx, $1, $3, $4); }
753 /* ECMA-262 3rd Edition 11.1.4 */
756 | Elision ',' { $$ = $1 + 1; }
758 /* ECMA-262 3rd Edition 11.1.4 */
760 : /* empty */ { $$ = 0; }
761 | Elision { $$ = $1; }
763 /* ECMA-262 3rd Edition 11.1.5 */
765 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
766 | '{' PropertyNameAndValueList '}'
767 { $$ = new_prop_and_value_expression(ctx, $2); }
769 /* ECMA-262 3rd Edition 11.1.5 */
770 PropertyNameAndValueList
771 : PropertyName ':' AssignmentExpression
772 { $$ = new_property_list(ctx, $1, $3); }
773 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
774 { $$ = property_list_add(ctx, $1, $3, $5); }
776 /* ECMA-262 3rd Edition 11.1.5 */
778 : tIdentifier { $$ = new_string_literal(ctx, $1); }
779 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
780 | tNumericLiteral { $$ = $1; }
782 /* ECMA-262 3rd Edition 7.6 */
784 : /* empty*/ { $$ = NULL; }
785 | tIdentifier { $$ = $1; }
787 /* ECMA-262 3rd Edition 7.8 */
789 : kNULL { $$ = new_null_literal(ctx); }
790 | kUNDEFINED { $$ = new_undefined_literal(ctx); }
791 | BooleanLiteral { $$ = $1; }
792 | tNumericLiteral { $$ = $1; }
793 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
794 | '/' { $$ = parse_regexp(ctx);
797 /* ECMA-262 3rd Edition 7.8.2 */
799 : kTRUE { $$ = new_boolean_literal(ctx, TRUE); }
800 | kFALSE { $$ = new_boolean_literal(ctx, FALSE); }
804 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
808 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
810 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
813 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
815 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
823 static literal_t *new_null_literal(parser_ctx_t *ctx)
825 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
832 static literal_t *new_undefined_literal(parser_ctx_t *ctx)
834 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
841 static literal_t *new_boolean_literal(parser_ctx_t *ctx, VARIANT_BOOL bval)
843 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
851 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
853 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
862 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
864 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
866 ret->head = ret->tail = new_prop_val(ctx, name, value);
871 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
873 list->tail = list->tail->next = new_prop_val(ctx, name, value);
878 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
880 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
882 ret->elision = elision;
889 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
891 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
893 ret->head = ret->tail = new_array_element(ctx, elision, expr);
898 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
900 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
905 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
907 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
915 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
917 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
919 ret->head = ret->tail = new_argument(ctx, expr);
924 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
926 list->tail = list->tail->next = new_argument(ctx, expr);
931 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
933 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
935 ret->identifier = identifier;
936 ret->statement = statement;
941 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
943 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
946 ret->stat = stat_list ? stat_list->head : NULL;
952 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
954 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
956 ret->head = ret->tail = case_clausule;
961 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
963 list->tail = list->tail->next = case_clausule;
968 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
969 case_clausule_t *default_clausule, case_list_t *case_list2)
971 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
972 statement_t *stat = NULL;
975 ret = case_list1->head;
976 iter = case_list1->tail;
979 if(default_clausule) {
981 iter = iter->next = default_clausule;
983 ret = iter = default_clausule;
988 iter->next = case_list2->head;
990 ret = case_list2->head;
996 for(iter = ret; iter; iter = iter->next) {
997 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1001 while(iter != iter2) {
1002 iter->stat = iter2->stat;
1009 stat->next = iter->stat;
1018 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1020 block_statement_t *ret = parser_alloc(ctx, sizeof(block_statement_t));
1022 ret->stat.eval = block_statement_eval;
1023 ret->stat.next = NULL;
1024 ret->stat_list = list ? list->head : NULL;
1029 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1031 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1032 var_list_t *var_list = parser_alloc(ctx, sizeof(var_list_t));
1034 ret->identifier = identifier;
1038 var_list->identifier = identifier;
1039 var_list->next = NULL;
1041 if(ctx->func_stack->var_tail)
1042 ctx->func_stack->var_tail = ctx->func_stack->var_tail->next = var_list;
1044 ctx->func_stack->var_head = ctx->func_stack->var_tail = var_list;
1049 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1051 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1053 ret->head = ret->tail = decl;
1058 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1060 list->tail = list->tail->next = decl;
1065 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1067 var_statement_t *ret = parser_alloc(ctx, sizeof(var_statement_t));
1069 ret->stat.eval = var_statement_eval;
1070 ret->stat.next = NULL;
1071 ret->variable_list = variable_list->head;
1076 static statement_t *new_empty_statement(parser_ctx_t *ctx)
1078 statement_t *ret = parser_alloc(ctx, sizeof(statement_t));
1080 ret->eval = empty_statement_eval;
1086 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1088 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1090 ret->stat.eval = expression_statement_eval;
1091 ret->stat.next = NULL;
1097 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1099 if_statement_t *ret = parser_alloc(ctx, sizeof(if_statement_t));
1101 ret->stat.eval = if_statement_eval;
1102 ret->stat.next = NULL;
1104 ret->if_stat = if_stat;
1105 ret->else_stat = else_stat;
1110 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1112 while_statement_t *ret = parser_alloc(ctx, sizeof(while_statement_t));
1114 ret->stat.eval = while_statement_eval;
1115 ret->stat.next = NULL;
1116 ret->do_while = dowhile;
1118 ret->statement = stat;
1123 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1124 expression_t *expr, expression_t *end_expr, statement_t *statement)
1126 for_statement_t *ret = parser_alloc(ctx, sizeof(for_statement_t));
1128 ret->stat.eval = for_statement_eval;
1129 ret->stat.next = NULL;
1130 ret->variable_list = variable_list ? variable_list->head : NULL;
1131 ret->begin_expr = begin_expr;
1133 ret->end_expr = end_expr;
1134 ret->statement = statement;
1139 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1140 expression_t *in_expr, statement_t *statement)
1142 forin_statement_t *ret = parser_alloc(ctx, sizeof(forin_statement_t));
1144 ret->stat.eval = forin_statement_eval;
1145 ret->stat.next = NULL;
1146 ret->variable = variable;
1148 ret->in_expr = in_expr;
1149 ret->statement = statement;
1154 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1156 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1158 ret->stat.eval = continue_statement_eval;
1159 ret->stat.next = NULL;
1160 ret->identifier = identifier;
1165 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1167 branch_statement_t *ret = parser_alloc(ctx, sizeof(branch_statement_t));
1169 ret->stat.eval = break_statement_eval;
1170 ret->stat.next = NULL;
1171 ret->identifier = identifier;
1176 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1178 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1180 ret->stat.eval = return_statement_eval;
1181 ret->stat.next = NULL;
1187 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1189 with_statement_t *ret = parser_alloc(ctx, sizeof(with_statement_t));
1191 ret->stat.eval = with_statement_eval;
1192 ret->stat.next = NULL;
1194 ret->statement = statement;
1199 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1201 labelled_statement_t *ret = parser_alloc(ctx, sizeof(labelled_statement_t));
1203 ret->stat.eval = labelled_statement_eval;
1204 ret->stat.next = NULL;
1205 ret->identifier = identifier;
1206 ret->statement = statement;
1211 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1213 switch_statement_t *ret = parser_alloc(ctx, sizeof(switch_statement_t));
1215 ret->stat.eval = switch_statement_eval;
1216 ret->stat.next = NULL;
1218 ret->case_list = case_list;
1223 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1225 expression_statement_t *ret = parser_alloc(ctx, sizeof(expression_statement_t));
1227 ret->stat.eval = throw_statement_eval;
1228 ret->stat.next = NULL;
1234 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1235 catch_block_t *catch_block, statement_t *finally_statement)
1237 try_statement_t *ret = parser_alloc(ctx, sizeof(try_statement_t));
1239 ret->stat.eval = try_statement_eval;
1240 ret->stat.next = NULL;
1241 ret->try_statement = try_statement;
1242 ret->catch_block = catch_block;
1243 ret->finally_statement = finally_statement;
1248 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1250 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1252 ret->identifier = identifier;
1258 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1260 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1262 ret->head = ret->tail = new_parameter(ctx, identifier);
1267 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1269 list->tail = list->tail->next = new_parameter(ctx, identifier);
1274 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier,
1275 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1277 function_expression_t *ret = parser_alloc(ctx, sizeof(function_expression_t));
1279 ret->expr.eval = function_expression_eval;
1280 ret->identifier = identifier;
1281 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1282 ret->source_elements = source_elements;
1283 ret->src_str = src_str;
1284 ret->src_len = src_len;
1289 static const expression_eval_t expression_eval_table[] = {
1290 comma_expression_eval,
1291 logical_or_expression_eval,
1292 logical_and_expression_eval,
1293 binary_or_expression_eval,
1294 binary_xor_expression_eval,
1295 binary_and_expression_eval,
1296 instanceof_expression_eval,
1298 add_expression_eval,
1299 sub_expression_eval,
1300 mul_expression_eval,
1301 div_expression_eval,
1302 mod_expression_eval,
1303 delete_expression_eval,
1304 void_expression_eval,
1305 typeof_expression_eval,
1306 minus_expression_eval,
1307 plus_expression_eval,
1308 post_increment_expression_eval,
1309 post_decrement_expression_eval,
1310 pre_increment_expression_eval,
1311 pre_decrement_expression_eval,
1312 equal_expression_eval,
1313 equal2_expression_eval,
1314 not_equal_expression_eval,
1315 not_equal2_expression_eval,
1316 less_expression_eval,
1317 lesseq_expression_eval,
1318 greater_expression_eval,
1319 greatereq_expression_eval,
1320 binary_negation_expression_eval,
1321 logical_negation_expression_eval,
1322 left_shift_expression_eval,
1323 right_shift_expression_eval,
1324 right2_shift_expression_eval,
1325 assign_expression_eval,
1326 assign_lshift_expression_eval,
1327 assign_rshift_expression_eval,
1328 assign_rrshift_expression_eval,
1329 assign_add_expression_eval,
1330 assign_sub_expression_eval,
1331 assign_mul_expression_eval,
1332 assign_div_expression_eval,
1333 assign_mod_expression_eval,
1334 assign_and_expression_eval,
1335 assign_or_expression_eval,
1336 assign_xor_expression_eval,
1339 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1340 expression_t *expression1, expression_t *expression2)
1342 binary_expression_t *ret = parser_alloc(ctx, sizeof(binary_expression_t));
1344 ret->expr.eval = expression_eval_table[type];
1345 ret->expression1 = expression1;
1346 ret->expression2 = expression2;
1351 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1353 unary_expression_t *ret = parser_alloc(ctx, sizeof(unary_expression_t));
1355 ret->expr.eval = expression_eval_table[type];
1356 ret->expression = expression;
1361 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1362 expression_t *true_expression, expression_t *false_expression)
1364 conditional_expression_t *ret = parser_alloc(ctx, sizeof(conditional_expression_t));
1366 ret->expr.eval = conditional_expression_eval;
1367 ret->expression = expression;
1368 ret->true_expression = true_expression;
1369 ret->false_expression = false_expression;
1374 static expression_t *new_array_expression(parser_ctx_t *ctx, expression_t *member_expr, expression_t *expression)
1376 array_expression_t *ret = parser_alloc(ctx, sizeof(array_expression_t));
1378 ret->expr.eval = array_expression_eval;
1379 ret->member_expr = member_expr;
1380 ret->expression = expression;
1385 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1387 member_expression_t *ret = parser_alloc(ctx, sizeof(member_expression_t));
1389 ret->expr.eval = member_expression_eval;
1390 ret->expression = expression;
1391 ret->identifier = identifier;
1396 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1398 call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1400 ret->expr.eval = new_expression_eval;
1401 ret->expression = expression;
1402 ret->argument_list = argument_list ? argument_list->head : NULL;
1407 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1409 call_expression_t *ret = parser_alloc(ctx, sizeof(call_expression_t));
1411 ret->expr.eval = call_expression_eval;
1412 ret->expression = expression;
1413 ret->argument_list = argument_list ? argument_list->head : NULL;
1418 static expression_t *new_this_expression(parser_ctx_t *ctx)
1420 expression_t *ret = parser_alloc(ctx, sizeof(expression_t));
1422 ret->eval = this_expression_eval;
1427 static int parser_error(const char *str)
1432 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1434 identifier_expression_t *ret = parser_alloc(ctx, sizeof(identifier_expression_t));
1436 ret->expr.eval = identifier_expression_eval;
1437 ret->identifier = identifier;
1442 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1444 array_literal_expression_t *ret = parser_alloc(ctx, sizeof(array_literal_expression_t));
1446 ret->expr.eval = array_literal_expression_eval;
1447 ret->element_list = element_list ? element_list->head : NULL;
1448 ret->length = length;
1453 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1455 property_value_expression_t *ret = parser_alloc(ctx, sizeof(property_value_expression_t));
1457 ret->expr.eval = property_value_expression_eval;
1458 ret->property_list = property_list ? property_list->head : NULL;
1463 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1465 literal_expression_t *ret = parser_alloc(ctx, sizeof(literal_expression_t));
1467 ret->expr.eval = literal_expression_eval;
1468 ret->literal = literal;
1473 static function_declaration_t *new_function_declaration(parser_ctx_t *ctx, const WCHAR *identifier,
1474 parameter_list_t *parameter_list, source_elements_t *source_elements, const WCHAR *src_str, DWORD src_len)
1476 function_declaration_t *ret = parser_alloc(ctx, sizeof(function_declaration_t));
1478 ret->identifier = identifier;
1479 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1480 ret->source_elements = source_elements;
1481 ret->src_str = src_str;
1482 ret->src_len = src_len;
1488 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1490 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1492 memset(ret, 0, sizeof(*ret));
1497 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1499 if(source_elements->statement_tail)
1500 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1502 source_elements->statement = source_elements->statement_tail = statement;
1504 return source_elements;
1507 static source_elements_t *source_elements_add_function(source_elements_t *source_elements,
1508 function_declaration_t *function_declaration)
1510 if(source_elements->functions_tail)
1511 source_elements->functions_tail = source_elements->functions_tail->next = function_declaration;
1513 source_elements->functions = source_elements->functions_tail = function_declaration;
1515 return source_elements;
1518 statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1520 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1522 ret->head = ret->tail = statement;
1527 statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1529 list->tail = list->tail->next = statement;
1534 static void push_func(parser_ctx_t *ctx)
1536 func_stack_t *new_func = parser_alloc_tmp(ctx, sizeof(func_stack_t));
1538 new_func->var_head = new_func->var_tail = NULL;
1540 new_func->next = ctx->func_stack;
1541 ctx->func_stack = new_func;
1544 static source_elements_t *function_body_parsed(parser_ctx_t *ctx, source_elements_t *source)
1546 source->variables = ctx->func_stack->var_head;
1552 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1554 source->variables = ctx->func_stack->var_head;
1557 ctx->source = source;
1561 void parser_release(parser_ctx_t *ctx)
1563 obj_literal_t *iter;
1568 for(iter = ctx->obj_literals; iter; iter = iter->next)
1569 jsdisp_release(iter->obj);
1571 jsheap_free(&ctx->heap);
1575 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, parser_ctx_t **ret)
1577 parser_ctx_t *parser_ctx;
1581 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1583 return E_OUTOFMEMORY;
1585 parser_ctx->ref = 1;
1586 parser_ctx->hres = E_FAIL;
1588 parser_ctx->begin = parser_ctx->ptr = code;
1589 parser_ctx->end = code + strlenW(code);
1592 parser_ctx->script = ctx;
1594 mark = jsheap_mark(&ctx->tmp_heap);
1595 jsheap_init(&parser_ctx->heap);
1597 push_func(parser_ctx);
1599 parser_parse(parser_ctx);
1601 if(FAILED(parser_ctx->hres)) {
1602 hres = parser_ctx->hres;
1603 parser_release(parser_ctx);