comctl32: Trace Header order array contents.
[wine/multimedia.git] / dlls / vbscript / parser.y
blob45e0e8a07a6bd98608019a2f5212c2a00cab60f7
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
21 #include "vbscript.h"
22 #include "parse.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 #define YYLEX_PARAM ctx
29 #define YYPARSE_PARAM ctx
31 static int parser_error(const char*);
33 static void parse_complete(parser_ctx_t*,BOOL);
35 static void source_add_statement(parser_ctx_t*,statement_t*);
36 static void source_add_class(parser_ctx_t*,class_decl_t*);
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*);
47 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
49 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
50 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
51 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
52 static statement_t *new_set_statement(parser_ctx_t*,member_expression_t*,expression_t*);
53 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
54 static statement_t *new_while_statement(parser_ctx_t*,statement_type_t,expression_t*,statement_t*);
55 static statement_t *new_forto_statement(parser_ctx_t*,const WCHAR*,expression_t*,expression_t*,expression_t*,statement_t*);
56 static statement_t *new_foreach_statement(parser_ctx_t*,const WCHAR*,expression_t*,statement_t*);
57 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
58 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
59 static statement_t *new_onerror_statement(parser_ctx_t*,BOOL);
60 static statement_t *new_const_statement(parser_ctx_t*,const_decl_t*);
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*);
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);
72 static statement_t *link_statements(statement_t*,statement_t*);
74 #define STORAGE_IS_PRIVATE 1
75 #define STORAGE_IS_DEFAULT 2
77 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
81 %pure_parser
82 %start Program
84 %union {
85 const WCHAR *string;
86 statement_t *statement;
87 expression_t *expression;
88 member_expression_t *member;
89 elseif_decl_t *elseif;
90 dim_decl_t *dim_decl;
91 function_decl_t *func_decl;
92 arg_decl_t *arg_decl;
93 class_decl_t *class_decl;
94 const_decl_t *const_decl;
95 unsigned uint;
96 LONG lng;
97 BOOL bool;
98 double dbl;
101 %token tEOF tNL tREM tEMPTYBRACKETS
102 %token tTRUE tFALSE
103 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
104 %token tIS tLTEQ tGTEQ tMOD
105 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET tCONST
106 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
107 %token tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tSTEP tEACH tIN
108 %token tBYREF tBYVAL
109 %token tOPTION tEXPLICIT
110 %token tSTOP
111 %token tNOTHING tEMPTY tNULL
112 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
113 %token tERROR tNEXT tON tRESUME tGOTO
114 %token <string> tIdentifier tString
115 %token <lng> tLong tShort
116 %token <dbl> tDouble
118 %type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
119 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
120 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
121 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
122 %type <member> MemberExpression
123 %type <expression> Arguments_opt ArgumentList_opt ArgumentList Step_opt
124 %type <bool> OptionExplicit_opt DoType
125 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
126 %type <func_decl> FunctionDecl PropertyDecl
127 %type <elseif> ElseIfs_opt ElseIfs ElseIf
128 %type <class_decl> ClassDeclaration ClassBody
129 %type <uint> Storage Storage_opt
130 %type <dim_decl> DimDeclList
131 %type <const_decl> ConstDecl ConstDeclList
135 Program
136 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
138 OptionExplicit_opt
139 : /* empty */ { $$ = FALSE; }
140 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
142 SourceElements
143 : /* empty */
144 | SourceElements StatementNl { source_add_statement(ctx, $2); }
145 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
147 StatementsNl_opt
148 : /* empty */ { $$ = NULL; }
149 | StatementsNl { $$ = $1; }
151 StatementsNl
152 : StatementNl { $$ = $1; }
153 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
155 StatementNl
156 : Statement tNL { $$ = $1; }
158 Statement
159 : ':' { $$ = NULL; }
160 | ':' Statement { $$ = $2; }
161 | SimpleStatement { $$ = $1; }
162 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
163 | SimpleStatement ':' { $$ = $1; }
165 SimpleStatement
166 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
167 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
168 | MemberExpression Arguments_opt '=' Expression
169 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
170 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
171 | IfStatement { $$ = $1; }
172 | tWHILE Expression tNL StatementsNl_opt tWEND
173 { $$ = new_while_statement(ctx, STAT_WHILE, $2, $4); CHECK_ERROR; }
174 | tDO DoType Expression tNL StatementsNl_opt tLOOP
175 { $$ = new_while_statement(ctx, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
176 CHECK_ERROR; }
177 | tDO tNL StatementsNl_opt tLOOP DoType Expression
178 { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
179 CHECK_ERROR; }
180 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
181 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0); CHECK_ERROR; }
182 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
183 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
184 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
185 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
186 | tSET MemberExpression Arguments_opt '=' Expression
187 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
188 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
189 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
190 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
191 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
192 | tFOR tIdentifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
193 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
194 | tFOR tEACH tIdentifier tIN Expression tNL StatementsNl_opt tNEXT
195 { $$ = new_foreach_statement(ctx, $3, $5, $7); }
197 MemberExpression
198 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
199 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
201 DimDeclList /* FIXME: Support arrays */
202 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
203 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
205 ConstDeclList
206 : ConstDecl { $$ = $1; }
207 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
209 ConstDecl
210 : tIdentifier '=' LiteralExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
212 DoType
213 : tWHILE { $$ = TRUE; }
214 | tUNTIL { $$ = FALSE; }
216 Step_opt
217 : /* empty */ { $$ = NULL;}
218 | tSTEP Expression { $$ = $2; }
220 IfStatement
221 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
222 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
223 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
224 | tIF Expression tTHEN Statement tELSE Statement
225 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
227 ElseIfs_opt
228 : /* empty */ { $$ = NULL; }
229 | ElseIfs { $$ = $1; }
231 ElseIfs
232 : ElseIf { $$ = $1; }
233 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
235 ElseIf
236 : tELSEIF Expression tTHEN tNL StatementsNl
237 { $$ = new_elseif_decl(ctx, $2, $5); }
239 Else_opt
240 : /* empty */ { $$ = NULL; }
241 | tELSE tNL StatementsNl { $$ = $3; }
243 Arguments_opt
244 : EmptyBrackets_opt { $$ = NULL; }
245 | '(' ArgumentList ')' { $$ = $2; }
247 ArgumentList_opt
248 : EmptyBrackets_opt { $$ = NULL; }
249 | ArgumentList { $$ = $1; }
251 ArgumentList
252 : Expression { $$ = $1; }
253 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
255 EmptyBrackets_opt
256 : /* empty */
257 | tEMPTYBRACKETS
259 Expression
260 : EqvExpression { $$ = $1; }
261 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
263 EqvExpression
264 : XorExpression { $$ = $1; }
265 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
267 XorExpression
268 : OrExpression { $$ = $1; }
269 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
271 OrExpression
272 : AndExpression { $$ = $1; }
273 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
275 AndExpression
276 : NotExpression { $$ = $1; }
277 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
279 NotExpression
280 : EqualityExpression { $$ = $1; }
281 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
283 EqualityExpression
284 : ConcatExpression { $$ = $1; }
285 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
286 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
287 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
288 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
289 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
290 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
291 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
293 ConcatExpression
294 : AdditiveExpression { $$ = $1; }
295 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
297 AdditiveExpression
298 : ModExpression { $$ = $1; }
299 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
300 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
302 ModExpression
303 : IntdivExpression { $$ = $1; }
304 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
306 IntdivExpression
307 : MultiplicativeExpression { $$ = $1; }
308 | IntdivExpression '\\' MultiplicativeExpression
309 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
311 MultiplicativeExpression
312 : ExpExpression { $$ = $1; }
313 | MultiplicativeExpression '*' ExpExpression
314 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
315 | MultiplicativeExpression '/' ExpExpression
316 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
318 ExpExpression
319 : UnaryExpression { $$ = $1; }
320 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
322 UnaryExpression
323 : LiteralExpression { $$ = $1; }
324 | CallExpression { $$ = $1; }
325 | tNEW tIdentifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
326 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
328 CallExpression
329 : PrimaryExpression { $$ = $1; }
330 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
332 LiteralExpression
333 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
334 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
335 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
336 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
337 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
338 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
339 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
340 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
341 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
342 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
344 PrimaryExpression
345 : '(' Expression ')' { $$ = $2; }
346 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
348 ClassDeclaration
349 : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
351 ClassBody
352 : /* empty */ { $$ = new_class_decl(ctx); }
353 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
354 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
355 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
357 PropertyDecl
358 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
359 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
360 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
361 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
362 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
363 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
365 FunctionDecl
366 : Storage_opt tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
367 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
368 | Storage_opt tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
369 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
371 Storage_opt
372 : /* empty*/ { $$ = 0; }
373 | Storage { $$ = $1; }
375 Storage
376 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
377 | tPUBLIC { $$ = 0; }
378 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
380 ArgumentsDecl_opt
381 : EmptyBrackets_opt { $$ = NULL; }
382 | '(' ArgumentDeclList ')' { $$ = $2; }
384 ArgumentDeclList
385 : ArgumentDecl { $$ = $1; }
386 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
388 ArgumentDecl
389 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
390 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
391 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
395 static int parser_error(const char *str)
397 return 0;
400 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
402 if(!stat)
403 return;
405 if(ctx->stats) {
406 ctx->stats_tail->next = stat;
407 ctx->stats_tail = stat;
408 }else {
409 ctx->stats = ctx->stats_tail = stat;
413 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
415 class_decl->next = ctx->class_decls;
416 ctx->class_decls = class_decl;
419 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
421 ctx->parse_complete = TRUE;
422 ctx->option_explicit = option_explicit;
425 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
427 expression_t *expr;
429 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
430 if(expr) {
431 expr->type = type;
432 expr->next = NULL;
435 return expr;
438 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
440 bool_expression_t *expr;
442 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
443 if(!expr)
444 return NULL;
446 expr->value = value;
447 return &expr->expr;
450 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
452 string_expression_t *expr;
454 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
455 if(!expr)
456 return NULL;
458 expr->value = value;
459 return &expr->expr;
462 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
464 int_expression_t *expr;
466 expr = new_expression(ctx, type, sizeof(*expr));
467 if(!expr)
468 return NULL;
470 expr->value = value;
471 return &expr->expr;
474 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
476 double_expression_t *expr;
478 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
479 if(!expr)
480 return NULL;
482 expr->value = value;
483 return &expr->expr;
486 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
488 unary_expression_t *expr;
490 expr = new_expression(ctx, type, sizeof(*expr));
491 if(!expr)
492 return NULL;
494 expr->subexpr = subexpr;
495 return &expr->expr;
498 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
500 binary_expression_t *expr;
502 expr = new_expression(ctx, type, sizeof(*expr));
503 if(!expr)
504 return NULL;
506 expr->left = left;
507 expr->right = right;
508 return &expr->expr;
511 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
513 member_expression_t *expr;
515 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
516 if(!expr)
517 return NULL;
519 expr->obj_expr = obj_expr;
520 expr->identifier = identifier;
521 expr->args = NULL;
522 return expr;
525 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
527 string_expression_t *expr;
529 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
530 if(!expr)
531 return NULL;
533 expr->value = identifier;
534 return &expr->expr;
537 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
539 statement_t *stat;
541 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
542 if(stat) {
543 stat->type = type;
544 stat->next = NULL;
547 return stat;
550 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
552 call_statement_t *stat;
554 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
555 if(!stat)
556 return NULL;
558 stat->expr = expr;
559 return &stat->stat;
562 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
564 assign_statement_t *stat;
566 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
567 if(!stat)
568 return NULL;
570 stat->member_expr = left;
571 stat->value_expr = right;
572 return &stat->stat;
575 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
577 assign_statement_t *stat;
579 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
580 if(!stat)
581 return NULL;
583 stat->member_expr = left;
584 stat->value_expr = right;
585 return &stat->stat;
588 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
590 dim_decl_t *decl;
592 decl = parser_alloc(ctx, sizeof(*decl));
593 if(!decl)
594 return NULL;
596 decl->name = name;
597 decl->next = next;
598 return decl;
601 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
603 dim_statement_t *stat;
605 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
606 if(!stat)
607 return NULL;
609 stat->dim_decls = decls;
610 return &stat->stat;
613 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
615 elseif_decl_t *decl;
617 decl = parser_alloc(ctx, sizeof(*decl));
618 if(!decl)
619 return NULL;
621 decl->expr = expr;
622 decl->stat = stat;
623 decl->next = NULL;
624 return decl;
627 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
629 while_statement_t *stat;
631 stat = new_statement(ctx, type, sizeof(*stat));
632 if(!stat)
633 return NULL;
635 stat->expr = expr;
636 stat->body = body;
637 return &stat->stat;
640 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
641 expression_t *to_expr, expression_t *step_expr, statement_t *body)
643 forto_statement_t *stat;
645 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
646 if(!stat)
647 return NULL;
649 stat->identifier = identifier;
650 stat->from_expr = from_expr;
651 stat->to_expr = to_expr;
652 stat->step_expr = step_expr;
653 stat->body = body;
654 return &stat->stat;
657 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
658 statement_t *body)
660 foreach_statement_t *stat;
662 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
663 if(!stat)
664 return NULL;
666 stat->identifier = identifier;
667 stat->group_expr = group_expr;
668 stat->body = body;
669 return &stat->stat;
672 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
673 statement_t *else_stat)
675 if_statement_t *stat;
677 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
678 if(!stat)
679 return NULL;
681 stat->expr = expr;
682 stat->if_stat = if_stat;
683 stat->elseifs = elseif_decl;
684 stat->else_stat = else_stat;
685 return &stat->stat;
688 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
690 onerror_statement_t *stat;
692 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
693 if(!stat)
694 return NULL;
696 stat->resume_next = resume_next;
697 return &stat->stat;
700 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
702 arg_decl_t *arg_decl;
704 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
705 if(!arg_decl)
706 return NULL;
708 arg_decl->name = name;
709 arg_decl->by_ref = by_ref;
710 arg_decl->next = NULL;
711 return arg_decl;
714 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
715 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
717 function_decl_t *decl;
719 if(storage_flags & STORAGE_IS_DEFAULT) {
720 if(type == FUNC_PROPGET) {
721 type = FUNC_DEFGET;
722 }else {
723 FIXME("Invalid default property\n");
724 ctx->hres = E_FAIL;
725 return NULL;
729 decl = parser_alloc(ctx, sizeof(*decl));
730 if(!decl)
731 return NULL;
733 decl->name = name;
734 decl->type = type;
735 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
736 decl->args = arg_decl;
737 decl->body = body;
738 decl->next = NULL;
739 decl->next_prop_func = NULL;
740 return decl;
743 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
745 function_statement_t *stat;
747 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
748 if(!stat)
749 return NULL;
751 stat->func_decl = decl;
752 return &stat->stat;
755 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
757 class_decl_t *class_decl;
759 class_decl = parser_alloc(ctx, sizeof(*class_decl));
760 if(!class_decl)
761 return NULL;
763 class_decl->funcs = NULL;
764 class_decl->props = NULL;
765 class_decl->next = NULL;
766 return class_decl;
769 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
771 function_decl_t *iter;
773 for(iter = class_decl->funcs; iter; iter = iter->next) {
774 if(!strcmpiW(iter->name, decl->name)) {
775 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
776 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
777 ctx->hres = E_FAIL;
778 return NULL;
781 while(1) {
782 if(iter->type == decl->type) {
783 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
784 ctx->hres = E_FAIL;
785 return NULL;
787 if(!iter->next_prop_func)
788 break;
789 iter = iter->next_prop_func;
792 iter->next_prop_func = decl;
793 return class_decl;
797 decl->next = class_decl->funcs;
798 class_decl->funcs = decl;
799 return class_decl;
802 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
804 class_prop_decl_t *prop;
806 if(storage_flags & STORAGE_IS_DEFAULT) {
807 FIXME("variant prop van't be default value\n");
808 ctx->hres = E_FAIL;
809 return NULL;
812 prop = parser_alloc(ctx, sizeof(*prop));
813 if(!prop)
814 return NULL;
816 prop->name = identifier;
817 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
818 prop->next = class_decl->props;
819 class_decl->props = prop;
820 return class_decl;
823 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
825 const_decl_t *decl;
827 decl = parser_alloc(ctx, sizeof(*decl));
828 if(!decl)
829 return NULL;
831 decl->name = name;
832 decl->value_expr = expr;
833 decl->next = NULL;
834 return decl;
837 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
839 const_statement_t *stat;
841 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
842 if(!stat)
843 return NULL;
845 stat->decls = decls;
846 return &stat->stat;
849 static statement_t *link_statements(statement_t *head, statement_t *tail)
851 statement_t *iter;
853 for(iter = head; iter->next; iter = iter->next);
854 iter->next = tail;
856 return head;
859 void *parser_alloc(parser_ctx_t *ctx, size_t size)
861 void *ret;
863 ret = vbsheap_alloc(&ctx->heap, size);
864 if(!ret)
865 ctx->hres = E_OUTOFMEMORY;
866 return ret;
869 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
871 ctx->code = ctx->ptr = code;
872 ctx->end = ctx->code + strlenW(ctx->code);
874 vbsheap_init(&ctx->heap);
876 ctx->parse_complete = FALSE;
877 ctx->hres = S_OK;
879 ctx->last_token = tNL;
880 ctx->last_nl = 0;
881 ctx->stats = ctx->stats_tail = NULL;
882 ctx->class_decls = NULL;
883 ctx->option_explicit = FALSE;
885 parser_parse(ctx);
887 if(FAILED(ctx->hres))
888 return ctx->hres;
889 if(!ctx->parse_complete) {
890 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
891 return E_FAIL;
894 return S_OK;
897 void parser_release(parser_ctx_t *ctx)
899 vbsheap_free(&ctx->heap);