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 void *new_statement(parser_ctx_t*,statement_type_t,size_t);
49 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
50 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
51 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
52 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
53 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
55 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
56 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
57 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,arg_decl_t*,statement_t*);
58 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
60 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
69 statement_t *statement;
70 expression_t *expression;
71 member_expression_t *member;
72 elseif_decl_t *elseif;
74 function_decl_t *func_decl;
81 %token tEOF tNL tREM tEMPTYBRACKETS
83 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
84 %token tIS tLTEQ tGTEQ tMOD
85 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
86 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
87 %token tWHILE tWEND tDO tLOOP tUNTIL
89 %token tOPTION tEXPLICIT
91 %token tNOTHING tEMPTY tNULL
92 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
93 %token tERROR tNEXT tON tRESUME tGOTO
94 %token <string> tIdentifier tString
95 %token <lng> tLong tShort
98 %type <statement> Statement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
99 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
100 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
101 %type <expression> NotExpression UnaryExpression
102 %type <member> MemberExpression
103 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
104 %type <bool> OptionExplicit_opt
105 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
106 %type <func_decl> FunctionDecl
107 %type <elseif> ElseIfs_opt ElseIfs ElseIf
108 %type <dim_decl> DimDeclList
113 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
116 : /* empty */ { $$ = FALSE; }
117 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
121 | SourceElements StatementNl { source_add_statement(ctx, $2); }
124 : /* empty */ { $$ = NULL; }
125 | StatementsNl { $$ = $1; }
128 : StatementNl { $$ = $1; }
129 | StatementNl StatementsNl { $1->next = $2; $$ = $1; }
132 : Statement tNL { $$ = $1; }
135 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
136 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
137 | MemberExpression Arguments_opt '=' Expression
138 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
139 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
140 | IfStatement { $$ = $1; }
141 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
142 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
145 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
146 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
148 DimDeclList /* FIXME: Support arrays */
149 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
150 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
153 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
154 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
155 /* FIXME: short if statement */
158 : /* empty */ { $$ = NULL; }
159 | ElseIfs { $$ = $1; }
162 : ElseIf { $$ = $1; }
163 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
166 : tELSEIF Expression tTHEN tNL StatementsNl
167 { $$ = new_elseif_decl(ctx, $2, $5); }
170 : /* empty */ { $$ = NULL; }
171 | tELSE tNL StatementsNl { $$ = $3; }
174 : EmptyBrackets_opt { $$ = NULL; }
175 | '(' ArgumentList ')' { $$ = $2; }
178 : EmptyBrackets_opt { $$ = NULL; }
179 | ArgumentList { $$ = $1; }
182 : Expression { $$ = $1; }
183 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
190 : NotExpression { $$ = $1; }
193 : EqualityExpression { $$ = $1; }
194 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
197 : ConcatExpression { $$ = $1; }
198 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
199 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
202 : AdditiveExpression { $$ = $1; }
203 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
206 : ModExpression { $$ = $1; }
207 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
208 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
211 : IntdivExpression { $$ = $1; }
212 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
215 : MultiplicativeExpression { $$ = $1; }
216 | IntdivExpression '\\' MultiplicativeExpression
217 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
219 MultiplicativeExpression
220 : ExpExpression { $$ = $1; }
221 | MultiplicativeExpression '*' ExpExpression
222 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
223 | MultiplicativeExpression '/' ExpExpression
224 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
227 : UnaryExpression { $$ = $1; }
228 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
231 : LiteralExpression { $$ = $1; }
232 | CallExpression { $$ = $1; }
233 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
236 : PrimaryExpression { $$ = $1; }
237 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
240 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
241 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
242 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
243 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
244 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
245 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
246 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
247 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
248 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
251 : '(' Expression ')' { $$ = $2; }
254 : /* Storage_opt */ tSUB tIdentifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
255 { $$ = new_function_decl(ctx, $2, FUNC_SUB, $3, $5); CHECK_ERROR; }
258 : EmptyBrackets_opt { $$ = NULL; }
259 | '(' ArgumentDeclList ')' { $$ = $2; }
262 : ArgumentDecl { $$ = $1; }
263 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
266 : tIdentifier { $$ = new_argument_decl(ctx, $1, TRUE); }
267 | tBYREF tIdentifier { $$ = new_argument_decl(ctx, $2, TRUE); }
268 | tBYVAL tIdentifier { $$ = new_argument_decl(ctx, $2, FALSE); }
272 static int parser_error(const char *str)
277 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
280 ctx->stats_tail->next = stat;
281 ctx->stats_tail = stat;
283 ctx->stats = ctx->stats_tail = stat;
287 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
289 ctx->parse_complete = TRUE;
290 ctx->option_explicit = option_explicit;
293 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
297 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
306 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
308 bool_expression_t *expr;
310 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
318 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
320 string_expression_t *expr;
322 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
330 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
332 int_expression_t *expr;
334 expr = new_expression(ctx, type, sizeof(*expr));
342 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
344 double_expression_t *expr;
346 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
354 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
356 unary_expression_t *expr;
358 expr = new_expression(ctx, type, sizeof(*expr));
362 expr->subexpr = subexpr;
366 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
368 binary_expression_t *expr;
370 expr = new_expression(ctx, type, sizeof(*expr));
379 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
381 member_expression_t *expr;
383 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
387 expr->obj_expr = obj_expr;
388 expr->identifier = identifier;
393 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
397 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
406 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
408 call_statement_t *stat;
410 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
418 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
420 assign_statement_t *stat;
422 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
426 stat->member_expr = left;
427 stat->value_expr = right;
431 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
435 decl = parser_alloc(ctx, sizeof(*decl));
444 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
446 dim_statement_t *stat;
448 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
452 stat->dim_decls = decls;
456 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
460 decl = parser_alloc(ctx, sizeof(*decl));
470 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
471 statement_t *else_stat)
473 if_statement_t *stat;
475 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
480 stat->if_stat = if_stat;
481 stat->elseifs = elseif_decl;
482 stat->else_stat = else_stat;
486 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
488 arg_decl_t *arg_decl;
490 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
494 arg_decl->name = name;
495 arg_decl->by_ref = by_ref;
496 arg_decl->next = NULL;
500 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
501 arg_decl_t *arg_decl, statement_t *body)
503 function_decl_t *decl;
505 decl = parser_alloc(ctx, sizeof(*decl));
511 decl->args = arg_decl;
516 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
518 function_statement_t *stat;
520 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
524 stat->func_decl = decl;
528 void *parser_alloc(parser_ctx_t *ctx, size_t size)
532 ret = vbsheap_alloc(&ctx->heap, size);
534 ctx->hres = E_OUTOFMEMORY;
538 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
540 ctx->code = ctx->ptr = code;
541 ctx->end = ctx->code + strlenW(ctx->code);
543 vbsheap_init(&ctx->heap);
545 ctx->parse_complete = FALSE;
548 ctx->last_token = tNL;
550 ctx->stats = ctx->stats_tail = NULL;
551 ctx->option_explicit = FALSE;
555 if(FAILED(ctx->hres))
557 if(!ctx->parse_complete) {
558 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
565 void parser_release(parser_ctx_t *ctx)
567 vbsheap_free(&ctx->heap);