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