dmusic: Pass creation parameters to DMUSIC_CreateDirectMusicBufferImpl then allocate...
[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 #define YYLEX_PARAM ctx
29 #define YYPARSE_PARAM ctx
30
31 static int parser_error(const char*);
32
33 static void parse_complete(parser_ctx_t*,BOOL);
34
35 static void source_add_statement(parser_ctx_t*,statement_t*);
36 static void source_add_class(parser_ctx_t*,class_decl_t*);
37
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*);
46
47 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
48
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*);
61
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*);
67
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);
71
72 static statement_t *link_statements(statement_t*,statement_t*);
73
74 #define STORAGE_IS_PRIVATE    1
75 #define STORAGE_IS_DEFAULT    2
76
77 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
78
79 %}
80
81 %pure_parser
82 %start Program
83
84 %union {
85     const WCHAR *string;
86     statement_t *statement;
87     expression_t *expression;
88     member_expression_t *member;
89     elseif_decl_t *elseif;
90     dim_decl_t *dim_decl;
91     function_decl_t *func_decl;
92     arg_decl_t *arg_decl;
93     class_decl_t *class_decl;
94     const_decl_t *const_decl;
95     unsigned uint;
96     LONG lng;
97     BOOL bool;
98     double dbl;
99 }
100
101 %token tEOF tNL tREM tEMPTYBRACKETS
102 %token tTRUE tFALSE
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
108 %token tBYREF tBYVAL
109 %token tOPTION tEXPLICIT
110 %token tSTOP
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
116 %token <dbl> tDouble
117
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
132
133 %%
134
135 Program
136     : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }
137
138 OptionExplicit_opt
139     : /* empty */                { $$ = FALSE; }
140     | tOPTION tEXPLICIT tNL      { $$ = TRUE; }
141
142 SourceElements
143     : /* empty */
144     | SourceElements StatementNl            { source_add_statement(ctx, $2); }
145     | SourceElements ClassDeclaration       { source_add_class(ctx, $2); }
146
147 StatementsNl_opt
148     : /* empty */                           { $$ = NULL; }
149     | StatementsNl                          { $$ = $1; }
150
151 StatementsNl
152     : StatementNl                           { $$ = $1; }
153     | StatementNl StatementsNl              { $$ = link_statements($1, $2); }
154
155 StatementNl
156     : Statement tNL                 { $$ = $1; }
157
158 Statement
159     : ':'                                   { $$ = NULL; }
160     | ':' Statement                         { $$ = $2; }
161     | SimpleStatement                       { $$ = $1; }
162     | SimpleStatement ':' Statement         { $1->next = $3; $$ = $1; }
163     | SimpleStatement ':'                   { $$ = $1; }
164
165 SimpleStatement
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);
176                                               CHECK_ERROR; }
177     | tDO tNL StatementsNl_opt tLOOP DoType Expression
178                                             { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
179                                               CHECK_ERROR; }
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); }
196
197 MemberExpression
198     : tIdentifier                           { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
199     | CallExpression '.' tIdentifier        { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
200
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; }
204
205 ConstDeclList
206     : ConstDecl                             { $$ = $1; }
207     | ConstDecl ',' ConstDeclList           { $1->next = $3; $$ = $1; }
208
209 ConstDecl
210     : tIdentifier '=' LiteralExpression     { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
211
212 DoType
213     : tWHILE        { $$ = TRUE; }
214     | tUNTIL        { $$ = FALSE; }
215
216 Step_opt
217     : /* empty */                           { $$ = NULL;}
218     | tSTEP Expression                      { $$ = $2; }
219
220 IfStatement
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; }
226
227 EndIf_opt
228     : /* empty */
229     | tEND tIF
230
231 ElseIfs_opt
232     : /* empty */                           { $$ = NULL; }
233     | ElseIfs                               { $$ = $1; }
234
235 ElseIfs
236     : ElseIf                                { $$ = $1; }
237     | ElseIf ElseIfs                        { $1->next = $2; $$ = $1; }
238
239 ElseIf
240     : tELSEIF Expression tTHEN tNL StatementsNl
241                                             { $$ = new_elseif_decl(ctx, $2, $5); }
242
243 Else_opt
244     : /* empty */                           { $$ = NULL; }
245     | tELSE tNL StatementsNl                { $$ = $3; }
246
247 Arguments_opt
248     : EmptyBrackets_opt             { $$ = NULL; }
249     | '(' ArgumentList ')'          { $$ = $2; }
250
251 ArgumentList_opt
252     : EmptyBrackets_opt             { $$ = NULL; }
253     | ArgumentList                  { $$ = $1; }
254
255 ArgumentList
256     : Expression                    { $$ = $1; }
257     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
258
259 EmptyBrackets_opt
260     : /* empty */
261     | tEMPTYBRACKETS
262
263 Expression
264     : EqvExpression                             { $$ = $1; }
265     | Expression tIMP EqvExpression             { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
266
267 EqvExpression
268     : XorExpression                             { $$ = $1; }
269     | EqvExpression tEQV XorExpression          { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
270
271 XorExpression
272     : OrExpression                              { $$ = $1; }
273     | XorExpression tXOR OrExpression           { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
274
275 OrExpression
276     : AndExpression                             { $$ = $1; }
277     | OrExpression tOR AndExpression            { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
278
279 AndExpression
280     : NotExpression                             { $$ = $1; }
281     | AndExpression tAND NotExpression          { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
282
283 NotExpression
284     : EqualityExpression            { $$ = $1; }
285     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
286
287 EqualityExpression
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; }
296
297 ConcatExpression
298     : AdditiveExpression                        { $$ = $1; }
299     | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
300
301 AdditiveExpression
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; }
305
306 ModExpression
307     : IntdivExpression                          { $$ = $1; }
308     | ModExpression tMOD IntdivExpression       { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
309
310 IntdivExpression
311     : MultiplicativeExpression                  { $$ = $1; }
312     | IntdivExpression '\\' MultiplicativeExpression
313                                                 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
314
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; }
321
322 ExpExpression
323     : UnaryExpression                           { $$ = $1; }
324     | ExpExpression '^' UnaryExpression         { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
325
326 UnaryExpression
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; }
331
332 CallExpression
333     : PrimaryExpression                 { $$ = $1; }
334     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
335
336 LiteralExpression
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; }
347
348 PrimaryExpression
349     : '(' Expression ')'            { $$ = $2; }
350     | tME                           { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
351
352 ClassDeclaration
353     : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL      { $4->name = $2; $$ = $4; }
354
355 ClassBody
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; }
360
361 PropertyDecl
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; }
368
369 FunctionDecl
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; }
374
375 Storage_opt
376     : /* empty*/                    { $$ = 0; }
377     | Storage                       { $$ = $1; }
378
379 Storage
380     : tPUBLIC tDEFAULT              { $$ = STORAGE_IS_DEFAULT; }
381     | tPUBLIC                       { $$ = 0; }
382     | tPRIVATE                      { $$ = STORAGE_IS_PRIVATE; }
383
384 ArgumentsDecl_opt
385     : EmptyBrackets_opt                         { $$ = NULL; }
386     | '(' ArgumentDeclList ')'                  { $$ = $2; }
387
388 ArgumentDeclList
389     : ArgumentDecl                              { $$ = $1; }
390     | ArgumentDecl ',' ArgumentDeclList         { $1->next = $3; $$ = $1; }
391
392 ArgumentDecl
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); }
396
397 %%
398
399 static int parser_error(const char *str)
400 {
401     return 0;
402 }
403
404 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
405 {
406     if(!stat)
407         return;
408
409     if(ctx->stats) {
410         ctx->stats_tail->next = stat;
411         ctx->stats_tail = stat;
412     }else {
413         ctx->stats = ctx->stats_tail = stat;
414     }
415 }
416
417 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
418 {
419     class_decl->next = ctx->class_decls;
420     ctx->class_decls = class_decl;
421 }
422
423 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
424 {
425     ctx->parse_complete = TRUE;
426     ctx->option_explicit = option_explicit;
427 }
428
429 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
430 {
431     expression_t *expr;
432
433     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
434     if(expr) {
435         expr->type = type;
436         expr->next = NULL;
437     }
438
439     return expr;
440 }
441
442 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
443 {
444     bool_expression_t *expr;
445
446     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
447     if(!expr)
448         return NULL;
449
450     expr->value = value;
451     return &expr->expr;
452 }
453
454 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
455 {
456     string_expression_t *expr;
457
458     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
459     if(!expr)
460         return NULL;
461
462     expr->value = value;
463     return &expr->expr;
464 }
465
466 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
467 {
468     int_expression_t *expr;
469
470     expr = new_expression(ctx, type, sizeof(*expr));
471     if(!expr)
472         return NULL;
473
474     expr->value = value;
475     return &expr->expr;
476 }
477
478 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
479 {
480     double_expression_t *expr;
481
482     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
483     if(!expr)
484         return NULL;
485
486     expr->value = value;
487     return &expr->expr;
488 }
489
490 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
491 {
492     unary_expression_t *expr;
493
494     expr = new_expression(ctx, type, sizeof(*expr));
495     if(!expr)
496         return NULL;
497
498     expr->subexpr = subexpr;
499     return &expr->expr;
500 }
501
502 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
503 {
504     binary_expression_t *expr;
505
506     expr = new_expression(ctx, type, sizeof(*expr));
507     if(!expr)
508         return NULL;
509
510     expr->left = left;
511     expr->right = right;
512     return &expr->expr;
513 }
514
515 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
516 {
517     member_expression_t *expr;
518
519     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
520     if(!expr)
521         return NULL;
522
523     expr->obj_expr = obj_expr;
524     expr->identifier = identifier;
525     expr->args = NULL;
526     return expr;
527 }
528
529 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
530 {
531     string_expression_t *expr;
532
533     expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
534     if(!expr)
535         return NULL;
536
537     expr->value = identifier;
538     return &expr->expr;
539 }
540
541 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
542 {
543     statement_t *stat;
544
545     stat = parser_alloc(ctx, size ? size : sizeof(*stat));
546     if(stat) {
547         stat->type = type;
548         stat->next = NULL;
549     }
550
551     return stat;
552 }
553
554 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
555 {
556     call_statement_t *stat;
557
558     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
559     if(!stat)
560         return NULL;
561
562     stat->expr = expr;
563     return &stat->stat;
564 }
565
566 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
567 {
568     assign_statement_t *stat;
569
570     stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
571     if(!stat)
572         return NULL;
573
574     stat->member_expr = left;
575     stat->value_expr = right;
576     return &stat->stat;
577 }
578
579 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
580 {
581     assign_statement_t *stat;
582
583     stat = new_statement(ctx, STAT_SET, sizeof(*stat));
584     if(!stat)
585         return NULL;
586
587     stat->member_expr = left;
588     stat->value_expr = right;
589     return &stat->stat;
590 }
591
592 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
593 {
594     dim_decl_t *decl;
595
596     decl = parser_alloc(ctx, sizeof(*decl));
597     if(!decl)
598         return NULL;
599
600     decl->name = name;
601     decl->next = next;
602     return decl;
603 }
604
605 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
606 {
607     dim_statement_t *stat;
608
609     stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
610     if(!stat)
611         return NULL;
612
613     stat->dim_decls = decls;
614     return &stat->stat;
615 }
616
617 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
618 {
619     elseif_decl_t *decl;
620
621     decl = parser_alloc(ctx, sizeof(*decl));
622     if(!decl)
623         return NULL;
624
625     decl->expr = expr;
626     decl->stat = stat;
627     decl->next = NULL;
628     return decl;
629 }
630
631 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
632 {
633     while_statement_t *stat;
634
635     stat = new_statement(ctx, type, sizeof(*stat));
636     if(!stat)
637         return NULL;
638
639     stat->expr = expr;
640     stat->body = body;
641     return &stat->stat;
642 }
643
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)
646 {
647     forto_statement_t *stat;
648
649     stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
650     if(!stat)
651         return NULL;
652
653     stat->identifier = identifier;
654     stat->from_expr = from_expr;
655     stat->to_expr = to_expr;
656     stat->step_expr = step_expr;
657     stat->body = body;
658     return &stat->stat;
659 }
660
661 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
662         statement_t *body)
663 {
664     foreach_statement_t *stat;
665
666     stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
667     if(!stat)
668         return NULL;
669
670     stat->identifier = identifier;
671     stat->group_expr = group_expr;
672     stat->body = body;
673     return &stat->stat;
674 }
675
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)
678 {
679     if_statement_t *stat;
680
681     stat = new_statement(ctx, STAT_IF, sizeof(*stat));
682     if(!stat)
683         return NULL;
684
685     stat->expr = expr;
686     stat->if_stat = if_stat;
687     stat->elseifs = elseif_decl;
688     stat->else_stat = else_stat;
689     return &stat->stat;
690 }
691
692 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
693 {
694     onerror_statement_t *stat;
695
696     stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
697     if(!stat)
698         return NULL;
699
700     stat->resume_next = resume_next;
701     return &stat->stat;
702 }
703
704 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
705 {
706     arg_decl_t *arg_decl;
707
708     arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
709     if(!arg_decl)
710         return NULL;
711
712     arg_decl->name = name;
713     arg_decl->by_ref = by_ref;
714     arg_decl->next = NULL;
715     return arg_decl;
716 }
717
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)
720 {
721     function_decl_t *decl;
722
723     if(storage_flags & STORAGE_IS_DEFAULT) {
724         if(type == FUNC_PROPGET) {
725             type = FUNC_DEFGET;
726         }else {
727             FIXME("Invalid default property\n");
728             ctx->hres = E_FAIL;
729             return NULL;
730         }
731     }
732
733     decl = parser_alloc(ctx, sizeof(*decl));
734     if(!decl)
735         return NULL;
736
737     decl->name = name;
738     decl->type = type;
739     decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
740     decl->args = arg_decl;
741     decl->body = body;
742     decl->next = NULL;
743     decl->next_prop_func = NULL;
744     return decl;
745 }
746
747 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
748 {
749     function_statement_t *stat;
750
751     stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
752     if(!stat)
753         return NULL;
754
755     stat->func_decl = decl;
756     return &stat->stat;
757 }
758
759 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
760 {
761     class_decl_t *class_decl;
762
763     class_decl = parser_alloc(ctx, sizeof(*class_decl));
764     if(!class_decl)
765         return NULL;
766
767     class_decl->funcs = NULL;
768     class_decl->props = NULL;
769     class_decl->next = NULL;
770     return class_decl;
771 }
772
773 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
774 {
775     function_decl_t *iter;
776
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));
781                 ctx->hres = E_FAIL;
782                 return NULL;
783             }
784
785             while(1) {
786                 if(iter->type == decl->type) {
787                     FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
788                     ctx->hres = E_FAIL;
789                     return NULL;
790                 }
791                 if(!iter->next_prop_func)
792                     break;
793                 iter = iter->next_prop_func;
794             }
795
796             iter->next_prop_func = decl;
797             return class_decl;
798         }
799     }
800
801     decl->next = class_decl->funcs;
802     class_decl->funcs = decl;
803     return class_decl;
804 }
805
806 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
807 {
808     class_prop_decl_t *prop;
809
810     if(storage_flags & STORAGE_IS_DEFAULT) {
811         FIXME("variant prop van't be default value\n");
812         ctx->hres = E_FAIL;
813         return NULL;
814     }
815
816     prop = parser_alloc(ctx, sizeof(*prop));
817     if(!prop)
818         return NULL;
819
820     prop->name = identifier;
821     prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
822     prop->next = class_decl->props;
823     class_decl->props = prop;
824     return class_decl;
825 }
826
827 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
828 {
829     const_decl_t *decl;
830
831     decl = parser_alloc(ctx, sizeof(*decl));
832     if(!decl)
833         return NULL;
834
835     decl->name = name;
836     decl->value_expr = expr;
837     decl->next = NULL;
838     return decl;
839 }
840
841 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
842 {
843     const_statement_t *stat;
844
845     stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
846     if(!stat)
847         return NULL;
848
849     stat->decls = decls;
850     return &stat->stat;
851 }
852
853 static statement_t *link_statements(statement_t *head, statement_t *tail)
854 {
855     statement_t *iter;
856
857     for(iter = head; iter->next; iter = iter->next);
858     iter->next = tail;
859
860     return head;
861 }
862
863 void *parser_alloc(parser_ctx_t *ctx, size_t size)
864 {
865     void *ret;
866
867     ret = vbsheap_alloc(&ctx->heap, size);
868     if(!ret)
869         ctx->hres = E_OUTOFMEMORY;
870     return ret;
871 }
872
873 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
874 {
875     ctx->code = ctx->ptr = code;
876     ctx->end = ctx->code + strlenW(ctx->code);
877
878     vbsheap_init(&ctx->heap);
879
880     ctx->parse_complete = FALSE;
881     ctx->hres = S_OK;
882
883     ctx->last_token = tNL;
884     ctx->last_nl = 0;
885     ctx->stats = ctx->stats_tail = NULL;
886     ctx->class_decls = NULL;
887     ctx->option_explicit = FALSE;
888
889     parser_parse(ctx);
890
891     if(FAILED(ctx->hres))
892         return ctx->hres;
893     if(!ctx->parse_complete) {
894         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
895         return E_FAIL;
896     }
897
898     return S_OK;
899 }
900
901 void parser_release(parser_ctx_t *ctx)
902 {
903     vbsheap_free(&ctx->heap);
904 }