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 expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
39 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
40 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
41 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
43 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
45 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
47 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
56 statement_t *statement;
57 expression_t *expression;
58 member_expression_t *member;
62 %token tEOF tNL tREM tEMPTYBRACKETS
64 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
65 %token tIS tLTEQ tGTEQ tMOD
66 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
67 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
68 %token tWHILE tWEND tDO tLOOP tUNTIL
70 %token tOPTION tEXPLICIT
72 %token tNOTHING tEMPTY tNULL
73 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
74 %token tERROR tNEXT tON tRESUME tGOTO
75 %token <string> tIdentifier tString
77 %type <statement> Statement StatementNl
78 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression
79 %type <expression> ConcatExpression
80 %type <expression> NotExpression
81 %type <member> MemberExpression
82 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
83 %type <bool> OptionExplicit_opt
88 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
91 : /* empty */ { $$ = FALSE; }
92 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
96 | SourceElements StatementNl { source_add_statement(ctx, $2); }
99 : Statement tNL { $$ = $1; }
102 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
103 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
106 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
107 /* FIXME: MemberExpressionArgs '.' tIdentifier */
110 : EmptyBrackets_opt { $$ = NULL; }
111 | '(' ArgumentList ')' { $$ = $2; }
114 : EmptyBrackets_opt { $$ = NULL; }
115 | ArgumentList { $$ = $1; }
118 : Expression { $$ = $1; }
119 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
126 : NotExpression { $$ = $1; }
129 : EqualityExpression { $$ = $1; }
130 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
133 : ConcatExpression { $$ = $1; }
134 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
137 : LiteralExpression /* FIXME */ { $$ = $1; }
138 | PrimaryExpression /* FIXME */ { $$ = $1; }
141 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
142 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
143 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
146 : '(' Expression ')' { $$ = $2; }
150 static int parser_error(const char *str)
155 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
158 ctx->stats_tail->next = stat;
159 ctx->stats_tail = stat;
161 ctx->stats = ctx->stats_tail = stat;
165 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
167 ctx->parse_complete = TRUE;
168 ctx->option_explicit = option_explicit;
171 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, unsigned size)
175 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
184 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
186 bool_expression_t *expr;
188 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
196 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
198 string_expression_t *expr;
200 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
208 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
210 unary_expression_t *expr;
212 expr = new_expression(ctx, type, sizeof(*expr));
216 expr->subexpr = subexpr;
220 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
222 binary_expression_t *expr;
224 expr = new_expression(ctx, type, sizeof(*expr));
233 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
235 member_expression_t *expr;
237 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
241 expr->obj_expr = obj_expr;
242 expr->identifier = identifier;
247 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
251 stat = parser_alloc(ctx, size);
260 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
262 call_statement_t *stat;
264 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
272 void *parser_alloc(parser_ctx_t *ctx, size_t size)
277 ret = heap_alloc(size);
279 ctx->hres = E_OUTOFMEMORY;
283 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
285 ctx->code = ctx->ptr = code;
286 ctx->end = ctx->code + strlenW(ctx->code);
288 ctx->parse_complete = FALSE;
291 ctx->last_token = tNL;
293 ctx->stats = ctx->stats_tail = NULL;
294 ctx->option_explicit = FALSE;
298 if(FAILED(ctx->hres))
300 if(!ctx->parse_complete) {
301 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));