vbscript: Added simple call test.
[wine] / dlls / vbscript / parser.y
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 %{
20
21 #include "vbscript.h"
22 #include "parse.h"
23
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
27
28
29 #define YYLEX_PARAM ctx
30 #define YYPARSE_PARAM ctx
31
32 static int parser_error(const char*);
33
34 static void parse_complete(parser_ctx_t*);
35
36 static void source_add_statement(parser_ctx_t*,statement_t*);
37
38 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
39
40 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
41
42 %}
43
44 %pure_parser
45 %start Program
46
47 %union {
48     const WCHAR *string;
49     statement_t *statement;
50     member_expression_t *member;
51 }
52
53 %token tEOF tNL blah
54 %token <string> tIdentifier
55
56 %type <statement> Statement StatementNl
57 %type <member> MemberExpression
58
59 %%
60
61 Program
62     : SourceElements tEOF           { parse_complete(ctx); }
63
64 SourceElements
65     : /* empty */
66     | SourceElements StatementNl    { source_add_statement(ctx, $2); }
67
68 StatementNl
69     : Statement tNL                 { $$ = $1; }
70
71 Statement
72     : MemberExpression /* FIXME: Arguments_opt */   { $$ = new_call_statement(ctx, $1); }
73
74 MemberExpression
75     : tIdentifier                   { $$ = new_member_expression(ctx, NULL, $1); }
76     /* FIXME: MemberExpressionArgs '.' tIdentifier */
77
78 %%
79
80 static int parser_error(const char *str)
81 {
82     return 0;
83 }
84
85 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
86 {
87     if(ctx->stats) {
88         ctx->stats_tail->next = stat;
89         ctx->stats_tail = stat;
90     }else {
91         ctx->stats = ctx->stats_tail = stat;
92     }
93 }
94
95 static void parse_complete(parser_ctx_t *ctx)
96 {
97     ctx->parse_complete = TRUE;
98 }
99
100 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, unsigned size)
101 {
102     expression_t *expr;
103
104     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
105     if(expr) {
106         expr->type = type;
107         expr->next = NULL;
108     }
109
110     return expr;
111 }
112
113 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
114 {
115     member_expression_t *expr;
116
117     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
118     if(!expr)
119         return NULL;
120
121     expr->obj_expr = obj_expr;
122     expr->identifier = identifier;
123     expr->args = NULL;
124     return expr;
125 }
126
127 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
128 {
129     statement_t *stat;
130
131     stat = parser_alloc(ctx, size);
132     if(stat) {
133         stat->type = type;
134         stat->next = NULL;
135     }
136
137     return stat;
138 }
139
140 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
141 {
142     call_statement_t *stat;
143
144     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
145     if(!stat)
146         return NULL;
147
148     stat->expr = expr;
149     return &stat->stat;
150 }
151
152 void *parser_alloc(parser_ctx_t *ctx, size_t size)
153 {
154     void *ret;
155
156     /* FIXME: leaks! */
157     ret = heap_alloc(size);
158     if(!ret)
159         ctx->hres = E_OUTOFMEMORY;
160     return ret;
161 }
162
163 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
164 {
165     ctx->code = ctx->ptr = code;
166     ctx->end = ctx->code + strlenW(ctx->code);
167
168     ctx->parse_complete = FALSE;
169     ctx->hres = S_OK;
170
171     ctx->last_token = tNL;
172     ctx->last_nl = 0;
173     ctx->stats = ctx->stats_tail = NULL;
174
175     parser_parse(ctx);
176
177     if(FAILED(ctx->hres))
178         return ctx->hres;
179     if(!ctx->parse_complete) {
180         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
181         return E_FAIL;
182     }
183
184     return S_OK;
185 }