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*);
50 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
59 statement_t *statement;
60 expression_t *expression;
61 member_expression_t *member;
67 %token tEOF tNL tREM tEMPTYBRACKETS
69 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
70 %token tIS tLTEQ tGTEQ tMOD
71 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
72 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
73 %token tWHILE tWEND tDO tLOOP tUNTIL
75 %token tOPTION tEXPLICIT
77 %token tNOTHING tEMPTY tNULL
78 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
79 %token tERROR tNEXT tON tRESUME tGOTO
80 %token <string> tIdentifier tString
81 %token <lng> tLong tShort
84 %type <statement> Statement StatementNl
85 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
86 %type <expression> ConcatExpression AdditiveExpression ModExpression
87 %type <expression> NotExpression UnaryExpression
88 %type <member> MemberExpression
89 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
90 %type <bool> OptionExplicit_opt
95 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
98 : /* empty */ { $$ = FALSE; }
99 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
103 | SourceElements StatementNl { source_add_statement(ctx, $2); }
106 : Statement tNL { $$ = $1; }
109 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
110 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
113 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
114 /* FIXME: MemberExpressionArgs '.' tIdentifier */
117 : EmptyBrackets_opt { $$ = NULL; }
118 | '(' ArgumentList ')' { $$ = $2; }
121 : EmptyBrackets_opt { $$ = NULL; }
122 | ArgumentList { $$ = $1; }
125 : Expression { $$ = $1; }
126 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
133 : NotExpression { $$ = $1; }
136 : EqualityExpression { $$ = $1; }
137 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
140 : ConcatExpression { $$ = $1; }
141 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
144 : AdditiveExpression { $$ = $1; }
145 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
148 : ModExpression { $$ = $1; }
149 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
150 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
153 : UnaryExpression /* FIXME */ { $$ = $1; }
157 : LiteralExpression { $$ = $1; }
158 | CallExpression { $$ = $1; }
159 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
162 : PrimaryExpression { $$ = $1; }
163 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
166 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
167 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
168 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
169 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
170 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
171 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
172 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
173 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
174 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
177 : '(' Expression ')' { $$ = $2; }
181 static int parser_error(const char *str)
186 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
189 ctx->stats_tail->next = stat;
190 ctx->stats_tail = stat;
192 ctx->stats = ctx->stats_tail = stat;
196 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
198 ctx->parse_complete = TRUE;
199 ctx->option_explicit = option_explicit;
202 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
206 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
215 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
217 bool_expression_t *expr;
219 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
227 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
229 string_expression_t *expr;
231 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
239 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
241 int_expression_t *expr;
243 expr = new_expression(ctx, type, sizeof(*expr));
251 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
253 double_expression_t *expr;
255 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
263 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
265 unary_expression_t *expr;
267 expr = new_expression(ctx, type, sizeof(*expr));
271 expr->subexpr = subexpr;
275 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
277 binary_expression_t *expr;
279 expr = new_expression(ctx, type, sizeof(*expr));
288 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
290 member_expression_t *expr;
292 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
296 expr->obj_expr = obj_expr;
297 expr->identifier = identifier;
302 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
306 stat = parser_alloc(ctx, size);
315 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
317 call_statement_t *stat;
319 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
327 void *parser_alloc(parser_ctx_t *ctx, size_t size)
331 ret = vbsheap_alloc(&ctx->heap, size);
333 ctx->hres = E_OUTOFMEMORY;
337 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
339 ctx->code = ctx->ptr = code;
340 ctx->end = ctx->code + strlenW(ctx->code);
342 vbsheap_init(&ctx->heap);
344 ctx->parse_complete = FALSE;
347 ctx->last_token = tNL;
349 ctx->stats = ctx->stats_tail = NULL;
350 ctx->option_explicit = FALSE;
354 if(FAILED(ctx->hres))
356 if(!ctx->parse_complete) {
357 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
364 void parser_release(parser_ctx_t *ctx)
366 vbsheap_free(&ctx->heap);