vbscript: Added class properties compiler implementation.
[wine] / dlls / vbscript / parser.y
1 /*
2  * Copyright 2011 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 "vbscript.h"
22 #include "parse.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
27
28
29 #define YYLEX_PARAM ctx
30 #define YYPARSE_PARAM ctx
31
32 static int parser_error(const char*);
33
34 static void parse_complete(parser_ctx_t*,BOOL);
35
36 static void source_add_statement(parser_ctx_t*,statement_t*);
37 static void source_add_class(parser_ctx_t*,class_decl_t*);
38
39 static void *new_expression(parser_ctx_t*,expression_type_t,size_t);
40 static expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
41 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
42 static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
43 static expression_t *new_double_expression(parser_ctx_t*,double);
44 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
45 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
46 static expression_t *new_new_expression(parser_ctx_t*,const WCHAR*);
47
48 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
49
50 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
51 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
52 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
53 static statement_t *new_set_statement(parser_ctx_t*,member_expression_t*,expression_t*);
54 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
55 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
56 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
57
58 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
59 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
60 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
61 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
62
63 static class_decl_t *new_class_decl(parser_ctx_t*);
64 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
65 static class_decl_t *add_variant_prop(parser_ctx_t*,class_decl_t*,const WCHAR*,unsigned);
66
67 #define STORAGE_IS_PRIVATE    1
68 #define STORAGE_IS_DEFAULT    2
69
70 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
71
72 %}
73
74 %pure_parser
75 %start Program
76
77 %union {
78     const WCHAR *string;
79     statement_t *statement;
80     expression_t *expression;
81     member_expression_t *member;
82     elseif_decl_t *elseif;
83     dim_decl_t *dim_decl;
84     function_decl_t *func_decl;
85     arg_decl_t *arg_decl;
86     class_decl_t *class_decl;
87     unsigned uint;
88     LONG lng;
89     BOOL bool;
90     double dbl;
91 }
92
93 %token tEOF tNL tREM tEMPTYBRACKETS
94 %token tTRUE tFALSE
95 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
96 %token tIS tLTEQ tGTEQ tMOD
97 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
98 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
99 %token tWHILE tWEND tDO tLOOP tUNTIL
100 %token tBYREF tBYVAL
101 %token tOPTION tEXPLICIT
102 %token tSTOP
103 %token tNOTHING tEMPTY tNULL
104 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
105 %token tERROR tNEXT tON tRESUME tGOTO
106 %token <string> tIdentifier tString
107 %token <lng> tLong tShort
108 %token <dbl> tDouble
109
110 %type <statement> Statement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
111 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
112 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
113 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
114 %type <member> MemberExpression
115 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
116 %type <bool> OptionExplicit_opt
117 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
118 %type <func_decl> FunctionDecl
119 %type <elseif> ElseIfs_opt ElseIfs ElseIf
120 %type <class_decl> ClassDeclaration ClassBody
121 %type <uint> Storage Storage_opt
122 %type <dim_decl> DimDeclList
123
124 %%
125
126 Program
127     : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }
128
129 OptionExplicit_opt
130     : /* empty */                { $$ = FALSE; }
131     | tOPTION tEXPLICIT tNL      { $$ = TRUE; }
132
133 SourceElements
134     : /* empty */
135     | SourceElements StatementNl            { source_add_statement(ctx, $2); }
136     | SourceElements ClassDeclaration       { source_add_class(ctx, $2); }
137
138 StatementsNl_opt
139     : /* empty */                           { $$ = NULL; }
140     | StatementsNl                          { $$ = $1; }
141
142 StatementsNl
143     : StatementNl                           { $$ = $1; }
144     | StatementNl StatementsNl              { $1->next = $2; $$ = $1; }
145
146 StatementNl
147     : Statement tNL                 { $$ = $1; }
148
149 Statement
150     : MemberExpression ArgumentList_opt     { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
151     | tCALL MemberExpression Arguments_opt  { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
152     | MemberExpression Arguments_opt '=' Expression
153                                             { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
154     | tDIM DimDeclList                      { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
155     | IfStatement                           { $$ = $1; }
156     | FunctionDecl                          { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
157     | tEXIT tFUNCTION                       { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
158     | tEXIT tSUB                            { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
159     | tSET MemberExpression Arguments_opt '=' Expression
160                                             { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
161     | tSTOP                                 { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
162
163 MemberExpression
164     : tIdentifier                           { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
165     | CallExpression '.' tIdentifier        { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
166
167 DimDeclList /* FIXME: Support arrays */
168     : tIdentifier                           { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
169     | tIdentifier ',' DimDeclList           { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
170
171 IfStatement
172     : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
173                                             { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
174     /* FIXME: short if statement */
175
176 ElseIfs_opt
177     : /* empty */                           { $$ = NULL; }
178     | ElseIfs                               { $$ = $1; }
179
180 ElseIfs
181     : ElseIf                                { $$ = $1; }
182     | ElseIf ElseIfs                        { $1->next = $2; $$ = $1; }
183
184 ElseIf
185     : tELSEIF Expression tTHEN tNL StatementsNl
186                                             { $$ = new_elseif_decl(ctx, $2, $5); }
187
188 Else_opt
189     : /* empty */                           { $$ = NULL; }
190     | tELSE tNL StatementsNl                { $$ = $3; }
191
192 Arguments_opt
193     : EmptyBrackets_opt             { $$ = NULL; }
194     | '(' ArgumentList ')'          { $$ = $2; }
195
196 ArgumentList_opt
197     : EmptyBrackets_opt             { $$ = NULL; }
198     | ArgumentList                  { $$ = $1; }
199
200 ArgumentList
201     : Expression                    { $$ = $1; }
202     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
203
204 EmptyBrackets_opt
205     : /* empty */
206     | tEMPTYBRACKETS
207
208 Expression
209     : EqvExpression                             { $$ = $1; }
210     | Expression tIMP EqvExpression             { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
211
212 EqvExpression
213     : XorExpression                             { $$ = $1; }
214     | EqvExpression tEQV XorExpression          { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
215
216 XorExpression
217     : OrExpression                              { $$ = $1; }
218     | XorExpression tXOR OrExpression           { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
219
220 OrExpression
221     : AndExpression                             { $$ = $1; }
222     | OrExpression tOR AndExpression            { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
223
224 AndExpression
225     : NotExpression                             { $$ = $1; }
226     | AndExpression tAND NotExpression          { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
227
228 NotExpression
229     : EqualityExpression            { $$ = $1; }
230     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
231
232 EqualityExpression
233     : ConcatExpression                          { $$ = $1; }
234     | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
235     | EqualityExpression tNEQ ConcatExpression  { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
236
237 ConcatExpression
238     : AdditiveExpression                        { $$ = $1; }
239     | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
240
241 AdditiveExpression
242     : ModExpression                             { $$ = $1; }
243     | AdditiveExpression '+' ModExpression      { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
244     | AdditiveExpression '-' ModExpression      { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
245
246 ModExpression
247     : IntdivExpression                          { $$ = $1; }
248     | ModExpression tMOD IntdivExpression       { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
249
250 IntdivExpression
251     : MultiplicativeExpression                  { $$ = $1; }
252     | IntdivExpression '\\' MultiplicativeExpression
253                                                 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
254
255 MultiplicativeExpression
256     : ExpExpression                             { $$ = $1; }
257     | MultiplicativeExpression '*' ExpExpression
258                                                 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
259     | MultiplicativeExpression '/' ExpExpression
260                                                 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
261
262 ExpExpression
263     : UnaryExpression                           { $$ = $1; }
264     | ExpExpression '^' UnaryExpression         { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
265
266 UnaryExpression
267     : LiteralExpression             { $$ = $1; }
268     | CallExpression                { $$ = $1; }
269     | tNEW tIdentifier              { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
270     | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
271
272 CallExpression
273     : PrimaryExpression                 { $$ = $1; }
274     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
275
276 LiteralExpression
277     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
278     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
279     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
280     | tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
281     | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
282     | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
283     | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
284     | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
285     | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
286     | tNOTHING                      { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
287
288 PrimaryExpression
289     : '(' Expression ')'            { $$ = $2; }
290
291 ClassDeclaration
292     : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL      { $4->name = $2; $$ = $4; }
293
294 ClassBody
295     : /* empty */                               { $$ = new_class_decl(ctx); }
296     | FunctionDecl tNL ClassBody                { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
297     | Storage tIdentifier tNL ClassBody         { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
298
299 FunctionDecl
300     : Storage_opt tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
301                                     { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
302     | Storage_opt tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
303                                     { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
304
305 Storage_opt
306     : /* empty*/                    { $$ = 0; }
307     | Storage                       { $$ = $1; }
308
309 Storage
310     : tPUBLIC tDEFAULT              { $$ = STORAGE_IS_DEFAULT; }
311     | tPUBLIC                       { $$ = 0; }
312     | tPRIVATE                      { $$ = STORAGE_IS_PRIVATE; }
313
314 ArgumentsDecl_opt
315     : EmptyBrackets_opt                         { $$ = NULL; }
316     | '(' ArgumentDeclList ')'                  { $$ = $2; }
317
318 ArgumentDeclList
319     : ArgumentDecl                              { $$ = $1; }
320     | ArgumentDecl ',' ArgumentDeclList         { $1->next = $3; $$ = $1; }
321
322 ArgumentDecl
323     : tIdentifier                               { $$ = new_argument_decl(ctx, $1, TRUE); }
324     | tBYREF tIdentifier                        { $$ = new_argument_decl(ctx, $2, TRUE); }
325     | tBYVAL tIdentifier                        { $$ = new_argument_decl(ctx, $2, FALSE); }
326
327 %%
328
329 static int parser_error(const char *str)
330 {
331     return 0;
332 }
333
334 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
335 {
336     if(ctx->stats) {
337         ctx->stats_tail->next = stat;
338         ctx->stats_tail = stat;
339     }else {
340         ctx->stats = ctx->stats_tail = stat;
341     }
342 }
343
344 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
345 {
346     class_decl->next = ctx->class_decls;
347     ctx->class_decls = class_decl;
348 }
349
350 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
351 {
352     ctx->parse_complete = TRUE;
353     ctx->option_explicit = option_explicit;
354 }
355
356 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
357 {
358     expression_t *expr;
359
360     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
361     if(expr) {
362         expr->type = type;
363         expr->next = NULL;
364     }
365
366     return expr;
367 }
368
369 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
370 {
371     bool_expression_t *expr;
372
373     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
374     if(!expr)
375         return NULL;
376
377     expr->value = value;
378     return &expr->expr;
379 }
380
381 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
382 {
383     string_expression_t *expr;
384
385     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
386     if(!expr)
387         return NULL;
388
389     expr->value = value;
390     return &expr->expr;
391 }
392
393 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
394 {
395     int_expression_t *expr;
396
397     expr = new_expression(ctx, type, sizeof(*expr));
398     if(!expr)
399         return NULL;
400
401     expr->value = value;
402     return &expr->expr;
403 }
404
405 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
406 {
407     double_expression_t *expr;
408
409     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
410     if(!expr)
411         return NULL;
412
413     expr->value = value;
414     return &expr->expr;
415 }
416
417 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
418 {
419     unary_expression_t *expr;
420
421     expr = new_expression(ctx, type, sizeof(*expr));
422     if(!expr)
423         return NULL;
424
425     expr->subexpr = subexpr;
426     return &expr->expr;
427 }
428
429 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
430 {
431     binary_expression_t *expr;
432
433     expr = new_expression(ctx, type, sizeof(*expr));
434     if(!expr)
435         return NULL;
436
437     expr->left = left;
438     expr->right = right;
439     return &expr->expr;
440 }
441
442 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
443 {
444     member_expression_t *expr;
445
446     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
447     if(!expr)
448         return NULL;
449
450     expr->obj_expr = obj_expr;
451     expr->identifier = identifier;
452     expr->args = NULL;
453     return expr;
454 }
455
456 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
457 {
458     string_expression_t *expr;
459
460     expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
461     if(!expr)
462         return NULL;
463
464     expr->value = identifier;
465     return &expr->expr;
466 }
467
468 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
469 {
470     statement_t *stat;
471
472     stat = parser_alloc(ctx, size ? size : sizeof(*stat));
473     if(stat) {
474         stat->type = type;
475         stat->next = NULL;
476     }
477
478     return stat;
479 }
480
481 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
482 {
483     call_statement_t *stat;
484
485     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
486     if(!stat)
487         return NULL;
488
489     stat->expr = expr;
490     return &stat->stat;
491 }
492
493 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
494 {
495     assign_statement_t *stat;
496
497     stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
498     if(!stat)
499         return NULL;
500
501     stat->member_expr = left;
502     stat->value_expr = right;
503     return &stat->stat;
504 }
505
506 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
507 {
508     assign_statement_t *stat;
509
510     stat = new_statement(ctx, STAT_SET, sizeof(*stat));
511     if(!stat)
512         return NULL;
513
514     stat->member_expr = left;
515     stat->value_expr = right;
516     return &stat->stat;
517 }
518
519 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
520 {
521     dim_decl_t *decl;
522
523     decl = parser_alloc(ctx, sizeof(*decl));
524     if(!decl)
525         return NULL;
526
527     decl->name = name;
528     decl->next = next;
529     return decl;
530 }
531
532 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
533 {
534     dim_statement_t *stat;
535
536     stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
537     if(!stat)
538         return NULL;
539
540     stat->dim_decls = decls;
541     return &stat->stat;
542 }
543
544 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
545 {
546     elseif_decl_t *decl;
547
548     decl = parser_alloc(ctx, sizeof(*decl));
549     if(!decl)
550         return NULL;
551
552     decl->expr = expr;
553     decl->stat = stat;
554     decl->next = NULL;
555     return decl;
556 }
557
558 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
559         statement_t *else_stat)
560 {
561     if_statement_t *stat;
562
563     stat = new_statement(ctx, STAT_IF, sizeof(*stat));
564     if(!stat)
565         return NULL;
566
567     stat->expr = expr;
568     stat->if_stat = if_stat;
569     stat->elseifs = elseif_decl;
570     stat->else_stat = else_stat;
571     return &stat->stat;
572 }
573
574 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
575 {
576     arg_decl_t *arg_decl;
577
578     arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
579     if(!arg_decl)
580         return NULL;
581
582     arg_decl->name = name;
583     arg_decl->by_ref = by_ref;
584     arg_decl->next = NULL;
585     return arg_decl;
586 }
587
588 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
589         unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
590 {
591     function_decl_t *decl;
592
593     if(storage_flags & STORAGE_IS_DEFAULT) {
594         FIXME("Function declared as default property\n");
595         ctx->hres = E_FAIL;
596         return NULL;
597     }
598
599     decl = parser_alloc(ctx, sizeof(*decl));
600     if(!decl)
601         return NULL;
602
603     decl->name = name;
604     decl->type = type;
605     decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
606     decl->args = arg_decl;
607     decl->body = body;
608     decl->next = NULL;
609     return decl;
610 }
611
612 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
613 {
614     function_statement_t *stat;
615
616     stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
617     if(!stat)
618         return NULL;
619
620     stat->func_decl = decl;
621     return &stat->stat;
622 }
623
624 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
625 {
626     class_decl_t *class_decl;
627
628     class_decl = parser_alloc(ctx, sizeof(*class_decl));
629     if(!class_decl)
630         return NULL;
631
632     class_decl->funcs = NULL;
633     class_decl->props = NULL;
634     class_decl->next = NULL;
635     return class_decl;
636 }
637
638 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
639 {
640     function_decl_t *iter;
641
642     for(iter = class_decl->funcs; iter; iter = iter->next) {
643         if(!strcmpiW(iter->name, decl->name)) {
644             if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
645                 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
646                 ctx->hres = E_FAIL;
647                 return NULL;
648             }
649         }
650     }
651
652     decl->next = class_decl->funcs;
653     class_decl->funcs = decl;
654     return class_decl;
655 }
656
657 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
658 {
659     class_prop_decl_t *prop;
660
661     if(storage_flags & STORAGE_IS_DEFAULT) {
662         FIXME("variant prop van't be default value\n");
663         ctx->hres = E_FAIL;
664         return NULL;
665     }
666
667     prop = parser_alloc(ctx, sizeof(*prop));
668     if(!prop)
669         return NULL;
670
671     prop->name = identifier;
672     prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
673     prop->next = class_decl->props;
674     class_decl->props = prop;
675     return class_decl;
676 }
677
678 void *parser_alloc(parser_ctx_t *ctx, size_t size)
679 {
680     void *ret;
681
682     ret = vbsheap_alloc(&ctx->heap, size);
683     if(!ret)
684         ctx->hres = E_OUTOFMEMORY;
685     return ret;
686 }
687
688 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
689 {
690     ctx->code = ctx->ptr = code;
691     ctx->end = ctx->code + strlenW(ctx->code);
692
693     vbsheap_init(&ctx->heap);
694
695     ctx->parse_complete = FALSE;
696     ctx->hres = S_OK;
697
698     ctx->last_token = tNL;
699     ctx->last_nl = 0;
700     ctx->stats = ctx->stats_tail = NULL;
701     ctx->class_decls = NULL;
702     ctx->option_explicit = FALSE;
703
704     parser_parse(ctx);
705
706     if(FAILED(ctx->hres))
707         return ctx->hres;
708     if(!ctx->parse_complete) {
709         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
710         return E_FAIL;
711     }
712
713     return S_OK;
714 }
715
716 void parser_release(parser_ctx_t *ctx)
717 {
718     vbsheap_free(&ctx->heap);
719 }