2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define YYLEX_PARAM ctx
25 #define YYPARSE_PARAM ctx
27 static int parser_error
(const char*);
28 static void set_error
(parser_ctx_t
*,UINT
);
29 static BOOL explicit_error
(parser_ctx_t
*,void*,WCHAR
);
30 static BOOL allow_auto_semicolon
(parser_ctx_t
*);
31 static void program_parsed
(parser_ctx_t
*,source_elements_t
*);
32 static source_elements_t
*function_body_parsed
(parser_ctx_t
*,source_elements_t
*);
34 typedef
struct _statement_list_t
{
39 static literal_t
*new_string_literal
(parser_ctx_t
*,const WCHAR
*);
40 static literal_t
*new_null_literal
(parser_ctx_t
*);
42 typedef
struct _property_list_t
{
47 static property_list_t
*new_property_list
(parser_ctx_t
*,literal_t
*,expression_t
*);
48 static property_list_t
*property_list_add
(parser_ctx_t
*,property_list_t
*,literal_t
*,expression_t
*);
50 typedef
struct _element_list_t
{
51 array_element_t
*head
;
52 array_element_t
*tail
;
55 static element_list_t
*new_element_list
(parser_ctx_t
*,int,expression_t
*);
56 static element_list_t
*element_list_add
(parser_ctx_t
*,element_list_t
*,int,expression_t
*);
58 typedef
struct _argument_list_t
{
63 static argument_list_t
*new_argument_list
(parser_ctx_t
*,expression_t
*);
64 static argument_list_t
*argument_list_add
(parser_ctx_t
*,argument_list_t
*,expression_t
*);
66 typedef
struct _case_list_t
{
67 case_clausule_t
*head
;
68 case_clausule_t
*tail
;
71 static catch_block_t
*new_catch_block
(parser_ctx_t
*,const WCHAR
*,statement_t
*);
72 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*,expression_t
*,statement_list_t
*);
73 static case_list_t
*new_case_list
(parser_ctx_t
*,case_clausule_t
*);
74 static case_list_t
*case_list_add
(parser_ctx_t
*,case_list_t
*,case_clausule_t
*);
75 static case_clausule_t
*new_case_block
(parser_ctx_t
*,case_list_t
*,case_clausule_t
*,case_list_t
*);
77 typedef
struct _variable_list_t
{
78 variable_declaration_t
*head
;
79 variable_declaration_t
*tail
;
82 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*,const WCHAR
*,expression_t
*);
83 static variable_list_t
*new_variable_list
(parser_ctx_t
*,variable_declaration_t
*);
84 static variable_list_t
*variable_list_add
(parser_ctx_t
*,variable_list_t
*,variable_declaration_t
*);
86 static statement_t
*new_block_statement
(parser_ctx_t
*,statement_list_t
*);
87 static statement_t
*new_var_statement
(parser_ctx_t
*,variable_list_t
*);
88 static statement_t
*new_empty_statement
(parser_ctx_t
*);
89 static statement_t
*new_expression_statement
(parser_ctx_t
*,expression_t
*);
90 static statement_t
*new_if_statement
(parser_ctx_t
*,expression_t
*,statement_t
*,statement_t
*);
91 static statement_t
*new_while_statement
(parser_ctx_t
*,BOOL
,expression_t
*,statement_t
*);
92 static statement_t
*new_for_statement
(parser_ctx_t
*,variable_list_t
*,expression_t
*,expression_t
*,
93 expression_t
*,statement_t
*);
94 static statement_t
*new_forin_statement
(parser_ctx_t
*,variable_declaration_t
*,expression_t
*,expression_t
*,statement_t
*);
95 static statement_t
*new_continue_statement
(parser_ctx_t
*,const WCHAR
*);
96 static statement_t
*new_break_statement
(parser_ctx_t
*,const WCHAR
*);
97 static statement_t
*new_return_statement
(parser_ctx_t
*,expression_t
*);
98 static statement_t
*new_with_statement
(parser_ctx_t
*,expression_t
*,statement_t
*);
99 static statement_t
*new_labelled_statement
(parser_ctx_t
*,const WCHAR
*,statement_t
*);
100 static statement_t
*new_switch_statement
(parser_ctx_t
*,expression_t
*,case_clausule_t
*);
101 static statement_t
*new_throw_statement
(parser_ctx_t
*,expression_t
*);
102 static statement_t
*new_try_statement
(parser_ctx_t
*,statement_t
*,catch_block_t
*,statement_t
*);
104 struct statement_list_t
{
109 static statement_list_t
*new_statement_list
(parser_ctx_t
*,statement_t
*);
110 static statement_list_t
*statement_list_add
(statement_list_t
*,statement_t
*);
112 typedef
struct _parameter_list_t
{
117 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*,const WCHAR
*);
118 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*,parameter_list_t
*,const WCHAR
*);
120 static void push_func
(parser_ctx_t
*);
121 static inline
void pop_func
(parser_ctx_t
*ctx
)
123 ctx
->func_stack
= ctx
->func_stack
->next
;
126 static void *new_expression
(parser_ctx_t
*ctx
,expression_type_t
,size_t);
127 static expression_t
*new_function_expression
(parser_ctx_t
*,const WCHAR
*,parameter_list_t
*,
128 source_elements_t
*,const WCHAR
*,DWORD
);
129 static expression_t
*new_binary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*,expression_t
*);
130 static expression_t
*new_unary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*);
131 static expression_t
*new_conditional_expression
(parser_ctx_t
*,expression_t
*,expression_t
*,expression_t
*);
132 static expression_t
*new_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_identifier_expression
(parser_ctx_t
*,const WCHAR
*);
136 static expression_t
*new_literal_expression
(parser_ctx_t
*,literal_t
*);
137 static expression_t
*new_array_literal_expression
(parser_ctx_t
*,element_list_t
*,int);
138 static expression_t
*new_prop_and_value_expression
(parser_ctx_t
*,property_list_t
*);
140 static source_elements_t
*new_source_elements
(parser_ctx_t
*);
141 static source_elements_t
*source_elements_add_statement
(source_elements_t
*,statement_t
*);
153 struct _argument_list_t
*argument_list
;
154 case_clausule_t
*case_clausule
;
155 struct _case_list_t
*case_list
;
156 catch_block_t
*catch_block
;
157 struct _element_list_t
*element_list
;
159 const WCHAR
*identifier
;
160 struct _parameter_list_t
*parameter_list
;
161 struct _property_list_t
*property_list
;
162 source_elements_t
*source_elements
;
163 statement_t
*statement
;
164 struct _statement_list_t
*statement_list
;
165 struct _variable_list_t
*variable_list
;
166 variable_declaration_t
*variable_declaration
;
170 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
171 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
172 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ
174 %token
<srcptr
> kFUNCTION
'}'
177 %token
<identifier
> tIdentifier
178 %token
<ival
> tAssignOper tEqOper tShiftOper tRelOper
179 %token
<literal
> tNumericLiteral tBooleanLiteral
180 %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 Expression_err
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
246 %type
<ival
> AssignOper
248 %nonassoc LOWER_THAN_ELSE
253 /* ECMA-262 3rd Edition 14 */
255 : SourceElements HtmlComment tEOF
256 { 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); }
268 /* ECMA-262 3rd Edition 13 */
270 : KFunction Identifier_opt left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
271 { $$
= new_function_expression
(ctx
, $2, $4, $7, $1, $8-$1+1); }
274 : kFUNCTION
{ push_func
(ctx
); $$
= $1; }
276 /* ECMA-262 3rd Edition 13 */
278 : SourceElements
{ $$
= function_body_parsed
(ctx
, $1); }
280 /* ECMA-262 3rd Edition 13 */
282 : tIdentifier
{ $$
= new_parameter_list
(ctx
, $1); }
283 | FormalParameterList
',' tIdentifier
284 { $$
= parameter_list_add
(ctx
, $1, $3); }
286 /* ECMA-262 3rd Edition 13 */
287 FormalParameterList_opt
288 : /* empty */ { $$
= NULL
; }
289 | FormalParameterList
{ $$
= $1; }
291 /* ECMA-262 3rd Edition 12 */
294 | VariableStatement
{ $$
= $1; }
295 | EmptyStatement
{ $$
= $1; }
296 | FunctionExpression
{ $$
= new_empty_statement
(ctx
); } /* FIXME: return NULL */
297 | ExpressionStatement
{ $$
= $1; }
298 | IfStatement
{ $$
= $1; }
299 | IterationStatement
{ $$
= $1; }
300 | ContinueStatement
{ $$
= $1; }
301 | BreakStatement
{ $$
= $1; }
302 | ReturnStatement
{ $$
= $1; }
303 | WithStatement
{ $$
= $1; }
304 | LabelledStatement
{ $$
= $1; }
305 | SwitchStatement
{ $$
= $1; }
306 | ThrowStatement
{ $$
= $1; }
307 | TryStatement
{ $$
= $1; }
309 /* ECMA-262 3rd Edition 12.2 */
311 : Statement
{ $$
= new_statement_list
(ctx
, $1); }
312 | StatementList Statement
313 { $$
= statement_list_add
($1, $2); }
315 /* ECMA-262 3rd Edition 12.2 */
317 : /* empty */ { $$
= NULL
; }
318 | StatementList
{ $$
= $1; }
320 /* ECMA-262 3rd Edition 12.1 */
322 : '{' StatementList
'}' { $$
= new_block_statement
(ctx
, $2); }
323 |
'{' '}' { $$
= new_block_statement
(ctx
, NULL
); }
325 /* ECMA-262 3rd Edition 12.2 */
327 : kVAR VariableDeclarationList semicolon_opt
328 { $$
= new_var_statement
(ctx
, $2); }
330 /* ECMA-262 3rd Edition 12.2 */
331 VariableDeclarationList
332 : VariableDeclaration
{ $$
= new_variable_list
(ctx
, $1); }
333 | VariableDeclarationList
',' VariableDeclaration
334 { $$
= variable_list_add
(ctx
, $1, $3); }
336 /* ECMA-262 3rd Edition 12.2 */
337 VariableDeclarationListNoIn
338 : VariableDeclarationNoIn
339 { $$
= new_variable_list
(ctx
, $1); }
340 | VariableDeclarationListNoIn
',' VariableDeclarationNoIn
341 { $$
= variable_list_add
(ctx
, $1, $3); }
343 /* ECMA-262 3rd Edition 12.2 */
345 : tIdentifier Initialiser_opt
346 { $$
= new_variable_declaration
(ctx
, $1, $2); }
348 /* ECMA-262 3rd Edition 12.2 */
349 VariableDeclarationNoIn
350 : tIdentifier InitialiserNoIn_opt
351 { $$
= new_variable_declaration
(ctx
, $1, $2); }
353 /* ECMA-262 3rd Edition 12.2 */
355 : /* empty */ { $$
= NULL
; }
356 | Initialiser
{ $$
= $1; }
358 /* ECMA-262 3rd Edition 12.2 */
360 : '=' AssignmentExpression
363 /* ECMA-262 3rd Edition 12.2 */
365 : /* empty */ { $$
= NULL
; }
366 | InitialiserNoIn
{ $$
= $1; }
368 /* ECMA-262 3rd Edition 12.2 */
370 : '=' AssignmentExpressionNoIn
373 /* ECMA-262 3rd Edition 12.3 */
375 : ';' { $$
= new_empty_statement
(ctx
); }
377 /* ECMA-262 3rd Edition 12.4 */
379 : Expression semicolon_opt
380 { $$
= new_expression_statement
(ctx
, $1); }
382 /* ECMA-262 3rd Edition 12.5 */
384 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
385 { $$
= new_if_statement
(ctx
, $3, $5, $7); }
386 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
387 { $$
= new_if_statement
(ctx
, $3, $5, NULL
); }
389 /* ECMA-262 3rd Edition 12.6 */
391 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
392 { $$
= new_while_statement
(ctx
, TRUE
, $5, $2); }
393 | kWHILE left_bracket Expression_err right_bracket Statement
394 { $$
= new_while_statement
(ctx
, FALSE
, $3, $5); }
395 | kFOR left_bracket ExpressionNoIn_opt
396 { if
(!explicit_error
(ctx
, $3, ';')) YYABORT; }
397 semicolon Expression_opt
398 { if
(!explicit_error
(ctx
, $6, ';')) YYABORT; }
399 semicolon Expression_opt right_bracket Statement
400 { $$
= new_for_statement
(ctx
, NULL
, $3, $6, $9, $11); }
401 | kFOR left_bracket kVAR VariableDeclarationListNoIn
402 { if
(!explicit_error
(ctx
, $4, ';')) YYABORT; }
403 semicolon Expression_opt
404 { if
(!explicit_error
(ctx
, $7, ';')) YYABORT; }
405 semicolon Expression_opt right_bracket Statement
406 { $$
= new_for_statement
(ctx
, $4, NULL
, $7, $10, $12); }
407 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
408 { $$
= new_forin_statement
(ctx
, NULL
, $3, $5, $7); }
409 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
410 { $$
= new_forin_statement
(ctx
, $4, NULL
, $6, $8); }
412 /* ECMA-262 3rd Edition 12.7 */
414 : kCONTINUE
/* NONL */ Identifier_opt semicolon_opt
415 { $$
= new_continue_statement
(ctx
, $2); }
417 /* ECMA-262 3rd Edition 12.8 */
419 : kBREAK
/* NONL */ Identifier_opt semicolon_opt
420 { $$
= new_break_statement
(ctx
, $2); }
422 /* ECMA-262 3rd Edition 12.9 */
424 : kRETURN
/* NONL */ Expression_opt semicolon_opt
425 { $$
= new_return_statement
(ctx
, $2); }
427 /* ECMA-262 3rd Edition 12.10 */
429 : kWITH left_bracket Expression right_bracket Statement
430 { $$
= new_with_statement
(ctx
, $3, $5); }
432 /* ECMA-262 3rd Edition 12.12 */
434 : tIdentifier
':' Statement
435 { $$
= new_labelled_statement
(ctx
, $1, $3); }
437 /* ECMA-262 3rd Edition 12.11 */
439 : kSWITCH left_bracket Expression right_bracket CaseBlock
440 { $$
= new_switch_statement
(ctx
, $3, $5); }
442 /* ECMA-262 3rd Edition 12.11 */
444 : '{' CaseClausules_opt
'}'
445 { $$
= new_case_block
(ctx
, $2, NULL
, NULL
); }
446 |
'{' CaseClausules_opt DefaultClausule CaseClausules_opt
'}'
447 { $$
= new_case_block
(ctx
, $2, $3, $4); }
449 /* ECMA-262 3rd Edition 12.11 */
451 : /* empty */ { $$
= NULL
; }
452 | CaseClausules
{ $$
= $1; }
454 /* ECMA-262 3rd Edition 12.11 */
456 : CaseClausule
{ $$
= new_case_list
(ctx
, $1); }
457 | CaseClausules CaseClausule
458 { $$
= case_list_add
(ctx
, $1, $2); }
460 /* ECMA-262 3rd Edition 12.11 */
462 : kCASE Expression
':' StatementList_opt
463 { $$
= new_case_clausule
(ctx
, $2, $4); }
465 /* ECMA-262 3rd Edition 12.11 */
467 : kDEFAULT
':' StatementList_opt
468 { $$
= new_case_clausule
(ctx
, NULL
, $3); }
470 /* ECMA-262 3rd Edition 12.13 */
472 : kTHROW
/* NONL */ Expression semicolon_opt
473 { $$
= new_throw_statement
(ctx
, $2); }
475 /* ECMA-262 3rd Edition 12.14 */
477 : kTRY Block Catch
{ $$
= new_try_statement
(ctx
, $2, $3, NULL
); }
478 | kTRY Block Finally
{ $$
= new_try_statement
(ctx
, $2, NULL
, $3); }
479 | kTRY Block Catch Finally
480 { $$
= new_try_statement
(ctx
, $2, $3, $4); }
482 /* ECMA-262 3rd Edition 12.14 */
484 : kCATCH left_bracket tIdentifier right_bracket Block
485 { $$
= new_catch_block
(ctx
, $3, $5); }
487 /* ECMA-262 3rd Edition 12.14 */
489 : kFINALLY Block
{ $$
= $2; }
491 /* ECMA-262 3rd Edition 11.14 */
493 : /* empty */ { $$
= NULL
; }
494 | Expression
{ $$
= $1; }
497 : Expression
{ $$
= $1; }
498 |
error { set_error
(ctx
, JS_E_SYNTAX
); YYABORT; }
500 /* ECMA-262 3rd Edition 11.14 */
502 : AssignmentExpression
{ $$
= $1; }
503 | Expression
',' AssignmentExpression
504 { $$
= new_binary_expression
(ctx
, EXPR_COMMA
, $1, $3); }
506 /* ECMA-262 3rd Edition 11.14 */
508 : /* empty */ { $$
= NULL
; }
509 | ExpressionNoIn
{ $$
= $1; }
511 /* ECMA-262 3rd Edition 11.14 */
513 : AssignmentExpressionNoIn
515 | ExpressionNoIn
',' AssignmentExpressionNoIn
516 { $$
= new_binary_expression
(ctx
, EXPR_COMMA
, $1, $3); }
519 : tAssignOper
{ $$
= $1; }
520 | kDIVEQ
{ $$
= EXPR_ASSIGNDIV
; }
522 /* ECMA-262 3rd Edition 11.13 */
524 : ConditionalExpression
{ $$
= $1; }
525 | LeftHandSideExpression
'=' AssignmentExpression
526 { $$
= new_binary_expression
(ctx
, EXPR_ASSIGN
, $1, $3); }
527 | LeftHandSideExpression AssignOper AssignmentExpression
528 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
530 /* ECMA-262 3rd Edition 11.13 */
531 AssignmentExpressionNoIn
532 : ConditionalExpressionNoIn
534 | LeftHandSideExpression
'=' AssignmentExpressionNoIn
535 { $$
= new_binary_expression
(ctx
, EXPR_ASSIGN
, $1, $3); }
536 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
537 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
539 /* ECMA-262 3rd Edition 11.12 */
540 ConditionalExpression
541 : LogicalORExpression
{ $$
= $1; }
542 | LogicalORExpression
'?' AssignmentExpression
':' AssignmentExpression
543 { $$
= new_conditional_expression
(ctx
, $1, $3, $5); }
545 /* ECMA-262 3rd Edition 11.12 */
546 ConditionalExpressionNoIn
547 : LogicalORExpressionNoIn
549 | LogicalORExpressionNoIn
'?' AssignmentExpressionNoIn
':' AssignmentExpressionNoIn
550 { $$
= new_conditional_expression
(ctx
, $1, $3, $5); }
552 /* ECMA-262 3rd Edition 11.11 */
554 : LogicalANDExpression
{ $$
= $1; }
555 | LogicalORExpression tOROR LogicalANDExpression
556 { $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); }
558 /* ECMA-262 3rd Edition 11.11 */
559 LogicalORExpressionNoIn
560 : LogicalANDExpressionNoIn
562 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
563 { $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); }
565 /* ECMA-262 3rd Edition 11.11 */
567 : BitwiseORExpression
{ $$
= $1; }
568 | LogicalANDExpression tANDAND BitwiseORExpression
569 { $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); }
571 /* ECMA-262 3rd Edition 11.11 */
572 LogicalANDExpressionNoIn
573 : BitwiseORExpressionNoIn
575 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
576 { $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); }
578 /* ECMA-262 3rd Edition 11.10 */
580 : BitwiseXORExpression
{ $$
= $1; }
581 | BitwiseORExpression
'|' BitwiseXORExpression
582 { $$
= new_binary_expression
(ctx
, EXPR_BOR
, $1, $3); }
584 /* ECMA-262 3rd Edition 11.10 */
585 BitwiseORExpressionNoIn
586 : BitwiseXORExpressionNoIn
588 | BitwiseORExpressionNoIn
'|' BitwiseXORExpressionNoIn
589 { $$
= new_binary_expression
(ctx
, EXPR_BOR
, $1, $3); }
591 /* ECMA-262 3rd Edition 11.10 */
593 : BitwiseANDExpression
{ $$
= $1; }
594 | BitwiseXORExpression
'^' BitwiseANDExpression
595 { $$
= new_binary_expression
(ctx
, EXPR_BXOR
, $1, $3); }
597 /* ECMA-262 3rd Edition 11.10 */
598 BitwiseXORExpressionNoIn
599 : BitwiseANDExpressionNoIn
601 | BitwiseXORExpressionNoIn
'^' BitwiseANDExpressionNoIn
602 { $$
= new_binary_expression
(ctx
, EXPR_BXOR
, $1, $3); }
604 /* ECMA-262 3rd Edition 11.10 */
606 : EqualityExpression
{ $$
= $1; }
607 | BitwiseANDExpression
'&' EqualityExpression
608 { $$
= new_binary_expression
(ctx
, EXPR_BAND
, $1, $3); }
610 /* ECMA-262 3rd Edition 11.10 */
611 BitwiseANDExpressionNoIn
612 : EqualityExpressionNoIn
614 | BitwiseANDExpressionNoIn
'&' EqualityExpressionNoIn
615 { $$
= new_binary_expression
(ctx
, EXPR_BAND
, $1, $3); }
617 /* ECMA-262 3rd Edition 11.9 */
619 : RelationalExpression
{ $$
= $1; }
620 | EqualityExpression tEqOper RelationalExpression
621 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
623 /* ECMA-262 3rd Edition 11.9 */
624 EqualityExpressionNoIn
625 : RelationalExpressionNoIn
{ $$
= $1; }
626 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
627 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
629 /* ECMA-262 3rd Edition 11.8 */
631 : ShiftExpression
{ $$
= $1; }
632 | RelationalExpression tRelOper ShiftExpression
633 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
634 | RelationalExpression kINSTANCEOF ShiftExpression
635 { $$
= new_binary_expression
(ctx
, EXPR_INSTANCEOF
, $1, $3); }
636 | RelationalExpression kIN ShiftExpression
637 { $$
= new_binary_expression
(ctx
, EXPR_IN
, $1, $3); }
639 /* ECMA-262 3rd Edition 11.8 */
640 RelationalExpressionNoIn
641 : ShiftExpression
{ $$
= $1; }
642 | RelationalExpressionNoIn tRelOper ShiftExpression
643 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
644 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
645 { $$
= new_binary_expression
(ctx
, EXPR_INSTANCEOF
, $1, $3); }
647 /* ECMA-262 3rd Edition 11.7 */
649 : AdditiveExpression
{ $$
= $1; }
650 | ShiftExpression tShiftOper AdditiveExpression
651 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
653 /* ECMA-262 3rd Edition 11.6 */
655 : MultiplicativeExpression
657 | AdditiveExpression
'+' MultiplicativeExpression
658 { $$
= new_binary_expression
(ctx
, EXPR_ADD
, $1, $3); }
659 | AdditiveExpression
'-' MultiplicativeExpression
660 { $$
= new_binary_expression
(ctx
, EXPR_SUB
, $1, $3); }
662 /* ECMA-262 3rd Edition 11.5 */
663 MultiplicativeExpression
664 : UnaryExpression
{ $$
= $1; }
665 | MultiplicativeExpression
'*' UnaryExpression
666 { $$
= new_binary_expression
(ctx
, EXPR_MUL
, $1, $3); }
667 | MultiplicativeExpression
'/' UnaryExpression
668 { $$
= new_binary_expression
(ctx
, EXPR_DIV
, $1, $3); }
669 | MultiplicativeExpression
'%' UnaryExpression
670 { $$
= new_binary_expression
(ctx
, EXPR_MOD
, $1, $3); }
672 /* ECMA-262 3rd Edition 11.4 */
674 : PostfixExpression
{ $$
= $1; }
675 | kDELETE UnaryExpression
676 { $$
= new_unary_expression
(ctx
, EXPR_DELETE
, $2); }
677 | kVOID UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_VOID
, $2); }
678 | kTYPEOF UnaryExpression
679 { $$
= new_unary_expression
(ctx
, EXPR_TYPEOF
, $2); }
680 | tINC UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PREINC
, $2); }
681 | tDEC UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PREDEC
, $2); }
682 |
'+' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PLUS
, $2); }
683 |
'-' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_MINUS
, $2); }
684 |
'~' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_BITNEG
, $2); }
685 |
'!' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_LOGNEG
, $2); }
687 /* ECMA-262 3rd Edition 11.2 */
689 : LeftHandSideExpression
691 | LeftHandSideExpression
/* NONL */ tINC
692 { $$
= new_unary_expression
(ctx
, EXPR_POSTINC
, $1); }
693 | LeftHandSideExpression
/* NONL */ tDEC
694 { $$
= new_unary_expression
(ctx
, EXPR_POSTDEC
, $1); }
697 /* ECMA-262 3rd Edition 11.2 */
698 LeftHandSideExpression
699 : NewExpression
{ $$
= $1; }
700 | CallExpression
{ $$
= $1; }
702 /* ECMA-262 3rd Edition 11.2 */
704 : MemberExpression
{ $$
= $1; }
705 | kNEW NewExpression
{ $$
= new_new_expression
(ctx
, $2, NULL
); }
707 /* ECMA-262 3rd Edition 11.2 */
709 : PrimaryExpression
{ $$
= $1; }
710 | FunctionExpression
{ $$
= $1; }
711 | MemberExpression
'[' Expression
']'
712 { $$
= new_binary_expression
(ctx
, EXPR_ARRAY
, $1, $3); }
713 | MemberExpression
'.' tIdentifier
714 { $$
= new_member_expression
(ctx
, $1, $3); }
715 | kNEW MemberExpression Arguments
716 { $$
= new_new_expression
(ctx
, $2, $3); }
718 /* ECMA-262 3rd Edition 11.2 */
720 : MemberExpression Arguments
721 { $$
= new_call_expression
(ctx
, $1, $2); }
722 | CallExpression Arguments
723 { $$
= new_call_expression
(ctx
, $1, $2); }
724 | CallExpression
'[' Expression
']'
725 { $$
= new_binary_expression
(ctx
, EXPR_ARRAY
, $1, $3); }
726 | CallExpression
'.' tIdentifier
727 { $$
= new_member_expression
(ctx
, $1, $3); }
729 /* ECMA-262 3rd Edition 11.2 */
731 : '(' ')' { $$
= NULL
; }
732 |
'(' ArgumentList
')' { $$
= $2; }
734 /* ECMA-262 3rd Edition 11.2 */
736 : AssignmentExpression
{ $$
= new_argument_list
(ctx
, $1); }
737 | ArgumentList
',' AssignmentExpression
738 { $$
= argument_list_add
(ctx
, $1, $3); }
740 /* ECMA-262 3rd Edition 11.1 */
742 : kTHIS
{ $$
= new_expression
(ctx
, EXPR_THIS
, 0); }
743 | tIdentifier
{ $$
= new_identifier_expression
(ctx
, $1); }
744 | Literal
{ $$
= new_literal_expression
(ctx
, $1); }
745 | ArrayLiteral
{ $$
= $1; }
746 | ObjectLiteral
{ $$
= $1; }
747 |
'(' Expression
')' { $$
= $2; }
749 /* ECMA-262 3rd Edition 11.1.4 */
751 : '[' ']' { $$
= new_array_literal_expression
(ctx
, NULL
, 0); }
752 |
'[' Elision
']' { $$
= new_array_literal_expression
(ctx
, NULL
, $2+1); }
753 |
'[' ElementList
']' { $$
= new_array_literal_expression
(ctx
, $2, 0); }
754 |
'[' ElementList
',' Elision_opt
']'
755 { $$
= new_array_literal_expression
(ctx
, $2, $4+1); }
757 /* ECMA-262 3rd Edition 11.1.4 */
759 : Elision_opt AssignmentExpression
760 { $$
= new_element_list
(ctx
, $1, $2); }
761 | ElementList
',' Elision_opt AssignmentExpression
762 { $$
= element_list_add
(ctx
, $1, $3, $4); }
764 /* ECMA-262 3rd Edition 11.1.4 */
767 | Elision
',' { $$
= $1 + 1; }
769 /* ECMA-262 3rd Edition 11.1.4 */
771 : /* empty */ { $$
= 0; }
772 | Elision
{ $$
= $1; }
774 /* ECMA-262 3rd Edition 11.1.5 */
776 : '{' '}' { $$
= new_prop_and_value_expression
(ctx
, NULL
); }
777 |
'{' PropertyNameAndValueList
'}'
778 { $$
= new_prop_and_value_expression
(ctx
, $2); }
780 /* ECMA-262 3rd Edition 11.1.5 */
781 PropertyNameAndValueList
782 : PropertyName
':' AssignmentExpression
783 { $$
= new_property_list
(ctx
, $1, $3); }
784 | PropertyNameAndValueList
',' PropertyName
':' AssignmentExpression
785 { $$
= property_list_add
(ctx
, $1, $3, $5); }
787 /* ECMA-262 3rd Edition 11.1.5 */
789 : tIdentifier
{ $$
= new_string_literal
(ctx
, $1); }
790 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
791 | tNumericLiteral
{ $$
= $1; }
793 /* ECMA-262 3rd Edition 7.6 */
795 : /* empty*/ { $$
= NULL
; }
796 | tIdentifier
{ $$
= $1; }
798 /* ECMA-262 3rd Edition 7.8 */
800 : kNULL
{ $$
= new_null_literal
(ctx
); }
801 | BooleanLiteral
{ $$
= $1; }
802 | tNumericLiteral
{ $$
= $1; }
803 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
804 |
'/' { $$
= parse_regexp
(ctx
);
806 | kDIVEQ
{ $$
= parse_regexp
(ctx
);
809 /* ECMA-262 3rd Edition 7.8.2 */
811 : kTRUE
{ $$
= new_boolean_literal
(ctx
, VARIANT_TRUE
); }
812 | kFALSE
{ $$
= new_boolean_literal
(ctx
, VARIANT_FALSE
); }
813 | tBooleanLiteral
{ $$
= $1; }
817 |
error { if
(!allow_auto_semicolon
(ctx
)) {YYABORT;} }
821 |
error { set_error
(ctx
, JS_E_MISSING_LBRACKET
); YYABORT; }
825 |
error { set_error
(ctx
, JS_E_MISSING_RBRACKET
); YYABORT; }
829 |
error { set_error
(ctx
, JS_E_MISSING_SEMICOLON
); YYABORT; }
833 static BOOL allow_auto_semicolon
(parser_ctx_t
*ctx
)
835 return ctx
->nl || ctx
->ptr
== ctx
->end ||
*(ctx
->ptr
-1) == '}';
838 static literal_t
*new_string_literal
(parser_ctx_t
*ctx
, const WCHAR
*str
)
840 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
842 ret
->type
= LT_STRING
;
848 static literal_t
*new_null_literal
(parser_ctx_t
*ctx
)
850 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
857 static prop_val_t
*new_prop_val
(parser_ctx_t
*ctx
, literal_t
*name
, expression_t
*value
)
859 prop_val_t
*ret
= parser_alloc
(ctx
, sizeof
(prop_val_t
));
868 static property_list_t
*new_property_list
(parser_ctx_t
*ctx
, literal_t
*name
, expression_t
*value
)
870 property_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(property_list_t
));
872 ret
->head
= ret
->tail
= new_prop_val
(ctx
, name
, value
);
877 static property_list_t
*property_list_add
(parser_ctx_t
*ctx
, property_list_t
*list
, literal_t
*name
, expression_t
*value
)
879 list
->tail
= list
->tail
->next
= new_prop_val
(ctx
, name
, value
);
884 static array_element_t
*new_array_element
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
886 array_element_t
*ret
= parser_alloc
(ctx
, sizeof
(array_element_t
));
888 ret
->elision
= elision
;
895 static element_list_t
*new_element_list
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
897 element_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(element_list_t
));
899 ret
->head
= ret
->tail
= new_array_element
(ctx
, elision
, expr
);
904 static element_list_t
*element_list_add
(parser_ctx_t
*ctx
, element_list_t
*list
, int elision
, expression_t
*expr
)
906 list
->tail
= list
->tail
->next
= new_array_element
(ctx
, elision
, expr
);
911 static argument_t
*new_argument
(parser_ctx_t
*ctx
, expression_t
*expr
)
913 argument_t
*ret
= parser_alloc
(ctx
, sizeof
(argument_t
));
921 static argument_list_t
*new_argument_list
(parser_ctx_t
*ctx
, expression_t
*expr
)
923 argument_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(argument_list_t
));
925 ret
->head
= ret
->tail
= new_argument
(ctx
, expr
);
930 static argument_list_t
*argument_list_add
(parser_ctx_t
*ctx
, argument_list_t
*list
, expression_t
*expr
)
932 list
->tail
= list
->tail
->next
= new_argument
(ctx
, expr
);
937 static catch_block_t
*new_catch_block
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
939 catch_block_t
*ret
= parser_alloc
(ctx
, sizeof
(catch_block_t
));
941 ret
->identifier
= identifier
;
942 ret
->statement
= statement
;
947 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_list_t
*stat_list
)
949 case_clausule_t
*ret
= parser_alloc
(ctx
, sizeof
(case_clausule_t
));
952 ret
->stat
= stat_list ? stat_list
->head
: NULL
;
958 static case_list_t
*new_case_list
(parser_ctx_t
*ctx
, case_clausule_t
*case_clausule
)
960 case_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(case_list_t
));
962 ret
->head
= ret
->tail
= case_clausule
;
967 static case_list_t
*case_list_add
(parser_ctx_t
*ctx
, case_list_t
*list
, case_clausule_t
*case_clausule
)
969 list
->tail
= list
->tail
->next
= case_clausule
;
974 static case_clausule_t
*new_case_block
(parser_ctx_t
*ctx
, case_list_t
*case_list1
,
975 case_clausule_t
*default_clausule
, case_list_t
*case_list2
)
977 case_clausule_t
*ret
= NULL
, *iter
= NULL
, *iter2
;
978 statement_t
*stat
= NULL
;
981 ret
= case_list1
->head
;
982 iter
= case_list1
->tail
;
985 if
(default_clausule
) {
987 iter
= iter
->next
= default_clausule
;
989 ret
= iter
= default_clausule
;
994 iter
->next
= case_list2
->head
;
996 ret
= case_list2
->head
;
1002 for
(iter
= ret
; iter
; iter
= iter
->next
) {
1003 for
(iter2
= iter
; iter2
&& !iter2
->stat
; iter2
= iter2
->next
);
1007 while
(iter
!= iter2
) {
1008 iter
->stat
= iter2
->stat
;
1015 stat
->next
= iter
->stat
;
1024 static statement_t
*new_block_statement
(parser_ctx_t
*ctx
, statement_list_t
*list
)
1026 block_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(block_statement_t
));
1028 ret
->stat.eval
= block_statement_eval
;
1029 ret
->stat.next
= NULL
;
1030 ret
->stat_list
= list ? list
->head
: NULL
;
1035 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, expression_t
*expr
)
1037 variable_declaration_t
*ret
= parser_alloc
(ctx
, sizeof
(variable_declaration_t
));
1038 var_list_t
*var_list
= parser_alloc
(ctx
, sizeof
(var_list_t
));
1040 ret
->identifier
= identifier
;
1044 var_list
->identifier
= identifier
;
1045 var_list
->next
= NULL
;
1047 if
(ctx
->func_stack
->var_tail
)
1048 ctx
->func_stack
->var_tail
= ctx
->func_stack
->var_tail
->next
= var_list
;
1050 ctx
->func_stack
->var_head
= ctx
->func_stack
->var_tail
= var_list
;
1055 static variable_list_t
*new_variable_list
(parser_ctx_t
*ctx
, variable_declaration_t
*decl
)
1057 variable_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(variable_list_t
));
1059 ret
->head
= ret
->tail
= decl
;
1064 static variable_list_t
*variable_list_add
(parser_ctx_t
*ctx
, variable_list_t
*list
, variable_declaration_t
*decl
)
1066 list
->tail
= list
->tail
->next
= decl
;
1071 static statement_t
*new_var_statement
(parser_ctx_t
*ctx
, variable_list_t
*variable_list
)
1073 var_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(var_statement_t
));
1075 ret
->stat.eval
= var_statement_eval
;
1076 ret
->stat.next
= NULL
;
1077 ret
->variable_list
= variable_list
->head
;
1082 static statement_t
*new_empty_statement
(parser_ctx_t
*ctx
)
1084 statement_t
*ret
= parser_alloc
(ctx
, sizeof
(statement_t
));
1086 ret
->eval
= empty_statement_eval
;
1092 static statement_t
*new_expression_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1094 expression_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(expression_statement_t
));
1096 ret
->stat.eval
= expression_statement_eval
;
1097 ret
->stat.next
= NULL
;
1103 static statement_t
*new_if_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_t
*if_stat
, statement_t
*else_stat
)
1105 if_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(if_statement_t
));
1107 ret
->stat.eval
= if_statement_eval
;
1108 ret
->stat.next
= NULL
;
1110 ret
->if_stat
= if_stat
;
1111 ret
->else_stat
= else_stat
;
1116 static statement_t
*new_while_statement
(parser_ctx_t
*ctx
, BOOL dowhile
, expression_t
*expr
, statement_t
*stat
)
1118 while_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(while_statement_t
));
1120 ret
->stat.eval
= while_statement_eval
;
1121 ret
->stat.next
= NULL
;
1122 ret
->do_while
= dowhile
;
1124 ret
->statement
= stat
;
1129 static statement_t
*new_for_statement
(parser_ctx_t
*ctx
, variable_list_t
*variable_list
, expression_t
*begin_expr
,
1130 expression_t
*expr
, expression_t
*end_expr
, statement_t
*statement
)
1132 for_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(for_statement_t
));
1134 ret
->stat.eval
= for_statement_eval
;
1135 ret
->stat.next
= NULL
;
1136 ret
->variable_list
= variable_list ? variable_list
->head
: NULL
;
1137 ret
->begin_expr
= begin_expr
;
1139 ret
->end_expr
= end_expr
;
1140 ret
->statement
= statement
;
1145 static statement_t
*new_forin_statement
(parser_ctx_t
*ctx
, variable_declaration_t
*variable
, expression_t
*expr
,
1146 expression_t
*in_expr
, statement_t
*statement
)
1148 forin_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(forin_statement_t
));
1150 ret
->stat.eval
= forin_statement_eval
;
1151 ret
->stat.next
= NULL
;
1152 ret
->variable
= variable
;
1154 ret
->in_expr
= in_expr
;
1155 ret
->statement
= statement
;
1160 static statement_t
*new_continue_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1162 branch_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(branch_statement_t
));
1164 ret
->stat.eval
= continue_statement_eval
;
1165 ret
->stat.next
= NULL
;
1166 ret
->identifier
= identifier
;
1171 static statement_t
*new_break_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1173 branch_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(branch_statement_t
));
1175 ret
->stat.eval
= break_statement_eval
;
1176 ret
->stat.next
= NULL
;
1177 ret
->identifier
= identifier
;
1182 static statement_t
*new_return_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1184 expression_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(expression_statement_t
));
1186 ret
->stat.eval
= return_statement_eval
;
1187 ret
->stat.next
= NULL
;
1193 static statement_t
*new_with_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_t
*statement
)
1195 with_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(with_statement_t
));
1197 ret
->stat.eval
= with_statement_eval
;
1198 ret
->stat.next
= NULL
;
1200 ret
->statement
= statement
;
1205 static statement_t
*new_labelled_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
1207 labelled_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(labelled_statement_t
));
1209 ret
->stat.eval
= labelled_statement_eval
;
1210 ret
->stat.next
= NULL
;
1211 ret
->identifier
= identifier
;
1212 ret
->statement
= statement
;
1217 static statement_t
*new_switch_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, case_clausule_t
*case_list
)
1219 switch_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(switch_statement_t
));
1221 ret
->stat.eval
= switch_statement_eval
;
1222 ret
->stat.next
= NULL
;
1224 ret
->case_list
= case_list
;
1229 static statement_t
*new_throw_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1231 expression_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(expression_statement_t
));
1233 ret
->stat.eval
= throw_statement_eval
;
1234 ret
->stat.next
= NULL
;
1240 static statement_t
*new_try_statement
(parser_ctx_t
*ctx
, statement_t
*try_statement
,
1241 catch_block_t
*catch_block
, statement_t
*finally_statement
)
1243 try_statement_t
*ret
= parser_alloc
(ctx
, sizeof
(try_statement_t
));
1245 ret
->stat.eval
= try_statement_eval
;
1246 ret
->stat.next
= NULL
;
1247 ret
->try_statement
= try_statement
;
1248 ret
->catch_block
= catch_block
;
1249 ret
->finally_statement
= finally_statement
;
1254 static parameter_t
*new_parameter
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1256 parameter_t
*ret
= parser_alloc
(ctx
, sizeof
(parameter_t
));
1258 ret
->identifier
= identifier
;
1264 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1266 parameter_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(parameter_list_t
));
1268 ret
->head
= ret
->tail
= new_parameter
(ctx
, identifier
);
1273 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*ctx
, parameter_list_t
*list
, const WCHAR
*identifier
)
1275 list
->tail
= list
->tail
->next
= new_parameter
(ctx
, identifier
);
1280 static expression_t
*new_function_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
,
1281 parameter_list_t
*parameter_list
, source_elements_t
*source_elements
, const WCHAR
*src_str
, DWORD src_len
)
1283 function_expression_t
*ret
= new_expression
(ctx
, EXPR_FUNC
, sizeof
(*ret
));
1285 ret
->identifier
= identifier
;
1286 ret
->parameter_list
= parameter_list ? parameter_list
->head
: NULL
;
1287 ret
->source_elements
= source_elements
;
1288 ret
->src_str
= src_str
;
1289 ret
->src_len
= src_len
;
1291 if
(ret
->identifier
) {
1292 function_declaration_t
*decl
= parser_alloc
(ctx
, sizeof
(function_declaration_t
));
1297 if
(ctx
->func_stack
->func_tail
)
1298 ctx
->func_stack
->func_tail
= ctx
->func_stack
->func_tail
->next
= decl
;
1300 ctx
->func_stack
->func_head
= ctx
->func_stack
->func_tail
= decl
;
1306 static const expression_eval_t expression_eval_table
[] = {
1307 compiled_expression_eval
,
1308 compiled_expression_eval
,
1309 compiled_expression_eval
,
1310 compiled_expression_eval
,
1311 compiled_expression_eval
,
1312 binary_and_expression_eval
,
1313 instanceof_expression_eval
,
1314 compiled_expression_eval
,
1315 compiled_expression_eval
,
1316 compiled_expression_eval
,
1317 compiled_expression_eval
,
1318 compiled_expression_eval
,
1319 compiled_expression_eval
,
1320 compiled_expression_eval
,
1321 compiled_expression_eval
,
1322 typeof_expression_eval
,
1323 compiled_expression_eval
,
1324 compiled_expression_eval
,
1325 compiled_expression_eval
,
1326 compiled_expression_eval
,
1327 pre_increment_expression_eval
,
1328 pre_decrement_expression_eval
,
1329 compiled_expression_eval
,
1330 compiled_expression_eval
,
1331 compiled_expression_eval
,
1332 compiled_expression_eval
,
1333 compiled_expression_eval
,
1334 compiled_expression_eval
,
1335 compiled_expression_eval
,
1336 compiled_expression_eval
,
1337 compiled_expression_eval
,
1338 compiled_expression_eval
,
1339 left_shift_expression_eval
,
1340 right_shift_expression_eval
,
1341 right2_shift_expression_eval
,
1342 compiled_expression_eval
,
1343 assign_lshift_expression_eval
,
1344 assign_rshift_expression_eval
,
1345 assign_rrshift_expression_eval
,
1346 compiled_expression_eval
,
1347 compiled_expression_eval
,
1348 compiled_expression_eval
,
1349 compiled_expression_eval
,
1350 compiled_expression_eval
,
1351 assign_and_expression_eval
,
1352 compiled_expression_eval
,
1353 compiled_expression_eval
,
1354 compiled_expression_eval
,
1355 array_expression_eval
,
1356 member_expression_eval
,
1357 compiled_expression_eval
,
1358 compiled_expression_eval
,
1359 compiled_expression_eval
,
1360 function_expression_eval
,
1361 identifier_expression_eval
,
1362 array_literal_expression_eval
,
1363 property_value_expression_eval
,
1364 compiled_expression_eval
1367 static void *new_expression
(parser_ctx_t
*ctx
, expression_type_t type
, size_t size
)
1369 expression_t
*ret
= parser_alloc
(ctx
, size ? size
: sizeof
(*ret
));
1372 ret
->eval
= expression_eval_table
[type
];
1377 static expression_t
*new_binary_expression
(parser_ctx_t
*ctx
, expression_type_t type
,
1378 expression_t
*expression1
, expression_t
*expression2
)
1380 binary_expression_t
*ret
= new_expression
(ctx
, type
, sizeof
(*ret
));
1382 ret
->expression1
= expression1
;
1383 ret
->expression2
= expression2
;
1388 static expression_t
*new_unary_expression
(parser_ctx_t
*ctx
, expression_type_t type
, expression_t
*expression
)
1390 unary_expression_t
*ret
= new_expression
(ctx
, type
, sizeof
(*ret
));
1392 ret
->expression
= expression
;
1397 static expression_t
*new_conditional_expression
(parser_ctx_t
*ctx
, expression_t
*expression
,
1398 expression_t
*true_expression
, expression_t
*false_expression
)
1400 conditional_expression_t
*ret
= new_expression
(ctx
, EXPR_COND
, sizeof
(*ret
));
1402 ret
->expression
= expression
;
1403 ret
->true_expression
= true_expression
;
1404 ret
->false_expression
= false_expression
;
1409 static expression_t
*new_member_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, const WCHAR
*identifier
)
1411 member_expression_t
*ret
= new_expression
(ctx
, EXPR_MEMBER
, sizeof
(*ret
));
1413 ret
->expression
= expression
;
1414 ret
->identifier
= identifier
;
1419 static expression_t
*new_new_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, argument_list_t
*argument_list
)
1421 call_expression_t
*ret
= new_expression
(ctx
, EXPR_NEW
, sizeof
(*ret
));
1423 ret
->expression
= expression
;
1424 ret
->argument_list
= argument_list ? argument_list
->head
: NULL
;
1429 static expression_t
*new_call_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, argument_list_t
*argument_list
)
1431 call_expression_t
*ret
= new_expression
(ctx
, EXPR_CALL
, sizeof
(*ret
));
1433 ret
->expression
= expression
;
1434 ret
->argument_list
= argument_list ? argument_list
->head
: NULL
;
1439 static int parser_error
(const char *str
)
1444 static void set_error
(parser_ctx_t
*ctx
, UINT
error)
1449 static BOOL explicit_error
(parser_ctx_t
*ctx
, void *obj
, WCHAR next
)
1451 if
(obj ||
*(ctx
->ptr
-1)==next
) return TRUE
;
1453 set_error
(ctx
, JS_E_SYNTAX
);
1458 static expression_t
*new_identifier_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1460 identifier_expression_t
*ret
= new_expression
(ctx
, EXPR_IDENT
, sizeof
(*ret
));
1462 ret
->identifier
= identifier
;
1467 static expression_t
*new_array_literal_expression
(parser_ctx_t
*ctx
, element_list_t
*element_list
, int length
)
1469 array_literal_expression_t
*ret
= new_expression
(ctx
, EXPR_ARRAYLIT
, sizeof
(*ret
));
1471 ret
->element_list
= element_list ? element_list
->head
: NULL
;
1472 ret
->length
= length
;
1477 static expression_t
*new_prop_and_value_expression
(parser_ctx_t
*ctx
, property_list_t
*property_list
)
1479 property_value_expression_t
*ret
= new_expression
(ctx
, EXPR_PROPVAL
, sizeof
(*ret
));
1481 ret
->property_list
= property_list ? property_list
->head
: NULL
;
1486 static expression_t
*new_literal_expression
(parser_ctx_t
*ctx
, literal_t
*literal
)
1488 literal_expression_t
*ret
= new_expression
(ctx
, EXPR_LITERAL
, sizeof
(*ret
));
1490 ret
->literal
= literal
;
1495 static source_elements_t
*new_source_elements
(parser_ctx_t
*ctx
)
1497 source_elements_t
*ret
= parser_alloc
(ctx
, sizeof
(source_elements_t
));
1499 memset
(ret
, 0, sizeof
(*ret
));
1504 static source_elements_t
*source_elements_add_statement
(source_elements_t
*source_elements
, statement_t
*statement
)
1506 if
(source_elements
->statement_tail
)
1507 source_elements
->statement_tail
= source_elements
->statement_tail
->next
= statement
;
1509 source_elements
->statement
= source_elements
->statement_tail
= statement
;
1511 return source_elements
;
1514 static statement_list_t
*new_statement_list
(parser_ctx_t
*ctx
, statement_t
*statement
)
1516 statement_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(statement_list_t
));
1518 ret
->head
= ret
->tail
= statement
;
1523 static statement_list_t
*statement_list_add
(statement_list_t
*list
, statement_t
*statement
)
1525 list
->tail
= list
->tail
->next
= statement
;
1530 static void push_func
(parser_ctx_t
*ctx
)
1532 func_stack_t
*new_func
= parser_alloc_tmp
(ctx
, sizeof
(func_stack_t
));
1534 new_func
->func_head
= new_func
->func_tail
= NULL
;
1535 new_func
->var_head
= new_func
->var_tail
= NULL
;
1537 new_func
->next
= ctx
->func_stack
;
1538 ctx
->func_stack
= new_func
;
1541 static source_elements_t
*function_body_parsed
(parser_ctx_t
*ctx
, source_elements_t
*source
)
1543 source
->functions
= ctx
->func_stack
->func_head
;
1544 source
->variables
= ctx
->func_stack
->var_head
;
1550 static void program_parsed
(parser_ctx_t
*ctx
, source_elements_t
*source
)
1552 source
->functions
= ctx
->func_stack
->func_head
;
1553 source
->variables
= ctx
->func_stack
->var_head
;
1556 ctx
->source
= source
;
1557 if
(!ctx
->lexer_error
)
1561 void parser_release
(parser_ctx_t
*ctx
)
1567 release_bytecode
(ctx
->code
);
1569 release_compiler
(ctx
->compiler
);
1570 script_release
(ctx
->script
);
1571 heap_free
(ctx
->begin
);
1572 jsheap_free
(&ctx
->heap
);
1576 HRESULT script_parse
(script_ctx_t
*ctx
, const WCHAR
*code
, const WCHAR
*delimiter
,
1579 parser_ctx_t
*parser_ctx
;
1583 const WCHAR html_tagW
[] = {'<','/','s','c','r','i','p','t','>',0};
1585 parser_ctx
= heap_alloc_zero
(sizeof
(parser_ctx_t
));
1587 return E_OUTOFMEMORY
;
1589 parser_ctx
->ref
= 1;
1590 parser_ctx
->hres
= JS_E_SYNTAX
;
1591 parser_ctx
->is_html
= delimiter
&& !strcmpiW
(delimiter
, html_tagW
);
1593 parser_ctx
->begin
= heap_strdupW
(code
);
1594 if
(!parser_ctx
->begin
) {
1595 heap_free
(parser_ctx
);
1596 return E_OUTOFMEMORY
;
1599 parser_ctx
->ptr
= parser_ctx
->begin
;
1600 parser_ctx
->end
= parser_ctx
->begin
+ strlenW
(parser_ctx
->begin
);
1603 parser_ctx
->script
= ctx
;
1605 mark
= jsheap_mark
(&ctx
->tmp_heap
);
1606 jsheap_init
(&parser_ctx
->heap
);
1608 push_func
(parser_ctx
);
1610 parser_parse
(parser_ctx
);
1612 if
(FAILED
(parser_ctx
->hres
)) {
1613 hres
= parser_ctx
->hres
;
1614 parser_release
(parser_ctx
);