vbscript: Added support for do..loop statement without an expression.
[wine.git] / dlls / vbscript / parser.y
blob3f86b041f9680e7262a444ffab8c46f82875df33
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*,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*);
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*);
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);
74 static statement_t *link_statements(statement_t*,statement_t*);
76 static const WCHAR propertyW[] = {'p','r','o','p','e','r','t','y',0};
78 #define STORAGE_IS_PRIVATE 1
79 #define STORAGE_IS_DEFAULT 2
81 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
85 %pure_parser
86 %start Program
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;
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
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
143 Program
144 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
146 OptionExplicit_opt
147 : /* empty */ { $$ = FALSE; }
148 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
150 SourceElements
151 : /* empty */
152 | SourceElements StatementNl { source_add_statement(ctx, $2); }
153 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
155 StatementsNl_opt
156 : /* empty */ { $$ = NULL; }
157 | StatementsNl { $$ = $1; }
159 StatementsNl
160 : StatementNl { $$ = $1; }
161 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
163 StatementNl
164 : Statement tNL { $$ = $1; }
166 Statement
167 : ':' { $$ = NULL; }
168 | ':' Statement { $$ = $2; }
169 | SimpleStatement { $$ = $1; }
170 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
171 | SimpleStatement ':' { $$ = $1; }
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 | tDO tNL StatementsNl_opt tLOOP { $$ = new_while_statement(ctx, STAT_DOWHILE, NULL, $3); CHECK_ERROR; }
189 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
190 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0); CHECK_ERROR; }
191 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
192 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
193 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
194 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
195 | tSET MemberExpression Arguments_opt '=' Expression
196 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
197 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
198 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
199 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
200 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
201 | tFOR Identifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
202 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
203 | tFOR tEACH Identifier tIN Expression tNL StatementsNl_opt tNEXT
204 { $$ = new_foreach_statement(ctx, $3, $5, $7); }
205 | tSELECT tCASE Expression tNL CaseClausules tEND tSELECT
206 { $$ = new_select_statement(ctx, $3, $5); }
208 MemberExpression
209 : Identifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
210 | CallExpression '.' Identifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
212 DimDeclList /* FIXME: Support arrays */
213 : Identifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
214 | Identifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
216 ConstDeclList
217 : ConstDecl { $$ = $1; }
218 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
220 ConstDecl
221 : Identifier '=' LiteralExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
223 DoType
224 : tWHILE { $$ = TRUE; }
225 | tUNTIL { $$ = FALSE; }
227 Step_opt
228 : /* empty */ { $$ = NULL;}
229 | tSTEP Expression { $$ = $2; }
231 IfStatement
232 : tIF Expression tTHEN tNL StatementsNl_opt ElseIfs_opt Else_opt tEND tIF
233 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
234 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
235 | tIF Expression tTHEN Statement tELSE Statement EndIf_opt
236 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
238 EndIf_opt
239 : /* empty */
240 | tEND tIF
242 ElseIfs_opt
243 : /* empty */ { $$ = NULL; }
244 | ElseIfs { $$ = $1; }
246 ElseIfs
247 : ElseIf { $$ = $1; }
248 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
250 ElseIf
251 : tELSEIF Expression tTHEN tNL StatementsNl_opt
252 { $$ = new_elseif_decl(ctx, $2, $5); }
254 Else_opt
255 : /* empty */ { $$ = NULL; }
256 | tELSE tNL StatementsNl_opt { $$ = $3; }
258 CaseClausules
259 : /* empty */ { $$ = NULL; }
260 | tCASE tELSE tNL StatementsNl { $$ = new_case_clausule(ctx, NULL, $4, NULL); }
261 | tCASE ExpressionList tNL StatementsNl_opt CaseClausules
262 { $$ = new_case_clausule(ctx, $2, $4, $5); }
264 Arguments_opt
265 : EmptyBrackets_opt { $$ = NULL; }
266 | '(' ExpressionList ')' { $$ = $2; }
268 ArgumentList_opt
269 : EmptyBrackets_opt { $$ = NULL; }
270 | ExpressionList { $$ = $1; }
272 EmptyBrackets_opt
273 : /* empty */
274 | tEMPTYBRACKETS
276 ExpressionList
277 : Expression { $$ = $1; }
278 | Expression ',' ExpressionList { $1->next = $3; $$ = $1; }
280 Expression
281 : EqvExpression { $$ = $1; }
282 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
284 EqvExpression
285 : XorExpression { $$ = $1; }
286 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
288 XorExpression
289 : OrExpression { $$ = $1; }
290 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
292 OrExpression
293 : AndExpression { $$ = $1; }
294 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
296 AndExpression
297 : NotExpression { $$ = $1; }
298 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
300 NotExpression
301 : EqualityExpression { $$ = $1; }
302 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
304 EqualityExpression
305 : ConcatExpression { $$ = $1; }
306 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
307 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
308 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
309 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
310 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
311 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
312 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
314 ConcatExpression
315 : AdditiveExpression { $$ = $1; }
316 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
318 AdditiveExpression
319 : ModExpression { $$ = $1; }
320 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
321 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
323 ModExpression
324 : IntdivExpression { $$ = $1; }
325 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
327 IntdivExpression
328 : MultiplicativeExpression { $$ = $1; }
329 | IntdivExpression '\\' MultiplicativeExpression
330 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
332 MultiplicativeExpression
333 : ExpExpression { $$ = $1; }
334 | MultiplicativeExpression '*' ExpExpression
335 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
336 | MultiplicativeExpression '/' ExpExpression
337 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
339 ExpExpression
340 : UnaryExpression { $$ = $1; }
341 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
343 UnaryExpression
344 : LiteralExpression { $$ = $1; }
345 | CallExpression { $$ = $1; }
346 | tNEW Identifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
347 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
349 CallExpression
350 : PrimaryExpression { $$ = $1; }
351 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
353 LiteralExpression
354 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
355 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
356 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
357 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
358 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
359 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
360 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
361 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
362 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
363 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
365 PrimaryExpression
366 : '(' Expression ')' { $$ = new_unary_expression(ctx, EXPR_BRACKETS, $2); }
367 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
369 ClassDeclaration
370 : tCLASS Identifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
372 ClassBody
373 : /* empty */ { $$ = new_class_decl(ctx); }
374 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
375 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
376 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
378 PropertyDecl
379 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
380 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
381 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
382 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
383 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
384 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
386 FunctionDecl
387 : Storage_opt tSUB Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
388 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
389 | Storage_opt tFUNCTION Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
390 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
392 Storage_opt
393 : /* empty*/ { $$ = 0; }
394 | Storage { $$ = $1; }
396 Storage
397 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
398 | tPUBLIC { $$ = 0; }
399 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
401 ArgumentsDecl_opt
402 : EmptyBrackets_opt { $$ = NULL; }
403 | '(' ArgumentDeclList ')' { $$ = $2; }
405 ArgumentDeclList
406 : ArgumentDecl { $$ = $1; }
407 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
409 ArgumentDecl
410 : Identifier { $$ = new_argument_decl(ctx, $1, TRUE); }
411 | tBYREF Identifier { $$ = new_argument_decl(ctx, $2, TRUE); }
412 | tBYVAL Identifier { $$ = new_argument_decl(ctx, $2, FALSE); }
414 /* 'property' may be both keyword and identifier, depending on context */
415 Identifier
416 : tIdentifier { $$ = $1; }
417 | tPROPERTY { $$ = propertyW; }
420 static int parser_error(const char *str)
422 return 0;
425 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
427 if(!stat)
428 return;
430 if(ctx->stats) {
431 ctx->stats_tail->next = stat;
432 ctx->stats_tail = stat;
433 }else {
434 ctx->stats = ctx->stats_tail = stat;
438 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
440 class_decl->next = ctx->class_decls;
441 ctx->class_decls = class_decl;
444 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
446 ctx->parse_complete = TRUE;
447 ctx->option_explicit = option_explicit;
450 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
452 expression_t *expr;
454 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
455 if(expr) {
456 expr->type = type;
457 expr->next = NULL;
460 return expr;
463 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
465 bool_expression_t *expr;
467 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
468 if(!expr)
469 return NULL;
471 expr->value = value;
472 return &expr->expr;
475 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
477 string_expression_t *expr;
479 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
480 if(!expr)
481 return NULL;
483 expr->value = value;
484 return &expr->expr;
487 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
489 int_expression_t *expr;
491 expr = new_expression(ctx, type, sizeof(*expr));
492 if(!expr)
493 return NULL;
495 expr->value = value;
496 return &expr->expr;
499 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
501 double_expression_t *expr;
503 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
504 if(!expr)
505 return NULL;
507 expr->value = value;
508 return &expr->expr;
511 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
513 unary_expression_t *expr;
515 expr = new_expression(ctx, type, sizeof(*expr));
516 if(!expr)
517 return NULL;
519 expr->subexpr = subexpr;
520 return &expr->expr;
523 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
525 binary_expression_t *expr;
527 expr = new_expression(ctx, type, sizeof(*expr));
528 if(!expr)
529 return NULL;
531 expr->left = left;
532 expr->right = right;
533 return &expr->expr;
536 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
538 member_expression_t *expr;
540 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
541 if(!expr)
542 return NULL;
544 expr->obj_expr = obj_expr;
545 expr->identifier = identifier;
546 expr->args = NULL;
547 return expr;
550 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
552 string_expression_t *expr;
554 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
555 if(!expr)
556 return NULL;
558 expr->value = identifier;
559 return &expr->expr;
562 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
564 statement_t *stat;
566 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
567 if(stat) {
568 stat->type = type;
569 stat->next = NULL;
572 return stat;
575 static statement_t *new_call_statement(parser_ctx_t *ctx, BOOL is_strict, member_expression_t *expr)
577 call_statement_t *stat;
579 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
580 if(!stat)
581 return NULL;
583 stat->expr = expr;
584 stat->is_strict = is_strict;
585 return &stat->stat;
588 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
590 assign_statement_t *stat;
592 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
593 if(!stat)
594 return NULL;
596 stat->member_expr = left;
597 stat->value_expr = right;
598 return &stat->stat;
601 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
603 assign_statement_t *stat;
605 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
606 if(!stat)
607 return NULL;
609 stat->member_expr = left;
610 stat->value_expr = right;
611 return &stat->stat;
614 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
616 dim_decl_t *decl;
618 decl = parser_alloc(ctx, sizeof(*decl));
619 if(!decl)
620 return NULL;
622 decl->name = name;
623 decl->next = next;
624 return decl;
627 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
629 dim_statement_t *stat;
631 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
632 if(!stat)
633 return NULL;
635 stat->dim_decls = decls;
636 return &stat->stat;
639 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
641 elseif_decl_t *decl;
643 decl = parser_alloc(ctx, sizeof(*decl));
644 if(!decl)
645 return NULL;
647 decl->expr = expr;
648 decl->stat = stat;
649 decl->next = NULL;
650 return decl;
653 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
655 while_statement_t *stat;
657 stat = new_statement(ctx, type, sizeof(*stat));
658 if(!stat)
659 return NULL;
661 stat->expr = expr;
662 stat->body = body;
663 return &stat->stat;
666 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
667 expression_t *to_expr, expression_t *step_expr, statement_t *body)
669 forto_statement_t *stat;
671 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
672 if(!stat)
673 return NULL;
675 stat->identifier = identifier;
676 stat->from_expr = from_expr;
677 stat->to_expr = to_expr;
678 stat->step_expr = step_expr;
679 stat->body = body;
680 return &stat->stat;
683 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
684 statement_t *body)
686 foreach_statement_t *stat;
688 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
689 if(!stat)
690 return NULL;
692 stat->identifier = identifier;
693 stat->group_expr = group_expr;
694 stat->body = body;
695 return &stat->stat;
698 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
699 statement_t *else_stat)
701 if_statement_t *stat;
703 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
704 if(!stat)
705 return NULL;
707 stat->expr = expr;
708 stat->if_stat = if_stat;
709 stat->elseifs = elseif_decl;
710 stat->else_stat = else_stat;
711 return &stat->stat;
714 static statement_t *new_select_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_clausules)
716 select_statement_t *stat;
718 stat = new_statement(ctx, STAT_SELECT, sizeof(*stat));
719 if(!stat)
720 return NULL;
722 stat->expr = expr;
723 stat->case_clausules = case_clausules;
724 return &stat->stat;
727 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_t *stat, case_clausule_t *next)
729 case_clausule_t *ret;
731 ret = parser_alloc(ctx, sizeof(*ret));
732 if(!ret)
733 return NULL;
735 ret->expr = expr;
736 ret->stat = stat;
737 ret->next = next;
738 return ret;
741 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
743 onerror_statement_t *stat;
745 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
746 if(!stat)
747 return NULL;
749 stat->resume_next = resume_next;
750 return &stat->stat;
753 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
755 arg_decl_t *arg_decl;
757 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
758 if(!arg_decl)
759 return NULL;
761 arg_decl->name = name;
762 arg_decl->by_ref = by_ref;
763 arg_decl->next = NULL;
764 return arg_decl;
767 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
768 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
770 function_decl_t *decl;
772 if(storage_flags & STORAGE_IS_DEFAULT) {
773 if(type == FUNC_PROPGET) {
774 type = FUNC_DEFGET;
775 }else {
776 FIXME("Invalid default property\n");
777 ctx->hres = E_FAIL;
778 return NULL;
782 decl = parser_alloc(ctx, sizeof(*decl));
783 if(!decl)
784 return NULL;
786 decl->name = name;
787 decl->type = type;
788 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
789 decl->args = arg_decl;
790 decl->body = body;
791 decl->next = NULL;
792 decl->next_prop_func = NULL;
793 return decl;
796 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
798 function_statement_t *stat;
800 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
801 if(!stat)
802 return NULL;
804 stat->func_decl = decl;
805 return &stat->stat;
808 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
810 class_decl_t *class_decl;
812 class_decl = parser_alloc(ctx, sizeof(*class_decl));
813 if(!class_decl)
814 return NULL;
816 class_decl->funcs = NULL;
817 class_decl->props = NULL;
818 class_decl->next = NULL;
819 return class_decl;
822 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
824 function_decl_t *iter;
826 for(iter = class_decl->funcs; iter; iter = iter->next) {
827 if(!strcmpiW(iter->name, decl->name)) {
828 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
829 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
830 ctx->hres = E_FAIL;
831 return NULL;
834 while(1) {
835 if(iter->type == decl->type) {
836 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
837 ctx->hres = E_FAIL;
838 return NULL;
840 if(!iter->next_prop_func)
841 break;
842 iter = iter->next_prop_func;
845 iter->next_prop_func = decl;
846 return class_decl;
850 decl->next = class_decl->funcs;
851 class_decl->funcs = decl;
852 return class_decl;
855 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
857 class_prop_decl_t *prop;
859 if(storage_flags & STORAGE_IS_DEFAULT) {
860 FIXME("variant prop van't be default value\n");
861 ctx->hres = E_FAIL;
862 return NULL;
865 prop = parser_alloc(ctx, sizeof(*prop));
866 if(!prop)
867 return NULL;
869 prop->name = identifier;
870 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
871 prop->next = class_decl->props;
872 class_decl->props = prop;
873 return class_decl;
876 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
878 const_decl_t *decl;
880 decl = parser_alloc(ctx, sizeof(*decl));
881 if(!decl)
882 return NULL;
884 decl->name = name;
885 decl->value_expr = expr;
886 decl->next = NULL;
887 return decl;
890 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
892 const_statement_t *stat;
894 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
895 if(!stat)
896 return NULL;
898 stat->decls = decls;
899 return &stat->stat;
902 static statement_t *link_statements(statement_t *head, statement_t *tail)
904 statement_t *iter;
906 for(iter = head; iter->next; iter = iter->next);
907 iter->next = tail;
909 return head;
912 void *parser_alloc(parser_ctx_t *ctx, size_t size)
914 void *ret;
916 ret = vbsheap_alloc(&ctx->heap, size);
917 if(!ret)
918 ctx->hres = E_OUTOFMEMORY;
919 return ret;
922 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
924 ctx->code = ctx->ptr = code;
925 ctx->end = ctx->code + strlenW(ctx->code);
927 vbsheap_init(&ctx->heap);
929 ctx->parse_complete = FALSE;
930 ctx->hres = S_OK;
932 ctx->last_token = tNL;
933 ctx->last_nl = 0;
934 ctx->stats = ctx->stats_tail = NULL;
935 ctx->class_decls = NULL;
936 ctx->option_explicit = FALSE;
938 parser_parse(ctx);
940 if(FAILED(ctx->hres))
941 return ctx->hres;
942 if(!ctx->parse_complete) {
943 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
944 return E_FAIL;
947 return S_OK;
950 void parser_release(parser_ctx_t *ctx)
952 vbsheap_free(&ctx->heap);