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
87 %type <expression> NotExpression
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 : LiteralExpression /* FIXME */ { $$ = $1; }
149 | CallExpression /* FIXME */ { $$ = $1; }
152 : PrimaryExpression { $$ = $1; }
153 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
156 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
157 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
158 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
159 | tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
160 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
161 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
162 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
163 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
164 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
167 : '(' Expression ')' { $$ = $2; }
171 static int parser_error(const char *str)
176 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
179 ctx->stats_tail->next = stat;
180 ctx->stats_tail = stat;
182 ctx->stats = ctx->stats_tail = stat;
186 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
188 ctx->parse_complete = TRUE;
189 ctx->option_explicit = option_explicit;
192 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
196 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
205 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
207 bool_expression_t *expr;
209 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
217 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
219 string_expression_t *expr;
221 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
229 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
231 int_expression_t *expr;
233 expr = new_expression(ctx, type, sizeof(*expr));
241 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
243 double_expression_t *expr;
245 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
253 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
255 unary_expression_t *expr;
257 expr = new_expression(ctx, type, sizeof(*expr));
261 expr->subexpr = subexpr;
265 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
267 binary_expression_t *expr;
269 expr = new_expression(ctx, type, sizeof(*expr));
278 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
280 member_expression_t *expr;
282 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
286 expr->obj_expr = obj_expr;
287 expr->identifier = identifier;
292 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
296 stat = parser_alloc(ctx, size);
305 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
307 call_statement_t *stat;
309 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
317 void *parser_alloc(parser_ctx_t *ctx, size_t size)
321 ret = vbsheap_alloc(&ctx->heap, size);
323 ctx->hres = E_OUTOFMEMORY;
327 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
329 ctx->code = ctx->ptr = code;
330 ctx->end = ctx->code + strlenW(ctx->code);
332 vbsheap_init(&ctx->heap);
334 ctx->parse_complete = FALSE;
337 ctx->last_token = tNL;
339 ctx->stats = ctx->stats_tail = NULL;
340 ctx->option_explicit = FALSE;
344 if(FAILED(ctx->hres))
346 if(!ctx->parse_complete) {
347 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
354 void parser_release(parser_ctx_t *ctx)
356 vbsheap_free(&ctx->heap);