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