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
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*,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*);
62 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
63 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
64 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
65 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
66 static const_decl_t *new_const_decl(parser_ctx_t*,const WCHAR*,expression_t*);
68 static class_decl_t *new_class_decl(parser_ctx_t*);
69 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
70 static class_decl_t *add_variant_prop(parser_ctx_t*,class_decl_t*,const WCHAR*,unsigned);
72 static statement_t *link_statements(statement_t*,statement_t*);
74 #define STORAGE_IS_PRIVATE 1
75 #define STORAGE_IS_DEFAULT 2
77 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
86 statement_t *statement;
87 expression_t *expression;
88 member_expression_t *member;
89 elseif_decl_t *elseif;
91 function_decl_t *func_decl;
93 class_decl_t *class_decl;
94 const_decl_t *const_decl;
101 %token tEOF tNL tREM tEMPTYBRACKETS
103 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
104 %token tIS tLTEQ tGTEQ tMOD
105 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET tCONST
106 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
107 %token tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tSTEP tEACH tIN
109 %token tOPTION tEXPLICIT
111 %token tNOTHING tEMPTY tNULL
112 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
113 %token tERROR tNEXT tON tRESUME tGOTO
114 %token <string> tIdentifier tString
115 %token <lng> tLong tShort
118 %type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
119 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
120 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
121 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
122 %type <member> MemberExpression
123 %type <expression> Arguments_opt ArgumentList_opt ArgumentList Step_opt
124 %type <bool> OptionExplicit_opt DoType
125 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
126 %type <func_decl> FunctionDecl PropertyDecl
127 %type <elseif> ElseIfs_opt ElseIfs ElseIf
128 %type <class_decl> ClassDeclaration ClassBody
129 %type <uint> Storage Storage_opt
130 %type <dim_decl> DimDeclList
131 %type <const_decl> ConstDecl ConstDeclList
136 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
139 : /* empty */ { $$ = FALSE; }
140 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
144 | SourceElements StatementNl { source_add_statement(ctx, $2); }
145 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
148 : /* empty */ { $$ = NULL; }
149 | StatementsNl { $$ = $1; }
152 : StatementNl { $$ = $1; }
153 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
156 : Statement tNL { $$ = $1; }
160 | ':' Statement { $$ = $2; }
161 | SimpleStatement { $$ = $1; }
162 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
163 | SimpleStatement ':' { $$ = $1; }
166 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
167 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
168 | MemberExpression Arguments_opt '=' Expression
169 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
170 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
171 | IfStatement { $$ = $1; }
172 | tWHILE Expression tNL StatementsNl_opt tWEND
173 { $$ = new_while_statement(ctx, STAT_WHILE, $2, $4); CHECK_ERROR; }
174 | tDO DoType Expression tNL StatementsNl_opt tLOOP
175 { $$ = new_while_statement(ctx, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
177 | tDO tNL StatementsNl_opt tLOOP DoType Expression
178 { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
180 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
181 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0); CHECK_ERROR; }
182 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
183 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
184 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
185 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
186 | tSET MemberExpression Arguments_opt '=' Expression
187 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
188 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
189 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
190 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
191 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
192 | tFOR tIdentifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
193 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
194 | tFOR tEACH tIdentifier tIN Expression tNL StatementsNl_opt tNEXT
195 { $$ = new_foreach_statement(ctx, $3, $5, $7); }
198 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
199 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
201 DimDeclList /* FIXME: Support arrays */
202 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
203 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
206 : ConstDecl { $$ = $1; }
207 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
210 : tIdentifier '=' LiteralExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
213 : tWHILE { $$ = TRUE; }
214 | tUNTIL { $$ = FALSE; }
217 : /* empty */ { $$ = NULL;}
218 | tSTEP Expression { $$ = $2; }
221 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
222 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
223 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
224 | tIF Expression tTHEN Statement tELSE Statement EndIf_opt
225 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
232 : /* empty */ { $$ = NULL; }
233 | ElseIfs { $$ = $1; }
236 : ElseIf { $$ = $1; }
237 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
240 : tELSEIF Expression tTHEN tNL StatementsNl
241 { $$ = new_elseif_decl(ctx, $2, $5); }
244 : /* empty */ { $$ = NULL; }
245 | tELSE tNL StatementsNl { $$ = $3; }
248 : EmptyBrackets_opt { $$ = NULL; }
249 | '(' ArgumentList ')' { $$ = $2; }
252 : EmptyBrackets_opt { $$ = NULL; }
253 | ArgumentList { $$ = $1; }
256 : Expression { $$ = $1; }
257 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
264 : EqvExpression { $$ = $1; }
265 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
268 : XorExpression { $$ = $1; }
269 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
272 : OrExpression { $$ = $1; }
273 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
276 : AndExpression { $$ = $1; }
277 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
280 : NotExpression { $$ = $1; }
281 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
284 : EqualityExpression { $$ = $1; }
285 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
288 : ConcatExpression { $$ = $1; }
289 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
290 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
291 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
292 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
293 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
294 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
295 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
298 : AdditiveExpression { $$ = $1; }
299 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
302 : ModExpression { $$ = $1; }
303 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
304 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
307 : IntdivExpression { $$ = $1; }
308 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
311 : MultiplicativeExpression { $$ = $1; }
312 | IntdivExpression '\\' MultiplicativeExpression
313 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
315 MultiplicativeExpression
316 : ExpExpression { $$ = $1; }
317 | MultiplicativeExpression '*' ExpExpression
318 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
319 | MultiplicativeExpression '/' ExpExpression
320 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
323 : UnaryExpression { $$ = $1; }
324 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
327 : LiteralExpression { $$ = $1; }
328 | CallExpression { $$ = $1; }
329 | tNEW tIdentifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
330 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
333 : PrimaryExpression { $$ = $1; }
334 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
337 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
338 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
339 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
340 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
341 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
342 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
343 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
344 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
345 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
346 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
349 : '(' Expression ')' { $$ = $2; }
350 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
353 : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
356 : /* empty */ { $$ = new_class_decl(ctx); }
357 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
358 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
359 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
362 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
363 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
364 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
365 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
366 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
367 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
370 : Storage_opt tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
371 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
372 | Storage_opt tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
373 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
376 : /* empty*/ { $$ = 0; }
377 | Storage { $$ = $1; }
380 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
381 | tPUBLIC { $$ = 0; }
382 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
385 : EmptyBrackets_opt { $$ = NULL; }
386 | '(' ArgumentDeclList ')' { $$ = $2; }
389 : ArgumentDecl { $$ = $1; }
390 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
393 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
394 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
395 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
399 static int parser_error(const char *str)
404 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
410 ctx->stats_tail->next = stat;
411 ctx->stats_tail = stat;
413 ctx->stats = ctx->stats_tail = stat;
417 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
419 class_decl->next = ctx->class_decls;
420 ctx->class_decls = class_decl;
423 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
425 ctx->parse_complete = TRUE;
426 ctx->option_explicit = option_explicit;
429 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
433 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
442 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
444 bool_expression_t *expr;
446 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
454 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
456 string_expression_t *expr;
458 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
466 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
468 int_expression_t *expr;
470 expr = new_expression(ctx, type, sizeof(*expr));
478 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
480 double_expression_t *expr;
482 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
490 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
492 unary_expression_t *expr;
494 expr = new_expression(ctx, type, sizeof(*expr));
498 expr->subexpr = subexpr;
502 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
504 binary_expression_t *expr;
506 expr = new_expression(ctx, type, sizeof(*expr));
515 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
517 member_expression_t *expr;
519 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
523 expr->obj_expr = obj_expr;
524 expr->identifier = identifier;
529 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
531 string_expression_t *expr;
533 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
537 expr->value = identifier;
541 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
545 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
554 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
556 call_statement_t *stat;
558 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
566 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
568 assign_statement_t *stat;
570 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
574 stat->member_expr = left;
575 stat->value_expr = right;
579 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
581 assign_statement_t *stat;
583 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
587 stat->member_expr = left;
588 stat->value_expr = right;
592 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
596 decl = parser_alloc(ctx, sizeof(*decl));
605 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
607 dim_statement_t *stat;
609 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
613 stat->dim_decls = decls;
617 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
621 decl = parser_alloc(ctx, sizeof(*decl));
631 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
633 while_statement_t *stat;
635 stat = new_statement(ctx, type, sizeof(*stat));
644 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
645 expression_t *to_expr, expression_t *step_expr, statement_t *body)
647 forto_statement_t *stat;
649 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
653 stat->identifier = identifier;
654 stat->from_expr = from_expr;
655 stat->to_expr = to_expr;
656 stat->step_expr = step_expr;
661 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
664 foreach_statement_t *stat;
666 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
670 stat->identifier = identifier;
671 stat->group_expr = group_expr;
676 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
677 statement_t *else_stat)
679 if_statement_t *stat;
681 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
686 stat->if_stat = if_stat;
687 stat->elseifs = elseif_decl;
688 stat->else_stat = else_stat;
692 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
694 onerror_statement_t *stat;
696 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
700 stat->resume_next = resume_next;
704 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
706 arg_decl_t *arg_decl;
708 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
712 arg_decl->name = name;
713 arg_decl->by_ref = by_ref;
714 arg_decl->next = NULL;
718 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
719 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
721 function_decl_t *decl;
723 if(storage_flags & STORAGE_IS_DEFAULT) {
724 if(type == FUNC_PROPGET) {
727 FIXME("Invalid default property\n");
733 decl = parser_alloc(ctx, sizeof(*decl));
739 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
740 decl->args = arg_decl;
743 decl->next_prop_func = NULL;
747 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
749 function_statement_t *stat;
751 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
755 stat->func_decl = decl;
759 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
761 class_decl_t *class_decl;
763 class_decl = parser_alloc(ctx, sizeof(*class_decl));
767 class_decl->funcs = NULL;
768 class_decl->props = NULL;
769 class_decl->next = NULL;
773 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
775 function_decl_t *iter;
777 for(iter = class_decl->funcs; iter; iter = iter->next) {
778 if(!strcmpiW(iter->name, decl->name)) {
779 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
780 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
786 if(iter->type == decl->type) {
787 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
791 if(!iter->next_prop_func)
793 iter = iter->next_prop_func;
796 iter->next_prop_func = decl;
801 decl->next = class_decl->funcs;
802 class_decl->funcs = decl;
806 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
808 class_prop_decl_t *prop;
810 if(storage_flags & STORAGE_IS_DEFAULT) {
811 FIXME("variant prop van't be default value\n");
816 prop = parser_alloc(ctx, sizeof(*prop));
820 prop->name = identifier;
821 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
822 prop->next = class_decl->props;
823 class_decl->props = prop;
827 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
831 decl = parser_alloc(ctx, sizeof(*decl));
836 decl->value_expr = expr;
841 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
843 const_statement_t *stat;
845 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
853 static statement_t *link_statements(statement_t *head, statement_t *tail)
857 for(iter = head; iter->next; iter = iter->next);
863 void *parser_alloc(parser_ctx_t *ctx, size_t size)
867 ret = vbsheap_alloc(&ctx->heap, size);
869 ctx->hres = E_OUTOFMEMORY;
873 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
875 ctx->code = ctx->ptr = code;
876 ctx->end = ctx->code + strlenW(ctx->code);
878 vbsheap_init(&ctx->heap);
880 ctx->parse_complete = FALSE;
883 ctx->last_token = tNL;
885 ctx->stats = ctx->stats_tail = NULL;
886 ctx->class_decls = NULL;
887 ctx->option_explicit = FALSE;
891 if(FAILED(ctx->hres))
893 if(!ctx->parse_complete) {
894 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
901 void parser_release(parser_ctx_t *ctx)
903 vbsheap_free(&ctx->heap);