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*);
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_dim_statement(parser_ctx_t*,dim_decl_t*);
53 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
54 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
56 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
57 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
58 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,arg_decl_t*,statement_t*);
59 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
60 static class_decl_t *new_class_decl(parser_ctx_t*);
62 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
71 statement_t *statement;
72 expression_t *expression;
73 member_expression_t *member;
74 elseif_decl_t *elseif;
76 function_decl_t *func_decl;
78 class_decl_t *class_decl;
84 %token tEOF tNL tREM tEMPTYBRACKETS
86 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
87 %token tIS tLTEQ tGTEQ tMOD
88 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
89 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
90 %token tWHILE tWEND tDO tLOOP tUNTIL
92 %token tOPTION tEXPLICIT
94 %token tNOTHING tEMPTY tNULL
95 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
96 %token tERROR tNEXT tON tRESUME tGOTO
97 %token <string> tIdentifier tString
98 %token <lng> tLong tShort
101 %type <statement> Statement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
102 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
103 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
104 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
105 %type <member> MemberExpression
106 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
107 %type <bool> OptionExplicit_opt
108 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
109 %type <func_decl> FunctionDecl
110 %type <elseif> ElseIfs_opt ElseIfs ElseIf
111 %type <class_decl> ClassDeclaration ClassBody
112 %type <dim_decl> DimDeclList
117 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
120 : /* empty */ { $$ = FALSE; }
121 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
125 | SourceElements StatementNl { source_add_statement(ctx, $2); }
126 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
129 : /* empty */ { $$ = NULL; }
130 | StatementsNl { $$ = $1; }
133 : StatementNl { $$ = $1; }
134 | StatementNl StatementsNl { $1->next = $2; $$ = $1; }
137 : Statement tNL { $$ = $1; }
140 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
141 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
142 | MemberExpression Arguments_opt '=' Expression
143 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
144 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
145 | IfStatement { $$ = $1; }
146 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
147 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
148 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
151 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
152 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
154 DimDeclList /* FIXME: Support arrays */
155 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
156 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
159 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
160 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
161 /* FIXME: short if statement */
164 : /* empty */ { $$ = NULL; }
165 | ElseIfs { $$ = $1; }
168 : ElseIf { $$ = $1; }
169 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
172 : tELSEIF Expression tTHEN tNL StatementsNl
173 { $$ = new_elseif_decl(ctx, $2, $5); }
176 : /* empty */ { $$ = NULL; }
177 | tELSE tNL StatementsNl { $$ = $3; }
180 : EmptyBrackets_opt { $$ = NULL; }
181 | '(' ArgumentList ')' { $$ = $2; }
184 : EmptyBrackets_opt { $$ = NULL; }
185 | ArgumentList { $$ = $1; }
188 : Expression { $$ = $1; }
189 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
196 : EqvExpression { $$ = $1; }
197 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
200 : XorExpression { $$ = $1; }
201 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
204 : OrExpression { $$ = $1; }
205 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
208 : AndExpression { $$ = $1; }
209 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
212 : NotExpression { $$ = $1; }
213 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
216 : EqualityExpression { $$ = $1; }
217 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
220 : ConcatExpression { $$ = $1; }
221 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
222 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
225 : AdditiveExpression { $$ = $1; }
226 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
229 : ModExpression { $$ = $1; }
230 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
231 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
234 : IntdivExpression { $$ = $1; }
235 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
238 : MultiplicativeExpression { $$ = $1; }
239 | IntdivExpression '\\' MultiplicativeExpression
240 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
242 MultiplicativeExpression
243 : ExpExpression { $$ = $1; }
244 | MultiplicativeExpression '*' ExpExpression
245 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
246 | MultiplicativeExpression '/' ExpExpression
247 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
250 : UnaryExpression { $$ = $1; }
251 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
254 : LiteralExpression { $$ = $1; }
255 | CallExpression { $$ = $1; }
256 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
259 : PrimaryExpression { $$ = $1; }
260 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
263 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
264 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
265 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
266 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
267 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
268 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
269 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
270 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
271 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
274 : '(' Expression ')' { $$ = $2; }
277 : tCLASS tIdentifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
280 : /* empty */ { $$ = new_class_decl(ctx); }
283 : /* Storage_opt */ tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
284 { $$ = new_function_decl(ctx, $2, FUNC_SUB, $3, $5); CHECK_ERROR; }
285 | /* Storage_opt */ tFUNCTION tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
286 { $$ = new_function_decl(ctx, $2, FUNC_FUNCTION, $3, $5); CHECK_ERROR; }
289 : EmptyBrackets_opt { $$ = NULL; }
290 | '(' ArgumentDeclList ')' { $$ = $2; }
293 : ArgumentDecl { $$ = $1; }
294 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
297 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
298 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
299 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
303 static int parser_error(const char *str)
308 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
311 ctx->stats_tail->next = stat;
312 ctx->stats_tail = stat;
314 ctx->stats = ctx->stats_tail = stat;
318 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
320 class_decl->next = ctx->class_decls;
321 ctx->class_decls = class_decl;
324 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
326 ctx->parse_complete = TRUE;
327 ctx->option_explicit = option_explicit;
330 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
334 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
343 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
345 bool_expression_t *expr;
347 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
355 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
357 string_expression_t *expr;
359 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
367 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
369 int_expression_t *expr;
371 expr = new_expression(ctx, type, sizeof(*expr));
379 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
381 double_expression_t *expr;
383 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
391 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
393 unary_expression_t *expr;
395 expr = new_expression(ctx, type, sizeof(*expr));
399 expr->subexpr = subexpr;
403 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
405 binary_expression_t *expr;
407 expr = new_expression(ctx, type, sizeof(*expr));
416 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
418 member_expression_t *expr;
420 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
424 expr->obj_expr = obj_expr;
425 expr->identifier = identifier;
430 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
434 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
443 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
445 call_statement_t *stat;
447 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
455 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
457 assign_statement_t *stat;
459 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
463 stat->member_expr = left;
464 stat->value_expr = right;
468 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
472 decl = parser_alloc(ctx, sizeof(*decl));
481 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
483 dim_statement_t *stat;
485 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
489 stat->dim_decls = decls;
493 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
497 decl = parser_alloc(ctx, sizeof(*decl));
507 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
508 statement_t *else_stat)
510 if_statement_t *stat;
512 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
517 stat->if_stat = if_stat;
518 stat->elseifs = elseif_decl;
519 stat->else_stat = else_stat;
523 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
525 arg_decl_t *arg_decl;
527 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
531 arg_decl->name = name;
532 arg_decl->by_ref = by_ref;
533 arg_decl->next = NULL;
537 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
538 arg_decl_t *arg_decl, statement_t *body)
540 function_decl_t *decl;
542 decl = parser_alloc(ctx, sizeof(*decl));
548 decl->args = arg_decl;
553 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
555 function_statement_t *stat;
557 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
561 stat->func_decl = decl;
565 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
567 class_decl_t *class_decl;
569 class_decl = parser_alloc(ctx, sizeof(*class_decl));
573 class_decl->next = NULL;
577 void *parser_alloc(parser_ctx_t *ctx, size_t size)
581 ret = vbsheap_alloc(&ctx->heap, size);
583 ctx->hres = E_OUTOFMEMORY;
587 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
589 ctx->code = ctx->ptr = code;
590 ctx->end = ctx->code + strlenW(ctx->code);
592 vbsheap_init(&ctx->heap);
594 ctx->parse_complete = FALSE;
597 ctx->last_token = tNL;
599 ctx->stats = ctx->stats_tail = NULL;
600 ctx->class_decls = NULL;
601 ctx->option_explicit = FALSE;
605 if(FAILED(ctx->hres))
607 if(!ctx->parse_complete) {
608 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
615 void parser_release(parser_ctx_t *ctx)
617 vbsheap_free(&ctx->heap);