oleaut32/tests: Don't use __tagBRECORD type.
[wine.git] / dlls / jscript / parser.y
blob42d695e08469097ed067877df86045f1caa8bd2b
1 /*
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
21 #include "jscript.h"
22 #include "engine.h"
23 #include "parser.h"
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 static literal_t *new_string_literal(parser_ctx_t*,jsstr_t*);
35 static literal_t *new_null_literal(parser_ctx_t*);
37 typedef struct _property_list_t {
38 property_definition_t *head;
39 property_definition_t *tail;
40 } property_list_t;
42 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t,
43 literal_t *name, expression_t *value);
44 static property_list_t *new_property_list(parser_ctx_t*,property_definition_t*);
45 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,property_definition_t*);
47 typedef struct _element_list_t {
48 array_element_t *head;
49 array_element_t *tail;
50 } element_list_t;
52 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
53 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
55 typedef struct _argument_list_t {
56 argument_t *head;
57 argument_t *tail;
58 } argument_list_t;
60 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
61 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
63 typedef struct _case_list_t {
64 case_clausule_t *head;
65 case_clausule_t *tail;
66 } case_list_t;
68 typedef struct _statement_list_t {
69 statement_t *head;
70 statement_t *tail;
71 } statement_list_t;
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;
82 } variable_list_t;
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*,BOOL,BOOL,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_declaration_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 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
107 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
109 typedef struct _parameter_list_t {
110 parameter_t *head;
111 parameter_t *tail;
112 } parameter_list_t;
114 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
115 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
117 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
118 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
119 statement_list_t*,const WCHAR*,const WCHAR*,DWORD);
120 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
121 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
122 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
123 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
124 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
125 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
126 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
127 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
128 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
129 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
131 #define PARSER_LTYPE unsigned
132 #define YYLLOC_DEFAULT(Cur, Rhs, N) Cur = YYRHSLOC((Rhs), (N) ? 1 : 0)
136 %lex-param { parser_ctx_t *ctx }
137 %parse-param { parser_ctx_t *ctx }
138 %define api.prefix {parser_}
139 %define api.pure
140 %start Script
142 %union {
143 int ival;
144 jsstr_t *str;
145 literal_t *literal;
146 struct _argument_list_t *argument_list;
147 case_clausule_t *case_clausule;
148 struct _case_list_t *case_list;
149 catch_block_t *catch_block;
150 struct _element_list_t *element_list;
151 expression_t *expr;
152 const WCHAR *identifier;
153 struct _parameter_list_t *parameter_list;
154 struct _property_list_t *property_list;
155 property_definition_t *property_definition;
156 statement_t *statement;
157 struct _statement_list_t *statement_list;
158 struct _variable_list_t *variable_list;
159 variable_declaration_t *variable_declaration;
162 /* keywords */
163 %token <identifier> kBREAK kCASE kCATCH kCONST kCONTINUE kDEFAULT kDELETE kDO kELSE kFUNCTION kIF kFINALLY kFOR
164 %token <identifier> kGET kIN kLET kSET kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE
165 %token <identifier> kTRY kTYPEOF kVAR kVOID kWHILE kWITH
166 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL
168 /* tokens */
169 %token <identifier> tIdentifier
170 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
171 %token <literal> tNumericLiteral tBooleanLiteral
172 %token <str> tStringLiteral
174 %type <statement_list> FunctionBody
175 %type <statement_list> ScriptBody
176 %type <statement_list> FunctionStatementList
177 %type <statement> Statement
178 %type <statement> Declaration
179 %type <statement> Block
180 %type <statement> LexicalDeclaration
181 %type <statement> LexicalDeclarationNoIn
182 %type <statement> VariableStatement
183 %type <statement> EmptyStatement
184 %type <statement> ExpressionStatement
185 %type <statement> IfStatement
186 %type <statement> IterationStatement
187 %type <statement> ContinueStatement
188 %type <statement> BreakStatement
189 %type <statement> ReturnStatement
190 %type <statement> WithStatement
191 %type <statement> LabelledStatement
192 %type <statement> SwitchStatement
193 %type <statement> ThrowStatement
194 %type <statement> TryStatement
195 %type <statement> Finally
196 %type <statement> StatementListItem
197 %type <statement_list> StatementList StatementList_opt
198 %type <parameter_list> FormalParameterList FormalParameterList_opt
199 %type <expr> Expression Expression_opt Expression_err
200 %type <expr> ExpressionNoIn ExpressionNoIn_opt
201 %type <expr> FunctionExpression
202 %type <expr> AssignmentExpression AssignmentExpressionNoIn
203 %type <expr> ConditionalExpression ConditionalExpressionNoIn
204 %type <expr> LeftHandSideExpression
205 %type <expr> LogicalORExpression LogicalORExpressionNoIn
206 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
207 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
208 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
209 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
210 %type <expr> EqualityExpression EqualityExpressionNoIn
211 %type <expr> RelationalExpression RelationalExpressionNoIn
212 %type <expr> ShiftExpression
213 %type <expr> AdditiveExpression
214 %type <expr> MultiplicativeExpression
215 %type <expr> Initialiser_opt Initialiser
216 %type <expr> InitialiserNoIn_opt InitialiserNoIn
217 %type <expr> UnaryExpression
218 %type <expr> PostfixExpression
219 %type <expr> NewExpression
220 %type <expr> CallExpression
221 %type <expr> MemberExpression
222 %type <expr> PrimaryExpression
223 %type <expr> GetterSetterMethod
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 <property_definition> PropertyDefinition
241 %type <literal> PropertyName
242 %type <literal> BooleanLiteral
243 %type <ival> AssignOper
244 %type <identifier> IdentifierName ReservedAsIdentifier
246 %nonassoc LOWER_THAN_ELSE
247 %nonassoc kELSE
251 /* ECMA-262 10th Edition 15.1 */
252 Script
253 : ScriptBody HtmlComment { ctx->source = $1 ? $1->head : NULL; }
255 /* ECMA-262 10th Edition 15.1 */
256 ScriptBody
257 : StatementList_opt { $$ = $1; }
259 HtmlComment
260 : tHTMLCOMMENT
261 | /* empty */
263 /* ECMA-262 10th Edition 14.1 */
264 FunctionStatementList
265 : StatementList_opt { $$ = $1; }
267 /* ECMA-262 3rd Edition 13 */
268 FunctionExpression
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 10th Edition 14.1 */
277 FunctionBody
278 : FunctionStatementList { $$ = $1; }
280 /* ECMA-262 3rd Edition 13 */
281 FormalParameterList
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 */
292 Statement
293 : Block { $$ = $1; }
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 10th Edition 13. TODO: HoistableDeclaration */
310 Declaration
311 : LexicalDeclaration { $$ = $1; }
313 /* ECMA-262 10th Edition 13.2 */
314 StatementListItem
315 : Statement { $$ = $1; }
316 | Declaration { $$ = $1; }
318 /* ECMA-262 10th Edition 13.2 */
319 StatementList
320 : StatementListItem { $$ = new_statement_list(ctx, $1); }
321 | StatementList StatementListItem
322 { $$ = statement_list_add($1, $2); }
324 /* ECMA-262 3rd Edition 12.2 */
325 StatementList_opt
326 : /* empty */ { $$ = NULL; }
327 | StatementList { $$ = $1; }
329 /* ECMA-262 3rd Edition 12.1 */
330 Block
331 : '{' StatementList '}' { $$ = new_block_statement(ctx, @2, $2); }
332 | '{' '}' { $$ = new_block_statement(ctx, @$, NULL); }
334 /* ECMA-262 10th Edition 13.3.1, TODO: BindingList*/
335 LexicalDeclaration
336 : kLET VariableDeclarationList semicolon_opt
337 { $$ = new_var_statement(ctx, TRUE, FALSE, @$, $2); }
338 | kCONST VariableDeclarationList semicolon_opt
340 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
341 WARN("const var declaration %s in legacy mode.\n",
342 debugstr_w($1));
343 set_error(ctx, @$, JS_E_SYNTAX);
344 YYABORT;
346 $$ = new_var_statement(ctx, TRUE, TRUE, @$, $2);
349 /* ECMA-262 10th Edition 13.3.1, TODO: BindingList*/
350 LexicalDeclarationNoIn
351 : kLET VariableDeclarationListNoIn semicolon_opt
352 { $$ = new_var_statement(ctx, TRUE, FALSE, @$, $2); }
353 | kCONST VariableDeclarationListNoIn semicolon_opt
355 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
356 WARN("const var declaration %s in legacy mode.\n",
357 debugstr_w($1));
358 set_error(ctx, @$, JS_E_SYNTAX);
359 YYABORT;
361 $$ = new_var_statement(ctx, TRUE, TRUE, @$, $2);
364 /* ECMA-262 3rd Edition 12.2 */
365 VariableStatement
366 : kVAR VariableDeclarationList semicolon_opt
367 { $$ = new_var_statement(ctx, FALSE, FALSE, @$, $2); }
369 /* ECMA-262 3rd Edition 12.2 */
370 VariableDeclarationList
371 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
372 | VariableDeclarationList ',' VariableDeclaration
373 { $$ = variable_list_add(ctx, $1, $3); }
375 /* ECMA-262 3rd Edition 12.2 */
376 VariableDeclarationListNoIn
377 : VariableDeclarationNoIn
378 { $$ = new_variable_list(ctx, $1); }
379 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
380 { $$ = variable_list_add(ctx, $1, $3); }
382 /* ECMA-262 3rd Edition 12.2 */
383 VariableDeclaration
384 : tIdentifier Initialiser_opt
385 { $$ = new_variable_declaration(ctx, $1, $2); }
387 /* ECMA-262 3rd Edition 12.2 */
388 VariableDeclarationNoIn
389 : tIdentifier InitialiserNoIn_opt
390 { $$ = new_variable_declaration(ctx, $1, $2); }
392 /* ECMA-262 3rd Edition 12.2 */
393 Initialiser_opt
394 : /* empty */ { $$ = NULL; }
395 | Initialiser { $$ = $1; }
397 /* ECMA-262 3rd Edition 12.2 */
398 Initialiser
399 : '=' AssignmentExpression
400 { $$ = $2; }
402 /* ECMA-262 3rd Edition 12.2 */
403 InitialiserNoIn_opt
404 : /* empty */ { $$ = NULL; }
405 | InitialiserNoIn { $$ = $1; }
407 /* ECMA-262 3rd Edition 12.2 */
408 InitialiserNoIn
409 : '=' AssignmentExpressionNoIn
410 { $$ = $2; }
412 /* ECMA-262 3rd Edition 12.3 */
413 EmptyStatement
414 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0, @$); }
416 /* ECMA-262 3rd Edition 12.4 */
417 ExpressionStatement
418 : Expression semicolon_opt
419 { $$ = new_expression_statement(ctx, @$, $1); }
421 /* ECMA-262 3rd Edition 12.5 */
422 IfStatement
423 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
424 { $$ = new_if_statement(ctx, @$, $3, $5, $7); }
425 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
426 { $$ = new_if_statement(ctx, @$, $3, $5, NULL); }
428 /* ECMA-262 10th Edition 13.7 */
429 IterationStatement
430 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
431 { $$ = new_while_statement(ctx, @3, TRUE, $5, $2); }
432 | kWHILE left_bracket Expression_err right_bracket Statement
433 { $$ = new_while_statement(ctx, @$, FALSE, $3, $5); }
434 | kFOR left_bracket ExpressionNoIn_opt
435 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
436 semicolon Expression_opt
437 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
438 semicolon Expression_opt right_bracket Statement
439 { $$ = new_for_statement(ctx, @3, NULL, $3, $6, @6, $9, @9, $11); }
440 | kFOR left_bracket kVAR VariableDeclarationListNoIn
441 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
442 semicolon Expression_opt
443 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
444 semicolon Expression_opt right_bracket Statement
445 { $$ = new_for_statement(ctx, @3, $4 ? $4->head : NULL, NULL, $7, @7, $10, @10, $12); }
446 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
447 { $$ = new_forin_statement(ctx, @$, NULL, $3, $5, $7); }
448 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
449 { $$ = new_forin_statement(ctx, @$, $4, NULL, $6, $8); }
450 | kFOR left_bracket LexicalDeclarationNoIn
451 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
452 Expression_opt
453 { if(!explicit_error(ctx, $5, ';')) YYABORT; }
454 semicolon Expression_opt right_bracket Statement
455 { $$ = new_for_statement(ctx, @3, ((var_statement_t *)$3)->variable_list,
456 NULL, $5, @5, $8, @8, $10); }
458 /* ECMA-262 3rd Edition 12.7 */
459 ContinueStatement
460 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
461 { $$ = new_continue_statement(ctx, @$, $2); }
463 /* ECMA-262 3rd Edition 12.8 */
464 BreakStatement
465 : kBREAK /* NONL */ Identifier_opt semicolon_opt
466 { $$ = new_break_statement(ctx, @$, $2); }
468 /* ECMA-262 3rd Edition 12.9 */
469 ReturnStatement
470 : kRETURN /* NONL */ Expression_opt semicolon_opt
471 { $$ = new_return_statement(ctx, @$, $2); }
473 /* ECMA-262 3rd Edition 12.10 */
474 WithStatement
475 : kWITH left_bracket Expression right_bracket Statement
476 { $$ = new_with_statement(ctx, @$, $3, $5); }
478 /* ECMA-262 3rd Edition 12.12 */
479 LabelledStatement
480 : tIdentifier ':' Statement
481 { $$ = new_labelled_statement(ctx, @$, $1, $3); }
483 /* ECMA-262 3rd Edition 12.11 */
484 SwitchStatement
485 : kSWITCH left_bracket Expression right_bracket CaseBlock
486 { $$ = new_switch_statement(ctx, @$, $3, $5); }
488 /* ECMA-262 3rd Edition 12.11 */
489 CaseBlock
490 : '{' CaseClausules_opt '}'
491 { $$ = new_case_block(ctx, $2, NULL, NULL); }
492 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
493 { $$ = new_case_block(ctx, $2, $3, $4); }
495 /* ECMA-262 3rd Edition 12.11 */
496 CaseClausules_opt
497 : /* empty */ { $$ = NULL; }
498 | CaseClausules { $$ = $1; }
500 /* ECMA-262 3rd Edition 12.11 */
501 CaseClausules
502 : CaseClausule { $$ = new_case_list(ctx, $1); }
503 | CaseClausules CaseClausule
504 { $$ = case_list_add(ctx, $1, $2); }
506 /* ECMA-262 3rd Edition 12.11 */
507 CaseClausule
508 : kCASE Expression ':' StatementList_opt
509 { $$ = new_case_clausule(ctx, @$, $2, $4); }
511 /* ECMA-262 3rd Edition 12.11 */
512 DefaultClausule
513 : kDEFAULT ':' StatementList_opt
514 { $$ = new_case_clausule(ctx, @$, NULL, $3); }
516 /* ECMA-262 3rd Edition 12.13 */
517 ThrowStatement
518 : kTHROW /* NONL */ Expression semicolon_opt
519 { $$ = new_throw_statement(ctx, @$, $2); }
521 /* ECMA-262 3rd Edition 12.14 */
522 TryStatement
523 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL, 0); }
524 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3, @3); }
525 | kTRY Block Catch Finally
526 { $$ = new_try_statement(ctx, $2, $3, $4, @4); }
528 /* ECMA-262 3rd Edition 12.14 */
529 Catch
530 : kCATCH left_bracket tIdentifier right_bracket Block
531 { $$ = new_catch_block(ctx, $3, $5); }
533 /* ECMA-262 3rd Edition 12.14 */
534 Finally
535 : kFINALLY Block { @$ = @2; $$ = $2; }
537 /* ECMA-262 3rd Edition 11.14 */
538 Expression_opt
539 : /* empty */ { $$ = NULL; }
540 | Expression { $$ = $1; }
542 Expression_err
543 : Expression { $$ = $1; }
544 | error { set_error(ctx, @$, JS_E_SYNTAX); YYABORT; }
546 /* ECMA-262 3rd Edition 11.14 */
547 Expression
548 : AssignmentExpression { $$ = $1; }
549 | Expression ',' AssignmentExpression
550 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
552 /* ECMA-262 3rd Edition 11.14 */
553 ExpressionNoIn_opt
554 : /* empty */ { $$ = NULL; }
555 | ExpressionNoIn { $$ = $1; }
557 /* ECMA-262 3rd Edition 11.14 */
558 ExpressionNoIn
559 : AssignmentExpressionNoIn
560 { $$ = $1; }
561 | ExpressionNoIn ',' AssignmentExpressionNoIn
562 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
564 AssignOper
565 : tAssignOper { $$ = $1; }
566 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
568 /* ECMA-262 3rd Edition 11.13 */
569 AssignmentExpression
570 : ConditionalExpression { $$ = $1; }
571 | LeftHandSideExpression '=' AssignmentExpression
572 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
573 | LeftHandSideExpression AssignOper AssignmentExpression
574 { $$ = new_binary_expression(ctx, $2, $1, $3); }
576 /* ECMA-262 3rd Edition 11.13 */
577 AssignmentExpressionNoIn
578 : ConditionalExpressionNoIn
579 { $$ = $1; }
580 | LeftHandSideExpression '=' AssignmentExpressionNoIn
581 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
582 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
583 { $$ = new_binary_expression(ctx, $2, $1, $3); }
585 /* ECMA-262 3rd Edition 11.12 */
586 ConditionalExpression
587 : LogicalORExpression { $$ = $1; }
588 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
589 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
591 /* ECMA-262 3rd Edition 11.12 */
592 ConditionalExpressionNoIn
593 : LogicalORExpressionNoIn
594 { $$ = $1; }
595 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
596 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
598 /* ECMA-262 3rd Edition 11.11 */
599 LogicalORExpression
600 : LogicalANDExpression { $$ = $1; }
601 | LogicalORExpression tOROR LogicalANDExpression
602 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
604 /* ECMA-262 3rd Edition 11.11 */
605 LogicalORExpressionNoIn
606 : LogicalANDExpressionNoIn
607 { $$ = $1; }
608 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
609 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
611 /* ECMA-262 3rd Edition 11.11 */
612 LogicalANDExpression
613 : BitwiseORExpression { $$ = $1; }
614 | LogicalANDExpression tANDAND BitwiseORExpression
615 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
617 /* ECMA-262 3rd Edition 11.11 */
618 LogicalANDExpressionNoIn
619 : BitwiseORExpressionNoIn
620 { $$ = $1; }
621 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
622 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
624 /* ECMA-262 3rd Edition 11.10 */
625 BitwiseORExpression
626 : BitwiseXORExpression { $$ = $1; }
627 | BitwiseORExpression '|' BitwiseXORExpression
628 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
630 /* ECMA-262 3rd Edition 11.10 */
631 BitwiseORExpressionNoIn
632 : BitwiseXORExpressionNoIn
633 { $$ = $1; }
634 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
635 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
637 /* ECMA-262 3rd Edition 11.10 */
638 BitwiseXORExpression
639 : BitwiseANDExpression { $$ = $1; }
640 | BitwiseXORExpression '^' BitwiseANDExpression
641 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
643 /* ECMA-262 3rd Edition 11.10 */
644 BitwiseXORExpressionNoIn
645 : BitwiseANDExpressionNoIn
646 { $$ = $1; }
647 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
648 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
650 /* ECMA-262 3rd Edition 11.10 */
651 BitwiseANDExpression
652 : EqualityExpression { $$ = $1; }
653 | BitwiseANDExpression '&' EqualityExpression
654 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
656 /* ECMA-262 3rd Edition 11.10 */
657 BitwiseANDExpressionNoIn
658 : EqualityExpressionNoIn
659 { $$ = $1; }
660 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
661 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
663 /* ECMA-262 3rd Edition 11.9 */
664 EqualityExpression
665 : RelationalExpression { $$ = $1; }
666 | EqualityExpression tEqOper RelationalExpression
667 { $$ = new_binary_expression(ctx, $2, $1, $3); }
669 /* ECMA-262 3rd Edition 11.9 */
670 EqualityExpressionNoIn
671 : RelationalExpressionNoIn { $$ = $1; }
672 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
673 { $$ = new_binary_expression(ctx, $2, $1, $3); }
675 /* ECMA-262 3rd Edition 11.8 */
676 RelationalExpression
677 : ShiftExpression { $$ = $1; }
678 | RelationalExpression tRelOper ShiftExpression
679 { $$ = new_binary_expression(ctx, $2, $1, $3); }
680 | RelationalExpression kINSTANCEOF ShiftExpression
681 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
682 | RelationalExpression kIN ShiftExpression
683 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
685 /* ECMA-262 3rd Edition 11.8 */
686 RelationalExpressionNoIn
687 : ShiftExpression { $$ = $1; }
688 | RelationalExpressionNoIn tRelOper ShiftExpression
689 { $$ = new_binary_expression(ctx, $2, $1, $3); }
690 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
691 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
693 /* ECMA-262 3rd Edition 11.7 */
694 ShiftExpression
695 : AdditiveExpression { $$ = $1; }
696 | ShiftExpression tShiftOper AdditiveExpression
697 { $$ = new_binary_expression(ctx, $2, $1, $3); }
699 /* ECMA-262 3rd Edition 11.6 */
700 AdditiveExpression
701 : MultiplicativeExpression
702 { $$ = $1; }
703 | AdditiveExpression '+' MultiplicativeExpression
704 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
705 | AdditiveExpression '-' MultiplicativeExpression
706 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
708 /* ECMA-262 3rd Edition 11.5 */
709 MultiplicativeExpression
710 : UnaryExpression { $$ = $1; }
711 | MultiplicativeExpression '*' UnaryExpression
712 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
713 | MultiplicativeExpression '/' UnaryExpression
714 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
715 | MultiplicativeExpression '%' UnaryExpression
716 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
718 /* ECMA-262 3rd Edition 11.4 */
719 UnaryExpression
720 : PostfixExpression { $$ = $1; }
721 | kDELETE UnaryExpression
722 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
723 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
724 | kTYPEOF UnaryExpression
725 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
726 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
727 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
728 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
729 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
730 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
731 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
733 /* ECMA-262 3rd Edition 11.2 */
734 PostfixExpression
735 : LeftHandSideExpression
736 { $$ = $1; }
737 | LeftHandSideExpression /* NONL */ tINC
738 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
739 | LeftHandSideExpression /* NONL */ tDEC
740 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
743 /* ECMA-262 3rd Edition 11.2 */
744 LeftHandSideExpression
745 : NewExpression { $$ = $1; }
746 | CallExpression { $$ = $1; }
748 /* ECMA-262 3rd Edition 11.2 */
749 NewExpression
750 : MemberExpression { $$ = $1; }
751 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
753 /* ECMA-262 3rd Edition 11.2 */
754 MemberExpression
755 : PrimaryExpression { $$ = $1; }
756 | FunctionExpression { $$ = $1; }
757 | MemberExpression '[' Expression ']'
758 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
759 | MemberExpression '.' IdentifierName
760 { $$ = new_member_expression(ctx, $1, $3); }
761 | kNEW MemberExpression Arguments
762 { $$ = new_new_expression(ctx, $2, $3); }
764 /* ECMA-262 3rd Edition 11.2 */
765 CallExpression
766 : MemberExpression Arguments
767 { $$ = new_call_expression(ctx, $1, $2); }
768 | CallExpression Arguments
769 { $$ = new_call_expression(ctx, $1, $2); }
770 | CallExpression '[' Expression ']'
771 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
772 | CallExpression '.' IdentifierName
773 { $$ = new_member_expression(ctx, $1, $3); }
775 /* ECMA-262 3rd Edition 11.2 */
776 Arguments
777 : '(' ')' { $$ = NULL; }
778 | '(' ArgumentList ')' { $$ = $2; }
780 /* ECMA-262 3rd Edition 11.2 */
781 ArgumentList
782 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
783 | ArgumentList ',' AssignmentExpression
784 { $$ = argument_list_add(ctx, $1, $3); }
786 /* ECMA-262 3rd Edition 11.1 */
787 PrimaryExpression
788 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
789 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
790 | Literal { $$ = new_literal_expression(ctx, $1); }
791 | ArrayLiteral { $$ = $1; }
792 | ObjectLiteral { $$ = $1; }
793 | '(' Expression ')' { $$ = $2; }
795 /* ECMA-262 3rd Edition 11.1.4 */
796 ArrayLiteral
797 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
798 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
799 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
800 | '[' ElementList ',' Elision_opt ']'
801 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
803 /* ECMA-262 3rd Edition 11.1.4 */
804 ElementList
805 : Elision_opt AssignmentExpression
806 { $$ = new_element_list(ctx, $1, $2); }
807 | ElementList ',' Elision_opt AssignmentExpression
808 { $$ = element_list_add(ctx, $1, $3, $4); }
810 /* ECMA-262 3rd Edition 11.1.4 */
811 Elision
812 : ',' { $$ = 1; }
813 | Elision ',' { $$ = $1 + 1; }
815 /* ECMA-262 3rd Edition 11.1.4 */
816 Elision_opt
817 : /* empty */ { $$ = 0; }
818 | Elision { $$ = $1; }
820 /* ECMA-262 3rd Edition 11.1.5 */
821 ObjectLiteral
822 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
823 | '{' PropertyNameAndValueList '}'
824 { $$ = new_prop_and_value_expression(ctx, $2); }
825 | '{' PropertyNameAndValueList ',' '}'
827 if(ctx->script->version < 2) {
828 WARN("Trailing comma in object literal is illegal in legacy mode.\n");
829 set_error(ctx, @3, JS_E_SYNTAX);
830 YYABORT;
832 $$ = new_prop_and_value_expression(ctx, $2);
835 /* ECMA-262 3rd Edition 11.1.5 */
836 PropertyNameAndValueList
837 : PropertyDefinition { $$ = new_property_list(ctx, $1); }
838 | PropertyNameAndValueList ',' PropertyDefinition
839 { $$ = property_list_add(ctx, $1, $3); }
841 /* ECMA-262 5.1 Edition 12.2.6 */
842 PropertyDefinition
843 : PropertyName ':' AssignmentExpression
844 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_VALUE, $1, $3); }
845 | kGET PropertyName GetterSetterMethod
846 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_GETTER, $2, $3); }
847 | kSET PropertyName GetterSetterMethod
848 { $$ = new_property_definition(ctx, PROPERTY_DEFINITION_SETTER, $2, $3); }
850 GetterSetterMethod
851 : left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
852 { $$ = new_function_expression(ctx, NULL, $2, $5, NULL, ctx->begin + @1, @6 - @1 + 1); }
854 /* Ecma-262 3rd Edition 11.1.5 */
855 PropertyName
856 : IdentifierName { $$ = new_string_literal(ctx, compiler_alloc_string_len(ctx->compiler, $1, lstrlenW($1))); }
857 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
858 | tNumericLiteral { $$ = $1; }
860 /* ECMA-262 3rd Edition 7.6 */
861 Identifier_opt
862 : /* empty*/ { $$ = NULL; }
863 | tIdentifier { $$ = $1; }
865 /* ECMA-262 5.1 Edition 7.6 */
866 IdentifierName
867 : tIdentifier { $$ = $1; }
868 | ReservedAsIdentifier
870 if(ctx->script->version < SCRIPTLANGUAGEVERSION_ES5) {
871 WARN("%s keyword used as an identifier in legacy mode.\n",
872 debugstr_w($1));
873 set_error(ctx, @$, JS_E_SYNTAX);
874 YYABORT;
876 $$ = $1;
879 ReservedAsIdentifier
880 : kBREAK { $$ = $1; }
881 | kCASE { $$ = $1; }
882 | kCATCH { $$ = $1; }
883 | kCONST { $$ = $1; }
884 | kCONTINUE { $$ = $1; }
885 | kDEFAULT { $$ = $1; }
886 | kDELETE { $$ = $1; }
887 | kDO { $$ = $1; }
888 | kELSE { $$ = $1; }
889 | kFALSE { $$ = $1; }
890 | kFINALLY { $$ = $1; }
891 | kFOR { $$ = $1; }
892 | kFUNCTION { $$ = $1; }
893 | kGET { $$ = $1; }
894 | kIF { $$ = $1; }
895 | kIN { $$ = $1; }
896 | kINSTANCEOF { $$ = $1; }
897 | kLET { $$ = $1; }
898 | kNEW { $$ = $1; }
899 | kNULL { $$ = $1; }
900 | kRETURN { $$ = $1; }
901 | kSET { $$ = $1; }
902 | kSWITCH { $$ = $1; }
903 | kTHIS { $$ = $1; }
904 | kTHROW { $$ = $1; }
905 | kTRUE { $$ = $1; }
906 | kTRY { $$ = $1; }
907 | kTYPEOF { $$ = $1; }
908 | kVAR { $$ = $1; }
909 | kVOID { $$ = $1; }
910 | kWHILE { $$ = $1; }
911 | kWITH { $$ = $1; }
913 /* ECMA-262 3rd Edition 7.8 */
914 Literal
915 : kNULL { $$ = new_null_literal(ctx); }
916 | BooleanLiteral { $$ = $1; }
917 | tNumericLiteral { $$ = $1; }
918 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
919 | '/' { $$ = parse_regexp(ctx);
920 if(!$$) YYABORT; }
921 | kDIVEQ { $$ = parse_regexp(ctx);
922 if(!$$) YYABORT; }
924 /* ECMA-262 3rd Edition 7.8.2 */
925 BooleanLiteral
926 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
927 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
928 | tBooleanLiteral { $$ = $1; }
930 semicolon_opt
931 : ';'
932 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} else { ctx->hres = S_OK; ctx->error_loc = -1; } }
934 left_bracket
935 : '('
936 | error { set_error(ctx, @$, JS_E_MISSING_LBRACKET); YYABORT; }
938 right_bracket
939 : ')'
940 | error { set_error(ctx, @$, JS_E_MISSING_RBRACKET); YYABORT; }
942 semicolon
943 : ';'
944 | error { set_error(ctx, @$, JS_E_MISSING_SEMICOLON); YYABORT; }
948 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
950 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
953 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size, unsigned loc)
955 statement_t *stat;
957 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
958 if(!stat)
959 return NULL;
961 stat->type = type;
962 stat->loc = loc;
963 stat->next = NULL;
965 return stat;
968 static literal_t *new_string_literal(parser_ctx_t *ctx, jsstr_t *str)
970 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
972 ret->type = LT_STRING;
973 ret->u.str = str;
975 return ret;
978 static literal_t *new_null_literal(parser_ctx_t *ctx)
980 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
982 ret->type = LT_NULL;
984 return ret;
987 static property_definition_t *new_property_definition(parser_ctx_t *ctx, property_definition_type_t type,
988 literal_t *name, expression_t *value)
990 property_definition_t *ret = parser_alloc(ctx, sizeof(property_definition_t));
992 ret->type = type;
993 ret->name = name;
994 ret->value = value;
995 ret->next = NULL;
997 return ret;
1000 static property_list_t *new_property_list(parser_ctx_t *ctx, property_definition_t *prop)
1002 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
1003 ret->head = ret->tail = prop;
1004 return ret;
1007 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, property_definition_t *prop)
1009 list->tail = list->tail->next = prop;
1010 return list;
1013 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
1015 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
1017 ret->elision = elision;
1018 ret->expr = expr;
1019 ret->next = NULL;
1021 return ret;
1024 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
1026 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
1028 ret->head = ret->tail = new_array_element(ctx, elision, expr);
1030 return ret;
1033 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
1035 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
1037 return list;
1040 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
1042 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
1044 ret->expr = expr;
1045 ret->next = NULL;
1047 return ret;
1050 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
1052 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
1054 ret->head = ret->tail = new_argument(ctx, expr);
1056 return ret;
1059 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
1061 list->tail = list->tail->next = new_argument(ctx, expr);
1063 return list;
1066 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1068 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
1070 ret->identifier = identifier;
1071 ret->statement = statement;
1073 return ret;
1076 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_list_t *stat_list)
1078 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
1080 ret->expr = expr;
1081 ret->stat = stat_list ? stat_list->head : NULL;
1082 ret->loc = loc;
1083 ret->next = NULL;
1085 return ret;
1088 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
1090 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
1092 ret->head = ret->tail = case_clausule;
1094 return ret;
1097 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
1099 list->tail = list->tail->next = case_clausule;
1101 return list;
1104 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
1105 case_clausule_t *default_clausule, case_list_t *case_list2)
1107 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
1108 statement_t *stat = NULL;
1110 if(case_list1) {
1111 ret = case_list1->head;
1112 iter = case_list1->tail;
1115 if(default_clausule) {
1116 if(ret)
1117 iter = iter->next = default_clausule;
1118 else
1119 ret = iter = default_clausule;
1122 if(case_list2) {
1123 if(ret)
1124 iter->next = case_list2->head;
1125 else
1126 ret = case_list2->head;
1129 if(!ret)
1130 return NULL;
1132 for(iter = ret; iter; iter = iter->next) {
1133 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1134 if(!iter2)
1135 break;
1137 while(iter != iter2) {
1138 iter->stat = iter2->stat;
1139 iter = iter->next;
1142 if(stat) {
1143 while(stat->next)
1144 stat = stat->next;
1145 stat->next = iter->stat;
1146 }else {
1147 stat = iter->stat;
1151 return ret;
1154 static statement_t *new_block_statement(parser_ctx_t *ctx, unsigned loc, statement_list_t *list)
1156 block_statement_t *ret;
1158 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret), loc);
1159 if(!ret)
1160 return NULL;
1162 ret->scope_index = 0;
1163 ret->stat_list = list ? list->head : NULL;
1165 return &ret->stat;
1168 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1170 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1172 ret->identifier = identifier;
1173 ret->expr = expr;
1174 ret->next = NULL;
1175 ret->global_next = NULL;
1176 ret->block_scope = FALSE;
1177 ret->constant = FALSE;
1179 return ret;
1182 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1184 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1186 ret->head = ret->tail = decl;
1188 return ret;
1191 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1193 list->tail = list->tail->next = decl;
1195 return list;
1198 static statement_t *new_var_statement(parser_ctx_t *ctx, BOOL block_scope, BOOL constant, unsigned loc,
1199 variable_list_t *variable_list)
1201 variable_declaration_t *var;
1202 var_statement_t *ret;
1204 ret = new_statement(ctx, STAT_VAR, sizeof(*ret), loc);
1205 if(!ret)
1206 return NULL;
1208 ret->variable_list = variable_list->head;
1209 for (var = ret->variable_list; var; var = var->next)
1211 var->block_scope = block_scope;
1212 var->constant = constant;
1215 return &ret->stat;
1218 static statement_t *new_expression_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1220 expression_statement_t *ret;
1222 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret), loc);
1223 if(!ret)
1224 return NULL;
1226 ret->expr = expr;
1228 return &ret->stat;
1231 static statement_t *new_if_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr,
1232 statement_t *if_stat, statement_t *else_stat)
1234 if_statement_t *ret;
1236 ret = new_statement(ctx, STAT_IF, sizeof(*ret), loc);
1237 if(!ret)
1238 return NULL;
1240 ret->expr = expr;
1241 ret->if_stat = if_stat;
1242 ret->else_stat = else_stat;
1244 return &ret->stat;
1247 static statement_t *new_while_statement(parser_ctx_t *ctx, unsigned loc, BOOL dowhile, expression_t *expr, statement_t *stat)
1249 while_statement_t *ret;
1251 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret), loc);
1252 if(!ret)
1253 return NULL;
1255 ret->do_while = dowhile;
1256 ret->expr = expr;
1257 ret->statement = stat;
1259 return &ret->stat;
1262 static statement_t *new_for_statement(parser_ctx_t *ctx, unsigned loc, variable_declaration_t *variable_list, expression_t *begin_expr,
1263 expression_t *expr, unsigned expr_loc, expression_t *end_expr, unsigned end_loc, statement_t *statement)
1265 for_statement_t *ret;
1267 ret = new_statement(ctx, STAT_FOR, sizeof(*ret), loc);
1268 if(!ret)
1269 return NULL;
1271 ret->variable_list = variable_list;
1272 ret->begin_expr = begin_expr;
1273 ret->expr = expr;
1274 ret->expr_loc = expr_loc;
1275 ret->end_expr = end_expr;
1276 ret->end_loc = end_loc;
1277 ret->statement = statement;
1278 ret->scope_index = 0;
1280 return &ret->stat;
1283 static statement_t *new_forin_statement(parser_ctx_t *ctx, unsigned loc, variable_declaration_t *variable, expression_t *expr,
1284 expression_t *in_expr, statement_t *statement)
1286 forin_statement_t *ret;
1288 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret), loc);
1289 if(!ret)
1290 return NULL;
1292 ret->variable = variable;
1293 ret->expr = expr;
1294 ret->in_expr = in_expr;
1295 ret->statement = statement;
1297 return &ret->stat;
1300 static statement_t *new_continue_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier)
1302 branch_statement_t *ret;
1304 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret), loc);
1305 if(!ret)
1306 return NULL;
1308 ret->identifier = identifier;
1310 return &ret->stat;
1313 static statement_t *new_break_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier)
1315 branch_statement_t *ret;
1317 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret), loc);
1318 if(!ret)
1319 return NULL;
1321 ret->identifier = identifier;
1323 return &ret->stat;
1326 static statement_t *new_return_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1328 expression_statement_t *ret;
1330 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret), loc);
1331 if(!ret)
1332 return NULL;
1334 ret->expr = expr;
1336 return &ret->stat;
1339 static statement_t *new_with_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_t *statement)
1341 with_statement_t *ret;
1343 ret = new_statement(ctx, STAT_WITH, sizeof(*ret), loc);
1344 if(!ret)
1345 return NULL;
1347 ret->expr = expr;
1348 ret->statement = statement;
1350 return &ret->stat;
1353 static statement_t *new_labelled_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier, statement_t *statement)
1355 labelled_statement_t *ret;
1357 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret), loc);
1358 if(!ret)
1359 return NULL;
1361 ret->identifier = identifier;
1362 ret->statement = statement;
1364 return &ret->stat;
1367 static statement_t *new_switch_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, case_clausule_t *case_list)
1369 switch_statement_t *ret;
1371 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret), loc);
1372 if(!ret)
1373 return NULL;
1375 ret->expr = expr;
1376 ret->case_list = case_list;
1378 return &ret->stat;
1381 static statement_t *new_throw_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
1383 expression_statement_t *ret;
1385 ret = new_statement(ctx, STAT_THROW, sizeof(*ret), loc);
1386 if(!ret)
1387 return NULL;
1389 ret->expr = expr;
1391 return &ret->stat;
1394 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1395 catch_block_t *catch_block, statement_t *finally_statement, unsigned finally_loc)
1397 try_statement_t *ret;
1399 ret = new_statement(ctx, STAT_TRY, sizeof(*ret), try_statement->loc);
1400 if(!ret)
1401 return NULL;
1403 ret->try_statement = try_statement;
1404 ret->catch_block = catch_block;
1405 ret->finally_statement = finally_statement;
1406 ret->finally_loc = finally_loc;
1408 return &ret->stat;
1411 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1413 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1415 ret->identifier = identifier;
1416 ret->next = NULL;
1418 return ret;
1421 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1423 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1425 ret->head = ret->tail = new_parameter(ctx, identifier);
1427 return ret;
1430 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1432 list->tail = list->tail->next = new_parameter(ctx, identifier);
1434 return list;
1437 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier, parameter_list_t *parameter_list,
1438 statement_list_t *statement_list, const WCHAR *event_target, const WCHAR *src_str, DWORD src_len)
1440 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1442 ret->identifier = identifier;
1443 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1444 ret->statement_list = statement_list ? statement_list->head : NULL;
1445 ret->event_target = event_target;
1446 ret->src_str = src_str;
1447 ret->src_len = src_len;
1448 ret->is_statement = FALSE;
1449 ret->scope_index = 0;
1450 ret->next = NULL;
1452 return &ret->expr;
1455 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1457 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1459 ret->type = type;
1461 return ret;
1464 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1465 expression_t *expression1, expression_t *expression2)
1467 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1469 ret->expression1 = expression1;
1470 ret->expression2 = expression2;
1472 return &ret->expr;
1475 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1477 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1479 ret->expression = expression;
1481 return &ret->expr;
1484 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1485 expression_t *true_expression, expression_t *false_expression)
1487 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1489 ret->expression = expression;
1490 ret->true_expression = true_expression;
1491 ret->false_expression = false_expression;
1493 return &ret->expr;
1496 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1498 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1500 ret->expression = expression;
1501 ret->identifier = identifier;
1503 return &ret->expr;
1506 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1508 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1510 ret->expression = expression;
1511 ret->argument_list = argument_list ? argument_list->head : NULL;
1513 return &ret->expr;
1516 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1518 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1520 ret->expression = expression;
1521 ret->argument_list = argument_list ? argument_list->head : NULL;
1523 return &ret->expr;
1526 static int parser_error(unsigned *loc, parser_ctx_t *ctx, const char *str)
1528 if(ctx->error_loc == -1)
1529 ctx->error_loc = *loc;
1530 if(ctx->hres == S_OK)
1531 ctx->hres = JS_E_SYNTAX;
1532 return 0;
1535 static void set_error(parser_ctx_t *ctx, unsigned loc, HRESULT error)
1537 ctx->hres = error;
1538 ctx->error_loc = loc;
1541 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1543 if(obj || *(ctx->ptr-1)==next) return TRUE;
1545 set_error(ctx, ctx->ptr - ctx->begin /* FIXME */, JS_E_SYNTAX);
1546 return FALSE;
1550 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1552 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1554 ret->identifier = identifier;
1556 return &ret->expr;
1559 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1561 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1563 ret->element_list = element_list ? element_list->head : NULL;
1564 ret->length = length;
1566 return &ret->expr;
1569 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1571 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1573 ret->property_list = property_list ? property_list->head : NULL;
1575 return &ret->expr;
1578 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1580 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1582 ret->literal = literal;
1584 return &ret->expr;
1587 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1589 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1591 ret->head = ret->tail = statement;
1593 return ret;
1596 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1598 list->tail = list->tail->next = statement;
1600 return list;
1603 void parser_release(parser_ctx_t *ctx)
1605 script_release(ctx->script);
1606 heap_pool_free(&ctx->heap);
1607 free(ctx);
1610 HRESULT script_parse(script_ctx_t *ctx, struct _compiler_ctx_t *compiler, bytecode_t *code, const WCHAR *delimiter, BOOL from_eval,
1611 parser_ctx_t **ret)
1613 parser_ctx_t *parser_ctx;
1614 heap_pool_t *mark;
1615 HRESULT hres;
1617 parser_ctx = calloc(1, sizeof(parser_ctx_t));
1618 if(!parser_ctx)
1619 return E_OUTOFMEMORY;
1621 parser_ctx->error_loc = -1;
1622 parser_ctx->is_html = delimiter && !wcsicmp(delimiter, L"</script>");
1624 parser_ctx->begin = parser_ctx->ptr = code->source;
1625 parser_ctx->end = parser_ctx->begin + lstrlenW(parser_ctx->begin);
1627 script_addref(ctx);
1628 parser_ctx->script = ctx;
1630 mark = heap_pool_mark(&ctx->tmp_heap);
1631 heap_pool_init(&parser_ctx->heap);
1633 parser_ctx->compiler = compiler;
1634 parser_parse(parser_ctx);
1635 parser_ctx->compiler = NULL;
1637 heap_pool_clear(mark);
1638 hres = parser_ctx->hres;
1639 if(FAILED(hres)) {
1640 unsigned int error_loc = parser_ctx->error_loc == -1 ? 0 : parser_ctx->error_loc;
1641 const WCHAR *line_start = code->source + error_loc, *line_end = line_start;
1642 jsstr_t *line_str;
1644 while(line_start > code->source && line_start[-1] != '\n')
1645 line_start--;
1646 while(*line_end && *line_end != '\n')
1647 line_end++;
1648 line_str = jsstr_alloc_len(line_start, line_end - line_start);
1650 WARN("parser failed around %s in line %s\n",
1651 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20),
1652 debugstr_jsstr(line_str));
1654 throw_error(ctx, hres, NULL);
1655 set_error_location(ctx->ei, code, error_loc, IDS_COMPILATION_ERROR, line_str);
1656 parser_release(parser_ctx);
1657 if(line_str)
1658 jsstr_release(line_str);
1659 return DISP_E_EXCEPTION;
1662 *ret = parser_ctx;
1663 return S_OK;