vbscript: Added interp_sub implementation.
[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*,BOOL);
35
36 static void source_add_statement(parser_ctx_t*,statement_t*);
37
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*);
45
46 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
47
48 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
49
50 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
51
52 %}
53
54 %pure_parser
55 %start Program
56
57 %union {
58     const WCHAR *string;
59     statement_t *statement;
60     expression_t *expression;
61     member_expression_t *member;
62     LONG lng;
63     BOOL bool;
64     double dbl;
65 }
66
67 %token tEOF tNL tREM tEMPTYBRACKETS
68 %token tTRUE tFALSE
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
74 %token tBYREF tBYVAL
75 %token tOPTION tEXPLICIT
76 %token tSTOP
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
82 %token <dbl> tDouble
83
84 %type <statement> Statement StatementNl
85 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
86 %type <expression> ConcatExpression AdditiveExpression ModExpression
87 %type <expression> NotExpression UnaryExpression
88 %type <member> MemberExpression
89 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
90 %type <bool> OptionExplicit_opt
91
92 %%
93
94 Program
95     : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }
96
97 OptionExplicit_opt
98     : /* empty */                { $$ = FALSE; }
99     | tOPTION tEXPLICIT tNL      { $$ = TRUE; }
100
101 SourceElements
102     : /* empty */
103     | SourceElements StatementNl    { source_add_statement(ctx, $2); }
104
105 StatementNl
106     : Statement tNL                 { $$ = $1; }
107
108 Statement
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; }
111
112 MemberExpression
113     : tIdentifier                   { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
114     /* FIXME: MemberExpressionArgs '.' tIdentifier */
115
116 Arguments_opt
117     : EmptyBrackets_opt             { $$ = NULL; }
118     | '(' ArgumentList ')'          { $$ = $2; }
119
120 ArgumentList_opt
121     : EmptyBrackets_opt             { $$ = NULL; }
122     | ArgumentList                  { $$ = $1; }
123
124 ArgumentList
125     : Expression                    { $$ = $1; }
126     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
127
128 EmptyBrackets_opt
129     : /* empty */
130     | tEMPTYBRACKETS
131
132 Expression
133     : NotExpression                 { $$ = $1; }
134
135 NotExpression
136     : EqualityExpression            { $$ = $1; }
137     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
138
139 EqualityExpression
140     : ConcatExpression                          { $$ = $1; }
141     | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
142
143 ConcatExpression
144     : AdditiveExpression                        { $$ = $1; }
145     | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
146
147 AdditiveExpression
148     : ModExpression                             { $$ = $1; }
149     | AdditiveExpression '+' ModExpression      { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
150     | AdditiveExpression '-' ModExpression      { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
151
152 ModExpression
153     : UnaryExpression /* FIXME */   { $$ = $1; }
154
155
156 UnaryExpression
157     : LiteralExpression             { $$ = $1; }
158     | CallExpression                { $$ = $1; }
159     | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
160
161 CallExpression
162     : PrimaryExpression                 { $$ = $1; }
163     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
164
165 LiteralExpression
166     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
167     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
168     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
169     | tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
170     | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
171     | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
172     | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
173     | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
174     | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
175
176 PrimaryExpression
177     : '(' Expression ')'            { $$ = $2; }
178
179 %%
180
181 static int parser_error(const char *str)
182 {
183     return 0;
184 }
185
186 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
187 {
188     if(ctx->stats) {
189         ctx->stats_tail->next = stat;
190         ctx->stats_tail = stat;
191     }else {
192         ctx->stats = ctx->stats_tail = stat;
193     }
194 }
195
196 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
197 {
198     ctx->parse_complete = TRUE;
199     ctx->option_explicit = option_explicit;
200 }
201
202 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
203 {
204     expression_t *expr;
205
206     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
207     if(expr) {
208         expr->type = type;
209         expr->next = NULL;
210     }
211
212     return expr;
213 }
214
215 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
216 {
217     bool_expression_t *expr;
218
219     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
220     if(!expr)
221         return NULL;
222
223     expr->value = value;
224     return &expr->expr;
225 }
226
227 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
228 {
229     string_expression_t *expr;
230
231     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
232     if(!expr)
233         return NULL;
234
235     expr->value = value;
236     return &expr->expr;
237 }
238
239 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
240 {
241     int_expression_t *expr;
242
243     expr = new_expression(ctx, type, sizeof(*expr));
244     if(!expr)
245         return NULL;
246
247     expr->value = value;
248     return &expr->expr;
249 }
250
251 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
252 {
253     double_expression_t *expr;
254
255     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
256     if(!expr)
257         return NULL;
258
259     expr->value = value;
260     return &expr->expr;
261 }
262
263 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
264 {
265     unary_expression_t *expr;
266
267     expr = new_expression(ctx, type, sizeof(*expr));
268     if(!expr)
269         return NULL;
270
271     expr->subexpr = subexpr;
272     return &expr->expr;
273 }
274
275 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
276 {
277     binary_expression_t *expr;
278
279     expr = new_expression(ctx, type, sizeof(*expr));
280     if(!expr)
281         return NULL;
282
283     expr->left = left;
284     expr->right = right;
285     return &expr->expr;
286 }
287
288 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
289 {
290     member_expression_t *expr;
291
292     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
293     if(!expr)
294         return NULL;
295
296     expr->obj_expr = obj_expr;
297     expr->identifier = identifier;
298     expr->args = NULL;
299     return expr;
300 }
301
302 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
303 {
304     statement_t *stat;
305
306     stat = parser_alloc(ctx, size);
307     if(stat) {
308         stat->type = type;
309         stat->next = NULL;
310     }
311
312     return stat;
313 }
314
315 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
316 {
317     call_statement_t *stat;
318
319     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
320     if(!stat)
321         return NULL;
322
323     stat->expr = expr;
324     return &stat->stat;
325 }
326
327 void *parser_alloc(parser_ctx_t *ctx, size_t size)
328 {
329     void *ret;
330
331     ret = vbsheap_alloc(&ctx->heap, size);
332     if(!ret)
333         ctx->hres = E_OUTOFMEMORY;
334     return ret;
335 }
336
337 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
338 {
339     ctx->code = ctx->ptr = code;
340     ctx->end = ctx->code + strlenW(ctx->code);
341
342     vbsheap_init(&ctx->heap);
343
344     ctx->parse_complete = FALSE;
345     ctx->hres = S_OK;
346
347     ctx->last_token = tNL;
348     ctx->last_nl = 0;
349     ctx->stats = ctx->stats_tail = NULL;
350     ctx->option_explicit = FALSE;
351
352     parser_parse(ctx);
353
354     if(FAILED(ctx->hres))
355         return ctx->hres;
356     if(!ctx->parse_complete) {
357         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
358         return E_FAIL;
359     }
360
361     return S_OK;
362 }
363
364 void parser_release(parser_ctx_t *ctx)
365 {
366     vbsheap_free(&ctx->heap);
367 }