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