vbscript: Added Call statement implementation.
[wine/wine-gecko.git] / dlls / vbscript / parser.y
blobcaf6f4964f35c929e521365f7fa12ab8b2cfa66e
1 /*
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
21 #include "vbscript.h"
22 #include "parse.h"
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 member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
40 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
42 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
46 %pure_parser
47 %start Program
49 %union {
50 const WCHAR *string;
51 statement_t *statement;
52 expression_t *expression;
53 member_expression_t *member;
56 %token tEOF tNL blah
57 %token tCALL
58 %token <string> tIdentifier
60 %type <statement> Statement StatementNl
61 %type <member> MemberExpression
62 %type <expression> Arguments_opt ArgumentList_opt
66 Program
67 : SourceElements tEOF { parse_complete(ctx); }
69 SourceElements
70 : /* empty */
71 | SourceElements StatementNl { source_add_statement(ctx, $2); }
73 StatementNl
74 : Statement tNL { $$ = $1; }
76 Statement
77 : MemberExpression Arguments_opt { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
78 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
80 MemberExpression
81 : tIdentifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
82 /* FIXME: MemberExpressionArgs '.' tIdentifier */
84 Arguments_opt
85 : /* empty */ { $$ = NULL; }
86 | '(' ArgumentList_opt ')' { $$ = $2; }
88 ArgumentList_opt
89 : /* empty */ { $$ = NULL; }
90 /* | ArgumentList { $$ = $1; } */
94 static int parser_error(const char *str)
96 return 0;
99 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
101 if(ctx->stats) {
102 ctx->stats_tail->next = stat;
103 ctx->stats_tail = stat;
104 }else {
105 ctx->stats = ctx->stats_tail = stat;
109 static void parse_complete(parser_ctx_t *ctx)
111 ctx->parse_complete = TRUE;
114 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, unsigned size)
116 expression_t *expr;
118 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
119 if(expr) {
120 expr->type = type;
121 expr->next = NULL;
124 return expr;
127 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
129 member_expression_t *expr;
131 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
132 if(!expr)
133 return NULL;
135 expr->obj_expr = obj_expr;
136 expr->identifier = identifier;
137 expr->args = NULL;
138 return expr;
141 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
143 statement_t *stat;
145 stat = parser_alloc(ctx, size);
146 if(stat) {
147 stat->type = type;
148 stat->next = NULL;
151 return stat;
154 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
156 call_statement_t *stat;
158 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
159 if(!stat)
160 return NULL;
162 stat->expr = expr;
163 return &stat->stat;
166 void *parser_alloc(parser_ctx_t *ctx, size_t size)
168 void *ret;
170 /* FIXME: leaks! */
171 ret = heap_alloc(size);
172 if(!ret)
173 ctx->hres = E_OUTOFMEMORY;
174 return ret;
177 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
179 ctx->code = ctx->ptr = code;
180 ctx->end = ctx->code + strlenW(ctx->code);
182 ctx->parse_complete = FALSE;
183 ctx->hres = S_OK;
185 ctx->last_token = tNL;
186 ctx->last_nl = 0;
187 ctx->stats = ctx->stats_tail = NULL;
189 parser_parse(ctx);
191 if(FAILED(ctx->hres))
192 return ctx->hres;
193 if(!ctx->parse_complete) {
194 FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
195 return E_FAIL;
198 return S_OK;