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