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*);
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*);
46 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
48 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
49 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
50 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
51 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
52 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
54 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
55 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
56 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,arg_decl_t*,statement_t*);
57 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
59 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
68 statement_t *statement;
69 expression_t *expression;
70 member_expression_t *member;
71 elseif_decl_t *elseif;
73 function_decl_t *func_decl;
80 %token tEOF tNL tREM tEMPTYBRACKETS
82 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
83 %token tIS tLTEQ tGTEQ tMOD
84 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
85 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
86 %token tWHILE tWEND tDO tLOOP tUNTIL
88 %token tOPTION tEXPLICIT
90 %token tNOTHING tEMPTY tNULL
91 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
92 %token tERROR tNEXT tON tRESUME tGOTO
93 %token <string> tIdentifier tString
94 %token <lng> tLong tShort
97 %type <statement> Statement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
98 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
99 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
100 %type <expression> NotExpression UnaryExpression
101 %type <member> MemberExpression
102 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
103 %type <bool> OptionExplicit_opt
104 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
105 %type <func_decl> FunctionDecl
106 %type <elseif> ElseIfs_opt ElseIfs ElseIf
107 %type <dim_decl> DimDeclList
112 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
115 : /* empty */ { $$ = FALSE; }
116 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
120 | SourceElements StatementNl { source_add_statement(ctx, $2); }
123 : /* empty */ { $$ = NULL; }
124 | StatementsNl { $$ = $1; }
127 : StatementNl { $$ = $1; }
128 | StatementNl StatementsNl { $1->next = $2; $$ = $1; }
131 : Statement tNL { $$ = $1; }
134 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
135 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
136 | MemberExpression Arguments_opt '=' Expression
137 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
138 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
139 | IfStatement { $$ = $1; }
140 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
143 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
144 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
146 DimDeclList /* FIXME: Support arrays */
147 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
148 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
151 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
152 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
153 /* FIXME: short if statement */
156 : /* empty */ { $$ = NULL; }
157 | ElseIfs { $$ = $1; }
160 : ElseIf { $$ = $1; }
161 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
164 : tELSEIF Expression tTHEN tNL StatementsNl
165 { $$ = new_elseif_decl(ctx, $2, $5); }
168 : /* empty */ { $$ = NULL; }
169 | tELSE tNL StatementsNl { $$ = $3; }
172 : EmptyBrackets_opt { $$ = NULL; }
173 | '(' ArgumentList ')' { $$ = $2; }
176 : EmptyBrackets_opt { $$ = NULL; }
177 | ArgumentList { $$ = $1; }
180 : Expression { $$ = $1; }
181 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
188 : NotExpression { $$ = $1; }
191 : EqualityExpression { $$ = $1; }
192 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
195 : ConcatExpression { $$ = $1; }
196 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
197 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
200 : AdditiveExpression { $$ = $1; }
201 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
204 : ModExpression { $$ = $1; }
205 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
206 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
209 : IntdivExpression { $$ = $1; }
210 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
213 : MultiplicativeExpression { $$ = $1; }
214 | IntdivExpression '\\' MultiplicativeExpression
215 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
217 MultiplicativeExpression
218 : ExpExpression { $$ = $1; }
219 | MultiplicativeExpression '*' ExpExpression
220 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
221 | MultiplicativeExpression '/' ExpExpression
222 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
225 : UnaryExpression { $$ = $1; }
226 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
229 : LiteralExpression { $$ = $1; }
230 | CallExpression { $$ = $1; }
231 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
234 : PrimaryExpression { $$ = $1; }
235 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
238 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
239 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
240 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
241 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
242 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
243 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
244 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
245 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
246 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
249 : '(' Expression ')' { $$ = $2; }
252 : /* Storage_opt */ tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
253 { $$ = new_function_decl(ctx, $2, FUNC_SUB, $3, $5); CHECK_ERROR; }
256 : EmptyBrackets_opt { $$ = NULL; }
257 | '(' ArgumentDeclList ')' { $$ = $2; }
260 : ArgumentDecl { $$ = $1; }
261 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
264 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
265 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
266 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
270 static int parser_error(const char *str)
275 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
278 ctx->stats_tail->next = stat;
279 ctx->stats_tail = stat;
281 ctx->stats = ctx->stats_tail = stat;
285 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
287 ctx->parse_complete = TRUE;
288 ctx->option_explicit = option_explicit;
291 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
295 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
304 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
306 bool_expression_t *expr;
308 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
316 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
318 string_expression_t *expr;
320 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
328 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
330 int_expression_t *expr;
332 expr = new_expression(ctx, type, sizeof(*expr));
340 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
342 double_expression_t *expr;
344 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
352 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
354 unary_expression_t *expr;
356 expr = new_expression(ctx, type, sizeof(*expr));
360 expr->subexpr = subexpr;
364 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
366 binary_expression_t *expr;
368 expr = new_expression(ctx, type, sizeof(*expr));
377 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
379 member_expression_t *expr;
381 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
385 expr->obj_expr = obj_expr;
386 expr->identifier = identifier;
391 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
395 stat = parser_alloc(ctx, size);
404 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
406 call_statement_t *stat;
408 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
416 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
418 assign_statement_t *stat;
420 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
424 stat->member_expr = left;
425 stat->value_expr = right;
429 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
433 decl = parser_alloc(ctx, sizeof(*decl));
442 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
444 dim_statement_t *stat;
446 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
450 stat->dim_decls = decls;
454 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
458 decl = parser_alloc(ctx, sizeof(*decl));
468 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
469 statement_t *else_stat)
471 if_statement_t *stat;
473 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
478 stat->if_stat = if_stat;
479 stat->elseifs = elseif_decl;
480 stat->else_stat = else_stat;
484 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
486 arg_decl_t *arg_decl;
488 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
492 arg_decl->name = name;
493 arg_decl->by_ref = by_ref;
494 arg_decl->next = NULL;
498 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
499 arg_decl_t *arg_decl, statement_t *body)
501 function_decl_t *decl;
503 decl = parser_alloc(ctx, sizeof(*decl));
509 decl->args = arg_decl;
514 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
516 function_statement_t *stat;
518 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
522 stat->func_decl = decl;
526 void *parser_alloc(parser_ctx_t *ctx, size_t size)
530 ret = vbsheap_alloc(&ctx->heap, size);
532 ctx->hres = E_OUTOFMEMORY;
536 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
538 ctx->code = ctx->ptr = code;
539 ctx->end = ctx->code + strlenW(ctx->code);
541 vbsheap_init(&ctx->heap);
543 ctx->parse_complete = FALSE;
546 ctx->last_token = tNL;
548 ctx->stats = ctx->stats_tail = NULL;
549 ctx->option_explicit = FALSE;
553 if(FAILED(ctx->hres))
555 if(!ctx->parse_complete) {
556 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
563 void parser_release(parser_ctx_t *ctx)
565 vbsheap_free(&ctx->heap);