[PSAPI_WINETEST] Sync with Wine Staging 2.16. CORE-13762
[reactos.git] / dll / win32 / jscript / parser.y
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 %{
20
21 #include "jscript.h"
22
23 static int parser_error(parser_ctx_t*,const char*);
24 static void set_error(parser_ctx_t*,UINT);
25 static BOOL explicit_error(parser_ctx_t*,void*,WCHAR);
26 static BOOL allow_auto_semicolon(parser_ctx_t*);
27 static void program_parsed(parser_ctx_t*,source_elements_t*);
28
29 typedef struct _statement_list_t {
30 statement_t *head;
31 statement_t *tail;
32 } statement_list_t;
33
34 static literal_t *new_string_literal(parser_ctx_t*,const WCHAR*);
35 static literal_t *new_null_literal(parser_ctx_t*);
36
37 typedef struct _property_list_t {
38 prop_val_t *head;
39 prop_val_t *tail;
40 } property_list_t;
41
42 static property_list_t *new_property_list(parser_ctx_t*,literal_t*,expression_t*);
43 static property_list_t *property_list_add(parser_ctx_t*,property_list_t*,literal_t*,expression_t*);
44
45 typedef struct _element_list_t {
46 array_element_t *head;
47 array_element_t *tail;
48 } element_list_t;
49
50 static element_list_t *new_element_list(parser_ctx_t*,int,expression_t*);
51 static element_list_t *element_list_add(parser_ctx_t*,element_list_t*,int,expression_t*);
52
53 typedef struct _argument_list_t {
54 argument_t *head;
55 argument_t *tail;
56 } argument_list_t;
57
58 static argument_list_t *new_argument_list(parser_ctx_t*,expression_t*);
59 static argument_list_t *argument_list_add(parser_ctx_t*,argument_list_t*,expression_t*);
60
61 typedef struct _case_list_t {
62 case_clausule_t *head;
63 case_clausule_t *tail;
64 } case_list_t;
65
66 static catch_block_t *new_catch_block(parser_ctx_t*,const WCHAR*,statement_t*);
67 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_list_t*);
68 static case_list_t *new_case_list(parser_ctx_t*,case_clausule_t*);
69 static case_list_t *case_list_add(parser_ctx_t*,case_list_t*,case_clausule_t*);
70 static case_clausule_t *new_case_block(parser_ctx_t*,case_list_t*,case_clausule_t*,case_list_t*);
71
72 typedef struct _variable_list_t {
73 variable_declaration_t *head;
74 variable_declaration_t *tail;
75 } variable_list_t;
76
77 static variable_declaration_t *new_variable_declaration(parser_ctx_t*,const WCHAR*,expression_t*);
78 static variable_list_t *new_variable_list(parser_ctx_t*,variable_declaration_t*);
79 static variable_list_t *variable_list_add(parser_ctx_t*,variable_list_t*,variable_declaration_t*);
80
81 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
82 static statement_t *new_block_statement(parser_ctx_t*,statement_list_t*);
83 static statement_t *new_var_statement(parser_ctx_t*,variable_list_t*);
84 static statement_t *new_expression_statement(parser_ctx_t*,expression_t*);
85 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,statement_t*);
86 static statement_t *new_while_statement(parser_ctx_t*,BOOL,expression_t*,statement_t*);
87 static statement_t *new_for_statement(parser_ctx_t*,variable_list_t*,expression_t*,expression_t*,
88 expression_t*,statement_t*);
89 static statement_t *new_forin_statement(parser_ctx_t*,variable_declaration_t*,expression_t*,expression_t*,statement_t*);
90 static statement_t *new_continue_statement(parser_ctx_t*,const WCHAR*);
91 static statement_t *new_break_statement(parser_ctx_t*,const WCHAR*);
92 static statement_t *new_return_statement(parser_ctx_t*,expression_t*);
93 static statement_t *new_with_statement(parser_ctx_t*,expression_t*,statement_t*);
94 static statement_t *new_labelled_statement(parser_ctx_t*,const WCHAR*,statement_t*);
95 static statement_t *new_switch_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
96 static statement_t *new_throw_statement(parser_ctx_t*,expression_t*);
97 static statement_t *new_try_statement(parser_ctx_t*,statement_t*,catch_block_t*,statement_t*);
98
99 struct statement_list_t {
100 statement_t *head;
101 statement_t *tail;
102 };
103
104 static statement_list_t *new_statement_list(parser_ctx_t*,statement_t*);
105 static statement_list_t *statement_list_add(statement_list_t*,statement_t*);
106
107 typedef struct _parameter_list_t {
108 parameter_t *head;
109 parameter_t *tail;
110 } parameter_list_t;
111
112 static parameter_list_t *new_parameter_list(parser_ctx_t*,const WCHAR*);
113 static parameter_list_t *parameter_list_add(parser_ctx_t*,parameter_list_t*,const WCHAR*);
114
115 static void *new_expression(parser_ctx_t *ctx,expression_type_t,size_t);
116 static expression_t *new_function_expression(parser_ctx_t*,const WCHAR*,parameter_list_t*,
117 source_elements_t*,const WCHAR*,const WCHAR*,DWORD);
118 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
119 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
120 static expression_t *new_conditional_expression(parser_ctx_t*,expression_t*,expression_t*,expression_t*);
121 static expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
122 static expression_t *new_new_expression(parser_ctx_t*,expression_t*,argument_list_t*);
123 static expression_t *new_call_expression(parser_ctx_t*,expression_t*,argument_list_t*);
124 static expression_t *new_identifier_expression(parser_ctx_t*,const WCHAR*);
125 static expression_t *new_literal_expression(parser_ctx_t*,literal_t*);
126 static expression_t *new_array_literal_expression(parser_ctx_t*,element_list_t*,int);
127 static expression_t *new_prop_and_value_expression(parser_ctx_t*,property_list_t*);
128
129 static source_elements_t *new_source_elements(parser_ctx_t*);
130 static source_elements_t *source_elements_add_statement(source_elements_t*,statement_t*);
131
132 %}
133
134 %lex-param { parser_ctx_t *ctx }
135 %parse-param { parser_ctx_t *ctx }
136 %pure-parser
137 %start Program
138
139 %union {
140 int ival;
141 const WCHAR *srcptr;
142 LPCWSTR wstr;
143 literal_t *literal;
144 struct _argument_list_t *argument_list;
145 case_clausule_t *case_clausule;
146 struct _case_list_t *case_list;
147 catch_block_t *catch_block;
148 struct _element_list_t *element_list;
149 expression_t *expr;
150 const WCHAR *identifier;
151 struct _parameter_list_t *parameter_list;
152 struct _property_list_t *property_list;
153 source_elements_t *source_elements;
154 statement_t *statement;
155 struct _statement_list_t *statement_list;
156 struct _variable_list_t *variable_list;
157 variable_declaration_t *variable_declaration;
158 }
159
160 /* keywords */
161 %token kBREAK kCASE kCATCH kCONTINUE kDEFAULT kDELETE kDO kELSE kIF kFINALLY kFOR kIN
162 %token kINSTANCEOF kNEW kNULL kRETURN kSWITCH kTHIS kTHROW kTRUE kFALSE kTRY kTYPEOF kVAR kVOID kWHILE kWITH
163 %token tANDAND tOROR tINC tDEC tHTMLCOMMENT kDIVEQ kDCOL
164
165 %token <srcptr> kFUNCTION '}'
166
167 /* tokens */
168 %token <identifier> tIdentifier
169 %token <ival> tAssignOper tEqOper tShiftOper tRelOper
170 %token <literal> tNumericLiteral tBooleanLiteral
171 %token <wstr> tStringLiteral
172 %token tEOF
173
174 %type <source_elements> SourceElements
175 %type <source_elements> FunctionBody
176 %type <statement> Statement
177 %type <statement> Block
178 %type <statement> VariableStatement
179 %type <statement> EmptyStatement
180 %type <statement> ExpressionStatement
181 %type <statement> IfStatement
182 %type <statement> IterationStatement
183 %type <statement> ContinueStatement
184 %type <statement> BreakStatement
185 %type <statement> ReturnStatement
186 %type <statement> WithStatement
187 %type <statement> LabelledStatement
188 %type <statement> SwitchStatement
189 %type <statement> ThrowStatement
190 %type <statement> TryStatement
191 %type <statement> Finally
192 %type <statement_list> StatementList StatementList_opt
193 %type <parameter_list> FormalParameterList FormalParameterList_opt
194 %type <expr> Expression Expression_opt Expression_err
195 %type <expr> ExpressionNoIn ExpressionNoIn_opt
196 %type <expr> FunctionExpression
197 %type <expr> AssignmentExpression AssignmentExpressionNoIn
198 %type <expr> ConditionalExpression ConditionalExpressionNoIn
199 %type <expr> LeftHandSideExpression
200 %type <expr> LogicalORExpression LogicalORExpressionNoIn
201 %type <expr> LogicalANDExpression LogicalANDExpressionNoIn
202 %type <expr> BitwiseORExpression BitwiseORExpressionNoIn
203 %type <expr> BitwiseXORExpression BitwiseXORExpressionNoIn
204 %type <expr> BitwiseANDExpression BitwiseANDExpressionNoIn
205 %type <expr> EqualityExpression EqualityExpressionNoIn
206 %type <expr> RelationalExpression RelationalExpressionNoIn
207 %type <expr> ShiftExpression
208 %type <expr> AdditiveExpression
209 %type <expr> MultiplicativeExpression
210 %type <expr> Initialiser_opt Initialiser
211 %type <expr> InitialiserNoIn_opt InitialiserNoIn
212 %type <expr> UnaryExpression
213 %type <expr> PostfixExpression
214 %type <expr> NewExpression
215 %type <expr> CallExpression
216 %type <expr> MemberExpression
217 %type <expr> PrimaryExpression
218 %type <identifier> Identifier_opt
219 %type <variable_list> VariableDeclarationList
220 %type <variable_list> VariableDeclarationListNoIn
221 %type <variable_declaration> VariableDeclaration
222 %type <variable_declaration> VariableDeclarationNoIn
223 %type <case_list> CaseClausules CaseClausules_opt
224 %type <case_clausule> CaseClausule DefaultClausule CaseBlock
225 %type <catch_block> Catch
226 %type <argument_list> Arguments
227 %type <argument_list> ArgumentList
228 %type <literal> Literal
229 %type <expr> ArrayLiteral
230 %type <expr> ObjectLiteral
231 %type <ival> Elision Elision_opt
232 %type <element_list> ElementList
233 %type <property_list> PropertyNameAndValueList
234 %type <literal> PropertyName
235 %type <literal> BooleanLiteral
236 %type <srcptr> KFunction
237 %type <ival> AssignOper
238
239 %nonassoc LOWER_THAN_ELSE
240 %nonassoc kELSE
241
242 %%
243
244 /* ECMA-262 3rd Edition 14 */
245 Program
246 : SourceElements HtmlComment tEOF
247 { program_parsed(ctx, $1); }
248
249 HtmlComment
250 : tHTMLCOMMENT {}
251 | /* empty */ {}
252
253 /* ECMA-262 3rd Edition 14 */
254 SourceElements
255 : /* empty */ { $$ = new_source_elements(ctx); }
256 | SourceElements Statement
257 { $$ = source_elements_add_statement($1, $2); }
258
259 /* ECMA-262 3rd Edition 13 */
260 FunctionExpression
261 : KFunction left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
262 { $$ = new_function_expression(ctx, NULL, $3, $6, NULL, $1, $7-$1+1); }
263 | KFunction tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
264 { $$ = new_function_expression(ctx, $2, $4, $7, NULL, $1, $8-$1+1); }
265 | KFunction tIdentifier kDCOL tIdentifier left_bracket FormalParameterList_opt right_bracket '{' FunctionBody '}'
266 { $$ = new_function_expression(ctx, $4, $6, $9, $2, $1, $10-$1+1); }
267
268 KFunction
269 : kFUNCTION { $$ = $1; }
270
271 /* ECMA-262 3rd Edition 13 */
272 FunctionBody
273 : SourceElements { $$ = $1; }
274
275 /* ECMA-262 3rd Edition 13 */
276 FormalParameterList
277 : tIdentifier { $$ = new_parameter_list(ctx, $1); }
278 | FormalParameterList ',' tIdentifier
279 { $$ = parameter_list_add(ctx, $1, $3); }
280
281 /* ECMA-262 3rd Edition 13 */
282 FormalParameterList_opt
283 : /* empty */ { $$ = NULL; }
284 | FormalParameterList { $$ = $1; }
285
286 /* ECMA-262 3rd Edition 12 */
287 Statement
288 : Block { $$ = $1; }
289 | VariableStatement { $$ = $1; }
290 | EmptyStatement { $$ = $1; }
291 | FunctionExpression { $$ = new_expression_statement(ctx, $1); }
292 | ExpressionStatement { $$ = $1; }
293 | IfStatement { $$ = $1; }
294 | IterationStatement { $$ = $1; }
295 | ContinueStatement { $$ = $1; }
296 | BreakStatement { $$ = $1; }
297 | ReturnStatement { $$ = $1; }
298 | WithStatement { $$ = $1; }
299 | LabelledStatement { $$ = $1; }
300 | SwitchStatement { $$ = $1; }
301 | ThrowStatement { $$ = $1; }
302 | TryStatement { $$ = $1; }
303
304 /* ECMA-262 3rd Edition 12.2 */
305 StatementList
306 : Statement { $$ = new_statement_list(ctx, $1); }
307 | StatementList Statement
308 { $$ = statement_list_add($1, $2); }
309
310 /* ECMA-262 3rd Edition 12.2 */
311 StatementList_opt
312 : /* empty */ { $$ = NULL; }
313 | StatementList { $$ = $1; }
314
315 /* ECMA-262 3rd Edition 12.1 */
316 Block
317 : '{' StatementList '}' { $$ = new_block_statement(ctx, $2); }
318 | '{' '}' { $$ = new_block_statement(ctx, NULL); }
319
320 /* ECMA-262 3rd Edition 12.2 */
321 VariableStatement
322 : kVAR VariableDeclarationList semicolon_opt
323 { $$ = new_var_statement(ctx, $2); }
324
325 /* ECMA-262 3rd Edition 12.2 */
326 VariableDeclarationList
327 : VariableDeclaration { $$ = new_variable_list(ctx, $1); }
328 | VariableDeclarationList ',' VariableDeclaration
329 { $$ = variable_list_add(ctx, $1, $3); }
330
331 /* ECMA-262 3rd Edition 12.2 */
332 VariableDeclarationListNoIn
333 : VariableDeclarationNoIn
334 { $$ = new_variable_list(ctx, $1); }
335 | VariableDeclarationListNoIn ',' VariableDeclarationNoIn
336 { $$ = variable_list_add(ctx, $1, $3); }
337
338 /* ECMA-262 3rd Edition 12.2 */
339 VariableDeclaration
340 : tIdentifier Initialiser_opt
341 { $$ = new_variable_declaration(ctx, $1, $2); }
342
343 /* ECMA-262 3rd Edition 12.2 */
344 VariableDeclarationNoIn
345 : tIdentifier InitialiserNoIn_opt
346 { $$ = new_variable_declaration(ctx, $1, $2); }
347
348 /* ECMA-262 3rd Edition 12.2 */
349 Initialiser_opt
350 : /* empty */ { $$ = NULL; }
351 | Initialiser { $$ = $1; }
352
353 /* ECMA-262 3rd Edition 12.2 */
354 Initialiser
355 : '=' AssignmentExpression
356 { $$ = $2; }
357
358 /* ECMA-262 3rd Edition 12.2 */
359 InitialiserNoIn_opt
360 : /* empty */ { $$ = NULL; }
361 | InitialiserNoIn { $$ = $1; }
362
363 /* ECMA-262 3rd Edition 12.2 */
364 InitialiserNoIn
365 : '=' AssignmentExpressionNoIn
366 { $$ = $2; }
367
368 /* ECMA-262 3rd Edition 12.3 */
369 EmptyStatement
370 : ';' { $$ = new_statement(ctx, STAT_EMPTY, 0); }
371
372 /* ECMA-262 3rd Edition 12.4 */
373 ExpressionStatement
374 : Expression semicolon_opt
375 { $$ = new_expression_statement(ctx, $1); }
376
377 /* ECMA-262 3rd Edition 12.5 */
378 IfStatement
379 : kIF left_bracket Expression_err right_bracket Statement kELSE Statement
380 { $$ = new_if_statement(ctx, $3, $5, $7); }
381 | kIF left_bracket Expression_err right_bracket Statement %prec LOWER_THAN_ELSE
382 { $$ = new_if_statement(ctx, $3, $5, NULL); }
383
384 /* ECMA-262 3rd Edition 12.6 */
385 IterationStatement
386 : kDO Statement kWHILE left_bracket Expression_err right_bracket semicolon_opt
387 { $$ = new_while_statement(ctx, TRUE, $5, $2); }
388 | kWHILE left_bracket Expression_err right_bracket Statement
389 { $$ = new_while_statement(ctx, FALSE, $3, $5); }
390 | kFOR left_bracket ExpressionNoIn_opt
391 { if(!explicit_error(ctx, $3, ';')) YYABORT; }
392 semicolon Expression_opt
393 { if(!explicit_error(ctx, $6, ';')) YYABORT; }
394 semicolon Expression_opt right_bracket Statement
395 { $$ = new_for_statement(ctx, NULL, $3, $6, $9, $11); }
396 | kFOR left_bracket kVAR VariableDeclarationListNoIn
397 { if(!explicit_error(ctx, $4, ';')) YYABORT; }
398 semicolon Expression_opt
399 { if(!explicit_error(ctx, $7, ';')) YYABORT; }
400 semicolon Expression_opt right_bracket Statement
401 { $$ = new_for_statement(ctx, $4, NULL, $7, $10, $12); }
402 | kFOR left_bracket LeftHandSideExpression kIN Expression_err right_bracket Statement
403 { $$ = new_forin_statement(ctx, NULL, $3, $5, $7); }
404 | kFOR left_bracket kVAR VariableDeclarationNoIn kIN Expression_err right_bracket Statement
405 { $$ = new_forin_statement(ctx, $4, NULL, $6, $8); }
406
407 /* ECMA-262 3rd Edition 12.7 */
408 ContinueStatement
409 : kCONTINUE /* NONL */ Identifier_opt semicolon_opt
410 { $$ = new_continue_statement(ctx, $2); }
411
412 /* ECMA-262 3rd Edition 12.8 */
413 BreakStatement
414 : kBREAK /* NONL */ Identifier_opt semicolon_opt
415 { $$ = new_break_statement(ctx, $2); }
416
417 /* ECMA-262 3rd Edition 12.9 */
418 ReturnStatement
419 : kRETURN /* NONL */ Expression_opt semicolon_opt
420 { $$ = new_return_statement(ctx, $2); }
421
422 /* ECMA-262 3rd Edition 12.10 */
423 WithStatement
424 : kWITH left_bracket Expression right_bracket Statement
425 { $$ = new_with_statement(ctx, $3, $5); }
426
427 /* ECMA-262 3rd Edition 12.12 */
428 LabelledStatement
429 : tIdentifier ':' Statement
430 { $$ = new_labelled_statement(ctx, $1, $3); }
431
432 /* ECMA-262 3rd Edition 12.11 */
433 SwitchStatement
434 : kSWITCH left_bracket Expression right_bracket CaseBlock
435 { $$ = new_switch_statement(ctx, $3, $5); }
436
437 /* ECMA-262 3rd Edition 12.11 */
438 CaseBlock
439 : '{' CaseClausules_opt '}'
440 { $$ = new_case_block(ctx, $2, NULL, NULL); }
441 | '{' CaseClausules_opt DefaultClausule CaseClausules_opt '}'
442 { $$ = new_case_block(ctx, $2, $3, $4); }
443
444 /* ECMA-262 3rd Edition 12.11 */
445 CaseClausules_opt
446 : /* empty */ { $$ = NULL; }
447 | CaseClausules { $$ = $1; }
448
449 /* ECMA-262 3rd Edition 12.11 */
450 CaseClausules
451 : CaseClausule { $$ = new_case_list(ctx, $1); }
452 | CaseClausules CaseClausule
453 { $$ = case_list_add(ctx, $1, $2); }
454
455 /* ECMA-262 3rd Edition 12.11 */
456 CaseClausule
457 : kCASE Expression ':' StatementList_opt
458 { $$ = new_case_clausule(ctx, $2, $4); }
459
460 /* ECMA-262 3rd Edition 12.11 */
461 DefaultClausule
462 : kDEFAULT ':' StatementList_opt
463 { $$ = new_case_clausule(ctx, NULL, $3); }
464
465 /* ECMA-262 3rd Edition 12.13 */
466 ThrowStatement
467 : kTHROW /* NONL */ Expression semicolon_opt
468 { $$ = new_throw_statement(ctx, $2); }
469
470 /* ECMA-262 3rd Edition 12.14 */
471 TryStatement
472 : kTRY Block Catch { $$ = new_try_statement(ctx, $2, $3, NULL); }
473 | kTRY Block Finally { $$ = new_try_statement(ctx, $2, NULL, $3); }
474 | kTRY Block Catch Finally
475 { $$ = new_try_statement(ctx, $2, $3, $4); }
476
477 /* ECMA-262 3rd Edition 12.14 */
478 Catch
479 : kCATCH left_bracket tIdentifier right_bracket Block
480 { $$ = new_catch_block(ctx, $3, $5); }
481
482 /* ECMA-262 3rd Edition 12.14 */
483 Finally
484 : kFINALLY Block { $$ = $2; }
485
486 /* ECMA-262 3rd Edition 11.14 */
487 Expression_opt
488 : /* empty */ { $$ = NULL; }
489 | Expression { $$ = $1; }
490
491 Expression_err
492 : Expression { $$ = $1; }
493 | error { set_error(ctx, JS_E_SYNTAX); YYABORT; }
494
495 /* ECMA-262 3rd Edition 11.14 */
496 Expression
497 : AssignmentExpression { $$ = $1; }
498 | Expression ',' AssignmentExpression
499 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
500
501 /* ECMA-262 3rd Edition 11.14 */
502 ExpressionNoIn_opt
503 : /* empty */ { $$ = NULL; }
504 | ExpressionNoIn { $$ = $1; }
505
506 /* ECMA-262 3rd Edition 11.14 */
507 ExpressionNoIn
508 : AssignmentExpressionNoIn
509 { $$ = $1; }
510 | ExpressionNoIn ',' AssignmentExpressionNoIn
511 { $$ = new_binary_expression(ctx, EXPR_COMMA, $1, $3); }
512
513 AssignOper
514 : tAssignOper { $$ = $1; }
515 | kDIVEQ { $$ = EXPR_ASSIGNDIV; }
516
517 /* ECMA-262 3rd Edition 11.13 */
518 AssignmentExpression
519 : ConditionalExpression { $$ = $1; }
520 | LeftHandSideExpression '=' AssignmentExpression
521 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
522 | LeftHandSideExpression AssignOper AssignmentExpression
523 { $$ = new_binary_expression(ctx, $2, $1, $3); }
524
525 /* ECMA-262 3rd Edition 11.13 */
526 AssignmentExpressionNoIn
527 : ConditionalExpressionNoIn
528 { $$ = $1; }
529 | LeftHandSideExpression '=' AssignmentExpressionNoIn
530 { $$ = new_binary_expression(ctx, EXPR_ASSIGN, $1, $3); }
531 | LeftHandSideExpression AssignOper AssignmentExpressionNoIn
532 { $$ = new_binary_expression(ctx, $2, $1, $3); }
533
534 /* ECMA-262 3rd Edition 11.12 */
535 ConditionalExpression
536 : LogicalORExpression { $$ = $1; }
537 | LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
538 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
539
540 /* ECMA-262 3rd Edition 11.12 */
541 ConditionalExpressionNoIn
542 : LogicalORExpressionNoIn
543 { $$ = $1; }
544 | LogicalORExpressionNoIn '?' AssignmentExpressionNoIn ':' AssignmentExpressionNoIn
545 { $$ = new_conditional_expression(ctx, $1, $3, $5); }
546
547 /* ECMA-262 3rd Edition 11.11 */
548 LogicalORExpression
549 : LogicalANDExpression { $$ = $1; }
550 | LogicalORExpression tOROR LogicalANDExpression
551 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
552
553 /* ECMA-262 3rd Edition 11.11 */
554 LogicalORExpressionNoIn
555 : LogicalANDExpressionNoIn
556 { $$ = $1; }
557 | LogicalORExpressionNoIn tOROR LogicalANDExpressionNoIn
558 { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); }
559
560 /* ECMA-262 3rd Edition 11.11 */
561 LogicalANDExpression
562 : BitwiseORExpression { $$ = $1; }
563 | LogicalANDExpression tANDAND BitwiseORExpression
564 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
565
566 /* ECMA-262 3rd Edition 11.11 */
567 LogicalANDExpressionNoIn
568 : BitwiseORExpressionNoIn
569 { $$ = $1; }
570 | LogicalANDExpressionNoIn tANDAND BitwiseORExpressionNoIn
571 { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); }
572
573 /* ECMA-262 3rd Edition 11.10 */
574 BitwiseORExpression
575 : BitwiseXORExpression { $$ = $1; }
576 | BitwiseORExpression '|' BitwiseXORExpression
577 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
578
579 /* ECMA-262 3rd Edition 11.10 */
580 BitwiseORExpressionNoIn
581 : BitwiseXORExpressionNoIn
582 { $$ = $1; }
583 | BitwiseORExpressionNoIn '|' BitwiseXORExpressionNoIn
584 { $$ = new_binary_expression(ctx, EXPR_BOR, $1, $3); }
585
586 /* ECMA-262 3rd Edition 11.10 */
587 BitwiseXORExpression
588 : BitwiseANDExpression { $$ = $1; }
589 | BitwiseXORExpression '^' BitwiseANDExpression
590 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
591
592 /* ECMA-262 3rd Edition 11.10 */
593 BitwiseXORExpressionNoIn
594 : BitwiseANDExpressionNoIn
595 { $$ = $1; }
596 | BitwiseXORExpressionNoIn '^' BitwiseANDExpressionNoIn
597 { $$ = new_binary_expression(ctx, EXPR_BXOR, $1, $3); }
598
599 /* ECMA-262 3rd Edition 11.10 */
600 BitwiseANDExpression
601 : EqualityExpression { $$ = $1; }
602 | BitwiseANDExpression '&' EqualityExpression
603 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
604
605 /* ECMA-262 3rd Edition 11.10 */
606 BitwiseANDExpressionNoIn
607 : EqualityExpressionNoIn
608 { $$ = $1; }
609 | BitwiseANDExpressionNoIn '&' EqualityExpressionNoIn
610 { $$ = new_binary_expression(ctx, EXPR_BAND, $1, $3); }
611
612 /* ECMA-262 3rd Edition 11.9 */
613 EqualityExpression
614 : RelationalExpression { $$ = $1; }
615 | EqualityExpression tEqOper RelationalExpression
616 { $$ = new_binary_expression(ctx, $2, $1, $3); }
617
618 /* ECMA-262 3rd Edition 11.9 */
619 EqualityExpressionNoIn
620 : RelationalExpressionNoIn { $$ = $1; }
621 | EqualityExpressionNoIn tEqOper RelationalExpressionNoIn
622 { $$ = new_binary_expression(ctx, $2, $1, $3); }
623
624 /* ECMA-262 3rd Edition 11.8 */
625 RelationalExpression
626 : ShiftExpression { $$ = $1; }
627 | RelationalExpression tRelOper ShiftExpression
628 { $$ = new_binary_expression(ctx, $2, $1, $3); }
629 | RelationalExpression kINSTANCEOF ShiftExpression
630 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
631 | RelationalExpression kIN ShiftExpression
632 { $$ = new_binary_expression(ctx, EXPR_IN, $1, $3); }
633
634 /* ECMA-262 3rd Edition 11.8 */
635 RelationalExpressionNoIn
636 : ShiftExpression { $$ = $1; }
637 | RelationalExpressionNoIn tRelOper ShiftExpression
638 { $$ = new_binary_expression(ctx, $2, $1, $3); }
639 | RelationalExpressionNoIn kINSTANCEOF ShiftExpression
640 { $$ = new_binary_expression(ctx, EXPR_INSTANCEOF, $1, $3); }
641
642 /* ECMA-262 3rd Edition 11.7 */
643 ShiftExpression
644 : AdditiveExpression { $$ = $1; }
645 | ShiftExpression tShiftOper AdditiveExpression
646 { $$ = new_binary_expression(ctx, $2, $1, $3); }
647
648 /* ECMA-262 3rd Edition 11.6 */
649 AdditiveExpression
650 : MultiplicativeExpression
651 { $$ = $1; }
652 | AdditiveExpression '+' MultiplicativeExpression
653 { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); }
654 | AdditiveExpression '-' MultiplicativeExpression
655 { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); }
656
657 /* ECMA-262 3rd Edition 11.5 */
658 MultiplicativeExpression
659 : UnaryExpression { $$ = $1; }
660 | MultiplicativeExpression '*' UnaryExpression
661 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); }
662 | MultiplicativeExpression '/' UnaryExpression
663 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); }
664 | MultiplicativeExpression '%' UnaryExpression
665 { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); }
666
667 /* ECMA-262 3rd Edition 11.4 */
668 UnaryExpression
669 : PostfixExpression { $$ = $1; }
670 | kDELETE UnaryExpression
671 { $$ = new_unary_expression(ctx, EXPR_DELETE, $2); }
672 | kVOID UnaryExpression { $$ = new_unary_expression(ctx, EXPR_VOID, $2); }
673 | kTYPEOF UnaryExpression
674 { $$ = new_unary_expression(ctx, EXPR_TYPEOF, $2); }
675 | tINC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREINC, $2); }
676 | tDEC UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PREDEC, $2); }
677 | '+' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_PLUS, $2); }
678 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_MINUS, $2); }
679 | '~' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_BITNEG, $2); }
680 | '!' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_LOGNEG, $2); }
681
682 /* ECMA-262 3rd Edition 11.2 */
683 PostfixExpression
684 : LeftHandSideExpression
685 { $$ = $1; }
686 | LeftHandSideExpression /* NONL */ tINC
687 { $$ = new_unary_expression(ctx, EXPR_POSTINC, $1); }
688 | LeftHandSideExpression /* NONL */ tDEC
689 { $$ = new_unary_expression(ctx, EXPR_POSTDEC, $1); }
690
691
692 /* ECMA-262 3rd Edition 11.2 */
693 LeftHandSideExpression
694 : NewExpression { $$ = $1; }
695 | CallExpression { $$ = $1; }
696
697 /* ECMA-262 3rd Edition 11.2 */
698 NewExpression
699 : MemberExpression { $$ = $1; }
700 | kNEW NewExpression { $$ = new_new_expression(ctx, $2, NULL); }
701
702 /* ECMA-262 3rd Edition 11.2 */
703 MemberExpression
704 : PrimaryExpression { $$ = $1; }
705 | FunctionExpression { $$ = $1; }
706 | MemberExpression '[' Expression ']'
707 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
708 | MemberExpression '.' tIdentifier
709 { $$ = new_member_expression(ctx, $1, $3); }
710 | kNEW MemberExpression Arguments
711 { $$ = new_new_expression(ctx, $2, $3); }
712
713 /* ECMA-262 3rd Edition 11.2 */
714 CallExpression
715 : MemberExpression Arguments
716 { $$ = new_call_expression(ctx, $1, $2); }
717 | CallExpression Arguments
718 { $$ = new_call_expression(ctx, $1, $2); }
719 | CallExpression '[' Expression ']'
720 { $$ = new_binary_expression(ctx, EXPR_ARRAY, $1, $3); }
721 | CallExpression '.' tIdentifier
722 { $$ = new_member_expression(ctx, $1, $3); }
723
724 /* ECMA-262 3rd Edition 11.2 */
725 Arguments
726 : '(' ')' { $$ = NULL; }
727 | '(' ArgumentList ')' { $$ = $2; }
728
729 /* ECMA-262 3rd Edition 11.2 */
730 ArgumentList
731 : AssignmentExpression { $$ = new_argument_list(ctx, $1); }
732 | ArgumentList ',' AssignmentExpression
733 { $$ = argument_list_add(ctx, $1, $3); }
734
735 /* ECMA-262 3rd Edition 11.1 */
736 PrimaryExpression
737 : kTHIS { $$ = new_expression(ctx, EXPR_THIS, 0); }
738 | tIdentifier { $$ = new_identifier_expression(ctx, $1); }
739 | Literal { $$ = new_literal_expression(ctx, $1); }
740 | ArrayLiteral { $$ = $1; }
741 | ObjectLiteral { $$ = $1; }
742 | '(' Expression ')' { $$ = $2; }
743
744 /* ECMA-262 3rd Edition 11.1.4 */
745 ArrayLiteral
746 : '[' ']' { $$ = new_array_literal_expression(ctx, NULL, 0); }
747 | '[' Elision ']' { $$ = new_array_literal_expression(ctx, NULL, $2+1); }
748 | '[' ElementList ']' { $$ = new_array_literal_expression(ctx, $2, 0); }
749 | '[' ElementList ',' Elision_opt ']'
750 { $$ = new_array_literal_expression(ctx, $2, $4+1); }
751
752 /* ECMA-262 3rd Edition 11.1.4 */
753 ElementList
754 : Elision_opt AssignmentExpression
755 { $$ = new_element_list(ctx, $1, $2); }
756 | ElementList ',' Elision_opt AssignmentExpression
757 { $$ = element_list_add(ctx, $1, $3, $4); }
758
759 /* ECMA-262 3rd Edition 11.1.4 */
760 Elision
761 : ',' { $$ = 1; }
762 | Elision ',' { $$ = $1 + 1; }
763
764 /* ECMA-262 3rd Edition 11.1.4 */
765 Elision_opt
766 : /* empty */ { $$ = 0; }
767 | Elision { $$ = $1; }
768
769 /* ECMA-262 3rd Edition 11.1.5 */
770 ObjectLiteral
771 : '{' '}' { $$ = new_prop_and_value_expression(ctx, NULL); }
772 | '{' PropertyNameAndValueList '}'
773 { $$ = new_prop_and_value_expression(ctx, $2); }
774
775 /* ECMA-262 3rd Edition 11.1.5 */
776 PropertyNameAndValueList
777 : PropertyName ':' AssignmentExpression
778 { $$ = new_property_list(ctx, $1, $3); }
779 | PropertyNameAndValueList ',' PropertyName ':' AssignmentExpression
780 { $$ = property_list_add(ctx, $1, $3, $5); }
781
782 /* ECMA-262 3rd Edition 11.1.5 */
783 PropertyName
784 : tIdentifier { $$ = new_string_literal(ctx, $1); }
785 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
786 | tNumericLiteral { $$ = $1; }
787
788 /* ECMA-262 3rd Edition 7.6 */
789 Identifier_opt
790 : /* empty*/ { $$ = NULL; }
791 | tIdentifier { $$ = $1; }
792
793 /* ECMA-262 3rd Edition 7.8 */
794 Literal
795 : kNULL { $$ = new_null_literal(ctx); }
796 | BooleanLiteral { $$ = $1; }
797 | tNumericLiteral { $$ = $1; }
798 | tStringLiteral { $$ = new_string_literal(ctx, $1); }
799 | '/' { $$ = parse_regexp(ctx);
800 if(!$$) YYABORT; }
801 | kDIVEQ { $$ = parse_regexp(ctx);
802 if(!$$) YYABORT; }
803
804 /* ECMA-262 3rd Edition 7.8.2 */
805 BooleanLiteral
806 : kTRUE { $$ = new_boolean_literal(ctx, VARIANT_TRUE); }
807 | kFALSE { $$ = new_boolean_literal(ctx, VARIANT_FALSE); }
808 | tBooleanLiteral { $$ = $1; }
809
810 semicolon_opt
811 : ';'
812 | error { if(!allow_auto_semicolon(ctx)) {YYABORT;} }
813
814 left_bracket
815 : '('
816 | error { set_error(ctx, JS_E_MISSING_LBRACKET); YYABORT; }
817
818 right_bracket
819 : ')'
820 | error { set_error(ctx, JS_E_MISSING_RBRACKET); YYABORT; }
821
822 semicolon
823 : ';'
824 | error { set_error(ctx, JS_E_MISSING_SEMICOLON); YYABORT; }
825
826 %%
827
828 static BOOL allow_auto_semicolon(parser_ctx_t *ctx)
829 {
830 return ctx->nl || ctx->ptr == ctx->end || *(ctx->ptr-1) == '}';
831 }
832
833 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
834 {
835 statement_t *stat;
836
837 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
838 if(!stat)
839 return NULL;
840
841 stat->type = type;
842 stat->next = NULL;
843
844 return stat;
845 }
846
847 static literal_t *new_string_literal(parser_ctx_t *ctx, const WCHAR *str)
848 {
849 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
850
851 ret->type = LT_STRING;
852 ret->u.wstr = str;
853
854 return ret;
855 }
856
857 static literal_t *new_null_literal(parser_ctx_t *ctx)
858 {
859 literal_t *ret = parser_alloc(ctx, sizeof(literal_t));
860
861 ret->type = LT_NULL;
862
863 return ret;
864 }
865
866 static prop_val_t *new_prop_val(parser_ctx_t *ctx, literal_t *name, expression_t *value)
867 {
868 prop_val_t *ret = parser_alloc(ctx, sizeof(prop_val_t));
869
870 ret->name = name;
871 ret->value = value;
872 ret->next = NULL;
873
874 return ret;
875 }
876
877 static property_list_t *new_property_list(parser_ctx_t *ctx, literal_t *name, expression_t *value)
878 {
879 property_list_t *ret = parser_alloc_tmp(ctx, sizeof(property_list_t));
880
881 ret->head = ret->tail = new_prop_val(ctx, name, value);
882
883 return ret;
884 }
885
886 static property_list_t *property_list_add(parser_ctx_t *ctx, property_list_t *list, literal_t *name, expression_t *value)
887 {
888 list->tail = list->tail->next = new_prop_val(ctx, name, value);
889
890 return list;
891 }
892
893 static array_element_t *new_array_element(parser_ctx_t *ctx, int elision, expression_t *expr)
894 {
895 array_element_t *ret = parser_alloc(ctx, sizeof(array_element_t));
896
897 ret->elision = elision;
898 ret->expr = expr;
899 ret->next = NULL;
900
901 return ret;
902 }
903
904 static element_list_t *new_element_list(parser_ctx_t *ctx, int elision, expression_t *expr)
905 {
906 element_list_t *ret = parser_alloc_tmp(ctx, sizeof(element_list_t));
907
908 ret->head = ret->tail = new_array_element(ctx, elision, expr);
909
910 return ret;
911 }
912
913 static element_list_t *element_list_add(parser_ctx_t *ctx, element_list_t *list, int elision, expression_t *expr)
914 {
915 list->tail = list->tail->next = new_array_element(ctx, elision, expr);
916
917 return list;
918 }
919
920 static argument_t *new_argument(parser_ctx_t *ctx, expression_t *expr)
921 {
922 argument_t *ret = parser_alloc(ctx, sizeof(argument_t));
923
924 ret->expr = expr;
925 ret->next = NULL;
926
927 return ret;
928 }
929
930 static argument_list_t *new_argument_list(parser_ctx_t *ctx, expression_t *expr)
931 {
932 argument_list_t *ret = parser_alloc_tmp(ctx, sizeof(argument_list_t));
933
934 ret->head = ret->tail = new_argument(ctx, expr);
935
936 return ret;
937 }
938
939 static argument_list_t *argument_list_add(parser_ctx_t *ctx, argument_list_t *list, expression_t *expr)
940 {
941 list->tail = list->tail->next = new_argument(ctx, expr);
942
943 return list;
944 }
945
946 static catch_block_t *new_catch_block(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
947 {
948 catch_block_t *ret = parser_alloc(ctx, sizeof(catch_block_t));
949
950 ret->identifier = identifier;
951 ret->statement = statement;
952
953 return ret;
954 }
955
956 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_list_t *stat_list)
957 {
958 case_clausule_t *ret = parser_alloc(ctx, sizeof(case_clausule_t));
959
960 ret->expr = expr;
961 ret->stat = stat_list ? stat_list->head : NULL;
962 ret->next = NULL;
963
964 return ret;
965 }
966
967 static case_list_t *new_case_list(parser_ctx_t *ctx, case_clausule_t *case_clausule)
968 {
969 case_list_t *ret = parser_alloc_tmp(ctx, sizeof(case_list_t));
970
971 ret->head = ret->tail = case_clausule;
972
973 return ret;
974 }
975
976 static case_list_t *case_list_add(parser_ctx_t *ctx, case_list_t *list, case_clausule_t *case_clausule)
977 {
978 list->tail = list->tail->next = case_clausule;
979
980 return list;
981 }
982
983 static case_clausule_t *new_case_block(parser_ctx_t *ctx, case_list_t *case_list1,
984 case_clausule_t *default_clausule, case_list_t *case_list2)
985 {
986 case_clausule_t *ret = NULL, *iter = NULL, *iter2;
987 statement_t *stat = NULL;
988
989 if(case_list1) {
990 ret = case_list1->head;
991 iter = case_list1->tail;
992 }
993
994 if(default_clausule) {
995 if(ret)
996 iter = iter->next = default_clausule;
997 else
998 ret = iter = default_clausule;
999 }
1000
1001 if(case_list2) {
1002 if(ret)
1003 iter->next = case_list2->head;
1004 else
1005 ret = case_list2->head;
1006 }
1007
1008 if(!ret)
1009 return NULL;
1010
1011 for(iter = ret; iter; iter = iter->next) {
1012 for(iter2 = iter; iter2 && !iter2->stat; iter2 = iter2->next);
1013 if(!iter2)
1014 break;
1015
1016 while(iter != iter2) {
1017 iter->stat = iter2->stat;
1018 iter = iter->next;
1019 }
1020
1021 if(stat) {
1022 while(stat->next)
1023 stat = stat->next;
1024 stat->next = iter->stat;
1025 }else {
1026 stat = iter->stat;
1027 }
1028 }
1029
1030 return ret;
1031 }
1032
1033 static statement_t *new_block_statement(parser_ctx_t *ctx, statement_list_t *list)
1034 {
1035 block_statement_t *ret;
1036
1037 ret = new_statement(ctx, STAT_BLOCK, sizeof(*ret));
1038 if(!ret)
1039 return NULL;
1040
1041 ret->stat_list = list ? list->head : NULL;
1042
1043 return &ret->stat;
1044 }
1045
1046 static variable_declaration_t *new_variable_declaration(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *expr)
1047 {
1048 variable_declaration_t *ret = parser_alloc(ctx, sizeof(variable_declaration_t));
1049
1050 ret->identifier = identifier;
1051 ret->expr = expr;
1052 ret->next = NULL;
1053 ret->global_next = NULL;
1054
1055 return ret;
1056 }
1057
1058 static variable_list_t *new_variable_list(parser_ctx_t *ctx, variable_declaration_t *decl)
1059 {
1060 variable_list_t *ret = parser_alloc_tmp(ctx, sizeof(variable_list_t));
1061
1062 ret->head = ret->tail = decl;
1063
1064 return ret;
1065 }
1066
1067 static variable_list_t *variable_list_add(parser_ctx_t *ctx, variable_list_t *list, variable_declaration_t *decl)
1068 {
1069 list->tail = list->tail->next = decl;
1070
1071 return list;
1072 }
1073
1074 static statement_t *new_var_statement(parser_ctx_t *ctx, variable_list_t *variable_list)
1075 {
1076 var_statement_t *ret;
1077
1078 ret = new_statement(ctx, STAT_VAR, sizeof(*ret));
1079 if(!ret)
1080 return NULL;
1081
1082 ret->variable_list = variable_list->head;
1083
1084 return &ret->stat;
1085 }
1086
1087 static statement_t *new_expression_statement(parser_ctx_t *ctx, expression_t *expr)
1088 {
1089 expression_statement_t *ret;
1090
1091 ret = new_statement(ctx, STAT_EXPR, sizeof(*ret));
1092 if(!ret)
1093 return NULL;
1094
1095 ret->expr = expr;
1096
1097 return &ret->stat;
1098 }
1099
1100 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, statement_t *else_stat)
1101 {
1102 if_statement_t *ret;
1103
1104 ret = new_statement(ctx, STAT_IF, sizeof(*ret));
1105 if(!ret)
1106 return NULL;
1107
1108 ret->expr = expr;
1109 ret->if_stat = if_stat;
1110 ret->else_stat = else_stat;
1111
1112 return &ret->stat;
1113 }
1114
1115 static statement_t *new_while_statement(parser_ctx_t *ctx, BOOL dowhile, expression_t *expr, statement_t *stat)
1116 {
1117 while_statement_t *ret;
1118
1119 ret = new_statement(ctx, STAT_WHILE, sizeof(*ret));
1120 if(!ret)
1121 return NULL;
1122
1123 ret->do_while = dowhile;
1124 ret->expr = expr;
1125 ret->statement = stat;
1126
1127 return &ret->stat;
1128 }
1129
1130 static statement_t *new_for_statement(parser_ctx_t *ctx, variable_list_t *variable_list, expression_t *begin_expr,
1131 expression_t *expr, expression_t *end_expr, statement_t *statement)
1132 {
1133 for_statement_t *ret;
1134
1135 ret = new_statement(ctx, STAT_FOR, sizeof(*ret));
1136 if(!ret)
1137 return NULL;
1138
1139 ret->variable_list = variable_list ? variable_list->head : NULL;
1140 ret->begin_expr = begin_expr;
1141 ret->expr = expr;
1142 ret->end_expr = end_expr;
1143 ret->statement = statement;
1144
1145 return &ret->stat;
1146 }
1147
1148 static statement_t *new_forin_statement(parser_ctx_t *ctx, variable_declaration_t *variable, expression_t *expr,
1149 expression_t *in_expr, statement_t *statement)
1150 {
1151 forin_statement_t *ret;
1152
1153 ret = new_statement(ctx, STAT_FORIN, sizeof(*ret));
1154 if(!ret)
1155 return NULL;
1156
1157 ret->variable = variable;
1158 ret->expr = expr;
1159 ret->in_expr = in_expr;
1160 ret->statement = statement;
1161
1162 return &ret->stat;
1163 }
1164
1165 static statement_t *new_continue_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1166 {
1167 branch_statement_t *ret;
1168
1169 ret = new_statement(ctx, STAT_CONTINUE, sizeof(*ret));
1170 if(!ret)
1171 return NULL;
1172
1173 ret->identifier = identifier;
1174
1175 return &ret->stat;
1176 }
1177
1178 static statement_t *new_break_statement(parser_ctx_t *ctx, const WCHAR *identifier)
1179 {
1180 branch_statement_t *ret;
1181
1182 ret = new_statement(ctx, STAT_BREAK, sizeof(*ret));
1183 if(!ret)
1184 return NULL;
1185
1186 ret->identifier = identifier;
1187
1188 return &ret->stat;
1189 }
1190
1191 static statement_t *new_return_statement(parser_ctx_t *ctx, expression_t *expr)
1192 {
1193 expression_statement_t *ret;
1194
1195 ret = new_statement(ctx, STAT_RETURN, sizeof(*ret));
1196 if(!ret)
1197 return NULL;
1198
1199 ret->expr = expr;
1200
1201 return &ret->stat;
1202 }
1203
1204 static statement_t *new_with_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *statement)
1205 {
1206 with_statement_t *ret;
1207
1208 ret = new_statement(ctx, STAT_WITH, sizeof(*ret));
1209 if(!ret)
1210 return NULL;
1211
1212 ret->expr = expr;
1213 ret->statement = statement;
1214
1215 return &ret->stat;
1216 }
1217
1218 static statement_t *new_labelled_statement(parser_ctx_t *ctx, const WCHAR *identifier, statement_t *statement)
1219 {
1220 labelled_statement_t *ret;
1221
1222 ret = new_statement(ctx, STAT_LABEL, sizeof(*ret));
1223 if(!ret)
1224 return NULL;
1225
1226 ret->identifier = identifier;
1227 ret->statement = statement;
1228
1229 return &ret->stat;
1230 }
1231
1232 static statement_t *new_switch_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_list)
1233 {
1234 switch_statement_t *ret;
1235
1236 ret = new_statement(ctx, STAT_SWITCH, sizeof(*ret));
1237 if(!ret)
1238 return NULL;
1239
1240 ret->expr = expr;
1241 ret->case_list = case_list;
1242
1243 return &ret->stat;
1244 }
1245
1246 static statement_t *new_throw_statement(parser_ctx_t *ctx, expression_t *expr)
1247 {
1248 expression_statement_t *ret;
1249
1250 ret = new_statement(ctx, STAT_THROW, sizeof(*ret));
1251 if(!ret)
1252 return NULL;
1253
1254 ret->expr = expr;
1255
1256 return &ret->stat;
1257 }
1258
1259 static statement_t *new_try_statement(parser_ctx_t *ctx, statement_t *try_statement,
1260 catch_block_t *catch_block, statement_t *finally_statement)
1261 {
1262 try_statement_t *ret;
1263
1264 ret = new_statement(ctx, STAT_TRY, sizeof(*ret));
1265 if(!ret)
1266 return NULL;
1267
1268 ret->try_statement = try_statement;
1269 ret->catch_block = catch_block;
1270 ret->finally_statement = finally_statement;
1271
1272 return &ret->stat;
1273 }
1274
1275 static parameter_t *new_parameter(parser_ctx_t *ctx, const WCHAR *identifier)
1276 {
1277 parameter_t *ret = parser_alloc(ctx, sizeof(parameter_t));
1278
1279 ret->identifier = identifier;
1280 ret->next = NULL;
1281
1282 return ret;
1283 }
1284
1285 static parameter_list_t *new_parameter_list(parser_ctx_t *ctx, const WCHAR *identifier)
1286 {
1287 parameter_list_t *ret = parser_alloc_tmp(ctx, sizeof(parameter_list_t));
1288
1289 ret->head = ret->tail = new_parameter(ctx, identifier);
1290
1291 return ret;
1292 }
1293
1294 static parameter_list_t *parameter_list_add(parser_ctx_t *ctx, parameter_list_t *list, const WCHAR *identifier)
1295 {
1296 list->tail = list->tail->next = new_parameter(ctx, identifier);
1297
1298 return list;
1299 }
1300
1301 static expression_t *new_function_expression(parser_ctx_t *ctx, const WCHAR *identifier, parameter_list_t *parameter_list,
1302 source_elements_t *source_elements, const WCHAR *event_target, const WCHAR *src_str, DWORD src_len)
1303 {
1304 function_expression_t *ret = new_expression(ctx, EXPR_FUNC, sizeof(*ret));
1305
1306 ret->identifier = identifier;
1307 ret->parameter_list = parameter_list ? parameter_list->head : NULL;
1308 ret->source_elements = source_elements;
1309 ret->event_target = event_target;
1310 ret->src_str = src_str;
1311 ret->src_len = src_len;
1312 ret->next = NULL;
1313
1314 return &ret->expr;
1315 }
1316
1317 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
1318 {
1319 expression_t *ret = parser_alloc(ctx, size ? size : sizeof(*ret));
1320
1321 ret->type = type;
1322
1323 return ret;
1324 }
1325
1326 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type,
1327 expression_t *expression1, expression_t *expression2)
1328 {
1329 binary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1330
1331 ret->expression1 = expression1;
1332 ret->expression2 = expression2;
1333
1334 return &ret->expr;
1335 }
1336
1337 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *expression)
1338 {
1339 unary_expression_t *ret = new_expression(ctx, type, sizeof(*ret));
1340
1341 ret->expression = expression;
1342
1343 return &ret->expr;
1344 }
1345
1346 static expression_t *new_conditional_expression(parser_ctx_t *ctx, expression_t *expression,
1347 expression_t *true_expression, expression_t *false_expression)
1348 {
1349 conditional_expression_t *ret = new_expression(ctx, EXPR_COND, sizeof(*ret));
1350
1351 ret->expression = expression;
1352 ret->true_expression = true_expression;
1353 ret->false_expression = false_expression;
1354
1355 return &ret->expr;
1356 }
1357
1358 static expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *expression, const WCHAR *identifier)
1359 {
1360 member_expression_t *ret = new_expression(ctx, EXPR_MEMBER, sizeof(*ret));
1361
1362 ret->expression = expression;
1363 ret->identifier = identifier;
1364
1365 return &ret->expr;
1366 }
1367
1368 static expression_t *new_new_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1369 {
1370 call_expression_t *ret = new_expression(ctx, EXPR_NEW, sizeof(*ret));
1371
1372 ret->expression = expression;
1373 ret->argument_list = argument_list ? argument_list->head : NULL;
1374
1375 return &ret->expr;
1376 }
1377
1378 static expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expression, argument_list_t *argument_list)
1379 {
1380 call_expression_t *ret = new_expression(ctx, EXPR_CALL, sizeof(*ret));
1381
1382 ret->expression = expression;
1383 ret->argument_list = argument_list ? argument_list->head : NULL;
1384
1385 return &ret->expr;
1386 }
1387
1388 static int parser_error(parser_ctx_t *ctx, const char *str)
1389 {
1390 return 0;
1391 }
1392
1393 static void set_error(parser_ctx_t *ctx, UINT error)
1394 {
1395 ctx->hres = error;
1396 }
1397
1398 static BOOL explicit_error(parser_ctx_t *ctx, void *obj, WCHAR next)
1399 {
1400 if(obj || *(ctx->ptr-1)==next) return TRUE;
1401
1402 set_error(ctx, JS_E_SYNTAX);
1403 return FALSE;
1404 }
1405
1406
1407 static expression_t *new_identifier_expression(parser_ctx_t *ctx, const WCHAR *identifier)
1408 {
1409 identifier_expression_t *ret = new_expression(ctx, EXPR_IDENT, sizeof(*ret));
1410
1411 ret->identifier = identifier;
1412
1413 return &ret->expr;
1414 }
1415
1416 static expression_t *new_array_literal_expression(parser_ctx_t *ctx, element_list_t *element_list, int length)
1417 {
1418 array_literal_expression_t *ret = new_expression(ctx, EXPR_ARRAYLIT, sizeof(*ret));
1419
1420 ret->element_list = element_list ? element_list->head : NULL;
1421 ret->length = length;
1422
1423 return &ret->expr;
1424 }
1425
1426 static expression_t *new_prop_and_value_expression(parser_ctx_t *ctx, property_list_t *property_list)
1427 {
1428 property_value_expression_t *ret = new_expression(ctx, EXPR_PROPVAL, sizeof(*ret));
1429
1430 ret->property_list = property_list ? property_list->head : NULL;
1431
1432 return &ret->expr;
1433 }
1434
1435 static expression_t *new_literal_expression(parser_ctx_t *ctx, literal_t *literal)
1436 {
1437 literal_expression_t *ret = new_expression(ctx, EXPR_LITERAL, sizeof(*ret));
1438
1439 ret->literal = literal;
1440
1441 return &ret->expr;
1442 }
1443
1444 static source_elements_t *new_source_elements(parser_ctx_t *ctx)
1445 {
1446 source_elements_t *ret = parser_alloc(ctx, sizeof(source_elements_t));
1447
1448 memset(ret, 0, sizeof(*ret));
1449
1450 return ret;
1451 }
1452
1453 static source_elements_t *source_elements_add_statement(source_elements_t *source_elements, statement_t *statement)
1454 {
1455 if(source_elements->statement_tail)
1456 source_elements->statement_tail = source_elements->statement_tail->next = statement;
1457 else
1458 source_elements->statement = source_elements->statement_tail = statement;
1459
1460 return source_elements;
1461 }
1462
1463 static statement_list_t *new_statement_list(parser_ctx_t *ctx, statement_t *statement)
1464 {
1465 statement_list_t *ret = parser_alloc_tmp(ctx, sizeof(statement_list_t));
1466
1467 ret->head = ret->tail = statement;
1468
1469 return ret;
1470 }
1471
1472 static statement_list_t *statement_list_add(statement_list_t *list, statement_t *statement)
1473 {
1474 list->tail = list->tail->next = statement;
1475
1476 return list;
1477 }
1478
1479 static void program_parsed(parser_ctx_t *ctx, source_elements_t *source)
1480 {
1481 ctx->source = source;
1482 if(!ctx->lexer_error)
1483 ctx->hres = S_OK;
1484 }
1485
1486 void parser_release(parser_ctx_t *ctx)
1487 {
1488 script_release(ctx->script);
1489 heap_pool_free(&ctx->heap);
1490 heap_free(ctx);
1491 }
1492
1493 HRESULT script_parse(script_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, BOOL from_eval,
1494 parser_ctx_t **ret)
1495 {
1496 parser_ctx_t *parser_ctx;
1497 heap_pool_t *mark;
1498 HRESULT hres;
1499
1500 const WCHAR html_tagW[] = {'<','/','s','c','r','i','p','t','>',0};
1501
1502 parser_ctx = heap_alloc_zero(sizeof(parser_ctx_t));
1503 if(!parser_ctx)
1504 return E_OUTOFMEMORY;
1505
1506 parser_ctx->hres = JS_E_SYNTAX;
1507 parser_ctx->is_html = delimiter && !strcmpiW(delimiter, html_tagW);
1508
1509 parser_ctx->begin = parser_ctx->ptr = code;
1510 parser_ctx->end = parser_ctx->begin + strlenW(parser_ctx->begin);
1511
1512 script_addref(ctx);
1513 parser_ctx->script = ctx;
1514
1515 mark = heap_pool_mark(&ctx->tmp_heap);
1516 heap_pool_init(&parser_ctx->heap);
1517
1518 parser_parse(parser_ctx);
1519 heap_pool_clear(mark);
1520 hres = parser_ctx->hres;
1521 if(FAILED(hres)) {
1522 WARN("parser failed around %s\n",
1523 debugstr_w(parser_ctx->begin+20 > parser_ctx->ptr ? parser_ctx->begin : parser_ctx->ptr-20));
1524 parser_release(parser_ctx);
1525 return hres;
1526 }
1527
1528 *ret = parser_ctx;
1529 return S_OK;
1530 }