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*);
51 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
60 statement_t *statement;
61 expression_t *expression;
62 member_expression_t *member;
68 %token tEOF tNL tREM tEMPTYBRACKETS
70 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
71 %token tIS tLTEQ tGTEQ tMOD
72 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
73 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
74 %token tWHILE tWEND tDO tLOOP tUNTIL
76 %token tOPTION tEXPLICIT
78 %token tNOTHING tEMPTY tNULL
79 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
80 %token tERROR tNEXT tON tRESUME tGOTO
81 %token <string> tIdentifier tString
82 %token <lng> tLong tShort
85 %type <statement> Statement StatementNl
86 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
87 %type <expression> ConcatExpression AdditiveExpression ModExpression
88 %type <expression> NotExpression UnaryExpression
89 %type <member> MemberExpression
90 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
91 %type <bool> OptionExplicit_opt
96 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
99 : /* empty */ { $$ = FALSE; }
100 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
104 | SourceElements StatementNl { source_add_statement(ctx, $2); }
107 : Statement tNL { $$ = $1; }
110 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
111 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
112 | MemberExpression Arguments_opt '=' Expression
113 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
116 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
117 /* FIXME: MemberExpressionArgs '.' tIdentifier */
120 : EmptyBrackets_opt { $$ = NULL; }
121 | '(' ArgumentList ')' { $$ = $2; }
124 : EmptyBrackets_opt { $$ = NULL; }
125 | ArgumentList { $$ = $1; }
128 : Expression { $$ = $1; }
129 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
136 : NotExpression { $$ = $1; }
139 : EqualityExpression { $$ = $1; }
140 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
143 : ConcatExpression { $$ = $1; }
144 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
145 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
148 : AdditiveExpression { $$ = $1; }
149 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
152 : ModExpression { $$ = $1; }
153 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
154 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
157 : UnaryExpression /* FIXME */ { $$ = $1; }
161 : LiteralExpression { $$ = $1; }
162 | CallExpression { $$ = $1; }
163 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
166 : PrimaryExpression { $$ = $1; }
167 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
170 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
171 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
172 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
173 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
174 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
175 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
176 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
177 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
178 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
181 : '(' Expression ')' { $$ = $2; }
185 static int parser_error(const char *str)
190 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
193 ctx->stats_tail->next = stat;
194 ctx->stats_tail = stat;
196 ctx->stats = ctx->stats_tail = stat;
200 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
202 ctx->parse_complete = TRUE;
203 ctx->option_explicit = option_explicit;
206 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
210 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
219 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
221 bool_expression_t *expr;
223 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
231 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
233 string_expression_t *expr;
235 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
243 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
245 int_expression_t *expr;
247 expr = new_expression(ctx, type, sizeof(*expr));
255 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
257 double_expression_t *expr;
259 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
267 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
269 unary_expression_t *expr;
271 expr = new_expression(ctx, type, sizeof(*expr));
275 expr->subexpr = subexpr;
279 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
281 binary_expression_t *expr;
283 expr = new_expression(ctx, type, sizeof(*expr));
292 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
294 member_expression_t *expr;
296 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
300 expr->obj_expr = obj_expr;
301 expr->identifier = identifier;
306 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
310 stat = parser_alloc(ctx, size);
319 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
321 call_statement_t *stat;
323 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
331 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
333 assign_statement_t *stat;
335 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
339 stat->member_expr = left;
340 stat->value_expr = right;
344 void *parser_alloc(parser_ctx_t *ctx, size_t size)
348 ret = vbsheap_alloc(&ctx->heap, size);
350 ctx->hres = E_OUTOFMEMORY;
354 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
356 ctx->code = ctx->ptr = code;
357 ctx->end = ctx->code + strlenW(ctx->code);
359 vbsheap_init(&ctx->heap);
361 ctx->parse_complete = FALSE;
364 ctx->last_token = tNL;
366 ctx->stats = ctx->stats_tail = NULL;
367 ctx->option_explicit = FALSE;
371 if(FAILED(ctx->hres))
373 if(!ctx->parse_complete) {
374 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
381 void parser_release(parser_ctx_t *ctx)
383 vbsheap_free(&ctx->heap);