ws2_32/tests: Avoid races.
[wine.git] / dlls / vbscript / parser.y
blob4b380ef0bc090265e37be34bf723a363f113cd99
1 /*
2 * Copyright 2011 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 "vbscript.h"
22 #include "parse.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 #define YYLEX_PARAM ctx
29 #define YYPARSE_PARAM ctx
31 static int parser_error(const char*);
33 static void parse_complete(parser_ctx_t*,BOOL);
35 static void source_add_statement(parser_ctx_t*,statement_t*);
36 static void source_add_class(parser_ctx_t*,class_decl_t*);
38 static void *new_expression(parser_ctx_t*,expression_type_t,size_t);
39 static expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
40 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
41 static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
42 static expression_t *new_double_expression(parser_ctx_t*,double);
43 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
44 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
45 static expression_t *new_new_expression(parser_ctx_t*,const WCHAR*);
47 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
49 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
50 static statement_t *new_call_statement(parser_ctx_t*,BOOL,member_expression_t*);
51 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
52 static statement_t *new_set_statement(parser_ctx_t*,member_expression_t*,expression_t*);
53 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
54 static statement_t *new_while_statement(parser_ctx_t*,statement_type_t,expression_t*,statement_t*);
55 static statement_t *new_forto_statement(parser_ctx_t*,const WCHAR*,expression_t*,expression_t*,expression_t*,statement_t*);
56 static statement_t *new_foreach_statement(parser_ctx_t*,const WCHAR*,expression_t*,statement_t*);
57 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
58 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
59 static statement_t *new_onerror_statement(parser_ctx_t*,BOOL);
60 static statement_t *new_const_statement(parser_ctx_t*,const_decl_t*);
61 static statement_t *new_select_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
63 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
64 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
65 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
66 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
67 static const_decl_t *new_const_decl(parser_ctx_t*,const WCHAR*,expression_t*);
68 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_t*,case_clausule_t*);
70 static class_decl_t *new_class_decl(parser_ctx_t*);
71 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
72 static class_decl_t *add_variant_prop(parser_ctx_t*,class_decl_t*,const WCHAR*,unsigned);
74 static statement_t *link_statements(statement_t*,statement_t*);
76 static const WCHAR propertyW[] = {'p','r','o','p','e','r','t','y',0};
78 #define STORAGE_IS_PRIVATE 1
79 #define STORAGE_IS_DEFAULT 2
81 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
85 %pure_parser
86 %start Program
88 %union {
89 const WCHAR *string;
90 statement_t *statement;
91 expression_t *expression;
92 member_expression_t *member;
93 elseif_decl_t *elseif;
94 dim_decl_t *dim_decl;
95 function_decl_t *func_decl;
96 arg_decl_t *arg_decl;
97 class_decl_t *class_decl;
98 const_decl_t *const_decl;
99 case_clausule_t *case_clausule;
100 unsigned uint;
101 LONG lng;
102 BOOL bool;
103 double dbl;
106 %token tEOF tNL tREM tEMPTYBRACKETS
107 %token tTRUE tFALSE
108 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
109 %token tIS tLTEQ tGTEQ tMOD
110 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET tCONST
111 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
112 %token tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tSTEP tEACH tIN
113 %token tSELECT tCASE
114 %token tBYREF tBYVAL
115 %token tOPTION tEXPLICIT
116 %token tSTOP
117 %token tNOTHING tEMPTY tNULL
118 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
119 %token tERROR tNEXT tON tRESUME tGOTO
120 %token <string> tIdentifier tString
121 %token <lng> tLong tShort
122 %token <dbl> tDouble
124 %type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
125 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
126 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
127 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
128 %type <expression> ConstExpression NumericLiteralExpression
129 %type <member> MemberExpression
130 %type <expression> Arguments_opt ArgumentList_opt Step_opt ExpressionList
131 %type <bool> OptionExplicit_opt DoType
132 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
133 %type <func_decl> FunctionDecl PropertyDecl
134 %type <elseif> ElseIfs_opt ElseIfs ElseIf
135 %type <class_decl> ClassDeclaration ClassBody
136 %type <uint> Storage Storage_opt
137 %type <dim_decl> DimDeclList
138 %type <const_decl> ConstDecl ConstDeclList
139 %type <string> Identifier
140 %type <case_clausule> CaseClausules
144 Program
145 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
147 OptionExplicit_opt
148 : /* empty */ { $$ = FALSE; }
149 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
151 SourceElements
152 : /* empty */
153 | SourceElements StatementNl { source_add_statement(ctx, $2); }
154 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
156 StatementsNl_opt
157 : /* empty */ { $$ = NULL; }
158 | StatementsNl { $$ = $1; }
160 StatementsNl
161 : StatementNl { $$ = $1; }
162 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
164 StatementNl
165 : Statement tNL { $$ = $1; }
167 Statement
168 : ':' { $$ = NULL; }
169 | ':' Statement { $$ = $2; }
170 | SimpleStatement { $$ = $1; }
171 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
172 | SimpleStatement ':' { $$ = $1; }
174 SimpleStatement
175 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, FALSE, $1); CHECK_ERROR; }
176 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, TRUE, $2); CHECK_ERROR; }
177 | MemberExpression Arguments_opt '=' Expression
178 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
179 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
180 | IfStatement { $$ = $1; }
181 | tWHILE Expression tNL StatementsNl_opt tWEND
182 { $$ = new_while_statement(ctx, STAT_WHILE, $2, $4); CHECK_ERROR; }
183 | tDO DoType Expression tNL StatementsNl_opt tLOOP
184 { $$ = new_while_statement(ctx, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
185 CHECK_ERROR; }
186 | tDO tNL StatementsNl_opt tLOOP DoType Expression
187 { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
188 CHECK_ERROR; }
189 | tDO tNL StatementsNl_opt tLOOP { $$ = new_while_statement(ctx, STAT_DOWHILE, NULL, $3); CHECK_ERROR; }
190 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
191 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0); CHECK_ERROR; }
192 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
193 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
194 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
195 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
196 | tSET MemberExpression Arguments_opt '=' Expression
197 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
198 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
199 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
200 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
201 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
202 | tFOR Identifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
203 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
204 | tFOR tEACH Identifier tIN Expression tNL StatementsNl_opt tNEXT
205 { $$ = new_foreach_statement(ctx, $3, $5, $7); }
206 | tSELECT tCASE Expression tNL CaseClausules tEND tSELECT
207 { $$ = new_select_statement(ctx, $3, $5); }
209 MemberExpression
210 : Identifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
211 | CallExpression '.' Identifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
213 DimDeclList /* FIXME: Support arrays */
214 : Identifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
215 | Identifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
217 ConstDeclList
218 : ConstDecl { $$ = $1; }
219 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
221 ConstDecl
222 : Identifier '=' ConstExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
224 ConstExpression
225 : LiteralExpression { $$ = $1; }
226 | '-' NumericLiteralExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
228 DoType
229 : tWHILE { $$ = TRUE; }
230 | tUNTIL { $$ = FALSE; }
232 Step_opt
233 : /* empty */ { $$ = NULL;}
234 | tSTEP Expression { $$ = $2; }
236 IfStatement
237 : tIF Expression tTHEN tNL StatementsNl_opt ElseIfs_opt Else_opt tEND tIF
238 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
239 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
240 | tIF Expression tTHEN Statement tELSE Statement EndIf_opt
241 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
243 EndIf_opt
244 : /* empty */
245 | tEND tIF
247 ElseIfs_opt
248 : /* empty */ { $$ = NULL; }
249 | ElseIfs { $$ = $1; }
251 ElseIfs
252 : ElseIf { $$ = $1; }
253 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
255 ElseIf
256 : tELSEIF Expression tTHEN tNL StatementsNl_opt
257 { $$ = new_elseif_decl(ctx, $2, $5); }
259 Else_opt
260 : /* empty */ { $$ = NULL; }
261 | tELSE tNL StatementsNl_opt { $$ = $3; }
263 CaseClausules
264 : /* empty */ { $$ = NULL; }
265 | tCASE tELSE tNL StatementsNl { $$ = new_case_clausule(ctx, NULL, $4, NULL); }
266 | tCASE ExpressionList tNL StatementsNl_opt CaseClausules
267 { $$ = new_case_clausule(ctx, $2, $4, $5); }
269 Arguments_opt
270 : EmptyBrackets_opt { $$ = NULL; }
271 | '(' ExpressionList ')' { $$ = $2; }
273 ArgumentList_opt
274 : EmptyBrackets_opt { $$ = NULL; }
275 | ExpressionList { $$ = $1; }
277 EmptyBrackets_opt
278 : /* empty */
279 | tEMPTYBRACKETS
281 ExpressionList
282 : Expression { $$ = $1; }
283 | Expression ',' ExpressionList { $1->next = $3; $$ = $1; }
285 Expression
286 : EqvExpression { $$ = $1; }
287 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
289 EqvExpression
290 : XorExpression { $$ = $1; }
291 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
293 XorExpression
294 : OrExpression { $$ = $1; }
295 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
297 OrExpression
298 : AndExpression { $$ = $1; }
299 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
301 AndExpression
302 : NotExpression { $$ = $1; }
303 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
305 NotExpression
306 : EqualityExpression { $$ = $1; }
307 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
309 EqualityExpression
310 : ConcatExpression { $$ = $1; }
311 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
312 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
313 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
314 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
315 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
316 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
317 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
319 ConcatExpression
320 : AdditiveExpression { $$ = $1; }
321 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
323 AdditiveExpression
324 : ModExpression { $$ = $1; }
325 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
326 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
328 ModExpression
329 : IntdivExpression { $$ = $1; }
330 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
332 IntdivExpression
333 : MultiplicativeExpression { $$ = $1; }
334 | IntdivExpression '\\' MultiplicativeExpression
335 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
337 MultiplicativeExpression
338 : ExpExpression { $$ = $1; }
339 | MultiplicativeExpression '*' ExpExpression
340 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
341 | MultiplicativeExpression '/' ExpExpression
342 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
344 ExpExpression
345 : UnaryExpression { $$ = $1; }
346 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
348 UnaryExpression
349 : LiteralExpression { $$ = $1; }
350 | CallExpression { $$ = $1; }
351 | tNEW Identifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
352 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
354 CallExpression
355 : PrimaryExpression { $$ = $1; }
356 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
358 LiteralExpression
359 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
360 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
361 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
362 | NumericLiteralExpression { $$ = $1; }
363 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
364 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
365 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
367 NumericLiteralExpression
368 : tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
369 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
370 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
371 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
374 PrimaryExpression
375 : '(' Expression ')' { $$ = new_unary_expression(ctx, EXPR_BRACKETS, $2); }
376 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
378 ClassDeclaration
379 : tCLASS Identifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
381 ClassBody
382 : /* empty */ { $$ = new_class_decl(ctx); }
383 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
384 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
385 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
387 PropertyDecl
388 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
389 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
390 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
391 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
392 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
393 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
395 FunctionDecl
396 : Storage_opt tSUB Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
397 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
398 | Storage_opt tFUNCTION Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
399 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
401 Storage_opt
402 : /* empty*/ { $$ = 0; }
403 | Storage { $$ = $1; }
405 Storage
406 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
407 | tPUBLIC { $$ = 0; }
408 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
410 ArgumentsDecl_opt
411 : EmptyBrackets_opt { $$ = NULL; }
412 | '(' ArgumentDeclList ')' { $$ = $2; }
414 ArgumentDeclList
415 : ArgumentDecl { $$ = $1; }
416 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
418 ArgumentDecl
419 : Identifier { $$ = new_argument_decl(ctx, $1, TRUE); }
420 | tBYREF Identifier { $$ = new_argument_decl(ctx, $2, TRUE); }
421 | tBYVAL Identifier { $$ = new_argument_decl(ctx, $2, FALSE); }
423 /* 'property' may be both keyword and identifier, depending on context */
424 Identifier
425 : tIdentifier { $$ = $1; }
426 | tPROPERTY { $$ = propertyW; }
429 static int parser_error(const char *str)
431 return 0;
434 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
436 if(!stat)
437 return;
439 if(ctx->stats) {
440 ctx->stats_tail->next = stat;
441 ctx->stats_tail = stat;
442 }else {
443 ctx->stats = ctx->stats_tail = stat;
447 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
449 class_decl->next = ctx->class_decls;
450 ctx->class_decls = class_decl;
453 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
455 ctx->parse_complete = TRUE;
456 ctx->option_explicit = option_explicit;
459 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
461 expression_t *expr;
463 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
464 if(expr) {
465 expr->type = type;
466 expr->next = NULL;
469 return expr;
472 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
474 bool_expression_t *expr;
476 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
477 if(!expr)
478 return NULL;
480 expr->value = value;
481 return &expr->expr;
484 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
486 string_expression_t *expr;
488 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
489 if(!expr)
490 return NULL;
492 expr->value = value;
493 return &expr->expr;
496 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
498 int_expression_t *expr;
500 expr = new_expression(ctx, type, sizeof(*expr));
501 if(!expr)
502 return NULL;
504 expr->value = value;
505 return &expr->expr;
508 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
510 double_expression_t *expr;
512 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
513 if(!expr)
514 return NULL;
516 expr->value = value;
517 return &expr->expr;
520 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
522 unary_expression_t *expr;
524 expr = new_expression(ctx, type, sizeof(*expr));
525 if(!expr)
526 return NULL;
528 expr->subexpr = subexpr;
529 return &expr->expr;
532 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
534 binary_expression_t *expr;
536 expr = new_expression(ctx, type, sizeof(*expr));
537 if(!expr)
538 return NULL;
540 expr->left = left;
541 expr->right = right;
542 return &expr->expr;
545 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
547 member_expression_t *expr;
549 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
550 if(!expr)
551 return NULL;
553 expr->obj_expr = obj_expr;
554 expr->identifier = identifier;
555 expr->args = NULL;
556 return expr;
559 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
561 string_expression_t *expr;
563 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
564 if(!expr)
565 return NULL;
567 expr->value = identifier;
568 return &expr->expr;
571 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
573 statement_t *stat;
575 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
576 if(stat) {
577 stat->type = type;
578 stat->next = NULL;
581 return stat;
584 static statement_t *new_call_statement(parser_ctx_t *ctx, BOOL is_strict, member_expression_t *expr)
586 call_statement_t *stat;
588 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
589 if(!stat)
590 return NULL;
592 stat->expr = expr;
593 stat->is_strict = is_strict;
594 return &stat->stat;
597 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
599 assign_statement_t *stat;
601 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
602 if(!stat)
603 return NULL;
605 stat->member_expr = left;
606 stat->value_expr = right;
607 return &stat->stat;
610 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
612 assign_statement_t *stat;
614 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
615 if(!stat)
616 return NULL;
618 stat->member_expr = left;
619 stat->value_expr = right;
620 return &stat->stat;
623 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
625 dim_decl_t *decl;
627 decl = parser_alloc(ctx, sizeof(*decl));
628 if(!decl)
629 return NULL;
631 decl->name = name;
632 decl->next = next;
633 return decl;
636 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
638 dim_statement_t *stat;
640 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
641 if(!stat)
642 return NULL;
644 stat->dim_decls = decls;
645 return &stat->stat;
648 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
650 elseif_decl_t *decl;
652 decl = parser_alloc(ctx, sizeof(*decl));
653 if(!decl)
654 return NULL;
656 decl->expr = expr;
657 decl->stat = stat;
658 decl->next = NULL;
659 return decl;
662 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
664 while_statement_t *stat;
666 stat = new_statement(ctx, type, sizeof(*stat));
667 if(!stat)
668 return NULL;
670 stat->expr = expr;
671 stat->body = body;
672 return &stat->stat;
675 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
676 expression_t *to_expr, expression_t *step_expr, statement_t *body)
678 forto_statement_t *stat;
680 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
681 if(!stat)
682 return NULL;
684 stat->identifier = identifier;
685 stat->from_expr = from_expr;
686 stat->to_expr = to_expr;
687 stat->step_expr = step_expr;
688 stat->body = body;
689 return &stat->stat;
692 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
693 statement_t *body)
695 foreach_statement_t *stat;
697 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
698 if(!stat)
699 return NULL;
701 stat->identifier = identifier;
702 stat->group_expr = group_expr;
703 stat->body = body;
704 return &stat->stat;
707 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
708 statement_t *else_stat)
710 if_statement_t *stat;
712 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
713 if(!stat)
714 return NULL;
716 stat->expr = expr;
717 stat->if_stat = if_stat;
718 stat->elseifs = elseif_decl;
719 stat->else_stat = else_stat;
720 return &stat->stat;
723 static statement_t *new_select_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_clausules)
725 select_statement_t *stat;
727 stat = new_statement(ctx, STAT_SELECT, sizeof(*stat));
728 if(!stat)
729 return NULL;
731 stat->expr = expr;
732 stat->case_clausules = case_clausules;
733 return &stat->stat;
736 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_t *stat, case_clausule_t *next)
738 case_clausule_t *ret;
740 ret = parser_alloc(ctx, sizeof(*ret));
741 if(!ret)
742 return NULL;
744 ret->expr = expr;
745 ret->stat = stat;
746 ret->next = next;
747 return ret;
750 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
752 onerror_statement_t *stat;
754 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
755 if(!stat)
756 return NULL;
758 stat->resume_next = resume_next;
759 return &stat->stat;
762 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
764 arg_decl_t *arg_decl;
766 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
767 if(!arg_decl)
768 return NULL;
770 arg_decl->name = name;
771 arg_decl->by_ref = by_ref;
772 arg_decl->next = NULL;
773 return arg_decl;
776 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
777 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
779 function_decl_t *decl;
781 if(storage_flags & STORAGE_IS_DEFAULT) {
782 if(type == FUNC_PROPGET) {
783 type = FUNC_DEFGET;
784 }else {
785 FIXME("Invalid default property\n");
786 ctx->hres = E_FAIL;
787 return NULL;
791 decl = parser_alloc(ctx, sizeof(*decl));
792 if(!decl)
793 return NULL;
795 decl->name = name;
796 decl->type = type;
797 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
798 decl->args = arg_decl;
799 decl->body = body;
800 decl->next = NULL;
801 decl->next_prop_func = NULL;
802 return decl;
805 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
807 function_statement_t *stat;
809 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
810 if(!stat)
811 return NULL;
813 stat->func_decl = decl;
814 return &stat->stat;
817 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
819 class_decl_t *class_decl;
821 class_decl = parser_alloc(ctx, sizeof(*class_decl));
822 if(!class_decl)
823 return NULL;
825 class_decl->funcs = NULL;
826 class_decl->props = NULL;
827 class_decl->next = NULL;
828 return class_decl;
831 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
833 function_decl_t *iter;
835 for(iter = class_decl->funcs; iter; iter = iter->next) {
836 if(!strcmpiW(iter->name, decl->name)) {
837 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
838 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
839 ctx->hres = E_FAIL;
840 return NULL;
843 while(1) {
844 if(iter->type == decl->type) {
845 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
846 ctx->hres = E_FAIL;
847 return NULL;
849 if(!iter->next_prop_func)
850 break;
851 iter = iter->next_prop_func;
854 iter->next_prop_func = decl;
855 return class_decl;
859 decl->next = class_decl->funcs;
860 class_decl->funcs = decl;
861 return class_decl;
864 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
866 class_prop_decl_t *prop;
868 if(storage_flags & STORAGE_IS_DEFAULT) {
869 FIXME("variant prop van't be default value\n");
870 ctx->hres = E_FAIL;
871 return NULL;
874 prop = parser_alloc(ctx, sizeof(*prop));
875 if(!prop)
876 return NULL;
878 prop->name = identifier;
879 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
880 prop->next = class_decl->props;
881 class_decl->props = prop;
882 return class_decl;
885 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
887 const_decl_t *decl;
889 decl = parser_alloc(ctx, sizeof(*decl));
890 if(!decl)
891 return NULL;
893 decl->name = name;
894 decl->value_expr = expr;
895 decl->next = NULL;
896 return decl;
899 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
901 const_statement_t *stat;
903 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
904 if(!stat)
905 return NULL;
907 stat->decls = decls;
908 return &stat->stat;
911 static statement_t *link_statements(statement_t *head, statement_t *tail)
913 statement_t *iter;
915 for(iter = head; iter->next; iter = iter->next);
916 iter->next = tail;
918 return head;
921 void *parser_alloc(parser_ctx_t *ctx, size_t size)
923 void *ret;
925 ret = heap_pool_alloc(&ctx->heap, size);
926 if(!ret)
927 ctx->hres = E_OUTOFMEMORY;
928 return ret;
931 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter)
933 const WCHAR html_delimiterW[] = {'<','/','s','c','r','i','p','t','>',0};
935 ctx->code = ctx->ptr = code;
936 ctx->end = ctx->code + strlenW(ctx->code);
938 heap_pool_init(&ctx->heap);
940 ctx->parse_complete = FALSE;
941 ctx->hres = S_OK;
943 ctx->last_token = tNL;
944 ctx->last_nl = 0;
945 ctx->stats = ctx->stats_tail = NULL;
946 ctx->class_decls = NULL;
947 ctx->option_explicit = FALSE;
948 ctx->is_html = delimiter && !strcmpiW(delimiter, html_delimiterW);
950 parser_parse(ctx);
952 if(FAILED(ctx->hres))
953 return ctx->hres;
954 if(!ctx->parse_complete) {
955 FIXME("parser failed around %s\n", debugstr_w(ctx->code+20 > ctx->ptr ? ctx->code : ctx->ptr-20));
956 return E_FAIL;
959 return S_OK;
962 void parser_release(parser_ctx_t *ctx)
964 heap_pool_free(&ctx->heap);