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*);
36 static void source_add_statement(parser_ctx_t*,statement_t*);
38 static expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
40 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
42 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
44 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
53 statement_t *statement;
54 expression_t *expression;
55 member_expression_t *member;
60 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
61 %token tIS tLTEQ tGTEQ tMOD
62 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
63 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
64 %token tWHILE tWEND tDO tLOOP tUNTIL
66 %token tOPTION tEXPLICIT
68 %token tNOTHING tEMPTY tNULL
69 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
70 %token tERROR tNEXT tON tRESUME tGOTO
71 %token <string> tIdentifier
73 %type <statement> Statement StatementNl
74 %type <expression> Expression LiteralExpression
75 %type <member> MemberExpression
76 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
81 : SourceElements tEOF { parse_complete(ctx); }
85 | SourceElements StatementNl { source_add_statement(ctx, $2); }
88 : Statement tNL { $$ = $1; }
91 : MemberExpression Arguments_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
92 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
95 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
96 /* FIXME: MemberExpressionArgs '.' tIdentifier */
99 : /* empty */ { $$ = NULL; }
100 | '(' ArgumentList_opt ')' { $$ = $2; }
103 : /* empty */ { $$ = NULL; }
104 | ArgumentList { $$ = $1; }
107 : Expression { $$ = $1; }
108 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
111 : LiteralExpression /* FIXME */ { $$ = $1; }
114 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
115 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
119 static int parser_error(const char *str)
124 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
127 ctx->stats_tail->next = stat;
128 ctx->stats_tail = stat;
130 ctx->stats = ctx->stats_tail = stat;
134 static void parse_complete(parser_ctx_t *ctx)
136 ctx->parse_complete = TRUE;
139 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, unsigned size)
143 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
152 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
154 bool_expression_t *expr;
156 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
164 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
166 member_expression_t *expr;
168 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
172 expr->obj_expr = obj_expr;
173 expr->identifier = identifier;
178 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
182 stat = parser_alloc(ctx, size);
191 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
193 call_statement_t *stat;
195 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
203 void *parser_alloc(parser_ctx_t *ctx, size_t size)
208 ret = heap_alloc(size);
210 ctx->hres = E_OUTOFMEMORY;
214 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
216 ctx->code = ctx->ptr = code;
217 ctx->end = ctx->code + strlenW(ctx->code);
219 ctx->parse_complete = FALSE;
222 ctx->last_token = tNL;
224 ctx->stats = ctx->stats_tail = NULL;
228 if(FAILED(ctx->hres))
230 if(!ctx->parse_complete) {
231 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));