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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL
(jscript
);
29 static int parser_error
(parser_ctx_t
*,const char*);
30 static void set_error
(parser_ctx_t
*,UINT
);
31 static BOOL explicit_error
(parser_ctx_t
*,void*,WCHAR
);
32 static BOOL allow_auto_semicolon
(parser_ctx_t
*);
33 static void program_parsed
(parser_ctx_t
*,source_elements_t
*);
35 typedef
struct _statement_list_t
{
40 static literal_t
*new_string_literal
(parser_ctx_t
*,const WCHAR
*);
41 static literal_t
*new_null_literal
(parser_ctx_t
*);
43 typedef
struct _property_list_t
{
48 static property_list_t
*new_property_list
(parser_ctx_t
*,literal_t
*,expression_t
*);
49 static property_list_t
*property_list_add
(parser_ctx_t
*,property_list_t
*,literal_t
*,expression_t
*);
51 typedef
struct _element_list_t
{
52 array_element_t
*head
;
53 array_element_t
*tail
;
56 static element_list_t
*new_element_list
(parser_ctx_t
*,int,expression_t
*);
57 static element_list_t
*element_list_add
(parser_ctx_t
*,element_list_t
*,int,expression_t
*);
59 typedef
struct _argument_list_t
{
64 static argument_list_t
*new_argument_list
(parser_ctx_t
*,expression_t
*);
65 static argument_list_t
*argument_list_add
(parser_ctx_t
*,argument_list_t
*,expression_t
*);
67 typedef
struct _case_list_t
{
68 case_clausule_t
*head
;
69 case_clausule_t
*tail
;
72 static catch_block_t
*new_catch_block
(parser_ctx_t
*,const WCHAR
*,statement_t
*);
73 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*,expression_t
*,statement_list_t
*);
74 static case_list_t
*new_case_list
(parser_ctx_t
*,case_clausule_t
*);
75 static case_list_t
*case_list_add
(parser_ctx_t
*,case_list_t
*,case_clausule_t
*);
76 static case_clausule_t
*new_case_block
(parser_ctx_t
*,case_list_t
*,case_clausule_t
*,case_list_t
*);
78 typedef
struct _variable_list_t
{
79 variable_declaration_t
*head
;
80 variable_declaration_t
*tail
;
83 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*,const WCHAR
*,expression_t
*);
84 static variable_list_t
*new_variable_list
(parser_ctx_t
*,variable_declaration_t
*);
85 static variable_list_t
*variable_list_add
(parser_ctx_t
*,variable_list_t
*,variable_declaration_t
*);
87 static void *new_statement
(parser_ctx_t
*,statement_type_t
,size_t);
88 static statement_t
*new_block_statement
(parser_ctx_t
*,statement_list_t
*);
89 static statement_t
*new_var_statement
(parser_ctx_t
*,variable_list_t
*);
90 static statement_t
*new_expression_statement
(parser_ctx_t
*,expression_t
*);
91 static statement_t
*new_if_statement
(parser_ctx_t
*,expression_t
*,statement_t
*,statement_t
*);
92 static statement_t
*new_while_statement
(parser_ctx_t
*,BOOL
,expression_t
*,statement_t
*);
93 static statement_t
*new_for_statement
(parser_ctx_t
*,variable_list_t
*,expression_t
*,expression_t
*,
94 expression_t
*,statement_t
*);
95 static statement_t
*new_forin_statement
(parser_ctx_t
*,variable_declaration_t
*,expression_t
*,expression_t
*,statement_t
*);
96 static statement_t
*new_continue_statement
(parser_ctx_t
*,const WCHAR
*);
97 static statement_t
*new_break_statement
(parser_ctx_t
*,const WCHAR
*);
98 static statement_t
*new_return_statement
(parser_ctx_t
*,expression_t
*);
99 static statement_t
*new_with_statement
(parser_ctx_t
*,expression_t
*,statement_t
*);
100 static statement_t
*new_labelled_statement
(parser_ctx_t
*,const WCHAR
*,statement_t
*);
101 static statement_t
*new_switch_statement
(parser_ctx_t
*,expression_t
*,case_clausule_t
*);
102 static statement_t
*new_throw_statement
(parser_ctx_t
*,expression_t
*);
103 static statement_t
*new_try_statement
(parser_ctx_t
*,statement_t
*,catch_block_t
*,statement_t
*);
105 struct statement_list_t
{
110 static statement_list_t
*new_statement_list
(parser_ctx_t
*,statement_t
*);
111 static statement_list_t
*statement_list_add
(statement_list_t
*,statement_t
*);
113 typedef
struct _parameter_list_t
{
118 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*,const WCHAR
*);
119 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*,parameter_list_t
*,const WCHAR
*);
121 static void *new_expression
(parser_ctx_t
*ctx
,expression_type_t
,size_t);
122 static expression_t
*new_function_expression
(parser_ctx_t
*,const WCHAR
*,parameter_list_t
*,
123 source_elements_t
*,const WCHAR
*,const WCHAR
*,DWORD
);
124 static expression_t
*new_binary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*,expression_t
*);
125 static expression_t
*new_unary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*);
126 static expression_t
*new_conditional_expression
(parser_ctx_t
*,expression_t
*,expression_t
*,expression_t
*);
127 static expression_t
*new_member_expression
(parser_ctx_t
*,expression_t
*,const WCHAR
*);
128 static expression_t
*new_new_expression
(parser_ctx_t
*,expression_t
*,argument_list_t
*);
129 static expression_t
*new_call_expression
(parser_ctx_t
*,expression_t
*,argument_list_t
*);
130 static expression_t
*new_identifier_expression
(parser_ctx_t
*,const WCHAR
*);
131 static expression_t
*new_literal_expression
(parser_ctx_t
*,literal_t
*);
132 static expression_t
*new_array_literal_expression
(parser_ctx_t
*,element_list_t
*,int);
133 static expression_t
*new_prop_and_value_expression
(parser_ctx_t
*,property_list_t
*);
135 static source_elements_t
*new_source_elements
(parser_ctx_t
*);
136 static source_elements_t
*source_elements_add_statement
(source_elements_t
*,statement_t
*);
140 %lex
-param
{ parser_ctx_t
*ctx
}
141 %parse
-param
{ parser_ctx_t
*ctx
}
150 struct _argument_list_t
*argument_list
;
151 case_clausule_t
*case_clausule
;
152 struct _case_list_t
*case_list
;
153 catch_block_t
*catch_block
;
154 struct _element_list_t
*element_list
;
156 const WCHAR
*identifier
;
157 struct _parameter_list_t
*parameter_list
;
158 struct _property_list_t
*property_list
;
159 source_elements_t
*source_elements
;
160 statement_t
*statement
;
161 struct _statement_list_t
*statement_list
;
162 struct _variable_list_t
*variable_list
;
163 variable_declaration_t
*variable_declaration
;
167 %token
<identifier
> kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kFUNCTION kIF kFINALLY kFOR kIN
168 %token
<identifier
> kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
169 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL
174 %token
<identifier
> tIdentifier
175 %token
<ival
> tAssignOper tEqOper tShiftOper tRelOper
176 %token
<literal
> tNumericLiteral tBooleanLiteral
177 %token
<wstr
> tStringLiteral
180 %type
<source_elements
> SourceElements
181 %type
<source_elements
> FunctionBody
182 %type
<statement
> Statement
183 %type
<statement
> Block
184 %type
<statement
> VariableStatement
185 %type
<statement
> EmptyStatement
186 %type
<statement
> ExpressionStatement
187 %type
<statement
> IfStatement
188 %type
<statement
> IterationStatement
189 %type
<statement
> ContinueStatement
190 %type
<statement
> BreakStatement
191 %type
<statement
> ReturnStatement
192 %type
<statement
> WithStatement
193 %type
<statement
> LabelledStatement
194 %type
<statement
> SwitchStatement
195 %type
<statement
> ThrowStatement
196 %type
<statement
> TryStatement
197 %type
<statement
> Finally
198 %type
<statement_list
> StatementList StatementList_opt
199 %type
<parameter_list
> FormalParameterList FormalParameterList_opt
200 %type
<expr
> Expression Expression_opt Expression_err
201 %type
<expr
> ExpressionNoIn ExpressionNoIn_opt
202 %type
<expr
> FunctionExpression
203 %type
<expr
> AssignmentExpression AssignmentExpressionNoIn
204 %type
<expr
> ConditionalExpression ConditionalExpressionNoIn
205 %type
<expr
> LeftHandSideExpression
206 %type
<expr
> LogicalORExpression LogicalORExpressionNoIn
207 %type
<expr
> LogicalANDExpression LogicalANDExpressionNoIn
208 %type
<expr
> BitwiseORExpression BitwiseORExpressionNoIn
209 %type
<expr
> BitwiseXORExpression BitwiseXORExpressionNoIn
210 %type
<expr
> BitwiseANDExpression BitwiseANDExpressionNoIn
211 %type
<expr
> EqualityExpression EqualityExpressionNoIn
212 %type
<expr
> RelationalExpression RelationalExpressionNoIn
213 %type
<expr
> ShiftExpression
214 %type
<expr
> AdditiveExpression
215 %type
<expr
> MultiplicativeExpression
216 %type
<expr
> Initialiser_opt Initialiser
217 %type
<expr
> InitialiserNoIn_opt InitialiserNoIn
218 %type
<expr
> UnaryExpression
219 %type
<expr
> PostfixExpression
220 %type
<expr
> NewExpression
221 %type
<expr
> CallExpression
222 %type
<expr
> MemberExpression
223 %type
<expr
> PrimaryExpression
224 %type
<identifier
> Identifier_opt
225 %type
<variable_list
> VariableDeclarationList
226 %type
<variable_list
> VariableDeclarationListNoIn
227 %type
<variable_declaration
> VariableDeclaration
228 %type
<variable_declaration
> VariableDeclarationNoIn
229 %type
<case_list
> CaseClausules CaseClausules_opt
230 %type
<case_clausule
> CaseClausule DefaultClausule CaseBlock
231 %type
<catch_block
> Catch
232 %type
<argument_list
> Arguments
233 %type
<argument_list
> ArgumentList
234 %type
<literal
> Literal
235 %type
<expr
> ArrayLiteral
236 %type
<expr
> ObjectLiteral
237 %type
<ival
> Elision Elision_opt
238 %type
<element_list
> ElementList
239 %type
<property_list
> PropertyNameAndValueList
240 %type
<literal
> PropertyName
241 %type
<literal
> BooleanLiteral
242 %type
<srcptr
> KFunction
243 %type
<ival
> AssignOper
244 %type
<identifier
> IdentifierName ReservedAsIdentifier
246 %nonassoc LOWER_THAN_ELSE
251 /* ECMA-262 3rd Edition 14 */
253 : SourceElements HtmlComment tEOF
254 { program_parsed
(ctx
, $1); }
260 /* ECMA-262 3rd Edition 14 */
262 : /* empty */ { $$
= new_source_elements
(ctx
); }
263 | SourceElements Statement
264 { $$
= source_elements_add_statement
($1, $2); }
266 /* ECMA-262 3rd Edition 13 */
268 : KFunction left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
269 { $$
= new_function_expression
(ctx
, NULL
, $3, $6, NULL
, $1, $7-$1+1); }
270 | KFunction tIdentifier left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
271 { $$
= new_function_expression
(ctx
, $2, $4, $7, NULL
, $1, $8-$1+1); }
272 | KFunction tIdentifier kDCOL tIdentifier left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
273 { $$
= new_function_expression
(ctx
, $4, $6, $9, $2, $1, $10-$1+1); }
276 : kFUNCTION
{ $$
= ctx
->ptr
- 8; }
278 /* ECMA-262 3rd Edition 13 */
280 : SourceElements
{ $$
= $1; }
282 /* ECMA-262 3rd Edition 13 */
284 : tIdentifier
{ $$
= new_parameter_list
(ctx
, $1); }
285 | FormalParameterList
',' tIdentifier
286 { $$
= parameter_list_add
(ctx
, $1, $3); }
288 /* ECMA-262 3rd Edition 13 */
289 FormalParameterList_opt
290 : /* empty */ { $$
= NULL
; }
291 | FormalParameterList
{ $$
= $1; }
293 /* ECMA-262 3rd Edition 12 */
296 | VariableStatement
{ $$
= $1; }
297 | EmptyStatement
{ $$
= $1; }
298 | FunctionExpression
{ $$
= new_expression_statement
(ctx
, $1); }
299 | ExpressionStatement
{ $$
= $1; }
300 | IfStatement
{ $$
= $1; }
301 | IterationStatement
{ $$
= $1; }
302 | ContinueStatement
{ $$
= $1; }
303 | BreakStatement
{ $$
= $1; }
304 | ReturnStatement
{ $$
= $1; }
305 | WithStatement
{ $$
= $1; }
306 | LabelledStatement
{ $$
= $1; }
307 | SwitchStatement
{ $$
= $1; }
308 | ThrowStatement
{ $$
= $1; }
309 | TryStatement
{ $$
= $1; }
311 /* ECMA-262 3rd Edition 12.2 */
313 : Statement
{ $$
= new_statement_list
(ctx
, $1); }
314 | StatementList Statement
315 { $$
= statement_list_add
($1, $2); }
317 /* ECMA-262 3rd Edition 12.2 */
319 : /* empty */ { $$
= NULL
; }
320 | StatementList
{ $$
= $1; }
322 /* ECMA-262 3rd Edition 12.1 */
324 : '{' StatementList
'}' { $$
= new_block_statement
(ctx
, $2); }
325 |
'{' '}' { $$
= new_block_statement
(ctx
, NULL
); }
327 /* ECMA-262 3rd Edition 12.2 */
329 : kVAR VariableDeclarationList semicolon_opt
330 { $$
= new_var_statement
(ctx
, $2); }
332 /* ECMA-262 3rd Edition 12.2 */
333 VariableDeclarationList
334 : VariableDeclaration
{ $$
= new_variable_list
(ctx
, $1); }
335 | VariableDeclarationList
',' VariableDeclaration
336 { $$
= variable_list_add
(ctx
, $1, $3); }
338 /* ECMA-262 3rd Edition 12.2 */
339 VariableDeclarationListNoIn
340 : VariableDeclarationNoIn
341 { $$
= new_variable_list
(ctx
, $1); }
342 | VariableDeclarationListNoIn
',' VariableDeclarationNoIn
343 { $$
= variable_list_add
(ctx
, $1, $3); }
345 /* ECMA-262 3rd Edition 12.2 */
347 : tIdentifier Initialiser_opt
348 { $$
= new_variable_declaration
(ctx
, $1, $2); }
350 /* ECMA-262 3rd Edition 12.2 */
351 VariableDeclarationNoIn
352 : tIdentifier InitialiserNoIn_opt
353 { $$
= new_variable_declaration
(ctx
, $1, $2); }
355 /* ECMA-262 3rd Edition 12.2 */
357 : /* empty */ { $$
= NULL
; }
358 | Initialiser
{ $$
= $1; }
360 /* ECMA-262 3rd Edition 12.2 */
362 : '=' AssignmentExpression
365 /* ECMA-262 3rd Edition 12.2 */
367 : /* empty */ { $$
= NULL
; }
368 | InitialiserNoIn
{ $$
= $1; }
370 /* ECMA-262 3rd Edition 12.2 */
372 : '=' AssignmentExpressionNoIn
375 /* ECMA-262 3rd Edition 12.3 */
377 : ';' { $$
= new_statement
(ctx
, STAT_EMPTY
, 0); }
379 /* ECMA-262 3rd Edition 12.4 */
381 : Expression semicolon_opt
382 { $$
= new_expression_statement
(ctx
, $1); }
384 /* ECMA-262 3rd Edition 12.5 */
386 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
387 { $$
= new_if_statement
(ctx
, $3, $5, $7); }
388 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
389 { $$
= new_if_statement
(ctx
, $3, $5, NULL
); }
391 /* ECMA-262 3rd Edition 12.6 */
393 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
394 { $$
= new_while_statement
(ctx
, TRUE
, $5, $2); }
395 | kWHILE left_bracket Expression_err right_bracket Statement
396 { $$
= new_while_statement
(ctx
, FALSE
, $3, $5); }
397 | kFOR left_bracket ExpressionNoIn_opt
398 { if
(!explicit_error
(ctx
, $3, ';')) YYABORT; }
399 semicolon Expression_opt
400 { if
(!explicit_error
(ctx
, $6, ';')) YYABORT; }
401 semicolon Expression_opt right_bracket Statement
402 { $$
= new_for_statement
(ctx
, NULL
, $3, $6, $9, $11); }
403 | kFOR left_bracket kVAR VariableDeclarationListNoIn
404 { if
(!explicit_error
(ctx
, $4, ';')) YYABORT; }
405 semicolon Expression_opt
406 { if
(!explicit_error
(ctx
, $7, ';')) YYABORT; }
407 semicolon Expression_opt right_bracket Statement
408 { $$
= new_for_statement
(ctx
, $4, NULL
, $7, $10, $12); }
409 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
410 { $$
= new_forin_statement
(ctx
, NULL
, $3, $5, $7); }
411 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
412 { $$
= new_forin_statement
(ctx
, $4, NULL
, $6, $8); }
414 /* ECMA-262 3rd Edition 12.7 */
416 : kCONTINUE
/* NONL */ Identifier_opt semicolon_opt
417 { $$
= new_continue_statement
(ctx
, $2); }
419 /* ECMA-262 3rd Edition 12.8 */
421 : kBREAK
/* NONL */ Identifier_opt semicolon_opt
422 { $$
= new_break_statement
(ctx
, $2); }
424 /* ECMA-262 3rd Edition 12.9 */
426 : kRETURN
/* NONL */ Expression_opt semicolon_opt
427 { $$
= new_return_statement
(ctx
, $2); }
429 /* ECMA-262 3rd Edition 12.10 */
431 : kWITH left_bracket Expression right_bracket Statement
432 { $$
= new_with_statement
(ctx
, $3, $5); }
434 /* ECMA-262 3rd Edition 12.12 */
436 : tIdentifier
':' Statement
437 { $$
= new_labelled_statement
(ctx
, $1, $3); }
439 /* ECMA-262 3rd Edition 12.11 */
441 : kSWITCH left_bracket Expression right_bracket CaseBlock
442 { $$
= new_switch_statement
(ctx
, $3, $5); }
444 /* ECMA-262 3rd Edition 12.11 */
446 : '{' CaseClausules_opt
'}'
447 { $$
= new_case_block
(ctx
, $2, NULL
, NULL
); }
448 |
'{' CaseClausules_opt DefaultClausule CaseClausules_opt
'}'
449 { $$
= new_case_block
(ctx
, $2, $3, $4); }
451 /* ECMA-262 3rd Edition 12.11 */
453 : /* empty */ { $$
= NULL
; }
454 | CaseClausules
{ $$
= $1; }
456 /* ECMA-262 3rd Edition 12.11 */
458 : CaseClausule
{ $$
= new_case_list
(ctx
, $1); }
459 | CaseClausules CaseClausule
460 { $$
= case_list_add
(ctx
, $1, $2); }
462 /* ECMA-262 3rd Edition 12.11 */
464 : kCASE Expression
':' StatementList_opt
465 { $$
= new_case_clausule
(ctx
, $2, $4); }
467 /* ECMA-262 3rd Edition 12.11 */
469 : kDEFAULT
':' StatementList_opt
470 { $$
= new_case_clausule
(ctx
, NULL
, $3); }
472 /* ECMA-262 3rd Edition 12.13 */
474 : kTHROW
/* NONL */ Expression semicolon_opt
475 { $$
= new_throw_statement
(ctx
, $2); }
477 /* ECMA-262 3rd Edition 12.14 */
479 : kTRY Block Catch
{ $$
= new_try_statement
(ctx
, $2, $3, NULL
); }
480 | kTRY Block Finally
{ $$
= new_try_statement
(ctx
, $2, NULL
, $3); }
481 | kTRY Block Catch Finally
482 { $$
= new_try_statement
(ctx
, $2, $3, $4); }
484 /* ECMA-262 3rd Edition 12.14 */
486 : kCATCH left_bracket tIdentifier right_bracket Block
487 { $$
= new_catch_block
(ctx
, $3, $5); }
489 /* ECMA-262 3rd Edition 12.14 */
491 : kFINALLY Block
{ $$
= $2; }
493 /* ECMA-262 3rd Edition 11.14 */
495 : /* empty */ { $$
= NULL
; }
496 | Expression
{ $$
= $1; }
499 : Expression
{ $$
= $1; }
500 |
error { set_error
(ctx
, JS_E_SYNTAX
); YYABORT; }
502 /* ECMA-262 3rd Edition 11.14 */
504 : AssignmentExpression
{ $$
= $1; }
505 | Expression
',' AssignmentExpression
506 { $$
= new_binary_expression
(ctx
, EXPR_COMMA
, $1, $3); }
508 /* ECMA-262 3rd Edition 11.14 */
510 : /* empty */ { $$
= NULL
; }
511 | ExpressionNoIn
{ $$
= $1; }
513 /* ECMA-262 3rd Edition 11.14 */
515 : AssignmentExpressionNoIn
517 | ExpressionNoIn
',' AssignmentExpressionNoIn
518 { $$
= new_binary_expression
(ctx
, EXPR_COMMA
, $1, $3); }
521 : tAssignOper
{ $$
= $1; }
522 | kDIVEQ
{ $$
= EXPR_ASSIGNDIV
; }
524 /* ECMA-262 3rd Edition 11.13 */
526 : ConditionalExpression
{ $$
= $1; }
527 | LeftHandSideExpression
'=' AssignmentExpression
528 { $$
= new_binary_expression
(ctx
, EXPR_ASSIGN
, $1, $3); }
529 | LeftHandSideExpression AssignOper AssignmentExpression
530 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
532 /* ECMA-262 3rd Edition 11.13 */
533 AssignmentExpressionNoIn
534 : ConditionalExpressionNoIn
536 | LeftHandSideExpression
'=' AssignmentExpressionNoIn
537 { $$
= new_binary_expression
(ctx
, EXPR_ASSIGN
, $1, $3); }
538 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
539 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
541 /* ECMA-262 3rd Edition 11.12 */
542 ConditionalExpression
543 : LogicalORExpression
{ $$
= $1; }
544 | LogicalORExpression
'?' AssignmentExpression
':' AssignmentExpression
545 { $$
= new_conditional_expression
(ctx
, $1, $3, $5); }
547 /* ECMA-262 3rd Edition 11.12 */
548 ConditionalExpressionNoIn
549 : LogicalORExpressionNoIn
551 | LogicalORExpressionNoIn
'?' AssignmentExpressionNoIn
':' AssignmentExpressionNoIn
552 { $$
= new_conditional_expression
(ctx
, $1, $3, $5); }
554 /* ECMA-262 3rd Edition 11.11 */
556 : LogicalANDExpression
{ $$
= $1; }
557 | LogicalORExpression tOROR LogicalANDExpression
558 { $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); }
560 /* ECMA-262 3rd Edition 11.11 */
561 LogicalORExpressionNoIn
562 : LogicalANDExpressionNoIn
564 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
565 { $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); }
567 /* ECMA-262 3rd Edition 11.11 */
569 : BitwiseORExpression
{ $$
= $1; }
570 | LogicalANDExpression tANDAND BitwiseORExpression
571 { $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); }
573 /* ECMA-262 3rd Edition 11.11 */
574 LogicalANDExpressionNoIn
575 : BitwiseORExpressionNoIn
577 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
578 { $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); }
580 /* ECMA-262 3rd Edition 11.10 */
582 : BitwiseXORExpression
{ $$
= $1; }
583 | BitwiseORExpression
'|' BitwiseXORExpression
584 { $$
= new_binary_expression
(ctx
, EXPR_BOR
, $1, $3); }
586 /* ECMA-262 3rd Edition 11.10 */
587 BitwiseORExpressionNoIn
588 : BitwiseXORExpressionNoIn
590 | BitwiseORExpressionNoIn
'|' BitwiseXORExpressionNoIn
591 { $$
= new_binary_expression
(ctx
, EXPR_BOR
, $1, $3); }
593 /* ECMA-262 3rd Edition 11.10 */
595 : BitwiseANDExpression
{ $$
= $1; }
596 | BitwiseXORExpression
'^' BitwiseANDExpression
597 { $$
= new_binary_expression
(ctx
, EXPR_BXOR
, $1, $3); }
599 /* ECMA-262 3rd Edition 11.10 */
600 BitwiseXORExpressionNoIn
601 : BitwiseANDExpressionNoIn
603 | BitwiseXORExpressionNoIn
'^' BitwiseANDExpressionNoIn
604 { $$
= new_binary_expression
(ctx
, EXPR_BXOR
, $1, $3); }
606 /* ECMA-262 3rd Edition 11.10 */
608 : EqualityExpression
{ $$
= $1; }
609 | BitwiseANDExpression
'&' EqualityExpression
610 { $$
= new_binary_expression
(ctx
, EXPR_BAND
, $1, $3); }
612 /* ECMA-262 3rd Edition 11.10 */
613 BitwiseANDExpressionNoIn
614 : EqualityExpressionNoIn
616 | BitwiseANDExpressionNoIn
'&' EqualityExpressionNoIn
617 { $$
= new_binary_expression
(ctx
, EXPR_BAND
, $1, $3); }
619 /* ECMA-262 3rd Edition 11.9 */
621 : RelationalExpression
{ $$
= $1; }
622 | EqualityExpression tEqOper RelationalExpression
623 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
625 /* ECMA-262 3rd Edition 11.9 */
626 EqualityExpressionNoIn
627 : RelationalExpressionNoIn
{ $$
= $1; }
628 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
629 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
631 /* ECMA-262 3rd Edition 11.8 */
633 : ShiftExpression
{ $$
= $1; }
634 | RelationalExpression tRelOper ShiftExpression
635 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
636 | RelationalExpression kINSTANCEOF ShiftExpression
637 { $$
= new_binary_expression
(ctx
, EXPR_INSTANCEOF
, $1, $3); }
638 | RelationalExpression kIN ShiftExpression
639 { $$
= new_binary_expression
(ctx
, EXPR_IN
, $1, $3); }
641 /* ECMA-262 3rd Edition 11.8 */
642 RelationalExpressionNoIn
643 : ShiftExpression
{ $$
= $1; }
644 | RelationalExpressionNoIn tRelOper ShiftExpression
645 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
646 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
647 { $$
= new_binary_expression
(ctx
, EXPR_INSTANCEOF
, $1, $3); }
649 /* ECMA-262 3rd Edition 11.7 */
651 : AdditiveExpression
{ $$
= $1; }
652 | ShiftExpression tShiftOper AdditiveExpression
653 { $$
= new_binary_expression
(ctx
, $2, $1, $3); }
655 /* ECMA-262 3rd Edition 11.6 */
657 : MultiplicativeExpression
659 | AdditiveExpression
'+' MultiplicativeExpression
660 { $$
= new_binary_expression
(ctx
, EXPR_ADD
, $1, $3); }
661 | AdditiveExpression
'-' MultiplicativeExpression
662 { $$
= new_binary_expression
(ctx
, EXPR_SUB
, $1, $3); }
664 /* ECMA-262 3rd Edition 11.5 */
665 MultiplicativeExpression
666 : UnaryExpression
{ $$
= $1; }
667 | MultiplicativeExpression
'*' UnaryExpression
668 { $$
= new_binary_expression
(ctx
, EXPR_MUL
, $1, $3); }
669 | MultiplicativeExpression
'/' UnaryExpression
670 { $$
= new_binary_expression
(ctx
, EXPR_DIV
, $1, $3); }
671 | MultiplicativeExpression
'%' UnaryExpression
672 { $$
= new_binary_expression
(ctx
, EXPR_MOD
, $1, $3); }
674 /* ECMA-262 3rd Edition 11.4 */
676 : PostfixExpression
{ $$
= $1; }
677 | kDELETE UnaryExpression
678 { $$
= new_unary_expression
(ctx
, EXPR_DELETE
, $2); }
679 | kVOID UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_VOID
, $2); }
680 | kTYPEOF UnaryExpression
681 { $$
= new_unary_expression
(ctx
, EXPR_TYPEOF
, $2); }
682 | tINC UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PREINC
, $2); }
683 | tDEC UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PREDEC
, $2); }
684 |
'+' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_PLUS
, $2); }
685 |
'-' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_MINUS
, $2); }
686 |
'~' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_BITNEG
, $2); }
687 |
'!' UnaryExpression
{ $$
= new_unary_expression
(ctx
, EXPR_LOGNEG
, $2); }
689 /* ECMA-262 3rd Edition 11.2 */
691 : LeftHandSideExpression
693 | LeftHandSideExpression
/* NONL */ tINC
694 { $$
= new_unary_expression
(ctx
, EXPR_POSTINC
, $1); }
695 | LeftHandSideExpression
/* NONL */ tDEC
696 { $$
= new_unary_expression
(ctx
, EXPR_POSTDEC
, $1); }
699 /* ECMA-262 3rd Edition 11.2 */
700 LeftHandSideExpression
701 : NewExpression
{ $$
= $1; }
702 | CallExpression
{ $$
= $1; }
704 /* ECMA-262 3rd Edition 11.2 */
706 : MemberExpression
{ $$
= $1; }
707 | kNEW NewExpression
{ $$
= new_new_expression
(ctx
, $2, NULL
); }
709 /* ECMA-262 3rd Edition 11.2 */
711 : PrimaryExpression
{ $$
= $1; }
712 | FunctionExpression
{ $$
= $1; }
713 | MemberExpression
'[' Expression
']'
714 { $$
= new_binary_expression
(ctx
, EXPR_ARRAY
, $1, $3); }
715 | MemberExpression
'.' IdentifierName
716 { $$
= new_member_expression
(ctx
, $1, $3); }
717 | kNEW MemberExpression Arguments
718 { $$
= new_new_expression
(ctx
, $2, $3); }
720 /* ECMA-262 3rd Edition 11.2 */
722 : MemberExpression Arguments
723 { $$
= new_call_expression
(ctx
, $1, $2); }
724 | CallExpression Arguments
725 { $$
= new_call_expression
(ctx
, $1, $2); }
726 | CallExpression
'[' Expression
']'
727 { $$
= new_binary_expression
(ctx
, EXPR_ARRAY
, $1, $3); }
728 | CallExpression
'.' IdentifierName
729 { $$
= new_member_expression
(ctx
, $1, $3); }
731 /* ECMA-262 3rd Edition 11.2 */
733 : '(' ')' { $$
= NULL
; }
734 |
'(' ArgumentList
')' { $$
= $2; }
736 /* ECMA-262 3rd Edition 11.2 */
738 : AssignmentExpression
{ $$
= new_argument_list
(ctx
, $1); }
739 | ArgumentList
',' AssignmentExpression
740 { $$
= argument_list_add
(ctx
, $1, $3); }
742 /* ECMA-262 3rd Edition 11.1 */
744 : kTHIS
{ $$
= new_expression
(ctx
, EXPR_THIS
, 0); }
745 | tIdentifier
{ $$
= new_identifier_expression
(ctx
, $1); }
746 | Literal
{ $$
= new_literal_expression
(ctx
, $1); }
747 | ArrayLiteral
{ $$
= $1; }
748 | ObjectLiteral
{ $$
= $1; }
749 |
'(' Expression
')' { $$
= $2; }
751 /* ECMA-262 3rd Edition 11.1.4 */
753 : '[' ']' { $$
= new_array_literal_expression
(ctx
, NULL
, 0); }
754 |
'[' Elision
']' { $$
= new_array_literal_expression
(ctx
, NULL
, $2+1); }
755 |
'[' ElementList
']' { $$
= new_array_literal_expression
(ctx
, $2, 0); }
756 |
'[' ElementList
',' Elision_opt
']'
757 { $$
= new_array_literal_expression
(ctx
, $2, $4+1); }
759 /* ECMA-262 3rd Edition 11.1.4 */
761 : Elision_opt AssignmentExpression
762 { $$
= new_element_list
(ctx
, $1, $2); }
763 | ElementList
',' Elision_opt AssignmentExpression
764 { $$
= element_list_add
(ctx
, $1, $3, $4); }
766 /* ECMA-262 3rd Edition 11.1.4 */
769 | Elision
',' { $$
= $1 + 1; }
771 /* ECMA-262 3rd Edition 11.1.4 */
773 : /* empty */ { $$
= 0; }
774 | Elision
{ $$
= $1; }
776 /* ECMA-262 3rd Edition 11.1.5 */
778 : '{' '}' { $$
= new_prop_and_value_expression
(ctx
, NULL
); }
779 |
'{' PropertyNameAndValueList
'}'
780 { $$
= new_prop_and_value_expression
(ctx
, $2); }
782 /* ECMA-262 3rd Edition 11.1.5 */
783 PropertyNameAndValueList
784 : PropertyName
':' AssignmentExpression
785 { $$
= new_property_list
(ctx
, $1, $3); }
786 | PropertyNameAndValueList
',' PropertyName
':' AssignmentExpression
787 { $$
= property_list_add
(ctx
, $1, $3, $5); }
789 /* ECMA-262 3rd Edition 11.1.5 */
791 : IdentifierName
{ $$
= new_string_literal
(ctx
, $1); }
792 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
793 | tNumericLiteral
{ $$
= $1; }
795 /* ECMA-262 3rd Edition 7.6 */
797 : /* empty*/ { $$
= NULL
; }
798 | tIdentifier
{ $$
= $1; }
800 /* ECMA-262 5.1 Edition 7.6 */
802 : tIdentifier
{ $$
= $1; }
803 | ReservedAsIdentifier
805 if
(ctx
->script
->version
< SCRIPTLANGUAGEVERSION_ES5
) {
806 WARN
("%s keyword used as an identifier in legacy mode.\n",
814 : kBREAK
{ $$
= $1; }
816 | kCATCH
{ $$
= $1; }
817 | kCONTINUE
{ $$
= $1; }
818 | kDEFAULT
{ $$
= $1; }
819 | kDELETE
{ $$
= $1; }
822 | kFALSE
{ $$
= $1; }
823 | kFINALLY
{ $$
= $1; }
825 | kFUNCTION
{ $$
= $1; }
828 | kINSTANCEOF
{ $$
= $1; }
831 | kRETURN
{ $$
= $1; }
832 | kSWITCH
{ $$
= $1; }
834 | kTHROW
{ $$
= $1; }
837 | kTYPEOF
{ $$
= $1; }
840 | kWHILE
{ $$
= $1; }
843 /* ECMA-262 3rd Edition 7.8 */
845 : kNULL
{ $$
= new_null_literal
(ctx
); }
846 | BooleanLiteral
{ $$
= $1; }
847 | tNumericLiteral
{ $$
= $1; }
848 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
849 |
'/' { $$
= parse_regexp
(ctx
);
851 | kDIVEQ
{ $$
= parse_regexp
(ctx
);
854 /* ECMA-262 3rd Edition 7.8.2 */
856 : kTRUE
{ $$
= new_boolean_literal
(ctx
, VARIANT_TRUE
); }
857 | kFALSE
{ $$
= new_boolean_literal
(ctx
, VARIANT_FALSE
); }
858 | tBooleanLiteral
{ $$
= $1; }
862 |
error { if
(!allow_auto_semicolon
(ctx
)) {YYABORT;} }
866 |
error { set_error
(ctx
, JS_E_MISSING_LBRACKET
); YYABORT; }
870 |
error { set_error
(ctx
, JS_E_MISSING_RBRACKET
); YYABORT; }
874 |
error { set_error
(ctx
, JS_E_MISSING_SEMICOLON
); YYABORT; }
878 static BOOL allow_auto_semicolon
(parser_ctx_t
*ctx
)
880 return ctx
->nl || ctx
->ptr
== ctx
->end ||
*(ctx
->ptr
-1) == '}';
883 static void *new_statement
(parser_ctx_t
*ctx
, statement_type_t type
, size_t size
)
887 stat
= parser_alloc
(ctx
, size ? size
: sizeof
(*stat
));
897 static literal_t
*new_string_literal
(parser_ctx_t
*ctx
, const WCHAR
*str
)
899 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
901 ret
->type
= LT_STRING
;
907 static literal_t
*new_null_literal
(parser_ctx_t
*ctx
)
909 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
916 static prop_val_t
*new_prop_val
(parser_ctx_t
*ctx
, literal_t
*name
, expression_t
*value
)
918 prop_val_t
*ret
= parser_alloc
(ctx
, sizeof
(prop_val_t
));
927 static property_list_t
*new_property_list
(parser_ctx_t
*ctx
, literal_t
*name
, expression_t
*value
)
929 property_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(property_list_t
));
931 ret
->head
= ret
->tail
= new_prop_val
(ctx
, name
, value
);
936 static property_list_t
*property_list_add
(parser_ctx_t
*ctx
, property_list_t
*list
, literal_t
*name
, expression_t
*value
)
938 list
->tail
= list
->tail
->next
= new_prop_val
(ctx
, name
, value
);
943 static array_element_t
*new_array_element
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
945 array_element_t
*ret
= parser_alloc
(ctx
, sizeof
(array_element_t
));
947 ret
->elision
= elision
;
954 static element_list_t
*new_element_list
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
956 element_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(element_list_t
));
958 ret
->head
= ret
->tail
= new_array_element
(ctx
, elision
, expr
);
963 static element_list_t
*element_list_add
(parser_ctx_t
*ctx
, element_list_t
*list
, int elision
, expression_t
*expr
)
965 list
->tail
= list
->tail
->next
= new_array_element
(ctx
, elision
, expr
);
970 static argument_t
*new_argument
(parser_ctx_t
*ctx
, expression_t
*expr
)
972 argument_t
*ret
= parser_alloc
(ctx
, sizeof
(argument_t
));
980 static argument_list_t
*new_argument_list
(parser_ctx_t
*ctx
, expression_t
*expr
)
982 argument_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(argument_list_t
));
984 ret
->head
= ret
->tail
= new_argument
(ctx
, expr
);
989 static argument_list_t
*argument_list_add
(parser_ctx_t
*ctx
, argument_list_t
*list
, expression_t
*expr
)
991 list
->tail
= list
->tail
->next
= new_argument
(ctx
, expr
);
996 static catch_block_t
*new_catch_block
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
998 catch_block_t
*ret
= parser_alloc
(ctx
, sizeof
(catch_block_t
));
1000 ret
->identifier
= identifier
;
1001 ret
->statement
= statement
;
1006 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_list_t
*stat_list
)
1008 case_clausule_t
*ret
= parser_alloc
(ctx
, sizeof
(case_clausule_t
));
1011 ret
->stat
= stat_list ? stat_list
->head
: NULL
;
1017 static case_list_t
*new_case_list
(parser_ctx_t
*ctx
, case_clausule_t
*case_clausule
)
1019 case_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(case_list_t
));
1021 ret
->head
= ret
->tail
= case_clausule
;
1026 static case_list_t
*case_list_add
(parser_ctx_t
*ctx
, case_list_t
*list
, case_clausule_t
*case_clausule
)
1028 list
->tail
= list
->tail
->next
= case_clausule
;
1033 static case_clausule_t
*new_case_block
(parser_ctx_t
*ctx
, case_list_t
*case_list1
,
1034 case_clausule_t
*default_clausule
, case_list_t
*case_list2
)
1036 case_clausule_t
*ret
= NULL
, *iter
= NULL
, *iter2
;
1037 statement_t
*stat
= NULL
;
1040 ret
= case_list1
->head
;
1041 iter
= case_list1
->tail
;
1044 if
(default_clausule
) {
1046 iter
= iter
->next
= default_clausule
;
1048 ret
= iter
= default_clausule
;
1053 iter
->next
= case_list2
->head
;
1055 ret
= case_list2
->head
;
1061 for
(iter
= ret
; iter
; iter
= iter
->next
) {
1062 for
(iter2
= iter
; iter2
&& !iter2
->stat
; iter2
= iter2
->next
);
1066 while
(iter
!= iter2
) {
1067 iter
->stat
= iter2
->stat
;
1074 stat
->next
= iter
->stat
;
1083 static statement_t
*new_block_statement
(parser_ctx_t
*ctx
, statement_list_t
*list
)
1085 block_statement_t
*ret
;
1087 ret
= new_statement
(ctx
, STAT_BLOCK
, sizeof
(*ret
));
1091 ret
->stat_list
= list ? list
->head
: NULL
;
1096 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, expression_t
*expr
)
1098 variable_declaration_t
*ret
= parser_alloc
(ctx
, sizeof
(variable_declaration_t
));
1100 ret
->identifier
= identifier
;
1103 ret
->global_next
= NULL
;
1108 static variable_list_t
*new_variable_list
(parser_ctx_t
*ctx
, variable_declaration_t
*decl
)
1110 variable_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(variable_list_t
));
1112 ret
->head
= ret
->tail
= decl
;
1117 static variable_list_t
*variable_list_add
(parser_ctx_t
*ctx
, variable_list_t
*list
, variable_declaration_t
*decl
)
1119 list
->tail
= list
->tail
->next
= decl
;
1124 static statement_t
*new_var_statement
(parser_ctx_t
*ctx
, variable_list_t
*variable_list
)
1126 var_statement_t
*ret
;
1128 ret
= new_statement
(ctx
, STAT_VAR
, sizeof
(*ret
));
1132 ret
->variable_list
= variable_list
->head
;
1137 static statement_t
*new_expression_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1139 expression_statement_t
*ret
;
1141 ret
= new_statement
(ctx
, STAT_EXPR
, sizeof
(*ret
));
1150 static statement_t
*new_if_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_t
*if_stat
, statement_t
*else_stat
)
1152 if_statement_t
*ret
;
1154 ret
= new_statement
(ctx
, STAT_IF
, sizeof
(*ret
));
1159 ret
->if_stat
= if_stat
;
1160 ret
->else_stat
= else_stat
;
1165 static statement_t
*new_while_statement
(parser_ctx_t
*ctx
, BOOL dowhile
, expression_t
*expr
, statement_t
*stat
)
1167 while_statement_t
*ret
;
1169 ret
= new_statement
(ctx
, STAT_WHILE
, sizeof
(*ret
));
1173 ret
->do_while
= dowhile
;
1175 ret
->statement
= stat
;
1180 static statement_t
*new_for_statement
(parser_ctx_t
*ctx
, variable_list_t
*variable_list
, expression_t
*begin_expr
,
1181 expression_t
*expr
, expression_t
*end_expr
, statement_t
*statement
)
1183 for_statement_t
*ret
;
1185 ret
= new_statement
(ctx
, STAT_FOR
, sizeof
(*ret
));
1189 ret
->variable_list
= variable_list ? variable_list
->head
: NULL
;
1190 ret
->begin_expr
= begin_expr
;
1192 ret
->end_expr
= end_expr
;
1193 ret
->statement
= statement
;
1198 static statement_t
*new_forin_statement
(parser_ctx_t
*ctx
, variable_declaration_t
*variable
, expression_t
*expr
,
1199 expression_t
*in_expr
, statement_t
*statement
)
1201 forin_statement_t
*ret
;
1203 ret
= new_statement
(ctx
, STAT_FORIN
, sizeof
(*ret
));
1207 ret
->variable
= variable
;
1209 ret
->in_expr
= in_expr
;
1210 ret
->statement
= statement
;
1215 static statement_t
*new_continue_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1217 branch_statement_t
*ret
;
1219 ret
= new_statement
(ctx
, STAT_CONTINUE
, sizeof
(*ret
));
1223 ret
->identifier
= identifier
;
1228 static statement_t
*new_break_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1230 branch_statement_t
*ret
;
1232 ret
= new_statement
(ctx
, STAT_BREAK
, sizeof
(*ret
));
1236 ret
->identifier
= identifier
;
1241 static statement_t
*new_return_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1243 expression_statement_t
*ret
;
1245 ret
= new_statement
(ctx
, STAT_RETURN
, sizeof
(*ret
));
1254 static statement_t
*new_with_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, statement_t
*statement
)
1256 with_statement_t
*ret
;
1258 ret
= new_statement
(ctx
, STAT_WITH
, sizeof
(*ret
));
1263 ret
->statement
= statement
;
1268 static statement_t
*new_labelled_statement
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
1270 labelled_statement_t
*ret
;
1272 ret
= new_statement
(ctx
, STAT_LABEL
, sizeof
(*ret
));
1276 ret
->identifier
= identifier
;
1277 ret
->statement
= statement
;
1282 static statement_t
*new_switch_statement
(parser_ctx_t
*ctx
, expression_t
*expr
, case_clausule_t
*case_list
)
1284 switch_statement_t
*ret
;
1286 ret
= new_statement
(ctx
, STAT_SWITCH
, sizeof
(*ret
));
1291 ret
->case_list
= case_list
;
1296 static statement_t
*new_throw_statement
(parser_ctx_t
*ctx
, expression_t
*expr
)
1298 expression_statement_t
*ret
;
1300 ret
= new_statement
(ctx
, STAT_THROW
, sizeof
(*ret
));
1309 static statement_t
*new_try_statement
(parser_ctx_t
*ctx
, statement_t
*try_statement
,
1310 catch_block_t
*catch_block
, statement_t
*finally_statement
)
1312 try_statement_t
*ret
;
1314 ret
= new_statement
(ctx
, STAT_TRY
, sizeof
(*ret
));
1318 ret
->try_statement
= try_statement
;
1319 ret
->catch_block
= catch_block
;
1320 ret
->finally_statement
= finally_statement
;
1325 static parameter_t
*new_parameter
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1327 parameter_t
*ret
= parser_alloc
(ctx
, sizeof
(parameter_t
));
1329 ret
->identifier
= identifier
;
1335 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1337 parameter_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(parameter_list_t
));
1339 ret
->head
= ret
->tail
= new_parameter
(ctx
, identifier
);
1344 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*ctx
, parameter_list_t
*list
, const WCHAR
*identifier
)
1346 list
->tail
= list
->tail
->next
= new_parameter
(ctx
, identifier
);
1351 static expression_t
*new_function_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, parameter_list_t
*parameter_list
,
1352 source_elements_t
*source_elements
, const WCHAR
*event_target
, const WCHAR
*src_str
, DWORD src_len
)
1354 function_expression_t
*ret
= new_expression
(ctx
, EXPR_FUNC
, sizeof
(*ret
));
1356 ret
->identifier
= identifier
;
1357 ret
->parameter_list
= parameter_list ? parameter_list
->head
: NULL
;
1358 ret
->source_elements
= source_elements
;
1359 ret
->event_target
= event_target
;
1360 ret
->src_str
= src_str
;
1361 ret
->src_len
= src_len
;
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
));
1376 static expression_t
*new_binary_expression
(parser_ctx_t
*ctx
, expression_type_t type
,
1377 expression_t
*expression1
, expression_t
*expression2
)
1379 binary_expression_t
*ret
= new_expression
(ctx
, type
, sizeof
(*ret
));
1381 ret
->expression1
= expression1
;
1382 ret
->expression2
= expression2
;
1387 static expression_t
*new_unary_expression
(parser_ctx_t
*ctx
, expression_type_t type
, expression_t
*expression
)
1389 unary_expression_t
*ret
= new_expression
(ctx
, type
, sizeof
(*ret
));
1391 ret
->expression
= expression
;
1396 static expression_t
*new_conditional_expression
(parser_ctx_t
*ctx
, expression_t
*expression
,
1397 expression_t
*true_expression
, expression_t
*false_expression
)
1399 conditional_expression_t
*ret
= new_expression
(ctx
, EXPR_COND
, sizeof
(*ret
));
1401 ret
->expression
= expression
;
1402 ret
->true_expression
= true_expression
;
1403 ret
->false_expression
= false_expression
;
1408 static expression_t
*new_member_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, const WCHAR
*identifier
)
1410 member_expression_t
*ret
= new_expression
(ctx
, EXPR_MEMBER
, sizeof
(*ret
));
1412 ret
->expression
= expression
;
1413 ret
->identifier
= identifier
;
1418 static expression_t
*new_new_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, argument_list_t
*argument_list
)
1420 call_expression_t
*ret
= new_expression
(ctx
, EXPR_NEW
, sizeof
(*ret
));
1422 ret
->expression
= expression
;
1423 ret
->argument_list
= argument_list ? argument_list
->head
: NULL
;
1428 static expression_t
*new_call_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, argument_list_t
*argument_list
)
1430 call_expression_t
*ret
= new_expression
(ctx
, EXPR_CALL
, sizeof
(*ret
));
1432 ret
->expression
= expression
;
1433 ret
->argument_list
= argument_list ? argument_list
->head
: NULL
;
1438 static int parser_error
(parser_ctx_t
*ctx
, const char *str
)
1443 static void set_error
(parser_ctx_t
*ctx
, UINT
error)
1448 static BOOL explicit_error
(parser_ctx_t
*ctx
, void *obj
, WCHAR next
)
1450 if
(obj ||
*(ctx
->ptr
-1)==next
) return TRUE
;
1452 set_error
(ctx
, JS_E_SYNTAX
);
1457 static expression_t
*new_identifier_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1459 identifier_expression_t
*ret
= new_expression
(ctx
, EXPR_IDENT
, sizeof
(*ret
));
1461 ret
->identifier
= identifier
;
1466 static expression_t
*new_array_literal_expression
(parser_ctx_t
*ctx
, element_list_t
*element_list
, int length
)
1468 array_literal_expression_t
*ret
= new_expression
(ctx
, EXPR_ARRAYLIT
, sizeof
(*ret
));
1470 ret
->element_list
= element_list ? element_list
->head
: NULL
;
1471 ret
->length
= length
;
1476 static expression_t
*new_prop_and_value_expression
(parser_ctx_t
*ctx
, property_list_t
*property_list
)
1478 property_value_expression_t
*ret
= new_expression
(ctx
, EXPR_PROPVAL
, sizeof
(*ret
));
1480 ret
->property_list
= property_list ? property_list
->head
: NULL
;
1485 static expression_t
*new_literal_expression
(parser_ctx_t
*ctx
, literal_t
*literal
)
1487 literal_expression_t
*ret
= new_expression
(ctx
, EXPR_LITERAL
, sizeof
(*ret
));
1489 ret
->literal
= literal
;
1494 static source_elements_t
*new_source_elements
(parser_ctx_t
*ctx
)
1496 source_elements_t
*ret
= parser_alloc
(ctx
, sizeof
(source_elements_t
));
1498 memset
(ret
, 0, sizeof
(*ret
));
1503 static source_elements_t
*source_elements_add_statement
(source_elements_t
*source_elements
, statement_t
*statement
)
1505 if
(source_elements
->statement_tail
)
1506 source_elements
->statement_tail
= source_elements
->statement_tail
->next
= statement
;
1508 source_elements
->statement
= source_elements
->statement_tail
= statement
;
1510 return source_elements
;
1513 static statement_list_t
*new_statement_list
(parser_ctx_t
*ctx
, statement_t
*statement
)
1515 statement_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(statement_list_t
));
1517 ret
->head
= ret
->tail
= statement
;
1522 static statement_list_t
*statement_list_add
(statement_list_t
*list
, statement_t
*statement
)
1524 list
->tail
= list
->tail
->next
= statement
;
1529 static void program_parsed
(parser_ctx_t
*ctx
, source_elements_t
*source
)
1531 ctx
->source
= source
;
1532 if
(!ctx
->lexer_error
)
1536 void parser_release
(parser_ctx_t
*ctx
)
1538 script_release
(ctx
->script
);
1539 heap_pool_free
(&ctx
->heap
);
1543 HRESULT script_parse
(script_ctx_t
*ctx
, const WCHAR
*code
, const WCHAR
*delimiter
, BOOL from_eval
,
1546 parser_ctx_t
*parser_ctx
;
1550 const WCHAR html_tagW
[] = {'<','/','s','c','r','i','p','t','>',0};
1552 parser_ctx
= heap_alloc_zero
(sizeof
(parser_ctx_t
));
1554 return E_OUTOFMEMORY
;
1556 parser_ctx
->hres
= JS_E_SYNTAX
;
1557 parser_ctx
->is_html
= delimiter
&& !strcmpiW
(delimiter
, html_tagW
);
1559 parser_ctx
->begin
= parser_ctx
->ptr
= code
;
1560 parser_ctx
->end
= parser_ctx
->begin
+ strlenW
(parser_ctx
->begin
);
1563 parser_ctx
->script
= ctx
;
1565 mark
= heap_pool_mark
(&ctx
->tmp_heap
);
1566 heap_pool_init
(&parser_ctx
->heap
);
1568 parser_parse
(parser_ctx
);
1569 heap_pool_clear
(mark
);
1570 hres
= parser_ctx
->hres
;
1572 WARN
("parser failed around %s\n",
1573 debugstr_w
(parser_ctx
->begin
+20 > parser_ctx
->ptr ? parser_ctx
->begin
: parser_ctx
->ptr
-20));
1574 parser_release
(parser_ctx
);