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 BOOL allow_auto_semicolon
(parser_ctx_t
*);
29 static void program_parsed
(parser_ctx_t
*,source_elements_t
*);
30 static source_elements_t
*function_body_parsed
(parser_ctx_t
*,source_elements_t
*);
32 typedef
struct _statement_list_t
{
37 static literal_t
*new_string_literal
(parser_ctx_t
*,const WCHAR
*);
38 static literal_t
*new_null_literal
(parser_ctx_t
*);
39 static literal_t
*new_undefined_literal
(parser_ctx_t
*);
40 static literal_t
*new_boolean_literal
(parser_ctx_t
*,VARIANT_BOOL
);
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 expression_t
*new_function_expression
(parser_ctx_t
*,const WCHAR
*,parameter_list_t
*,
127 source_elements_t
*,const WCHAR
*,DWORD
);
128 static expression_t
*new_binary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*,expression_t
*);
129 static expression_t
*new_unary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*);
130 static expression_t
*new_conditional_expression
(parser_ctx_t
*,expression_t
*,expression_t
*,expression_t
*);
131 static expression_t
*new_array_expression
(parser_ctx_t
*,expression_t
*,expression_t
*);
132 static expression_t
*new_member_expression
(parser_ctx_t
*,expression_t
*,const WCHAR
*);
133 static expression_t
*new_new_expression
(parser_ctx_t
*,expression_t
*,argument_list_t
*);
134 static expression_t
*new_call_expression
(parser_ctx_t
*,expression_t
*,argument_list_t
*);
135 static expression_t
*new_this_expression
(parser_ctx_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 kUNDEFINED kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
173 %token tANDAND tOROR tINC tDEC
175 %token
<srcptr
> kFUNCTION
'}'
178 %token
<identifier
> tIdentifier
179 %token
<ival
> tAssignOper tEqOper tShiftOper tRelOper
180 %token
<literal
> tNumericLiteral
181 %token
<wstr
> tStringLiteral
183 %type
<source_elements
> SourceElements
184 %type
<source_elements
> FunctionBody
185 %type
<statement
> Statement
186 %type
<statement
> Block
187 %type
<statement
> VariableStatement
188 %type
<statement
> EmptyStatement
189 %type
<statement
> ExpressionStatement
190 %type
<statement
> IfStatement
191 %type
<statement
> IterationStatement
192 %type
<statement
> ContinueStatement
193 %type
<statement
> BreakStatement
194 %type
<statement
> ReturnStatement
195 %type
<statement
> WithStatement
196 %type
<statement
> LabelledStatement
197 %type
<statement
> SwitchStatement
198 %type
<statement
> ThrowStatement
199 %type
<statement
> TryStatement
200 %type
<statement
> Finally
201 %type
<statement_list
> StatementList StatementList_opt
202 %type
<parameter_list
> FormalParameterList FormalParameterList_opt
203 %type
<expr
> Expression Expression_opt
204 %type
<expr
> ExpressionNoIn ExpressionNoIn_opt
205 %type
<expr
> FunctionExpression
206 %type
<expr
> AssignmentExpression AssignmentExpressionNoIn
207 %type
<expr
> ConditionalExpression ConditionalExpressionNoIn
208 %type
<expr
> LeftHandSideExpression
209 %type
<expr
> LogicalORExpression LogicalORExpressionNoIn
210 %type
<expr
> LogicalANDExpression LogicalANDExpressionNoIn
211 %type
<expr
> BitwiseORExpression BitwiseORExpressionNoIn
212 %type
<expr
> BitwiseXORExpression BitwiseXORExpressionNoIn
213 %type
<expr
> BitwiseANDExpression BitwiseANDExpressionNoIn
214 %type
<expr
> EqualityExpression EqualityExpressionNoIn
215 %type
<expr
> RelationalExpression RelationalExpressionNoIn
216 %type
<expr
> ShiftExpression
217 %type
<expr
> AdditiveExpression
218 %type
<expr
> MultiplicativeExpression
219 %type
<expr
> Initialiser_opt Initialiser
220 %type
<expr
> InitialiserNoIn_opt InitialiserNoIn
221 %type
<expr
> UnaryExpression
222 %type
<expr
> PostfixExpression
223 %type
<expr
> NewExpression
224 %type
<expr
> CallExpression
225 %type
<expr
> MemberExpression
226 %type
<expr
> PrimaryExpression
227 %type
<identifier
> Identifier_opt
228 %type
<variable_list
> VariableDeclarationList
229 %type
<variable_list
> VariableDeclarationListNoIn
230 %type
<variable_declaration
> VariableDeclaration
231 %type
<variable_declaration
> VariableDeclarationNoIn
232 %type
<case_list
> CaseClausules CaseClausules_opt
233 %type
<case_clausule
> CaseClausule DefaultClausule CaseBlock
234 %type
<catch_block
> Catch
235 %type
<argument_list
> Arguments
236 %type
<argument_list
> ArgumentList
237 %type
<literal
> Literal
238 %type
<expr
> ArrayLiteral
239 %type
<expr
> ObjectLiteral
240 %type
<ival
> Elision Elision_opt
241 %type
<element_list
> ElementList
242 %type
<property_list
> PropertyNameAndValueList
243 %type
<literal
> PropertyName
244 %type
<literal
> BooleanLiteral
245 %type
<srcptr
> KFunction
247 %nonassoc LOWER_THAN_ELSE
252 /* ECMA-262 3rd Edition 14 */
254 : SourceElements
{ program_parsed
(ctx
, $1); }
256 /* ECMA-262 3rd Edition 14 */
258 : /* empty */ { $$
= new_source_elements
(ctx
); }
259 | SourceElements Statement
260 { $$
= source_elements_add_statement
($1, $2); }
262 /* ECMA-262 3rd Edition 13 */
264 : KFunction Identifier_opt
'(' FormalParameterList_opt
')' '{' FunctionBody
'}'
265 { $$
= new_function_expression
(ctx
, $2, $4, $7, $1, $8-$1+1); }
268 : kFUNCTION
{ push_func
(ctx
); $$
= $1; }
270 /* ECMA-262 3rd Edition 13 */
272 : SourceElements
{ $$
= function_body_parsed
(ctx
, $1); }
274 /* ECMA-262 3rd Edition 13 */
276 : tIdentifier
{ $$
= new_parameter_list
(ctx
, $1); }
277 | FormalParameterList
',' tIdentifier
278 { $$
= parameter_list_add
(ctx
, $1, $3); }
280 /* ECMA-262 3rd Edition 13 */
281 FormalParameterList_opt
282 : /* empty */ { $$
= NULL
; }
283 | FormalParameterList
{ $$
= $1; }
285 /* ECMA-262 3rd Edition 12 */
288 | VariableStatement
{ $$
= $1; }
289 | EmptyStatement
{ $$
= $1; }
290 | ExpressionStatement
{ $$
= $1; }
291 | IfStatement
{ $$
= $1; }
292 | IterationStatement
{ $$
= $1; }
293 | ContinueStatement
{ $$
= $1; }
294 | BreakStatement
{ $$
= $1; }
295 | ReturnStatement
{ $$
= $1; }
296 | WithStatement
{ $$
= $1; }
297 | LabelledStatement
{ $$
= $1; }
298 | SwitchStatement
{ $$
= $1; }
299 | ThrowStatement
{ $$
= $1; }
300 | TryStatement
{ $$
= $1; }
302 /* ECMA-262 3rd Edition 12.2 */
304 : Statement
{ $$
= new_statement_list
(ctx
, $1); }
305 | StatementList Statement
306 { $$
= statement_list_add
($1, $2); }
308 /* ECMA-262 3rd Edition 12.2 */
310 : /* empty */ { $$
= NULL
; }
311 | StatementList
{ $$
= $1; }
313 /* ECMA-262 3rd Edition 12.1 */
315 : '{' StatementList
'}' { $$
= new_block_statement
(ctx
, $2); }
316 |
'{' '}' { $$
= new_block_statement
(ctx
, NULL
); }
318 /* ECMA-262 3rd Edition 12.2 */
320 : kVAR VariableDeclarationList semicolon_opt
321 { $$
= new_var_statement
(ctx
, $2); }
323 /* ECMA-262 3rd Edition 12.2 */
324 VariableDeclarationList
325 : VariableDeclaration
{ $$
= new_variable_list
(ctx
, $1); }
326 | VariableDeclarationList
',' VariableDeclaration
327 { $$
= variable_list_add
(ctx
, $1, $3); }
329 /* ECMA-262 3rd Edition 12.2 */
330 VariableDeclarationListNoIn
331 : VariableDeclarationNoIn
332 { $$
= new_variable_list
(ctx
, $1); }
333 | VariableDeclarationListNoIn
',' VariableDeclarationNoIn
334 { $$
= variable_list_add
(ctx
, $1, $3); }
336 /* ECMA-262 3rd Edition 12.2 */
338 : tIdentifier Initialiser_opt
339 { $$
= new_variable_declaration
(ctx
, $1, $2); }
341 /* ECMA-262 3rd Edition 12.2 */
342 VariableDeclarationNoIn
343 : tIdentifier InitialiserNoIn_opt
344 { $$
= new_variable_declaration
(ctx
, $1, $2); }
346 /* ECMA-262 3rd Edition 12.2 */
348 : /* empty */ { $$
= NULL
; }
349 | Initialiser
{ $$
= $1; }
351 /* ECMA-262 3rd Edition 12.2 */
353 : '=' AssignmentExpression
356 /* ECMA-262 3rd Edition 12.2 */
358 : /* empty */ { $$
= NULL
; }
359 | InitialiserNoIn
{ $$
= $1; }
361 /* ECMA-262 3rd Edition 12.2 */
363 : '=' AssignmentExpressionNoIn
366 /* ECMA-262 3rd Edition 12.3 */
368 : ';' { $$
= new_empty_statement
(ctx
); }
370 /* ECMA-262 3rd Edition 12.4 */
372 : Expression semicolon_opt
373 { $$
= new_expression_statement
(ctx
, $1); }
375 /* ECMA-262 3rd Edition 12.5 */
377 : kIF
'(' Expression
')' Statement kELSE Statement
378 { $$
= new_if_statement
(ctx
, $3, $5, $7); }
379 | kIF
'(' Expression
')' Statement %prec LOWER_THAN_ELSE
380 { $$
= new_if_statement
(ctx
, $3, $5, NULL
); }
382 /* ECMA-262 3rd Edition 12.6 */
384 : kDO Statement kWHILE
'(' Expression
')' ';'
385 { $$
= new_while_statement
(ctx
, TRUE
, $5, $2); }
386 | kWHILE
'(' Expression
')' Statement
387 { $$
= new_while_statement
(ctx
, FALSE
, $3, $5); }
388 | kFOR
'(' ExpressionNoIn_opt
';' Expression_opt
';' Expression_opt
')' Statement
389 { $$
= new_for_statement
(ctx
, NULL
, $3, $5, $7, $9); }
390 | kFOR
'(' kVAR VariableDeclarationListNoIn
';' Expression_opt
';' Expression_opt
')' Statement
391 { $$
= new_for_statement
(ctx
, $4, NULL
, $6, $8, $10); }
392 | kFOR
'(' LeftHandSideExpression kIN Expression
')' Statement
393 { $$
= new_forin_statement
(ctx
, NULL
, $3, $5, $7); }
394 | kFOR
'(' kVAR VariableDeclarationNoIn kIN Expression
')' Statement
395 { $$
= new_forin_statement
(ctx
, $4, NULL
, $6, $8); }
397 /* ECMA-262 3rd Edition 12.7 */
399 : kCONTINUE
/* NONL */ Identifier_opt semicolon_opt
400 { $$
= new_continue_statement
(ctx
, $2); }
402 /* ECMA-262 3rd Edition 12.8 */
404 : kBREAK
/* NONL */ Identifier_opt semicolon_opt
405 { $$
= new_break_statement
(ctx
, $2); }
407 /* ECMA-262 3rd Edition 12.9 */
409 : kRETURN
/* NONL */ Expression_opt semicolon_opt
410 { $$
= new_return_statement
(ctx
, $2); }
412 /* ECMA-262 3rd Edition 12.10 */
414 : kWITH
'(' Expression
')' Statement
415 { $$
= new_with_statement
(ctx
, $3, $5); }
417 /* ECMA-262 3rd Edition 12.12 */
419 : tIdentifier
':' Statement
420 { $$
= new_labelled_statement
(ctx
, $1, $3); }
422 /* ECMA-262 3rd Edition 12.11 */
424 : kSWITCH
'(' Expression
')' CaseBlock
425 { $$
= new_switch_statement
(ctx
, $3, $5); }
427 /* ECMA-262 3rd Edition 12.11 */
429 : '{' CaseClausules_opt
'}'
430 { $$
= new_case_block
(ctx
, $2, NULL
, NULL
); }
431 |
'{' CaseClausules_opt DefaultClausule CaseClausules_opt
'}'
432 { $$
= new_case_block
(ctx
, $2, $3, $4); }
434 /* ECMA-262 3rd Edition 12.11 */
436 : /* empty */ { $$
= NULL
; }
437 | CaseClausules
{ $$
= $1; }
439 /* ECMA-262 3rd Edition 12.11 */
441 : CaseClausule
{ $$
= new_case_list
(ctx
, $1); }
442 | CaseClausules CaseClausule
443 { $$
= case_list_add
(ctx
, $1, $2); }
445 /* ECMA-262 3rd Edition 12.11 */
447 : kCASE Expression
':' StatementList_opt
448 { $$
= new_case_clausule
(ctx
, $2, $4); }
450 /* ECMA-262 3rd Edition 12.11 */
452 : kDEFAULT
':' StatementList_opt
453 { $$
= new_case_clausule
(ctx
, NULL
, $3); }
455 /* ECMA-262 3rd Edition 12.13 */
457 : kTHROW
/* NONL */ Expression semicolon_opt
458 { $$
= new_throw_statement
(ctx
, $2); }
460 /* ECMA-262 3rd Edition 12.14 */
462 : kTRY Block Catch
{ $$
= new_try_statement
(ctx
, $2, $3, NULL
); }
463 | kTRY Block Finally
{ $$
= new_try_statement
(ctx
, $2, NULL
, $3); }
464 | kTRY Block Catch Finally
465 { $$
= new_try_statement
(ctx
, $2, $3, $4); }
467 /* ECMA-262 3rd Edition 12.14 */
469 : kCATCH
'(' tIdentifier
')' Block
470 { $$
= new_catch_block
(ctx
, $3, $5); }
472 /* ECMA-262 3rd Edition 12.14 */
474 : kFINALLY Block
{ $$
= $2; }
476 /* ECMA-262 3rd Edition 11.14 */
478 : /* empty */ { $$
= NULL
; }
479 | Expression
{ $$
= $1; }
481 /* ECMA-262 3rd Edition 11.14 */
483 : AssignmentExpression
{ $$
= $1; }
484 | Expression
',' AssignmentExpression
485 { $$
= new_binary_expression
(ctx
, EXPR_COMMA
, $1, $3); }
487 /* ECMA-262 3rd Edition 11.14 */
489 : /* empty */ { $$
= NULL
; }
490 | ExpressionNoIn
{ $$
= $1; }
492 /* ECMA-262 3rd Edition 11.14 */
494 : AssignmentExpressionNoIn
496 | ExpressionNoIn
',' AssignmentExpressionNoIn
497 { $$
= new_binary_expression
(ctx
, EXPR_COMMA
, $1, $3); }
499 /* ECMA-262 3rd Edition 11.13 */
501 : ConditionalExpression
{ $$
= $1; }
502 | LeftHandSideExpression
'=' AssignmentExpression
503 { $$
= new_binary_expression
(ctx
, EXPR_ASSIGN
, $1, $3); }
504 | LeftHandSideExpression tAssignOper AssignmentExpression
505 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
507 /* ECMA-262 3rd Edition 11.13 */
508 AssignmentExpressionNoIn
509 : ConditionalExpressionNoIn
511 | LeftHandSideExpression
'=' AssignmentExpressionNoIn
512 { $$
= new_binary_expression
(ctx
, EXPR_ASSIGN
, $1, $3); }
513 | LeftHandSideExpression tAssignOper AssignmentExpressionNoIn
514 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
516 /* ECMA-262 3rd Edition 11.12 */
517 ConditionalExpression
518 : LogicalORExpression
{ $$
= $1; }
519 | LogicalORExpression
'?' AssignmentExpression
':' AssignmentExpression
520 { $$
= new_conditional_expression
(ctx
, $1, $3, $5); }
522 /* ECMA-262 3rd Edition 11.12 */
523 ConditionalExpressionNoIn
524 : LogicalORExpressionNoIn
526 | LogicalORExpressionNoIn
'?' AssignmentExpressionNoIn
':' AssignmentExpressionNoIn
527 { $$
= new_conditional_expression
(ctx
, $1, $3, $5); }
529 /* ECMA-262 3rd Edition 11.11 */
531 : LogicalANDExpression
{ $$
= $1; }
532 | LogicalORExpression tOROR LogicalANDExpression
533 { $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); }
535 /* ECMA-262 3rd Edition 11.11 */
536 LogicalORExpressionNoIn
537 : LogicalANDExpressionNoIn
539 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
540 { $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); }
542 /* ECMA-262 3rd Edition 11.11 */
544 : BitwiseORExpression
{ $$
= $1; }
545 | LogicalANDExpression tANDAND BitwiseORExpression
546 { $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); }
548 /* ECMA-262 3rd Edition 11.11 */
549 LogicalANDExpressionNoIn
550 : BitwiseORExpressionNoIn
552 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
553 { $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); }
555 /* ECMA-262 3rd Edition 11.10 */
557 : BitwiseXORExpression
{ $$
= $1; }
558 | BitwiseORExpression
'|' BitwiseXORExpression
559 { $$
= new_binary_expression
(ctx
, EXPR_BOR
, $1, $3); }
561 /* ECMA-262 3rd Edition 11.10 */
562 BitwiseORExpressionNoIn
563 : BitwiseXORExpressionNoIn
565 | BitwiseORExpressionNoIn
'|' BitwiseXORExpressionNoIn
566 { $$
= new_binary_expression
(ctx
, EXPR_BOR
, $1, $3); }
568 /* ECMA-262 3rd Edition 11.10 */
570 : BitwiseANDExpression
{ $$
= $1; }
571 | BitwiseXORExpression
'^' BitwiseANDExpression
572 { $$
= new_binary_expression
(ctx
, EXPR_BXOR
, $1, $3); }
574 /* ECMA-262 3rd Edition 11.10 */
575 BitwiseXORExpressionNoIn
576 : BitwiseANDExpressionNoIn
578 | BitwiseXORExpressionNoIn
'^' BitwiseANDExpressionNoIn
579 { $$
= new_binary_expression
(ctx
, EXPR_BXOR
, $1, $3); }
581 /* ECMA-262 3rd Edition 11.10 */
583 : EqualityExpression
{ $$
= $1; }
584 | BitwiseANDExpression
'&' EqualityExpression
585 { $$
= new_binary_expression
(ctx
, EXPR_BAND
, $1, $3); }
587 /* ECMA-262 3rd Edition 11.10 */
588 BitwiseANDExpressionNoIn
589 : EqualityExpressionNoIn
591 | BitwiseANDExpressionNoIn
'&' EqualityExpressionNoIn
592 { $$
= new_binary_expression
(ctx
, EXPR_BAND
, $1, $3); }
594 /* ECMA-262 3rd Edition 11.9 */
596 : RelationalExpression
{ $$
= $1; }
597 | EqualityExpression tEqOper RelationalExpression
598 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
600 /* ECMA-262 3rd Edition 11.9 */
601 EqualityExpressionNoIn
602 : RelationalExpressionNoIn
{ $$
= $1; }
603 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
604 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
606 /* ECMA-262 3rd Edition 11.8 */
608 : ShiftExpression
{ $$
= $1; }
609 | RelationalExpression tRelOper ShiftExpression
610 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
611 | RelationalExpression kINSTANCEOF ShiftExpression
612 { $$
= new_binary_expression
(ctx
, EXPR_INSTANCEOF
, $1, $3); }
613 | RelationalExpression kIN ShiftExpression
614 { $$
= new_binary_expression
(ctx
, EXPR_IN
, $1, $3); }
616 /* ECMA-262 3rd Edition 11.8 */
617 RelationalExpressionNoIn
618 : ShiftExpression
{ $$
= $1; }
619 | RelationalExpressionNoIn tRelOper ShiftExpression
620 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
621 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
622 { $$
= new_binary_expression
(ctx
, EXPR_INSTANCEOF
, $1, $3); }
624 /* ECMA-262 3rd Edition 11.7 */
626 : AdditiveExpression
{ $$
= $1; }
627 | ShiftExpression tShiftOper AdditiveExpression
628 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
630 /* ECMA-262 3rd Edition 11.6 */
632 : MultiplicativeExpression
634 | AdditiveExpression
'+' MultiplicativeExpression
635 { $$
= new_binary_expression
(ctx
, EXPR_ADD
, $1, $3); }
636 | AdditiveExpression
'-' MultiplicativeExpression
637 { $$
= new_binary_expression
(ctx
, EXPR_SUB
, $1, $3); }
639 /* ECMA-262 3rd Edition 11.5 */
640 MultiplicativeExpression
641 : UnaryExpression
{ $$
= $1; }
642 | MultiplicativeExpression
'*' UnaryExpression
643 { $$
= new_binary_expression
(ctx
, EXPR_MUL
, $1, $3); }
644 | MultiplicativeExpression
'/' UnaryExpression
645 { $$
= new_binary_expression
(ctx
, EXPR_DIV
, $1, $3); }
646 | MultiplicativeExpression
'%' UnaryExpression
647 { $$
= new_binary_expression
(ctx
, EXPR_MOD
, $1, $3); }
649 /* ECMA-262 3rd Edition 11.4 */
651 : PostfixExpression
{ $$
= $1; }
652 | kDELETE UnaryExpression
653 { $$
= new_unary_expression
(ctx
, EXPR_DELETE
, $2); }
654 | kVOID UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_VOID
, $2); }
655 | kTYPEOF UnaryExpression
656 { $$
= new_unary_expression
(ctx
, EXPR_TYPEOF
, $2); }
657 | tINC UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PREINC
, $2); }
658 | tDEC UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PREDEC
, $2); }
659 |
'+' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PLUS
, $2); }
660 |
'-' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_MINUS
, $2); }
661 |
'~' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_BITNEG
, $2); }
662 |
'!' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_LOGNEG
, $2); }
664 /* ECMA-262 3rd Edition 11.2 */
666 : LeftHandSideExpression
668 | LeftHandSideExpression
/* NONL */ tINC
669 { $$
= new_unary_expression
(ctx
, EXPR_POSTINC
, $1); }
670 | LeftHandSideExpression
/* NONL */ tDEC
671 { $$
= new_unary_expression
(ctx
, EXPR_POSTDEC
, $1); }
674 /* ECMA-262 3rd Edition 11.2 */
675 LeftHandSideExpression
676 : NewExpression
{ $$
= $1; }
677 | CallExpression
{ $$
= $1; }
679 /* ECMA-262 3rd Edition 11.2 */
681 : MemberExpression
{ $$
= $1; }
682 | kNEW NewExpression
{ $$
= new_new_expression
(ctx
, $2, NULL
); }
684 /* ECMA-262 3rd Edition 11.2 */
686 : PrimaryExpression
{ $$
= $1; }
687 | FunctionExpression
{ $$
= $1; }
688 | MemberExpression
'[' Expression
']'
689 { $$
= new_array_expression
(ctx
, $1, $3); }
690 | MemberExpression
'.' tIdentifier
691 { $$
= new_member_expression
(ctx
, $1, $3); }
692 | kNEW MemberExpression Arguments
693 { $$
= new_new_expression
(ctx
, $2, $3); }
695 /* ECMA-262 3rd Edition 11.2 */
697 : MemberExpression Arguments
698 { $$
= new_call_expression
(ctx
, $1, $2); }
699 | CallExpression Arguments
700 { $$
= new_call_expression
(ctx
, $1, $2); }
701 | CallExpression
'[' Expression
']'
702 { $$
= new_array_expression
(ctx
, $1, $3); }
703 | CallExpression
'.' tIdentifier
704 { $$
= new_member_expression
(ctx
, $1, $3); }
706 /* ECMA-262 3rd Edition 11.2 */
708 : '(' ')' { $$
= NULL
; }
709 |
'(' ArgumentList
')' { $$
= $2; }
711 /* ECMA-262 3rd Edition 11.2 */
713 : AssignmentExpression
{ $$
= new_argument_list
(ctx
, $1); }
714 | ArgumentList
',' AssignmentExpression
715 { $$
= argument_list_add
(ctx
, $1, $3); }
717 /* ECMA-262 3rd Edition 11.1 */
719 : kTHIS
{ $$
= new_this_expression
(ctx
); }
720 | tIdentifier
{ $$
= new_identifier_expression
(ctx
, $1); }
721 | Literal
{ $$
= new_literal_expression
(ctx
, $1); }
722 | ArrayLiteral
{ $$
= $1; }
723 | ObjectLiteral
{ $$
= $1; }
724 |
'(' Expression
')' { $$
= $2; }
726 /* ECMA-262 3rd Edition 11.1.4 */
728 : '[' ']' { $$
= new_array_literal_expression
(ctx
, NULL
, 0); }
729 |
'[' Elision
']' { $$
= new_array_literal_expression
(ctx
, NULL
, $2+1); }
730 |
'[' ElementList
']' { $$
= new_array_literal_expression
(ctx
, $2, 0); }
731 |
'[' ElementList
',' Elision_opt
']'
732 { $$
= new_array_literal_expression
(ctx
, $2, $4+1); }
734 /* ECMA-262 3rd Edition 11.1.4 */
736 : Elision_opt AssignmentExpression
737 { $$
= new_element_list
(ctx
, $1, $2); }
738 | ElementList
',' Elision_opt AssignmentExpression
739 { $$
= element_list_add
(ctx
, $1, $3, $4); }
741 /* ECMA-262 3rd Edition 11.1.4 */
744 | Elision
',' { $$
= $1 + 1; }
746 /* ECMA-262 3rd Edition 11.1.4 */
748 : /* empty */ { $$
= 0; }
749 | Elision
{ $$
= $1; }
751 /* ECMA-262 3rd Edition 11.1.5 */
753 : '{' '}' { $$
= new_prop_and_value_expression
(ctx
, NULL
); }
754 |
'{' PropertyNameAndValueList
'}'
755 { $$
= new_prop_and_value_expression
(ctx
, $2); }
757 /* ECMA-262 3rd Edition 11.1.5 */
758 PropertyNameAndValueList
759 : PropertyName
':' AssignmentExpression
760 { $$
= new_property_list
(ctx
, $1, $3); }
761 | PropertyNameAndValueList
',' PropertyName
':' AssignmentExpression
762 { $$
= property_list_add
(ctx
, $1, $3, $5); }
764 /* ECMA-262 3rd Edition 11.1.5 */
766 : tIdentifier
{ $$
= new_string_literal
(ctx
, $1); }
767 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
768 | tNumericLiteral
{ $$
= $1; }
770 /* ECMA-262 3rd Edition 7.6 */
772 : /* empty*/ { $$
= NULL
; }
773 | tIdentifier
{ $$
= $1; }
775 /* ECMA-262 3rd Edition 7.8 */
777 : kNULL
{ $$
= new_null_literal
(ctx
); }
778 | kUNDEFINED
{ $$
= new_undefined_literal
(ctx
); }
779 | BooleanLiteral
{ $$
= $1; }
780 | tNumericLiteral
{ $$
= $1; }
781 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
782 |
'/' { $$
= parse_regexp
(ctx
);
785 /* ECMA-262 3rd Edition 7.8.2 */
787 : kTRUE
{ $$
= new_boolean_literal
(ctx
, TRUE
); }
788 | kFALSE
{ $$
= new_boolean_literal
(ctx
, FALSE
); }
792 |
error { if
(!allow_auto_semicolon
(ctx
)) {YYABORT;} }
796 static BOOL allow_auto_semicolon
(parser_ctx_t
*ctx
)
798 return ctx
->nl || ctx
->ptr
== ctx
->end ||
*(ctx
->ptr
-1) == '}';
801 static literal_t
*new_string_literal
(parser_ctx_t
*ctx
, const WCHAR
*str
)
803 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
811 static literal_t
*new_null_literal
(parser_ctx_t
*ctx
)
813 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
820 static literal_t
*new_undefined_literal
(parser_ctx_t
*ctx
)
822 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
829 static literal_t
*new_boolean_literal
(parser_ctx_t
*ctx
, VARIANT_BOOL bval
)
831 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
839 static prop_val_t
*new_prop_val
(parser_ctx_t
*ctx
, literal_t
*name
, expression_t
*value
)
841 prop_val_t
*ret
= parser_alloc
(ctx
, sizeof
(prop_val_t
));
850 static property_list_t
*new_property_list
(parser_ctx_t
*ctx
, literal_t
*name
, expression_t
*value
)
852 property_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(property_list_t
));
854 ret
->head
= ret
->tail
= new_prop_val
(ctx
, name
, value
);
859 static property_list_t
*property_list_add
(parser_ctx_t
*ctx
, property_list_t
*list
, literal_t
*name
, expression_t
*value
)
861 list
->tail
= list
->tail
->next
= new_prop_val
(ctx
, name
, value
);
866 static array_element_t
*new_array_element
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
868 array_element_t
*ret
= parser_alloc
(ctx
, sizeof
(array_element_t
));
870 ret
->elision
= elision
;
877 static element_list_t
*new_element_list
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
879 element_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(element_list_t
));
881 ret
->head
= ret
->tail
= new_array_element
(ctx
, elision
, expr
);
886 static element_list_t
*element_list_add
(parser_ctx_t
*ctx
, element_list_t
*list
, int elision
, expression_t
*expr
)
888 list
->tail
= list
->tail
->next
= new_array_element
(ctx
, elision
, expr
);
893 static argument_t
*new_argument
(parser_ctx_t
*ctx
, expression_t
*expr
)
895 argument_t
*ret
= parser_alloc
(ctx
, sizeof
(argument_t
));
903 static argument_list_t
*new_argument_list
(parser_ctx_t
*ctx
, expression_t
*expr
)
905 argument_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(argument_list_t
));
907 ret
->head
= ret
->tail
= new_argument
(ctx
, expr
);
912 static argument_list_t
*argument_list_add
(parser_ctx_t
*ctx
, argument_list_t
*list
, expression_t
*expr
)
914 list
->tail
= list
->tail
->next
= new_argument
(ctx
, expr
);
919 static catch_block_t
*new_catch_block
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
921 catch_block_t
*ret
= parser_alloc
(ctx
, sizeof
(catch_block_t
));
923 ret
->identifier
= identifier
;
924 ret
->statement
= statement
;
929 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_list_t
*stat_list
)
931 case_clausule_t
*ret
= parser_alloc
(ctx
, sizeof
(case_clausule_t
));
934 ret
->stat
= stat_list ? stat_list
->head
: NULL
;
940 static case_list_t
*new_case_list
(parser_ctx_t
*ctx
, case_clausule_t
*case_clausule
)
942 case_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(case_list_t
));
944 ret
->head
= ret
->tail
= case_clausule
;
949 static case_list_t
*case_list_add
(parser_ctx_t
*ctx
, case_list_t
*list
, case_clausule_t
*case_clausule
)
951 list
->tail
= list
->tail
->next
= case_clausule
;
956 static case_clausule_t
*new_case_block
(parser_ctx_t
*ctx
, case_list_t
*case_list1
,
957 case_clausule_t
*default_clausule
, case_list_t
*case_list2
)
959 case_clausule_t
*ret
= NULL
, *iter
= NULL
, *iter2
;
960 statement_t
*stat
= NULL
;
963 ret
= case_list1
->head
;
964 iter
= case_list1
->tail
;
967 if
(default_clausule
) {
969 iter
= iter
->next
= default_clausule
;
971 ret
= iter
= default_clausule
;
976 iter
->next
= case_list2
->head
;
978 ret
= case_list2
->head
;
984 for
(iter
= ret
; iter
; iter
= iter
->next
) {
985 for
(iter2
= iter
; iter2
&& !iter2
->stat
; iter2
= iter2
->next
);
989 while
(iter
!= iter2
) {
990 iter
->stat
= iter2
->stat
;
997 stat
->next
= iter
->stat
;
1006 static statement_t
*new_block_statement
(parser_ctx_t
*ctx
, statement_list_t
*list
)
1008 block_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(block_statement_t
));
1010 ret
->stat.eval
= block_statement_eval
;
1011 ret
->stat.next
= NULL
;
1012 ret
->stat_list
= list ? list
->head
: NULL
;
1017 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, expression_t
*expr
)
1019 variable_declaration_t
*ret
= parser_alloc
(ctx
, sizeof
(variable_declaration_t
));
1020 var_list_t
*var_list
= parser_alloc
(ctx
, sizeof
(var_list_t
));
1022 ret
->identifier
= identifier
;
1026 var_list
->identifier
= identifier
;
1027 var_list
->next
= NULL
;
1029 if
(ctx
->func_stack
->var_tail
)
1030 ctx
->func_stack
->var_tail
= ctx
->func_stack
->var_tail
->next
= var_list
;
1032 ctx
->func_stack
->var_head
= ctx
->func_stack
->var_tail
= var_list
;
1037 static variable_list_t
*new_variable_list
(parser_ctx_t
*ctx
, variable_declaration_t
*decl
)
1039 variable_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(variable_list_t
));
1041 ret
->head
= ret
->tail
= decl
;
1046 static variable_list_t
*variable_list_add
(parser_ctx_t
*ctx
, variable_list_t
*list
, variable_declaration_t
*decl
)
1048 list
->tail
= list
->tail
->next
= decl
;
1053 static statement_t
*new_var_statement
(parser_ctx_t
*ctx
, variable_list_t
*variable_list
)
1055 var_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(var_statement_t
));
1057 ret
->stat.eval
= var_statement_eval
;
1058 ret
->stat.next
= NULL
;
1059 ret
->variable_list
= variable_list
->head
;
1064 static statement_t
*new_empty_statement
(parser_ctx_t
*ctx
)
1066 statement_t
*ret
= parser_alloc
(ctx
, sizeof
(statement_t
));
1068 ret
->eval
= empty_statement_eval
;
1074 static statement_t
*new_expression_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1076 expression_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(expression_statement_t
));
1078 ret
->stat.eval
= expression_statement_eval
;
1079 ret
->stat.next
= NULL
;
1085 static statement_t
*new_if_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_t
*if_stat
, statement_t
*else_stat
)
1087 if_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(if_statement_t
));
1089 ret
->stat.eval
= if_statement_eval
;
1090 ret
->stat.next
= NULL
;
1092 ret
->if_stat
= if_stat
;
1093 ret
->else_stat
= else_stat
;
1098 static statement_t
*new_while_statement
(parser_ctx_t
*ctx
, BOOL dowhile
, expression_t
*expr
, statement_t
*stat
)
1100 while_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(while_statement_t
));
1102 ret
->stat.eval
= while_statement_eval
;
1103 ret
->stat.next
= NULL
;
1104 ret
->do_while
= dowhile
;
1106 ret
->statement
= stat
;
1111 static statement_t
*new_for_statement
(parser_ctx_t
*ctx
, variable_list_t
*variable_list
, expression_t
*begin_expr
,
1112 expression_t
*expr
, expression_t
*end_expr
, statement_t
*statement
)
1114 for_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(for_statement_t
));
1116 ret
->stat.eval
= for_statement_eval
;
1117 ret
->stat.next
= NULL
;
1118 ret
->variable_list
= variable_list ? variable_list
->head
: NULL
;
1119 ret
->begin_expr
= begin_expr
;
1121 ret
->end_expr
= end_expr
;
1122 ret
->statement
= statement
;
1127 static statement_t
*new_forin_statement
(parser_ctx_t
*ctx
, variable_declaration_t
*variable
, expression_t
*expr
,
1128 expression_t
*in_expr
, statement_t
*statement
)
1130 forin_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(forin_statement_t
));
1132 ret
->stat.eval
= forin_statement_eval
;
1133 ret
->stat.next
= NULL
;
1134 ret
->variable
= variable
;
1136 ret
->in_expr
= in_expr
;
1137 ret
->statement
= statement
;
1142 static statement_t
*new_continue_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1144 branch_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(branch_statement_t
));
1146 ret
->stat.eval
= continue_statement_eval
;
1147 ret
->stat.next
= NULL
;
1148 ret
->identifier
= identifier
;
1153 static statement_t
*new_break_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1155 branch_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(branch_statement_t
));
1157 ret
->stat.eval
= break_statement_eval
;
1158 ret
->stat.next
= NULL
;
1159 ret
->identifier
= identifier
;
1164 static statement_t
*new_return_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1166 expression_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(expression_statement_t
));
1168 ret
->stat.eval
= return_statement_eval
;
1169 ret
->stat.next
= NULL
;
1175 static statement_t
*new_with_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_t
*statement
)
1177 with_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(with_statement_t
));
1179 ret
->stat.eval
= with_statement_eval
;
1180 ret
->stat.next
= NULL
;
1182 ret
->statement
= statement
;
1187 static statement_t
*new_labelled_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
1189 labelled_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(labelled_statement_t
));
1191 ret
->stat.eval
= labelled_statement_eval
;
1192 ret
->stat.next
= NULL
;
1193 ret
->identifier
= identifier
;
1194 ret
->statement
= statement
;
1199 static statement_t
*new_switch_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, case_clausule_t
*case_list
)
1201 switch_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(switch_statement_t
));
1203 ret
->stat.eval
= switch_statement_eval
;
1204 ret
->stat.next
= NULL
;
1206 ret
->case_list
= case_list
;
1211 static statement_t
*new_throw_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1213 expression_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(expression_statement_t
));
1215 ret
->stat.eval
= throw_statement_eval
;
1216 ret
->stat.next
= NULL
;
1222 static statement_t
*new_try_statement
(parser_ctx_t
*ctx
, statement_t
*try_statement
,
1223 catch_block_t
*catch_block
, statement_t
*finally_statement
)
1225 try_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(try_statement_t
));
1227 ret
->stat.eval
= try_statement_eval
;
1228 ret
->stat.next
= NULL
;
1229 ret
->try_statement
= try_statement
;
1230 ret
->catch_block
= catch_block
;
1231 ret
->finally_statement
= finally_statement
;
1236 static parameter_t
*new_parameter
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1238 parameter_t
*ret
= parser_alloc
(ctx
, sizeof
(parameter_t
));
1240 ret
->identifier
= identifier
;
1246 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1248 parameter_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(parameter_list_t
));
1250 ret
->head
= ret
->tail
= new_parameter
(ctx
, identifier
);
1255 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*ctx
, parameter_list_t
*list
, const WCHAR
*identifier
)
1257 list
->tail
= list
->tail
->next
= new_parameter
(ctx
, identifier
);
1262 static expression_t
*new_function_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
,
1263 parameter_list_t
*parameter_list
, source_elements_t
*source_elements
, const WCHAR
*src_str
, DWORD src_len
)
1265 function_expression_t
*ret
= parser_alloc
(ctx
, sizeof
(function_expression_t
));
1267 ret
->expr.eval
= function_expression_eval
;
1268 ret
->identifier
= identifier
;
1269 ret
->parameter_list
= parameter_list ? parameter_list
->head
: NULL
;
1270 ret
->source_elements
= source_elements
;
1271 ret
->src_str
= src_str
;
1272 ret
->src_len
= src_len
;
1274 if
(ret
->identifier
) {
1275 function_declaration_t
*decl
= parser_alloc
(ctx
, sizeof
(function_declaration_t
));
1280 if
(ctx
->func_stack
->func_tail
)
1281 ctx
->func_stack
->func_tail
= ctx
->func_stack
->func_tail
->next
= decl
;
1283 ctx
->func_stack
->func_head
= ctx
->func_stack
->func_tail
= decl
;
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 source_elements_t
*new_source_elements
(parser_ctx_t
*ctx
)
1475 source_elements_t
*ret
= parser_alloc
(ctx
, sizeof
(source_elements_t
));
1477 memset
(ret
, 0, sizeof
(*ret
));
1482 static source_elements_t
*source_elements_add_statement
(source_elements_t
*source_elements
, statement_t
*statement
)
1484 if
(source_elements
->statement_tail
)
1485 source_elements
->statement_tail
= source_elements
->statement_tail
->next
= statement
;
1487 source_elements
->statement
= source_elements
->statement_tail
= statement
;
1489 return source_elements
;
1492 static statement_list_t
*new_statement_list
(parser_ctx_t
*ctx
, statement_t
*statement
)
1494 statement_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(statement_list_t
));
1496 ret
->head
= ret
->tail
= statement
;
1501 static statement_list_t
*statement_list_add
(statement_list_t
*list
, statement_t
*statement
)
1503 list
->tail
= list
->tail
->next
= statement
;
1508 static void push_func
(parser_ctx_t
*ctx
)
1510 func_stack_t
*new_func
= parser_alloc_tmp
(ctx
, sizeof
(func_stack_t
));
1512 new_func
->func_head
= new_func
->func_tail
= NULL
;
1513 new_func
->var_head
= new_func
->var_tail
= NULL
;
1515 new_func
->next
= ctx
->func_stack
;
1516 ctx
->func_stack
= new_func
;
1519 static source_elements_t
*function_body_parsed
(parser_ctx_t
*ctx
, source_elements_t
*source
)
1521 source
->functions
= ctx
->func_stack
->func_head
;
1522 source
->variables
= ctx
->func_stack
->var_head
;
1528 static void program_parsed
(parser_ctx_t
*ctx
, source_elements_t
*source
)
1530 source
->functions
= ctx
->func_stack
->func_head
;
1531 source
->variables
= ctx
->func_stack
->var_head
;
1534 ctx
->source
= source
;
1538 void parser_release
(parser_ctx_t
*ctx
)
1540 obj_literal_t
*iter
;
1545 for
(iter
= ctx
->obj_literals
; iter
; iter
= iter
->next
)
1546 jsdisp_release
(iter
->obj
);
1548 jsheap_free
(&ctx
->heap
);
1552 HRESULT script_parse
(script_ctx_t
*ctx
, const WCHAR
*code
, parser_ctx_t
**ret
)
1554 parser_ctx_t
*parser_ctx
;
1558 parser_ctx
= heap_alloc_zero
(sizeof
(parser_ctx_t
));
1560 return E_OUTOFMEMORY
;
1562 parser_ctx
->ref
= 1;
1563 parser_ctx
->hres
= E_FAIL
;
1565 parser_ctx
->begin
= parser_ctx
->ptr
= code
;
1566 parser_ctx
->end
= code
+ strlenW
(code
);
1569 parser_ctx
->script
= ctx
;
1571 mark
= jsheap_mark
(&ctx
->tmp_heap
);
1572 jsheap_init
(&parser_ctx
->heap
);
1574 push_func
(parser_ctx
);
1576 parser_parse
(parser_ctx
);
1578 if
(FAILED
(parser_ctx
->hres
)) {
1579 hres
= parser_ctx
->hres
;
1580 parser_release
(parser_ctx
);