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
(unsigned*,parser_ctx_t
*,const char*);
30 static void set_error
(parser_ctx_t
*,unsigned,HRESULT
);
31 static BOOL explicit_error
(parser_ctx_t
*,void*,WCHAR
);
32 static BOOL allow_auto_semicolon
(parser_ctx_t
*);
34 typedef
struct _statement_list_t
{
39 static literal_t
*new_string_literal
(parser_ctx_t
*,jsstr_t
*);
40 static literal_t
*new_null_literal
(parser_ctx_t
*);
42 typedef
struct _property_list_t
{
43 property_definition_t
*head
;
44 property_definition_t
*tail
;
47 static property_definition_t
*new_property_definition
(parser_ctx_t
*ctx
, property_definition_type_t
,
48 literal_t
*name
, expression_t
*value
);
49 static property_list_t
*new_property_list
(parser_ctx_t
*,property_definition_t
*);
50 static property_list_t
*property_list_add
(parser_ctx_t
*,property_list_t
*,property_definition_t
*);
52 typedef
struct _element_list_t
{
53 array_element_t
*head
;
54 array_element_t
*tail
;
57 static element_list_t
*new_element_list
(parser_ctx_t
*,int,expression_t
*);
58 static element_list_t
*element_list_add
(parser_ctx_t
*,element_list_t
*,int,expression_t
*);
60 typedef
struct _argument_list_t
{
65 static argument_list_t
*new_argument_list
(parser_ctx_t
*,expression_t
*);
66 static argument_list_t
*argument_list_add
(parser_ctx_t
*,argument_list_t
*,expression_t
*);
68 typedef
struct _case_list_t
{
69 case_clausule_t
*head
;
70 case_clausule_t
*tail
;
73 static catch_block_t
*new_catch_block
(parser_ctx_t
*,const WCHAR
*,statement_t
*);
74 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*,unsigned,expression_t
*,statement_list_t
*);
75 static case_list_t
*new_case_list
(parser_ctx_t
*,case_clausule_t
*);
76 static case_list_t
*case_list_add
(parser_ctx_t
*,case_list_t
*,case_clausule_t
*);
77 static case_clausule_t
*new_case_block
(parser_ctx_t
*,case_list_t
*,case_clausule_t
*,case_list_t
*);
79 typedef
struct _variable_list_t
{
80 variable_declaration_t
*head
;
81 variable_declaration_t
*tail
;
84 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*,const WCHAR
*,expression_t
*);
85 static variable_list_t
*new_variable_list
(parser_ctx_t
*,variable_declaration_t
*);
86 static variable_list_t
*variable_list_add
(parser_ctx_t
*,variable_list_t
*,variable_declaration_t
*);
88 static void *new_statement
(parser_ctx_t
*,statement_type_t
,size_t,unsigned);
89 static statement_t
*new_block_statement
(parser_ctx_t
*,unsigned,statement_list_t
*);
90 static statement_t
*new_var_statement
(parser_ctx_t
*,unsigned,variable_list_t
*);
91 static statement_t
*new_expression_statement
(parser_ctx_t
*,unsigned,expression_t
*);
92 static statement_t
*new_if_statement
(parser_ctx_t
*,unsigned,expression_t
*,statement_t
*,statement_t
*);
93 static statement_t
*new_while_statement
(parser_ctx_t
*,unsigned,BOOL
,expression_t
*,statement_t
*);
94 static statement_t
*new_for_statement
(parser_ctx_t
*,unsigned,variable_list_t
*,expression_t
*,expression_t
*,unsigned,
95 expression_t
*,unsigned,statement_t
*);
96 static statement_t
*new_forin_statement
(parser_ctx_t
*,unsigned,variable_declaration_t
*,expression_t
*,expression_t
*,statement_t
*);
97 static statement_t
*new_continue_statement
(parser_ctx_t
*,unsigned,const WCHAR
*);
98 static statement_t
*new_break_statement
(parser_ctx_t
*,unsigned,const WCHAR
*);
99 static statement_t
*new_return_statement
(parser_ctx_t
*,unsigned,expression_t
*);
100 static statement_t
*new_with_statement
(parser_ctx_t
*,unsigned,expression_t
*,statement_t
*);
101 static statement_t
*new_labelled_statement
(parser_ctx_t
*,unsigned,const WCHAR
*,statement_t
*);
102 static statement_t
*new_switch_statement
(parser_ctx_t
*,unsigned,expression_t
*,case_clausule_t
*);
103 static statement_t
*new_throw_statement
(parser_ctx_t
*,unsigned,expression_t
*);
104 static statement_t
*new_try_statement
(parser_ctx_t
*,statement_t
*,catch_block_t
*,statement_t
*,unsigned);
106 struct statement_list_t
{
111 static statement_list_t
*new_statement_list
(parser_ctx_t
*,statement_t
*);
112 static statement_list_t
*statement_list_add
(statement_list_t
*,statement_t
*);
114 typedef
struct _parameter_list_t
{
119 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*,const WCHAR
*);
120 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*,parameter_list_t
*,const WCHAR
*);
122 static void *new_expression
(parser_ctx_t
*ctx
,expression_type_t
,size_t);
123 static expression_t
*new_function_expression
(parser_ctx_t
*,const WCHAR
*,parameter_list_t
*,
124 source_elements_t
*,const WCHAR
*,const WCHAR
*,DWORD
);
125 static expression_t
*new_binary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*,expression_t
*);
126 static expression_t
*new_unary_expression
(parser_ctx_t
*,expression_type_t
,expression_t
*);
127 static expression_t
*new_conditional_expression
(parser_ctx_t
*,expression_t
*,expression_t
*,expression_t
*);
128 static expression_t
*new_member_expression
(parser_ctx_t
*,expression_t
*,const WCHAR
*);
129 static expression_t
*new_new_expression
(parser_ctx_t
*,expression_t
*,argument_list_t
*);
130 static expression_t
*new_call_expression
(parser_ctx_t
*,expression_t
*,argument_list_t
*);
131 static expression_t
*new_identifier_expression
(parser_ctx_t
*,const WCHAR
*);
132 static expression_t
*new_literal_expression
(parser_ctx_t
*,literal_t
*);
133 static expression_t
*new_array_literal_expression
(parser_ctx_t
*,element_list_t
*,int);
134 static expression_t
*new_prop_and_value_expression
(parser_ctx_t
*,property_list_t
*);
136 static source_elements_t
*new_source_elements
(parser_ctx_t
*);
137 static source_elements_t
*source_elements_add_statement
(source_elements_t
*,statement_t
*);
139 #define YYLTYPE unsigned
140 #define YYLLOC_DEFAULT(Cur, Rhs, N) Cur = YYRHSLOC((Rhs), (N) ? 1 : 0)
144 %lex
-param
{ parser_ctx_t
*ctx
}
145 %parse
-param
{ parser_ctx_t
*ctx
}
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 property_definition_t
*property_definition
;
163 source_elements_t
*source_elements
;
164 statement_t
*statement
;
165 struct _statement_list_t
*statement_list
;
166 struct _variable_list_t
*variable_list
;
167 variable_declaration_t
*variable_declaration
;
171 %token
<identifier
> kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kFUNCTION kIF kFINALLY kFOR kGET kIN kSET
172 %token
<identifier
> kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
173 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL
176 %token
<identifier
> tIdentifier
177 %token
<ival
> tAssignOper tEqOper tShiftOper tRelOper
178 %token
<literal
> tNumericLiteral tBooleanLiteral
179 %token
<str
> tStringLiteral
181 %type
<source_elements
> SourceElements
182 %type
<source_elements
> FunctionBody
183 %type
<statement
> Statement
184 %type
<statement
> Block
185 %type
<statement
> VariableStatement
186 %type
<statement
> EmptyStatement
187 %type
<statement
> ExpressionStatement
188 %type
<statement
> IfStatement
189 %type
<statement
> IterationStatement
190 %type
<statement
> ContinueStatement
191 %type
<statement
> BreakStatement
192 %type
<statement
> ReturnStatement
193 %type
<statement
> WithStatement
194 %type
<statement
> LabelledStatement
195 %type
<statement
> SwitchStatement
196 %type
<statement
> ThrowStatement
197 %type
<statement
> TryStatement
198 %type
<statement
> Finally
199 %type
<statement_list
> StatementList StatementList_opt
200 %type
<parameter_list
> FormalParameterList FormalParameterList_opt
201 %type
<expr
> Expression Expression_opt Expression_err
202 %type
<expr
> ExpressionNoIn ExpressionNoIn_opt
203 %type
<expr
> FunctionExpression
204 %type
<expr
> AssignmentExpression AssignmentExpressionNoIn
205 %type
<expr
> ConditionalExpression ConditionalExpressionNoIn
206 %type
<expr
> LeftHandSideExpression
207 %type
<expr
> LogicalORExpression LogicalORExpressionNoIn
208 %type
<expr
> LogicalANDExpression LogicalANDExpressionNoIn
209 %type
<expr
> BitwiseORExpression BitwiseORExpressionNoIn
210 %type
<expr
> BitwiseXORExpression BitwiseXORExpressionNoIn
211 %type
<expr
> BitwiseANDExpression BitwiseANDExpressionNoIn
212 %type
<expr
> EqualityExpression EqualityExpressionNoIn
213 %type
<expr
> RelationalExpression RelationalExpressionNoIn
214 %type
<expr
> ShiftExpression
215 %type
<expr
> AdditiveExpression
216 %type
<expr
> MultiplicativeExpression
217 %type
<expr
> Initialiser_opt Initialiser
218 %type
<expr
> InitialiserNoIn_opt InitialiserNoIn
219 %type
<expr
> UnaryExpression
220 %type
<expr
> PostfixExpression
221 %type
<expr
> NewExpression
222 %type
<expr
> CallExpression
223 %type
<expr
> MemberExpression
224 %type
<expr
> PrimaryExpression
225 %type
<expr
> GetterSetterMethod
226 %type
<identifier
> Identifier_opt
227 %type
<variable_list
> VariableDeclarationList
228 %type
<variable_list
> VariableDeclarationListNoIn
229 %type
<variable_declaration
> VariableDeclaration
230 %type
<variable_declaration
> VariableDeclarationNoIn
231 %type
<case_list
> CaseClausules CaseClausules_opt
232 %type
<case_clausule
> CaseClausule DefaultClausule CaseBlock
233 %type
<catch_block
> Catch
234 %type
<argument_list
> Arguments
235 %type
<argument_list
> ArgumentList
236 %type
<literal
> Literal
237 %type
<expr
> ArrayLiteral
238 %type
<expr
> ObjectLiteral
239 %type
<ival
> Elision Elision_opt
240 %type
<element_list
> ElementList
241 %type
<property_list
> PropertyNameAndValueList
242 %type
<property_definition
> PropertyDefinition
243 %type
<literal
> PropertyName
244 %type
<literal
> BooleanLiteral
245 %type
<ival
> AssignOper
246 %type
<identifier
> IdentifierName ReservedAsIdentifier
248 %nonassoc LOWER_THAN_ELSE
253 /* ECMA-262 3rd Edition 14 */
255 : SourceElements HtmlComment
{ ctx
->source
= $1; }
261 /* ECMA-262 3rd Edition 14 */
263 : /* empty */ { $$
= new_source_elements
(ctx
); }
264 | SourceElements Statement
265 { $$
= source_elements_add_statement
($1, $2); }
267 /* ECMA-262 3rd Edition 13 */
269 : kFUNCTION left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
270 { $$
= new_function_expression
(ctx
, NULL
, $3, $6, NULL
, ctx
->begin
+ @
1, @
7 - @
1 + 1); }
271 | kFUNCTION tIdentifier left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
272 { $$
= new_function_expression
(ctx
, $2, $4, $7, NULL
, ctx
->begin
+ @
1, @
8 - @
1 + 1); }
273 | kFUNCTION tIdentifier kDCOL tIdentifier left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
274 { $$
= new_function_expression
(ctx
, $4, $6, $9, $2, ctx
->begin
+ @
1, @
10 - @
1 + 1); }
276 /* ECMA-262 3rd Edition 13 */
278 : SourceElements
{ $$
= $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_expression_statement
(ctx
, @$
, $1); }
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, $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_statement
(ctx
, STAT_EMPTY
, 0, @$
); }
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
, @
3, 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
, @
3, NULL
, $3, $6, @
6, $9, @
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
, @
3, $4, NULL
, $7, @
7, $10, @
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
, 0); }
478 | kTRY Block Finally
{ $$
= new_try_statement
(ctx
, $2, NULL
, $3, @
3); }
479 | kTRY Block Catch Finally
480 { $$
= new_try_statement
(ctx
, $2, $3, $4, @
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; $$
= $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
'.' IdentifierName
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
'.' IdentifierName
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); }
779 |
'{' PropertyNameAndValueList
',' '}'
781 if
(ctx
->script
->version
< 2) {
782 WARN
("Trailing comma in object literal is illegal in legacy mode.\n");
783 set_error
(ctx
, @
3, JS_E_SYNTAX
);
786 $$
= new_prop_and_value_expression
(ctx
, $2);
789 /* ECMA-262 3rd Edition 11.1.5 */
790 PropertyNameAndValueList
791 : PropertyDefinition
{ $$
= new_property_list
(ctx
, $1); }
792 | PropertyNameAndValueList
',' PropertyDefinition
793 { $$
= property_list_add
(ctx
, $1, $3); }
795 /* ECMA-262 5.1 Edition 12.2.6 */
797 : PropertyName
':' AssignmentExpression
798 { $$
= new_property_definition
(ctx
, PROPERTY_DEFINITION_VALUE
, $1, $3); }
799 | kGET PropertyName GetterSetterMethod
800 { $$
= new_property_definition
(ctx
, PROPERTY_DEFINITION_GETTER
, $2, $3); }
801 | kSET PropertyName GetterSetterMethod
802 { $$
= new_property_definition
(ctx
, PROPERTY_DEFINITION_SETTER
, $2, $3); }
805 : left_bracket FormalParameterList_opt right_bracket
'{' FunctionBody
'}'
806 { $$
= new_function_expression
(ctx
, NULL
, $2, $5, NULL
, ctx
->begin
+ @
1, @
6 - @
1 + 1); }
808 /* Ecma-262 3rd Edition 11.1.5 */
810 : IdentifierName
{ $$
= new_string_literal
(ctx
, compiler_alloc_string_len
(ctx
->compiler
, $1, lstrlenW
($1))); }
811 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
812 | tNumericLiteral
{ $$
= $1; }
814 /* ECMA-262 3rd Edition 7.6 */
816 : /* empty*/ { $$
= NULL
; }
817 | tIdentifier
{ $$
= $1; }
819 /* ECMA-262 5.1 Edition 7.6 */
821 : tIdentifier
{ $$
= $1; }
822 | ReservedAsIdentifier
824 if
(ctx
->script
->version
< SCRIPTLANGUAGEVERSION_ES5
) {
825 WARN
("%s keyword used as an identifier in legacy mode.\n",
827 set_error
(ctx
, @$
, JS_E_SYNTAX
);
834 : kBREAK
{ $$
= $1; }
836 | kCATCH
{ $$
= $1; }
837 | kCONTINUE
{ $$
= $1; }
838 | kDEFAULT
{ $$
= $1; }
839 | kDELETE
{ $$
= $1; }
842 | kFALSE
{ $$
= $1; }
843 | kFINALLY
{ $$
= $1; }
845 | kFUNCTION
{ $$
= $1; }
849 | kINSTANCEOF
{ $$
= $1; }
852 | kRETURN
{ $$
= $1; }
854 | kSWITCH
{ $$
= $1; }
856 | kTHROW
{ $$
= $1; }
859 | kTYPEOF
{ $$
= $1; }
862 | kWHILE
{ $$
= $1; }
865 /* ECMA-262 3rd Edition 7.8 */
867 : kNULL
{ $$
= new_null_literal
(ctx
); }
868 | BooleanLiteral
{ $$
= $1; }
869 | tNumericLiteral
{ $$
= $1; }
870 | tStringLiteral
{ $$
= new_string_literal
(ctx
, $1); }
871 |
'/' { $$
= parse_regexp
(ctx
);
873 | kDIVEQ
{ $$
= parse_regexp
(ctx
);
876 /* ECMA-262 3rd Edition 7.8.2 */
878 : kTRUE
{ $$
= new_boolean_literal
(ctx
, VARIANT_TRUE
); }
879 | kFALSE
{ $$
= new_boolean_literal
(ctx
, VARIANT_FALSE
); }
880 | tBooleanLiteral
{ $$
= $1; }
884 |
error { if
(!allow_auto_semicolon
(ctx
)) {YYABORT;} else
{ ctx
->hres
= S_OK
; ctx
->error_loc
= -1; } }
888 |
error { set_error
(ctx
, @$
, JS_E_MISSING_LBRACKET
); YYABORT; }
892 |
error { set_error
(ctx
, @$
, JS_E_MISSING_RBRACKET
); YYABORT; }
896 |
error { set_error
(ctx
, @$
, JS_E_MISSING_SEMICOLON
); YYABORT; }
900 static BOOL allow_auto_semicolon
(parser_ctx_t
*ctx
)
902 return ctx
->nl || ctx
->ptr
== ctx
->end ||
*(ctx
->ptr
-1) == '}';
905 static void *new_statement
(parser_ctx_t
*ctx
, statement_type_t type
, size_t size
, unsigned loc
)
909 stat
= parser_alloc
(ctx
, size ? size
: sizeof
(*stat
));
920 static literal_t
*new_string_literal
(parser_ctx_t
*ctx
, jsstr_t
*str
)
922 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
924 ret
->type
= LT_STRING
;
930 static literal_t
*new_null_literal
(parser_ctx_t
*ctx
)
932 literal_t
*ret
= parser_alloc
(ctx
, sizeof
(literal_t
));
939 static property_definition_t
*new_property_definition
(parser_ctx_t
*ctx
, property_definition_type_t type
,
940 literal_t
*name
, expression_t
*value
)
942 property_definition_t
*ret
= parser_alloc
(ctx
, sizeof
(property_definition_t
));
952 static property_list_t
*new_property_list
(parser_ctx_t
*ctx
, property_definition_t
*prop
)
954 property_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(property_list_t
));
955 ret
->head
= ret
->tail
= prop
;
959 static property_list_t
*property_list_add
(parser_ctx_t
*ctx
, property_list_t
*list
, property_definition_t
*prop
)
961 list
->tail
= list
->tail
->next
= prop
;
965 static array_element_t
*new_array_element
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
967 array_element_t
*ret
= parser_alloc
(ctx
, sizeof
(array_element_t
));
969 ret
->elision
= elision
;
976 static element_list_t
*new_element_list
(parser_ctx_t
*ctx
, int elision
, expression_t
*expr
)
978 element_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(element_list_t
));
980 ret
->head
= ret
->tail
= new_array_element
(ctx
, elision
, expr
);
985 static element_list_t
*element_list_add
(parser_ctx_t
*ctx
, element_list_t
*list
, int elision
, expression_t
*expr
)
987 list
->tail
= list
->tail
->next
= new_array_element
(ctx
, elision
, expr
);
992 static argument_t
*new_argument
(parser_ctx_t
*ctx
, expression_t
*expr
)
994 argument_t
*ret
= parser_alloc
(ctx
, sizeof
(argument_t
));
1002 static argument_list_t
*new_argument_list
(parser_ctx_t
*ctx
, expression_t
*expr
)
1004 argument_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(argument_list_t
));
1006 ret
->head
= ret
->tail
= new_argument
(ctx
, expr
);
1011 static argument_list_t
*argument_list_add
(parser_ctx_t
*ctx
, argument_list_t
*list
, expression_t
*expr
)
1013 list
->tail
= list
->tail
->next
= new_argument
(ctx
, expr
);
1018 static catch_block_t
*new_catch_block
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, statement_t
*statement
)
1020 catch_block_t
*ret
= parser_alloc
(ctx
, sizeof
(catch_block_t
));
1022 ret
->identifier
= identifier
;
1023 ret
->statement
= statement
;
1028 static case_clausule_t
*new_case_clausule
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
, statement_list_t
*stat_list
)
1030 case_clausule_t
*ret
= parser_alloc
(ctx
, sizeof
(case_clausule_t
));
1033 ret
->stat
= stat_list ? stat_list
->head
: NULL
;
1040 static case_list_t
*new_case_list
(parser_ctx_t
*ctx
, case_clausule_t
*case_clausule
)
1042 case_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(case_list_t
));
1044 ret
->head
= ret
->tail
= case_clausule
;
1049 static case_list_t
*case_list_add
(parser_ctx_t
*ctx
, case_list_t
*list
, case_clausule_t
*case_clausule
)
1051 list
->tail
= list
->tail
->next
= case_clausule
;
1056 static case_clausule_t
*new_case_block
(parser_ctx_t
*ctx
, case_list_t
*case_list1
,
1057 case_clausule_t
*default_clausule
, case_list_t
*case_list2
)
1059 case_clausule_t
*ret
= NULL
, *iter
= NULL
, *iter2
;
1060 statement_t
*stat
= NULL
;
1063 ret
= case_list1
->head
;
1064 iter
= case_list1
->tail
;
1067 if
(default_clausule
) {
1069 iter
= iter
->next
= default_clausule
;
1071 ret
= iter
= default_clausule
;
1076 iter
->next
= case_list2
->head
;
1078 ret
= case_list2
->head
;
1084 for
(iter
= ret
; iter
; iter
= iter
->next
) {
1085 for
(iter2
= iter
; iter2
&& !iter2
->stat
; iter2
= iter2
->next
);
1089 while
(iter
!= iter2
) {
1090 iter
->stat
= iter2
->stat
;
1097 stat
->next
= iter
->stat
;
1106 static statement_t
*new_block_statement
(parser_ctx_t
*ctx
, unsigned loc
, statement_list_t
*list
)
1108 block_statement_t
*ret
;
1110 ret
= new_statement
(ctx
, STAT_BLOCK
, sizeof
(*ret
), loc
);
1114 ret
->stat_list
= list ? list
->head
: NULL
;
1119 static variable_declaration_t
*new_variable_declaration
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, expression_t
*expr
)
1121 variable_declaration_t
*ret
= parser_alloc
(ctx
, sizeof
(variable_declaration_t
));
1123 ret
->identifier
= identifier
;
1126 ret
->global_next
= NULL
;
1131 static variable_list_t
*new_variable_list
(parser_ctx_t
*ctx
, variable_declaration_t
*decl
)
1133 variable_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(variable_list_t
));
1135 ret
->head
= ret
->tail
= decl
;
1140 static variable_list_t
*variable_list_add
(parser_ctx_t
*ctx
, variable_list_t
*list
, variable_declaration_t
*decl
)
1142 list
->tail
= list
->tail
->next
= decl
;
1147 static statement_t
*new_var_statement
(parser_ctx_t
*ctx
, unsigned loc
, variable_list_t
*variable_list
)
1149 var_statement_t
*ret
;
1151 ret
= new_statement
(ctx
, STAT_VAR
, sizeof
(*ret
), loc
);
1155 ret
->variable_list
= variable_list
->head
;
1160 static statement_t
*new_expression_statement
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
)
1162 expression_statement_t
*ret
;
1164 ret
= new_statement
(ctx
, STAT_EXPR
, sizeof
(*ret
), loc
);
1173 static statement_t
*new_if_statement
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
,
1174 statement_t
*if_stat
, statement_t
*else_stat
)
1176 if_statement_t
*ret
;
1178 ret
= new_statement
(ctx
, STAT_IF
, sizeof
(*ret
), loc
);
1183 ret
->if_stat
= if_stat
;
1184 ret
->else_stat
= else_stat
;
1189 static statement_t
*new_while_statement
(parser_ctx_t
*ctx
, unsigned loc
, BOOL dowhile
, expression_t
*expr
, statement_t
*stat
)
1191 while_statement_t
*ret
;
1193 ret
= new_statement
(ctx
, STAT_WHILE
, sizeof
(*ret
), loc
);
1197 ret
->do_while
= dowhile
;
1199 ret
->statement
= stat
;
1204 static statement_t
*new_for_statement
(parser_ctx_t
*ctx
, unsigned loc
, variable_list_t
*variable_list
, expression_t
*begin_expr
,
1205 expression_t
*expr
, unsigned expr_loc
, expression_t
*end_expr
, unsigned end_loc
, statement_t
*statement
)
1207 for_statement_t
*ret
;
1209 ret
= new_statement
(ctx
, STAT_FOR
, sizeof
(*ret
), loc
);
1213 ret
->variable_list
= variable_list ? variable_list
->head
: NULL
;
1214 ret
->begin_expr
= begin_expr
;
1216 ret
->expr_loc
= expr_loc
;
1217 ret
->end_expr
= end_expr
;
1218 ret
->end_loc
= end_loc
;
1219 ret
->statement
= statement
;
1224 static statement_t
*new_forin_statement
(parser_ctx_t
*ctx
, unsigned loc
, variable_declaration_t
*variable
, expression_t
*expr
,
1225 expression_t
*in_expr
, statement_t
*statement
)
1227 forin_statement_t
*ret
;
1229 ret
= new_statement
(ctx
, STAT_FORIN
, sizeof
(*ret
), loc
);
1233 ret
->variable
= variable
;
1235 ret
->in_expr
= in_expr
;
1236 ret
->statement
= statement
;
1241 static statement_t
*new_continue_statement
(parser_ctx_t
*ctx
, unsigned loc
, const WCHAR
*identifier
)
1243 branch_statement_t
*ret
;
1245 ret
= new_statement
(ctx
, STAT_CONTINUE
, sizeof
(*ret
), loc
);
1249 ret
->identifier
= identifier
;
1254 static statement_t
*new_break_statement
(parser_ctx_t
*ctx
, unsigned loc
, const WCHAR
*identifier
)
1256 branch_statement_t
*ret
;
1258 ret
= new_statement
(ctx
, STAT_BREAK
, sizeof
(*ret
), loc
);
1262 ret
->identifier
= identifier
;
1267 static statement_t
*new_return_statement
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
)
1269 expression_statement_t
*ret
;
1271 ret
= new_statement
(ctx
, STAT_RETURN
, sizeof
(*ret
), loc
);
1280 static statement_t
*new_with_statement
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
, statement_t
*statement
)
1282 with_statement_t
*ret
;
1284 ret
= new_statement
(ctx
, STAT_WITH
, sizeof
(*ret
), loc
);
1289 ret
->statement
= statement
;
1294 static statement_t
*new_labelled_statement
(parser_ctx_t
*ctx
, unsigned loc
, const WCHAR
*identifier
, statement_t
*statement
)
1296 labelled_statement_t
*ret
;
1298 ret
= new_statement
(ctx
, STAT_LABEL
, sizeof
(*ret
), loc
);
1302 ret
->identifier
= identifier
;
1303 ret
->statement
= statement
;
1308 static statement_t
*new_switch_statement
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
, case_clausule_t
*case_list
)
1310 switch_statement_t
*ret
;
1312 ret
= new_statement
(ctx
, STAT_SWITCH
, sizeof
(*ret
), loc
);
1317 ret
->case_list
= case_list
;
1322 static statement_t
*new_throw_statement
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
)
1324 expression_statement_t
*ret
;
1326 ret
= new_statement
(ctx
, STAT_THROW
, sizeof
(*ret
), loc
);
1335 static statement_t
*new_try_statement
(parser_ctx_t
*ctx
, statement_t
*try_statement
,
1336 catch_block_t
*catch_block
, statement_t
*finally_statement
, unsigned finally_loc
)
1338 try_statement_t
*ret
;
1340 ret
= new_statement
(ctx
, STAT_TRY
, sizeof
(*ret
), try_statement
->loc
);
1344 ret
->try_statement
= try_statement
;
1345 ret
->catch_block
= catch_block
;
1346 ret
->finally_statement
= finally_statement
;
1347 ret
->finally_loc
= finally_loc
;
1352 static parameter_t
*new_parameter
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1354 parameter_t
*ret
= parser_alloc
(ctx
, sizeof
(parameter_t
));
1356 ret
->identifier
= identifier
;
1362 static parameter_list_t
*new_parameter_list
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1364 parameter_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(parameter_list_t
));
1366 ret
->head
= ret
->tail
= new_parameter
(ctx
, identifier
);
1371 static parameter_list_t
*parameter_list_add
(parser_ctx_t
*ctx
, parameter_list_t
*list
, const WCHAR
*identifier
)
1373 list
->tail
= list
->tail
->next
= new_parameter
(ctx
, identifier
);
1378 static expression_t
*new_function_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, parameter_list_t
*parameter_list
,
1379 source_elements_t
*source_elements
, const WCHAR
*event_target
, const WCHAR
*src_str
, DWORD src_len
)
1381 function_expression_t
*ret
= new_expression
(ctx
, EXPR_FUNC
, sizeof
(*ret
));
1383 ret
->identifier
= identifier
;
1384 ret
->parameter_list
= parameter_list ? parameter_list
->head
: NULL
;
1385 ret
->source_elements
= source_elements
;
1386 ret
->event_target
= event_target
;
1387 ret
->src_str
= src_str
;
1388 ret
->src_len
= src_len
;
1394 static void *new_expression
(parser_ctx_t
*ctx
, expression_type_t type
, size_t size
)
1396 expression_t
*ret
= parser_alloc
(ctx
, size ? size
: sizeof
(*ret
));
1403 static expression_t
*new_binary_expression
(parser_ctx_t
*ctx
, expression_type_t type
,
1404 expression_t
*expression1
, expression_t
*expression2
)
1406 binary_expression_t
*ret
= new_expression
(ctx
, type
, sizeof
(*ret
));
1408 ret
->expression1
= expression1
;
1409 ret
->expression2
= expression2
;
1414 static expression_t
*new_unary_expression
(parser_ctx_t
*ctx
, expression_type_t type
, expression_t
*expression
)
1416 unary_expression_t
*ret
= new_expression
(ctx
, type
, sizeof
(*ret
));
1418 ret
->expression
= expression
;
1423 static expression_t
*new_conditional_expression
(parser_ctx_t
*ctx
, expression_t
*expression
,
1424 expression_t
*true_expression
, expression_t
*false_expression
)
1426 conditional_expression_t
*ret
= new_expression
(ctx
, EXPR_COND
, sizeof
(*ret
));
1428 ret
->expression
= expression
;
1429 ret
->true_expression
= true_expression
;
1430 ret
->false_expression
= false_expression
;
1435 static expression_t
*new_member_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, const WCHAR
*identifier
)
1437 member_expression_t
*ret
= new_expression
(ctx
, EXPR_MEMBER
, sizeof
(*ret
));
1439 ret
->expression
= expression
;
1440 ret
->identifier
= identifier
;
1445 static expression_t
*new_new_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, argument_list_t
*argument_list
)
1447 call_expression_t
*ret
= new_expression
(ctx
, EXPR_NEW
, sizeof
(*ret
));
1449 ret
->expression
= expression
;
1450 ret
->argument_list
= argument_list ? argument_list
->head
: NULL
;
1455 static expression_t
*new_call_expression
(parser_ctx_t
*ctx
, expression_t
*expression
, argument_list_t
*argument_list
)
1457 call_expression_t
*ret
= new_expression
(ctx
, EXPR_CALL
, sizeof
(*ret
));
1459 ret
->expression
= expression
;
1460 ret
->argument_list
= argument_list ? argument_list
->head
: NULL
;
1465 static int parser_error
(unsigned *loc
, parser_ctx_t
*ctx
, const char *str
)
1467 if
(ctx
->error_loc
== -1)
1468 ctx
->error_loc
= *loc
;
1469 if
(ctx
->hres
== S_OK
)
1470 ctx
->hres
= JS_E_SYNTAX
;
1471 WARN
("%s: %s\n", debugstr_w
(ctx
->begin
+ *loc
), str
);
1475 static void set_error
(parser_ctx_t
*ctx
, unsigned loc
, HRESULT
error)
1478 ctx
->error_loc
= loc
;
1481 static BOOL explicit_error
(parser_ctx_t
*ctx
, void *obj
, WCHAR next
)
1483 if
(obj ||
*(ctx
->ptr
-1)==next
) return TRUE
;
1485 set_error
(ctx
, ctx
->ptr
- ctx
->begin
/* FIXME */, JS_E_SYNTAX
);
1490 static expression_t
*new_identifier_expression
(parser_ctx_t
*ctx
, const WCHAR
*identifier
)
1492 identifier_expression_t
*ret
= new_expression
(ctx
, EXPR_IDENT
, sizeof
(*ret
));
1494 ret
->identifier
= identifier
;
1499 static expression_t
*new_array_literal_expression
(parser_ctx_t
*ctx
, element_list_t
*element_list
, int length
)
1501 array_literal_expression_t
*ret
= new_expression
(ctx
, EXPR_ARRAYLIT
, sizeof
(*ret
));
1503 ret
->element_list
= element_list ? element_list
->head
: NULL
;
1504 ret
->length
= length
;
1509 static expression_t
*new_prop_and_value_expression
(parser_ctx_t
*ctx
, property_list_t
*property_list
)
1511 property_value_expression_t
*ret
= new_expression
(ctx
, EXPR_PROPVAL
, sizeof
(*ret
));
1513 ret
->property_list
= property_list ? property_list
->head
: NULL
;
1518 static expression_t
*new_literal_expression
(parser_ctx_t
*ctx
, literal_t
*literal
)
1520 literal_expression_t
*ret
= new_expression
(ctx
, EXPR_LITERAL
, sizeof
(*ret
));
1522 ret
->literal
= literal
;
1527 static source_elements_t
*new_source_elements
(parser_ctx_t
*ctx
)
1529 source_elements_t
*ret
= parser_alloc
(ctx
, sizeof
(source_elements_t
));
1531 memset
(ret
, 0, sizeof
(*ret
));
1536 static source_elements_t
*source_elements_add_statement
(source_elements_t
*source_elements
, statement_t
*statement
)
1538 if
(source_elements
->statement_tail
)
1539 source_elements
->statement_tail
= source_elements
->statement_tail
->next
= statement
;
1541 source_elements
->statement
= source_elements
->statement_tail
= statement
;
1543 return source_elements
;
1546 static statement_list_t
*new_statement_list
(parser_ctx_t
*ctx
, statement_t
*statement
)
1548 statement_list_t
*ret
= parser_alloc_tmp
(ctx
, sizeof
(statement_list_t
));
1550 ret
->head
= ret
->tail
= statement
;
1555 static statement_list_t
*statement_list_add
(statement_list_t
*list
, statement_t
*statement
)
1557 list
->tail
= list
->tail
->next
= statement
;
1562 void parser_release
(parser_ctx_t
*ctx
)
1564 script_release
(ctx
->script
);
1565 heap_pool_free
(&ctx
->heap
);
1569 HRESULT script_parse
(script_ctx_t
*ctx
, struct _compiler_ctx_t
*compiler
, bytecode_t
*code
, const WCHAR
*delimiter
, BOOL from_eval
,
1572 parser_ctx_t
*parser_ctx
;
1576 parser_ctx
= heap_alloc_zero
(sizeof
(parser_ctx_t
));
1578 return E_OUTOFMEMORY
;
1580 parser_ctx
->error_loc
= -1;
1581 parser_ctx
->is_html
= delimiter
&& !wcsicmp
(delimiter
, L
"</script>");
1583 parser_ctx
->begin
= parser_ctx
->ptr
= code
->source
;
1584 parser_ctx
->end
= parser_ctx
->begin
+ lstrlenW
(parser_ctx
->begin
);
1587 parser_ctx
->script
= ctx
;
1589 mark
= heap_pool_mark
(&ctx
->tmp_heap
);
1590 heap_pool_init
(&parser_ctx
->heap
);
1592 parser_ctx
->compiler
= compiler
;
1593 parser_parse
(parser_ctx
);
1594 parser_ctx
->compiler
= NULL
;
1596 heap_pool_clear
(mark
);
1597 hres
= parser_ctx
->hres
;
1599 const WCHAR
*line_start
= code
->source
+ parser_ctx
->error_loc
, *line_end
= line_start
;
1602 while
(line_start
> code
->source
&& line_start
[-1] != '\n')
1604 while
(*line_end
&& *line_end
!= '\n')
1606 line_str
= jsstr_alloc_len
(line_start
, line_end
- line_start
);
1608 WARN
("parser failed around %s in line %s\n",
1609 debugstr_w
(parser_ctx
->begin
+20 > parser_ctx
->ptr ? parser_ctx
->begin
: parser_ctx
->ptr
-20),
1610 debugstr_jsstr
(line_str
));
1612 throw_error
(ctx
, hres
, NULL
);
1613 set_error_location
(ctx
->ei
, code
, parser_ctx
->error_loc
, IDS_COMPILATION_ERROR
, line_str
);
1614 parser_release
(parser_ctx
);
1616 jsstr_release
(line_str
);
1617 return DISP_E_EXCEPTION
;