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*);
53 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
54 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
56 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
65 statement_t *statement;
66 expression_t *expression;
67 member_expression_t *member;
68 elseif_decl_t *elseif;
75 %token tEOF tNL tREM tEMPTYBRACKETS
77 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
78 %token tIS tLTEQ tGTEQ tMOD
79 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
80 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
81 %token tWHILE tWEND tDO tLOOP tUNTIL
83 %token tOPTION tEXPLICIT
85 %token tNOTHING tEMPTY tNULL
86 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
87 %token tERROR tNEXT tON tRESUME tGOTO
88 %token <string> tIdentifier tString
89 %token <lng> tLong tShort
92 %type <statement> Statement StatementNl StatementsNl IfStatement Else_opt
93 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
94 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression
95 %type <expression> NotExpression UnaryExpression
96 %type <member> MemberExpression
97 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
98 %type <bool> OptionExplicit_opt
99 %type <elseif> ElseIfs_opt ElseIfs ElseIf
100 %type <dim_decl> DimDeclList
105 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
108 : /* empty */ { $$ = FALSE; }
109 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
113 | SourceElements StatementNl { source_add_statement(ctx, $2); }
116 : StatementNl { $$ = $1; }
117 | StatementNl StatementsNl { $1->next = $2; $$ = $1; }
120 : Statement tNL { $$ = $1; }
123 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
124 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
125 | MemberExpression Arguments_opt '=' Expression
126 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
127 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
128 | IfStatement { $$ = $1; }
131 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
132 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
134 DimDeclList /* FIXME: Support arrays */
135 : tIdentifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
136 | tIdentifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
139 : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
140 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
141 /* FIXME: short if statement */
144 : /* empty */ { $$ = NULL; }
145 | ElseIfs { $$ = $1; }
148 : ElseIf { $$ = $1; }
149 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
152 : tELSEIF Expression tTHEN tNL StatementsNl
153 { $$ = new_elseif_decl(ctx, $2, $5); }
156 : /* empty */ { $$ = NULL; }
157 | tELSE tNL StatementsNl { $$ = $3; }
160 : EmptyBrackets_opt { $$ = NULL; }
161 | '(' ArgumentList ')' { $$ = $2; }
164 : EmptyBrackets_opt { $$ = NULL; }
165 | ArgumentList { $$ = $1; }
168 : Expression { $$ = $1; }
169 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
176 : NotExpression { $$ = $1; }
179 : EqualityExpression { $$ = $1; }
180 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
183 : ConcatExpression { $$ = $1; }
184 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
185 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
188 : AdditiveExpression { $$ = $1; }
189 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
192 : ModExpression { $$ = $1; }
193 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
194 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
197 : IntdivExpression { $$ = $1; }
198 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
201 : MultiplicativeExpression { $$ = $1; }
202 | IntdivExpression '\\' MultiplicativeExpression
203 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
205 MultiplicativeExpression
206 : UnaryExpression /* FIXME */ { $$ = $1; }
209 : LiteralExpression { $$ = $1; }
210 | CallExpression { $$ = $1; }
211 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
214 : PrimaryExpression { $$ = $1; }
215 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
218 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
219 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
220 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
221 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
222 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
223 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
224 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
225 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
226 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
229 : '(' Expression ')' { $$ = $2; }
233 static int parser_error(const char *str)
238 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
241 ctx->stats_tail->next = stat;
242 ctx->stats_tail = stat;
244 ctx->stats = ctx->stats_tail = stat;
248 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
250 ctx->parse_complete = TRUE;
251 ctx->option_explicit = option_explicit;
254 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
258 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
267 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
269 bool_expression_t *expr;
271 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
279 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
281 string_expression_t *expr;
283 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
291 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
293 int_expression_t *expr;
295 expr = new_expression(ctx, type, sizeof(*expr));
303 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
305 double_expression_t *expr;
307 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
315 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
317 unary_expression_t *expr;
319 expr = new_expression(ctx, type, sizeof(*expr));
323 expr->subexpr = subexpr;
327 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
329 binary_expression_t *expr;
331 expr = new_expression(ctx, type, sizeof(*expr));
340 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
342 member_expression_t *expr;
344 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
348 expr->obj_expr = obj_expr;
349 expr->identifier = identifier;
354 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
358 stat = parser_alloc(ctx, size);
367 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
369 call_statement_t *stat;
371 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
379 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
381 assign_statement_t *stat;
383 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
387 stat->member_expr = left;
388 stat->value_expr = right;
392 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
396 decl = parser_alloc(ctx, sizeof(*decl));
405 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
407 dim_statement_t *stat;
409 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
413 stat->dim_decls = decls;
417 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
421 decl = parser_alloc(ctx, sizeof(*decl));
431 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
432 statement_t *else_stat)
434 if_statement_t *stat;
436 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
441 stat->if_stat = if_stat;
442 stat->elseifs = elseif_decl;
443 stat->else_stat = else_stat;
447 void *parser_alloc(parser_ctx_t *ctx, size_t size)
451 ret = vbsheap_alloc(&ctx->heap, size);
453 ctx->hres = E_OUTOFMEMORY;
457 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
459 ctx->code = ctx->ptr = code;
460 ctx->end = ctx->code + strlenW(ctx->code);
462 vbsheap_init(&ctx->heap);
464 ctx->parse_complete = FALSE;
467 ctx->last_token = tNL;
469 ctx->stats = ctx->stats_tail = NULL;
470 ctx->option_explicit = FALSE;
474 if(FAILED(ctx->hres))
476 if(!ctx->parse_complete) {
477 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
484 void parser_release(parser_ctx_t *ctx)
486 vbsheap_free(&ctx->heap);