vbscript: Use default object value for objets in stack_pop_val.
[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 PropertyDecl
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 tPROPERTY                       { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
159     | tEXIT tSUB                            { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
160     | tSET MemberExpression Arguments_opt '=' Expression
161                                             { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
162     | tSTOP                                 { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
163
164 MemberExpression
165     : tIdentifier                           { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
166     | CallExpression '.' tIdentifier        { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
167
168 DimDeclList /* FIXME: Support arrays */
169     : tIdentifier                           { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
170     | tIdentifier ',' DimDeclList           { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
171
172 IfStatement
173     : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
174                                             { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
175     /* FIXME: short if statement */
176
177 ElseIfs_opt
178     : /* empty */                           { $$ = NULL; }
179     | ElseIfs                               { $$ = $1; }
180
181 ElseIfs
182     : ElseIf                                { $$ = $1; }
183     | ElseIf ElseIfs                        { $1->next = $2; $$ = $1; }
184
185 ElseIf
186     : tELSEIF Expression tTHEN tNL StatementsNl
187                                             { $$ = new_elseif_decl(ctx, $2, $5); }
188
189 Else_opt
190     : /* empty */                           { $$ = NULL; }
191     | tELSE tNL StatementsNl                { $$ = $3; }
192
193 Arguments_opt
194     : EmptyBrackets_opt             { $$ = NULL; }
195     | '(' ArgumentList ')'          { $$ = $2; }
196
197 ArgumentList_opt
198     : EmptyBrackets_opt             { $$ = NULL; }
199     | ArgumentList                  { $$ = $1; }
200
201 ArgumentList
202     : Expression                    { $$ = $1; }
203     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
204
205 EmptyBrackets_opt
206     : /* empty */
207     | tEMPTYBRACKETS
208
209 Expression
210     : EqvExpression                             { $$ = $1; }
211     | Expression tIMP EqvExpression             { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
212
213 EqvExpression
214     : XorExpression                             { $$ = $1; }
215     | EqvExpression tEQV XorExpression          { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
216
217 XorExpression
218     : OrExpression                              { $$ = $1; }
219     | XorExpression tXOR OrExpression           { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
220
221 OrExpression
222     : AndExpression                             { $$ = $1; }
223     | OrExpression tOR AndExpression            { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
224
225 AndExpression
226     : NotExpression                             { $$ = $1; }
227     | AndExpression tAND NotExpression          { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
228
229 NotExpression
230     : EqualityExpression            { $$ = $1; }
231     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
232
233 EqualityExpression
234     : ConcatExpression                          { $$ = $1; }
235     | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
236     | EqualityExpression tNEQ ConcatExpression  { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
237
238 ConcatExpression
239     : AdditiveExpression                        { $$ = $1; }
240     | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
241
242 AdditiveExpression
243     : ModExpression                             { $$ = $1; }
244     | AdditiveExpression '+' ModExpression      { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
245     | AdditiveExpression '-' ModExpression      { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
246
247 ModExpression
248     : IntdivExpression                          { $$ = $1; }
249     | ModExpression tMOD IntdivExpression       { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
250
251 IntdivExpression
252     : MultiplicativeExpression                  { $$ = $1; }
253     | IntdivExpression '\\' MultiplicativeExpression
254                                                 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
255
256 MultiplicativeExpression
257     : ExpExpression                             { $$ = $1; }
258     | MultiplicativeExpression '*' ExpExpression
259                                                 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
260     | MultiplicativeExpression '/' ExpExpression
261                                                 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
262
263 ExpExpression
264     : UnaryExpression                           { $$ = $1; }
265     | ExpExpression '^' UnaryExpression         { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
266
267 UnaryExpression
268     : LiteralExpression             { $$ = $1; }
269     | CallExpression                { $$ = $1; }
270     | tNEW tIdentifier              { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
271     | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
272
273 CallExpression
274     : PrimaryExpression                 { $$ = $1; }
275     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
276
277 LiteralExpression
278     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
279     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
280     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
281     | tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
282     | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
283     | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
284     | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
285     | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
286     | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
287     | tNOTHING                      { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
288
289 PrimaryExpression
290     : '(' Expression ')'            { $$ = $2; }
291
292 ClassDeclaration
293     : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL      { $4->name = $2; $$ = $4; }
294
295 ClassBody
296     : /* empty */                               { $$ = new_class_decl(ctx); }
297     | FunctionDecl tNL ClassBody                { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
298     | Storage tIdentifier tNL ClassBody         { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
299     | PropertyDecl tNL ClassBody                { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
300
301 PropertyDecl
302     : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
303                                     { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
304     | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
305                                     { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
306     | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
307                                     { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
308
309 FunctionDecl
310     : Storage_opt tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
311                                     { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
312     | Storage_opt tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
313                                     { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
314
315 Storage_opt
316     : /* empty*/                    { $$ = 0; }
317     | Storage                       { $$ = $1; }
318
319 Storage
320     : tPUBLIC tDEFAULT              { $$ = STORAGE_IS_DEFAULT; }
321     | tPUBLIC                       { $$ = 0; }
322     | tPRIVATE                      { $$ = STORAGE_IS_PRIVATE; }
323
324 ArgumentsDecl_opt
325     : EmptyBrackets_opt                         { $$ = NULL; }
326     | '(' ArgumentDeclList ')'                  { $$ = $2; }
327
328 ArgumentDeclList
329     : ArgumentDecl                              { $$ = $1; }
330     | ArgumentDecl ',' ArgumentDeclList         { $1->next = $3; $$ = $1; }
331
332 ArgumentDecl
333     : tIdentifier                               { $$ = new_argument_decl(ctx, $1, TRUE); }
334     | tBYREF tIdentifier                        { $$ = new_argument_decl(ctx, $2, TRUE); }
335     | tBYVAL tIdentifier                        { $$ = new_argument_decl(ctx, $2, FALSE); }
336
337 %%
338
339 static int parser_error(const char *str)
340 {
341     return 0;
342 }
343
344 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
345 {
346     if(ctx->stats) {
347         ctx->stats_tail->next = stat;
348         ctx->stats_tail = stat;
349     }else {
350         ctx->stats = ctx->stats_tail = stat;
351     }
352 }
353
354 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
355 {
356     class_decl->next = ctx->class_decls;
357     ctx->class_decls = class_decl;
358 }
359
360 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
361 {
362     ctx->parse_complete = TRUE;
363     ctx->option_explicit = option_explicit;
364 }
365
366 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
367 {
368     expression_t *expr;
369
370     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
371     if(expr) {
372         expr->type = type;
373         expr->next = NULL;
374     }
375
376     return expr;
377 }
378
379 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
380 {
381     bool_expression_t *expr;
382
383     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
384     if(!expr)
385         return NULL;
386
387     expr->value = value;
388     return &expr->expr;
389 }
390
391 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
392 {
393     string_expression_t *expr;
394
395     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
396     if(!expr)
397         return NULL;
398
399     expr->value = value;
400     return &expr->expr;
401 }
402
403 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
404 {
405     int_expression_t *expr;
406
407     expr = new_expression(ctx, type, sizeof(*expr));
408     if(!expr)
409         return NULL;
410
411     expr->value = value;
412     return &expr->expr;
413 }
414
415 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
416 {
417     double_expression_t *expr;
418
419     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
420     if(!expr)
421         return NULL;
422
423     expr->value = value;
424     return &expr->expr;
425 }
426
427 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
428 {
429     unary_expression_t *expr;
430
431     expr = new_expression(ctx, type, sizeof(*expr));
432     if(!expr)
433         return NULL;
434
435     expr->subexpr = subexpr;
436     return &expr->expr;
437 }
438
439 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
440 {
441     binary_expression_t *expr;
442
443     expr = new_expression(ctx, type, sizeof(*expr));
444     if(!expr)
445         return NULL;
446
447     expr->left = left;
448     expr->right = right;
449     return &expr->expr;
450 }
451
452 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
453 {
454     member_expression_t *expr;
455
456     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
457     if(!expr)
458         return NULL;
459
460     expr->obj_expr = obj_expr;
461     expr->identifier = identifier;
462     expr->args = NULL;
463     return expr;
464 }
465
466 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
467 {
468     string_expression_t *expr;
469
470     expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
471     if(!expr)
472         return NULL;
473
474     expr->value = identifier;
475     return &expr->expr;
476 }
477
478 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
479 {
480     statement_t *stat;
481
482     stat = parser_alloc(ctx, size ? size : sizeof(*stat));
483     if(stat) {
484         stat->type = type;
485         stat->next = NULL;
486     }
487
488     return stat;
489 }
490
491 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
492 {
493     call_statement_t *stat;
494
495     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
496     if(!stat)
497         return NULL;
498
499     stat->expr = expr;
500     return &stat->stat;
501 }
502
503 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
504 {
505     assign_statement_t *stat;
506
507     stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
508     if(!stat)
509         return NULL;
510
511     stat->member_expr = left;
512     stat->value_expr = right;
513     return &stat->stat;
514 }
515
516 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
517 {
518     assign_statement_t *stat;
519
520     stat = new_statement(ctx, STAT_SET, sizeof(*stat));
521     if(!stat)
522         return NULL;
523
524     stat->member_expr = left;
525     stat->value_expr = right;
526     return &stat->stat;
527 }
528
529 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
530 {
531     dim_decl_t *decl;
532
533     decl = parser_alloc(ctx, sizeof(*decl));
534     if(!decl)
535         return NULL;
536
537     decl->name = name;
538     decl->next = next;
539     return decl;
540 }
541
542 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
543 {
544     dim_statement_t *stat;
545
546     stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
547     if(!stat)
548         return NULL;
549
550     stat->dim_decls = decls;
551     return &stat->stat;
552 }
553
554 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
555 {
556     elseif_decl_t *decl;
557
558     decl = parser_alloc(ctx, sizeof(*decl));
559     if(!decl)
560         return NULL;
561
562     decl->expr = expr;
563     decl->stat = stat;
564     decl->next = NULL;
565     return decl;
566 }
567
568 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
569         statement_t *else_stat)
570 {
571     if_statement_t *stat;
572
573     stat = new_statement(ctx, STAT_IF, sizeof(*stat));
574     if(!stat)
575         return NULL;
576
577     stat->expr = expr;
578     stat->if_stat = if_stat;
579     stat->elseifs = elseif_decl;
580     stat->else_stat = else_stat;
581     return &stat->stat;
582 }
583
584 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
585 {
586     arg_decl_t *arg_decl;
587
588     arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
589     if(!arg_decl)
590         return NULL;
591
592     arg_decl->name = name;
593     arg_decl->by_ref = by_ref;
594     arg_decl->next = NULL;
595     return arg_decl;
596 }
597
598 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
599         unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
600 {
601     function_decl_t *decl;
602
603     if(storage_flags & STORAGE_IS_DEFAULT) {
604         if(type == FUNC_PROPGET) {
605             type = FUNC_DEFGET;
606         }else {
607             FIXME("Invalid default property\n");
608             ctx->hres = E_FAIL;
609             return NULL;
610         }
611     }
612
613     decl = parser_alloc(ctx, sizeof(*decl));
614     if(!decl)
615         return NULL;
616
617     decl->name = name;
618     decl->type = type;
619     decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
620     decl->args = arg_decl;
621     decl->body = body;
622     decl->next = NULL;
623     decl->next_prop_func = NULL;
624     return decl;
625 }
626
627 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
628 {
629     function_statement_t *stat;
630
631     stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
632     if(!stat)
633         return NULL;
634
635     stat->func_decl = decl;
636     return &stat->stat;
637 }
638
639 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
640 {
641     class_decl_t *class_decl;
642
643     class_decl = parser_alloc(ctx, sizeof(*class_decl));
644     if(!class_decl)
645         return NULL;
646
647     class_decl->funcs = NULL;
648     class_decl->props = NULL;
649     class_decl->next = NULL;
650     return class_decl;
651 }
652
653 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
654 {
655     function_decl_t *iter;
656
657     for(iter = class_decl->funcs; iter; iter = iter->next) {
658         if(!strcmpiW(iter->name, decl->name)) {
659             if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
660                 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
661                 ctx->hres = E_FAIL;
662                 return NULL;
663             }
664
665             while(1) {
666                 if(iter->type == decl->type) {
667                     FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
668                     ctx->hres = E_FAIL;
669                     return NULL;
670                 }
671                 if(!iter->next_prop_func)
672                     break;
673                 iter = iter->next_prop_func;
674             }
675
676             iter->next_prop_func = decl;
677             return class_decl;
678         }
679     }
680
681     decl->next = class_decl->funcs;
682     class_decl->funcs = decl;
683     return class_decl;
684 }
685
686 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
687 {
688     class_prop_decl_t *prop;
689
690     if(storage_flags & STORAGE_IS_DEFAULT) {
691         FIXME("variant prop van't be default value\n");
692         ctx->hres = E_FAIL;
693         return NULL;
694     }
695
696     prop = parser_alloc(ctx, sizeof(*prop));
697     if(!prop)
698         return NULL;
699
700     prop->name = identifier;
701     prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
702     prop->next = class_decl->props;
703     class_decl->props = prop;
704     return class_decl;
705 }
706
707 void *parser_alloc(parser_ctx_t *ctx, size_t size)
708 {
709     void *ret;
710
711     ret = vbsheap_alloc(&ctx->heap, size);
712     if(!ret)
713         ctx->hres = E_OUTOFMEMORY;
714     return ret;
715 }
716
717 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
718 {
719     ctx->code = ctx->ptr = code;
720     ctx->end = ctx->code + strlenW(ctx->code);
721
722     vbsheap_init(&ctx->heap);
723
724     ctx->parse_complete = FALSE;
725     ctx->hres = S_OK;
726
727     ctx->last_token = tNL;
728     ctx->last_nl = 0;
729     ctx->stats = ctx->stats_tail = NULL;
730     ctx->class_decls = NULL;
731     ctx->option_explicit = FALSE;
732
733     parser_parse(ctx);
734
735     if(FAILED(ctx->hres))
736         return ctx->hres;
737     if(!ctx->parse_complete) {
738         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
739         return E_FAIL;
740     }
741
742     return S_OK;
743 }
744
745 void parser_release(parser_ctx_t *ctx)
746 {
747     vbsheap_free(&ctx->heap);
748 }