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
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
29 #define YYLEX_PARAM ctx
30 #define YYPARSE_PARAM ctx
32 static int parser_error(const char*);
34 static void parse_complete(parser_ctx_t*,BOOL);
36 static void source_add_statement(parser_ctx_t*,statement_t*);
37 static void source_add_class(parser_ctx_t*,class_decl_t*);
39 static void *new_expression(parser_ctx_t*,expression_type_t,size_t);
40 static expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
41 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
42 static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
43 static expression_t *new_double_expression(parser_ctx_t*,double);
44 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
45 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
46 static expression_t *new_new_expression(parser_ctx_t*,const WCHAR*);
48 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
50 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
51 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
52 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
53 static statement_t *new_set_statement(parser_ctx_t*,member_expression_t*,expression_t*);
54 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
55 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
56 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
58 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
59 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
60 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
61 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
63 static class_decl_t *new_class_decl(parser_ctx_t*);
64 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
65 static class_decl_t *add_variant_prop(parser_ctx_t*,class_decl_t*,const WCHAR*,unsigned);
67 #define STORAGE_IS_PRIVATE 1
68 #define STORAGE_IS_DEFAULT 2
70 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
79 statement_t *statement;
80 expression_t *expression;
81 member_expression_t *member;
82 elseif_decl_t *elseif;
84 function_decl_t *func_decl;
86 class_decl_t *class_decl;
93 %token tEOF tNL tREM tEMPTYBRACKETS
95 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
96 %token tIS tLTEQ tGTEQ tMOD
97 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
98 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
99 %token tWHILE tWEND tDO tLOOP tUNTIL
101 %token tOPTION tEXPLICIT
103 %token tNOTHING tEMPTY tNULL
104 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
105 %token tERROR tNEXT tON tRESUME tGOTO
106 %token <string> tIdentifier tString
107 %token <lng> tLong tShort
110 %type <statement> Statement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
111 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
112 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
113 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
114 %type <member> MemberExpression
115 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
116 %type <bool> OptionExplicit_opt
117 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
118 %type <func_decl> FunctionDecl
119 %type <elseif> ElseIfs_opt ElseIfs ElseIf
120 %type <class_decl> ClassDeclaration ClassBody
121 %type <uint> Storage Storage_opt
122 %type <dim_decl> DimDeclList
127 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
130 : /* empty */ { $$ = FALSE; }
131 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
135 | SourceElements StatementNl { source_add_statement(ctx, $2); }
136 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
139 : /* empty */ { $$ = NULL; }
140 | StatementsNl { $$ = $1; }
143 : StatementNl { $$ = $1; }
144 | StatementNl StatementsNl { $1->next = $2; $$ = $1; }
147 : Statement tNL { $$ = $1; }
150 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
151 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
152 | MemberExpression Arguments_opt '=' Expression
153 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
154 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
155 | IfStatement { $$ = $1; }
156 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
157 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
158 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
159 | tSET MemberExpression Arguments_opt '=' Expression
160 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
161 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
164 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
165 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
167 DimDeclList /* FIXME: Support arrays */
168 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
169 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
172 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
173 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
174 /* FIXME: short if statement */
177 : /* empty */ { $$ = NULL; }
178 | ElseIfs { $$ = $1; }
181 : ElseIf { $$ = $1; }
182 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
185 : tELSEIF Expression tTHEN tNL StatementsNl
186 { $$ = new_elseif_decl(ctx, $2, $5); }
189 : /* empty */ { $$ = NULL; }
190 | tELSE tNL StatementsNl { $$ = $3; }
193 : EmptyBrackets_opt { $$ = NULL; }
194 | '(' ArgumentList ')' { $$ = $2; }
197 : EmptyBrackets_opt { $$ = NULL; }
198 | ArgumentList { $$ = $1; }
201 : Expression { $$ = $1; }
202 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
209 : EqvExpression { $$ = $1; }
210 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
213 : XorExpression { $$ = $1; }
214 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
217 : OrExpression { $$ = $1; }
218 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
221 : AndExpression { $$ = $1; }
222 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
225 : NotExpression { $$ = $1; }
226 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
229 : EqualityExpression { $$ = $1; }
230 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
233 : ConcatExpression { $$ = $1; }
234 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
235 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
238 : AdditiveExpression { $$ = $1; }
239 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
242 : ModExpression { $$ = $1; }
243 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
244 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
247 : IntdivExpression { $$ = $1; }
248 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
251 : MultiplicativeExpression { $$ = $1; }
252 | IntdivExpression '\\' MultiplicativeExpression
253 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
255 MultiplicativeExpression
256 : ExpExpression { $$ = $1; }
257 | MultiplicativeExpression '*' ExpExpression
258 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
259 | MultiplicativeExpression '/' ExpExpression
260 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
263 : UnaryExpression { $$ = $1; }
264 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
267 : LiteralExpression { $$ = $1; }
268 | CallExpression { $$ = $1; }
269 | tNEW tIdentifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
270 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
273 : PrimaryExpression { $$ = $1; }
274 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
277 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
278 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
279 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
280 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
281 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
282 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
283 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
284 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
285 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
286 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
289 : '(' Expression ')' { $$ = $2; }
292 : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
295 : /* empty */ { $$ = new_class_decl(ctx); }
296 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
297 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
300 : Storage_opt tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
301 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
302 | Storage_opt tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
303 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
306 : /* empty*/ { $$ = 0; }
307 | Storage { $$ = $1; }
310 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
311 | tPUBLIC { $$ = 0; }
312 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
315 : EmptyBrackets_opt { $$ = NULL; }
316 | '(' ArgumentDeclList ')' { $$ = $2; }
319 : ArgumentDecl { $$ = $1; }
320 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
323 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
324 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
325 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
329 static int parser_error(const char *str)
334 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
337 ctx->stats_tail->next = stat;
338 ctx->stats_tail = stat;
340 ctx->stats = ctx->stats_tail = stat;
344 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
346 class_decl->next = ctx->class_decls;
347 ctx->class_decls = class_decl;
350 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
352 ctx->parse_complete = TRUE;
353 ctx->option_explicit = option_explicit;
356 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
360 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
369 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
371 bool_expression_t *expr;
373 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
381 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
383 string_expression_t *expr;
385 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
393 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
395 int_expression_t *expr;
397 expr = new_expression(ctx, type, sizeof(*expr));
405 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
407 double_expression_t *expr;
409 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
417 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
419 unary_expression_t *expr;
421 expr = new_expression(ctx, type, sizeof(*expr));
425 expr->subexpr = subexpr;
429 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
431 binary_expression_t *expr;
433 expr = new_expression(ctx, type, sizeof(*expr));
442 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
444 member_expression_t *expr;
446 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
450 expr->obj_expr = obj_expr;
451 expr->identifier = identifier;
456 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
458 string_expression_t *expr;
460 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
464 expr->value = identifier;
468 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
472 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
481 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
483 call_statement_t *stat;
485 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
493 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
495 assign_statement_t *stat;
497 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
501 stat->member_expr = left;
502 stat->value_expr = right;
506 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
508 assign_statement_t *stat;
510 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
514 stat->member_expr = left;
515 stat->value_expr = right;
519 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
523 decl = parser_alloc(ctx, sizeof(*decl));
532 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
534 dim_statement_t *stat;
536 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
540 stat->dim_decls = decls;
544 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
548 decl = parser_alloc(ctx, sizeof(*decl));
558 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
559 statement_t *else_stat)
561 if_statement_t *stat;
563 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
568 stat->if_stat = if_stat;
569 stat->elseifs = elseif_decl;
570 stat->else_stat = else_stat;
574 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
576 arg_decl_t *arg_decl;
578 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
582 arg_decl->name = name;
583 arg_decl->by_ref = by_ref;
584 arg_decl->next = NULL;
588 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
589 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
591 function_decl_t *decl;
593 if(storage_flags & STORAGE_IS_DEFAULT) {
594 FIXME("Function declared as default property\n");
599 decl = parser_alloc(ctx, sizeof(*decl));
605 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
606 decl->args = arg_decl;
612 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
614 function_statement_t *stat;
616 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
620 stat->func_decl = decl;
624 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
626 class_decl_t *class_decl;
628 class_decl = parser_alloc(ctx, sizeof(*class_decl));
632 class_decl->funcs = NULL;
633 class_decl->props = NULL;
634 class_decl->next = NULL;
638 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
640 function_decl_t *iter;
642 for(iter = class_decl->funcs; iter; iter = iter->next) {
643 if(!strcmpiW(iter->name, decl->name)) {
644 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
645 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
652 decl->next = class_decl->funcs;
653 class_decl->funcs = decl;
657 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
659 class_prop_decl_t *prop;
661 if(storage_flags & STORAGE_IS_DEFAULT) {
662 FIXME("variant prop van't be default value\n");
667 prop = parser_alloc(ctx, sizeof(*prop));
671 prop->name = identifier;
672 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
673 prop->next = class_decl->props;
674 class_decl->props = prop;
678 void *parser_alloc(parser_ctx_t *ctx, size_t size)
682 ret = vbsheap_alloc(&ctx->heap, size);
684 ctx->hres = E_OUTOFMEMORY;
688 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
690 ctx->code = ctx->ptr = code;
691 ctx->end = ctx->code + strlenW(ctx->code);
693 vbsheap_init(&ctx->heap);
695 ctx->parse_complete = FALSE;
698 ctx->last_token = tNL;
700 ctx->stats = ctx->stats_tail = NULL;
701 ctx->class_decls = NULL;
702 ctx->option_explicit = FALSE;
706 if(FAILED(ctx->hres))
708 if(!ctx->parse_complete) {
709 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
716 void parser_release(parser_ctx_t *ctx)
718 vbsheap_free(&ctx->heap);