jscript: No longer support per-statement compilation.
[wine/multimedia.git] / dlls / vbscript / parser.y
blobd6bda55a7ee915cd5121e4a2f8d65eb13c2da976
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_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*);
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*);
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);
71 static statement_t *link_statements(statement_t*,statement_t*);
73 #define STORAGE_IS_PRIVATE 1
74 #define STORAGE_IS_DEFAULT 2
76 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
80 %pure_parser
81 %start Program
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;
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
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
134 Program
135 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
137 OptionExplicit_opt
138 : /* empty */ { $$ = FALSE; }
139 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
141 SourceElements
142 : /* empty */
143 | SourceElements StatementNl { source_add_statement(ctx, $2); }
144 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
146 StatementsNl_opt
147 : /* empty */ { $$ = NULL; }
148 | StatementsNl { $$ = $1; }
150 StatementsNl
151 : StatementNl { $$ = $1; }
152 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
154 StatementNl
155 : Statement tNL { $$ = $1; }
157 Statement
158 : ':' { $$ = NULL; }
159 | ':' Statement { $$ = $2; }
160 | SimpleStatement { $$ = $1; }
161 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
162 | SimpleStatement ':' { $$ = $1; }
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 tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
182 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
183 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
184 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
185 | tSET MemberExpression Arguments_opt '=' Expression
186 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
187 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
188 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
189 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
190 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
191 | tFOR tIdentifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
192 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
194 MemberExpression
195 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
196 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
198 DimDeclList /* FIXME: Support arrays */
199 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
200 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
202 ConstDeclList
203 : ConstDecl { $$ = $1; }
204 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
206 ConstDecl
207 : tIdentifier '=' LiteralExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
209 DoType
210 : tWHILE { $$ = TRUE; }
211 | tUNTIL { $$ = FALSE; }
213 Step_opt
214 : /* empty */ { $$ = NULL;}
215 | tSTEP Expression { $$ = $2; }
217 IfStatement
218 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
219 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
220 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
221 | tIF Expression tTHEN Statement tELSE Statement
222 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
224 ElseIfs_opt
225 : /* empty */ { $$ = NULL; }
226 | ElseIfs { $$ = $1; }
228 ElseIfs
229 : ElseIf { $$ = $1; }
230 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
232 ElseIf
233 : tELSEIF Expression tTHEN tNL StatementsNl
234 { $$ = new_elseif_decl(ctx, $2, $5); }
236 Else_opt
237 : /* empty */ { $$ = NULL; }
238 | tELSE tNL StatementsNl { $$ = $3; }
240 Arguments_opt
241 : EmptyBrackets_opt { $$ = NULL; }
242 | '(' ArgumentList ')' { $$ = $2; }
244 ArgumentList_opt
245 : EmptyBrackets_opt { $$ = NULL; }
246 | ArgumentList { $$ = $1; }
248 ArgumentList
249 : Expression { $$ = $1; }
250 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
252 EmptyBrackets_opt
253 : /* empty */
254 | tEMPTYBRACKETS
256 Expression
257 : EqvExpression { $$ = $1; }
258 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
260 EqvExpression
261 : XorExpression { $$ = $1; }
262 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
264 XorExpression
265 : OrExpression { $$ = $1; }
266 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
268 OrExpression
269 : AndExpression { $$ = $1; }
270 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
272 AndExpression
273 : NotExpression { $$ = $1; }
274 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
276 NotExpression
277 : EqualityExpression { $$ = $1; }
278 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
280 EqualityExpression
281 : ConcatExpression { $$ = $1; }
282 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
283 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
284 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
285 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
286 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
287 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
288 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
290 ConcatExpression
291 : AdditiveExpression { $$ = $1; }
292 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
294 AdditiveExpression
295 : ModExpression { $$ = $1; }
296 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
297 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
299 ModExpression
300 : IntdivExpression { $$ = $1; }
301 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
303 IntdivExpression
304 : MultiplicativeExpression { $$ = $1; }
305 | IntdivExpression '\\' MultiplicativeExpression
306 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
308 MultiplicativeExpression
309 : ExpExpression { $$ = $1; }
310 | MultiplicativeExpression '*' ExpExpression
311 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
312 | MultiplicativeExpression '/' ExpExpression
313 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
315 ExpExpression
316 : UnaryExpression { $$ = $1; }
317 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
319 UnaryExpression
320 : LiteralExpression { $$ = $1; }
321 | CallExpression { $$ = $1; }
322 | tNEW tIdentifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
323 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
325 CallExpression
326 : PrimaryExpression { $$ = $1; }
327 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
329 LiteralExpression
330 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
331 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
332 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
333 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
334 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
335 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
336 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
337 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
338 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
339 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
341 PrimaryExpression
342 : '(' Expression ')' { $$ = $2; }
343 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
345 ClassDeclaration
346 : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
348 ClassBody
349 : /* empty */ { $$ = new_class_decl(ctx); }
350 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
351 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
352 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
354 PropertyDecl
355 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
356 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
357 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
358 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
359 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
360 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
362 FunctionDecl
363 : Storage_opt tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
364 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
365 | Storage_opt tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
366 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
368 Storage_opt
369 : /* empty*/ { $$ = 0; }
370 | Storage { $$ = $1; }
372 Storage
373 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
374 | tPUBLIC { $$ = 0; }
375 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
377 ArgumentsDecl_opt
378 : EmptyBrackets_opt { $$ = NULL; }
379 | '(' ArgumentDeclList ')' { $$ = $2; }
381 ArgumentDeclList
382 : ArgumentDecl { $$ = $1; }
383 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
385 ArgumentDecl
386 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
387 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
388 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
392 static int parser_error(const char *str)
394 return 0;
397 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
399 if(!stat)
400 return;
402 if(ctx->stats) {
403 ctx->stats_tail->next = stat;
404 ctx->stats_tail = stat;
405 }else {
406 ctx->stats = ctx->stats_tail = stat;
410 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
412 class_decl->next = ctx->class_decls;
413 ctx->class_decls = class_decl;
416 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
418 ctx->parse_complete = TRUE;
419 ctx->option_explicit = option_explicit;
422 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
424 expression_t *expr;
426 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
427 if(expr) {
428 expr->type = type;
429 expr->next = NULL;
432 return expr;
435 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
437 bool_expression_t *expr;
439 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
440 if(!expr)
441 return NULL;
443 expr->value = value;
444 return &expr->expr;
447 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
449 string_expression_t *expr;
451 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
452 if(!expr)
453 return NULL;
455 expr->value = value;
456 return &expr->expr;
459 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
461 int_expression_t *expr;
463 expr = new_expression(ctx, type, sizeof(*expr));
464 if(!expr)
465 return NULL;
467 expr->value = value;
468 return &expr->expr;
471 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
473 double_expression_t *expr;
475 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
476 if(!expr)
477 return NULL;
479 expr->value = value;
480 return &expr->expr;
483 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
485 unary_expression_t *expr;
487 expr = new_expression(ctx, type, sizeof(*expr));
488 if(!expr)
489 return NULL;
491 expr->subexpr = subexpr;
492 return &expr->expr;
495 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
497 binary_expression_t *expr;
499 expr = new_expression(ctx, type, sizeof(*expr));
500 if(!expr)
501 return NULL;
503 expr->left = left;
504 expr->right = right;
505 return &expr->expr;
508 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
510 member_expression_t *expr;
512 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
513 if(!expr)
514 return NULL;
516 expr->obj_expr = obj_expr;
517 expr->identifier = identifier;
518 expr->args = NULL;
519 return expr;
522 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
524 string_expression_t *expr;
526 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
527 if(!expr)
528 return NULL;
530 expr->value = identifier;
531 return &expr->expr;
534 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
536 statement_t *stat;
538 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
539 if(stat) {
540 stat->type = type;
541 stat->next = NULL;
544 return stat;
547 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
549 call_statement_t *stat;
551 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
552 if(!stat)
553 return NULL;
555 stat->expr = expr;
556 return &stat->stat;
559 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
561 assign_statement_t *stat;
563 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
564 if(!stat)
565 return NULL;
567 stat->member_expr = left;
568 stat->value_expr = right;
569 return &stat->stat;
572 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
574 assign_statement_t *stat;
576 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
577 if(!stat)
578 return NULL;
580 stat->member_expr = left;
581 stat->value_expr = right;
582 return &stat->stat;
585 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
587 dim_decl_t *decl;
589 decl = parser_alloc(ctx, sizeof(*decl));
590 if(!decl)
591 return NULL;
593 decl->name = name;
594 decl->next = next;
595 return decl;
598 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
600 dim_statement_t *stat;
602 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
603 if(!stat)
604 return NULL;
606 stat->dim_decls = decls;
607 return &stat->stat;
610 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
612 elseif_decl_t *decl;
614 decl = parser_alloc(ctx, sizeof(*decl));
615 if(!decl)
616 return NULL;
618 decl->expr = expr;
619 decl->stat = stat;
620 decl->next = NULL;
621 return decl;
624 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
626 while_statement_t *stat;
628 stat = new_statement(ctx, type, sizeof(*stat));
629 if(!stat)
630 return NULL;
632 stat->expr = expr;
633 stat->body = body;
634 return &stat->stat;
637 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
638 expression_t *to_expr, expression_t *step_expr, statement_t *body)
640 forto_statement_t *stat;
642 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
643 if(!stat)
644 return NULL;
646 stat->identifier = identifier;
647 stat->from_expr = from_expr;
648 stat->to_expr = to_expr;
649 stat->step_expr = step_expr;
650 stat->body = body;
651 return &stat->stat;
654 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
655 statement_t *else_stat)
657 if_statement_t *stat;
659 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
660 if(!stat)
661 return NULL;
663 stat->expr = expr;
664 stat->if_stat = if_stat;
665 stat->elseifs = elseif_decl;
666 stat->else_stat = else_stat;
667 return &stat->stat;
670 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
672 onerror_statement_t *stat;
674 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
675 if(!stat)
676 return NULL;
678 stat->resume_next = resume_next;
679 return &stat->stat;
682 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
684 arg_decl_t *arg_decl;
686 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
687 if(!arg_decl)
688 return NULL;
690 arg_decl->name = name;
691 arg_decl->by_ref = by_ref;
692 arg_decl->next = NULL;
693 return arg_decl;
696 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
697 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
699 function_decl_t *decl;
701 if(storage_flags & STORAGE_IS_DEFAULT) {
702 if(type == FUNC_PROPGET) {
703 type = FUNC_DEFGET;
704 }else {
705 FIXME("Invalid default property\n");
706 ctx->hres = E_FAIL;
707 return NULL;
711 decl = parser_alloc(ctx, sizeof(*decl));
712 if(!decl)
713 return NULL;
715 decl->name = name;
716 decl->type = type;
717 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
718 decl->args = arg_decl;
719 decl->body = body;
720 decl->next = NULL;
721 decl->next_prop_func = NULL;
722 return decl;
725 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
727 function_statement_t *stat;
729 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
730 if(!stat)
731 return NULL;
733 stat->func_decl = decl;
734 return &stat->stat;
737 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
739 class_decl_t *class_decl;
741 class_decl = parser_alloc(ctx, sizeof(*class_decl));
742 if(!class_decl)
743 return NULL;
745 class_decl->funcs = NULL;
746 class_decl->props = NULL;
747 class_decl->next = NULL;
748 return class_decl;
751 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
753 function_decl_t *iter;
755 for(iter = class_decl->funcs; iter; iter = iter->next) {
756 if(!strcmpiW(iter->name, decl->name)) {
757 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
758 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
759 ctx->hres = E_FAIL;
760 return NULL;
763 while(1) {
764 if(iter->type == decl->type) {
765 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
766 ctx->hres = E_FAIL;
767 return NULL;
769 if(!iter->next_prop_func)
770 break;
771 iter = iter->next_prop_func;
774 iter->next_prop_func = decl;
775 return class_decl;
779 decl->next = class_decl->funcs;
780 class_decl->funcs = decl;
781 return class_decl;
784 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
786 class_prop_decl_t *prop;
788 if(storage_flags & STORAGE_IS_DEFAULT) {
789 FIXME("variant prop van't be default value\n");
790 ctx->hres = E_FAIL;
791 return NULL;
794 prop = parser_alloc(ctx, sizeof(*prop));
795 if(!prop)
796 return NULL;
798 prop->name = identifier;
799 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
800 prop->next = class_decl->props;
801 class_decl->props = prop;
802 return class_decl;
805 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
807 const_decl_t *decl;
809 decl = parser_alloc(ctx, sizeof(*decl));
810 if(!decl)
811 return NULL;
813 decl->name = name;
814 decl->value_expr = expr;
815 decl->next = NULL;
816 return decl;
819 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
821 const_statement_t *stat;
823 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
824 if(!stat)
825 return NULL;
827 stat->decls = decls;
828 return &stat->stat;
831 static statement_t *link_statements(statement_t *head, statement_t *tail)
833 statement_t *iter;
835 for(iter = head; iter->next; iter = iter->next);
836 iter->next = tail;
838 return head;
841 void *parser_alloc(parser_ctx_t *ctx, size_t size)
843 void *ret;
845 ret = vbsheap_alloc(&ctx->heap, size);
846 if(!ret)
847 ctx->hres = E_OUTOFMEMORY;
848 return ret;
851 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
853 ctx->code = ctx->ptr = code;
854 ctx->end = ctx->code + strlenW(ctx->code);
856 vbsheap_init(&ctx->heap);
858 ctx->parse_complete = FALSE;
859 ctx->hres = S_OK;
861 ctx->last_token = tNL;
862 ctx->last_nl = 0;
863 ctx->stats = ctx->stats_tail = NULL;
864 ctx->class_decls = NULL;
865 ctx->option_explicit = FALSE;
867 parser_parse(ctx);
869 if(FAILED(ctx->hres))
870 return ctx->hres;
871 if(!ctx->parse_complete) {
872 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
873 return E_FAIL;
876 return S_OK;
879 void parser_release(parser_ctx_t *ctx)
881 vbsheap_free(&ctx->heap);