vbscript: Added interp_mul 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 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
50 static statement_t *new_dim_statement(parser_ctx_t*,dim_decl_t*);
51  static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
52
53 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
54 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
55
56 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
57
58 %}
59
60 %pure_parser
61 %start Program
62
63 %union {
64     const WCHAR *string;
65     statement_t *statement;
66     expression_t *expression;
67     member_expression_t *member;
68     elseif_decl_t *elseif;
69     dim_decl_t *dim_decl;
70     LONG lng;
71     BOOL bool;
72     double dbl;
73 }
74
75 %token tEOF tNL tREM tEMPTYBRACKETS
76 %token tTRUE tFALSE
77 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
78 %token tIS tLTEQ tGTEQ tMOD
79 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
80 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
81 %token tWHILE tWEND tDO tLOOP tUNTIL
82 %token tBYREF tBYVAL
83 %token tOPTION tEXPLICIT
84 %token tSTOP
85 %token tNOTHING tEMPTY tNULL
86 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
87 %token tERROR tNEXT tON tRESUME tGOTO
88 %token <string> tIdentifier tString
89 %token <lng> tLong tShort
90 %token <dbl> tDouble
91
92 %type <statement> Statement StatementNl StatementsNl IfStatement Else_opt
93 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
94 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
95 %type <expression> NotExpression UnaryExpression
96 %type <member> MemberExpression
97 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
98 %type <bool> OptionExplicit_opt
99 %type <elseif> ElseIfs_opt ElseIfs ElseIf
100 %type <dim_decl> DimDeclList
101
102 %%
103
104 Program
105     : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }
106
107 OptionExplicit_opt
108     : /* empty */                { $$ = FALSE; }
109     | tOPTION tEXPLICIT tNL      { $$ = TRUE; }
110
111 SourceElements
112     : /* empty */
113     | SourceElements StatementNl    { source_add_statement(ctx, $2); }
114
115 StatementsNl
116     : StatementNl                           { $$ = $1; }
117     | StatementNl StatementsNl              { $1->next = $2; $$ = $1; }
118
119 StatementNl
120     : Statement tNL                 { $$ = $1; }
121
122 Statement
123     : MemberExpression ArgumentList_opt     { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
124     | tCALL MemberExpression Arguments_opt  { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
125     | MemberExpression Arguments_opt '=' Expression
126                                             { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
127     | tDIM DimDeclList                      { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
128     | IfStatement                           { $$ = $1; }
129
130 MemberExpression
131     : tIdentifier                           { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
132     | CallExpression '.' tIdentifier        { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
133
134 DimDeclList /* FIXME: Support arrays */
135     : tIdentifier                           { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
136     | tIdentifier ',' DimDeclList           { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
137
138 IfStatement
139     : tIF Expression tTHEN tNL StatementsNl ElseIfs_opt Else_opt tEND tIF
140                                             { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
141     /* FIXME: short if statement */
142
143 ElseIfs_opt
144     : /* empty */                           { $$ = NULL; }
145     | ElseIfs                               { $$ = $1; }
146
147 ElseIfs
148     : ElseIf                                { $$ = $1; }
149     | ElseIf ElseIfs                        { $1->next = $2; $$ = $1; }
150
151 ElseIf
152     : tELSEIF Expression tTHEN tNL StatementsNl
153                                             { $$ = new_elseif_decl(ctx, $2, $5); }
154
155 Else_opt
156     : /* empty */                           { $$ = NULL; }
157     | tELSE tNL StatementsNl                { $$ = $3; }
158
159 Arguments_opt
160     : EmptyBrackets_opt             { $$ = NULL; }
161     | '(' ArgumentList ')'          { $$ = $2; }
162
163 ArgumentList_opt
164     : EmptyBrackets_opt             { $$ = NULL; }
165     | ArgumentList                  { $$ = $1; }
166
167 ArgumentList
168     : Expression                    { $$ = $1; }
169     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
170
171 EmptyBrackets_opt
172     : /* empty */
173     | tEMPTYBRACKETS
174
175 Expression
176     : NotExpression                 { $$ = $1; }
177
178 NotExpression
179     : EqualityExpression            { $$ = $1; }
180     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
181
182 EqualityExpression
183     : ConcatExpression                          { $$ = $1; }
184     | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
185     | EqualityExpression tNEQ ConcatExpression  { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
186
187 ConcatExpression
188     : AdditiveExpression                        { $$ = $1; }
189     | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
190
191 AdditiveExpression
192     : ModExpression                             { $$ = $1; }
193     | AdditiveExpression '+' ModExpression      { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
194     | AdditiveExpression '-' ModExpression      { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
195
196 ModExpression
197     : IntdivExpression                          { $$ = $1; }
198     | ModExpression tMOD IntdivExpression       { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
199
200 IntdivExpression
201     : MultiplicativeExpression                  { $$ = $1; }
202     | IntdivExpression '\\' MultiplicativeExpression
203                                                 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
204
205 MultiplicativeExpression
206     : ExpExpression                             { $$ = $1; }
207     | MultiplicativeExpression '*' ExpExpression
208                                                 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
209     | MultiplicativeExpression '/' ExpExpression
210                                                 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
211
212 ExpExpression
213     : UnaryExpression /* FIXME */   { $$ = $1; }
214
215 UnaryExpression
216     : LiteralExpression             { $$ = $1; }
217     | CallExpression                { $$ = $1; }
218     | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
219
220 CallExpression
221     : PrimaryExpression                 { $$ = $1; }
222     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
223
224 LiteralExpression
225     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
226     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
227     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
228     | tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
229     | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
230     | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
231     | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
232     | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
233     | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
234
235 PrimaryExpression
236     : '(' Expression ')'            { $$ = $2; }
237
238 %%
239
240 static int parser_error(const char *str)
241 {
242     return 0;
243 }
244
245 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
246 {
247     if(ctx->stats) {
248         ctx->stats_tail->next = stat;
249         ctx->stats_tail = stat;
250     }else {
251         ctx->stats = ctx->stats_tail = stat;
252     }
253 }
254
255 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
256 {
257     ctx->parse_complete = TRUE;
258     ctx->option_explicit = option_explicit;
259 }
260
261 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
262 {
263     expression_t *expr;
264
265     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
266     if(expr) {
267         expr->type = type;
268         expr->next = NULL;
269     }
270
271     return expr;
272 }
273
274 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
275 {
276     bool_expression_t *expr;
277
278     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
279     if(!expr)
280         return NULL;
281
282     expr->value = value;
283     return &expr->expr;
284 }
285
286 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
287 {
288     string_expression_t *expr;
289
290     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
291     if(!expr)
292         return NULL;
293
294     expr->value = value;
295     return &expr->expr;
296 }
297
298 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
299 {
300     int_expression_t *expr;
301
302     expr = new_expression(ctx, type, sizeof(*expr));
303     if(!expr)
304         return NULL;
305
306     expr->value = value;
307     return &expr->expr;
308 }
309
310 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
311 {
312     double_expression_t *expr;
313
314     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
315     if(!expr)
316         return NULL;
317
318     expr->value = value;
319     return &expr->expr;
320 }
321
322 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
323 {
324     unary_expression_t *expr;
325
326     expr = new_expression(ctx, type, sizeof(*expr));
327     if(!expr)
328         return NULL;
329
330     expr->subexpr = subexpr;
331     return &expr->expr;
332 }
333
334 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
335 {
336     binary_expression_t *expr;
337
338     expr = new_expression(ctx, type, sizeof(*expr));
339     if(!expr)
340         return NULL;
341
342     expr->left = left;
343     expr->right = right;
344     return &expr->expr;
345 }
346
347 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
348 {
349     member_expression_t *expr;
350
351     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
352     if(!expr)
353         return NULL;
354
355     expr->obj_expr = obj_expr;
356     expr->identifier = identifier;
357     expr->args = NULL;
358     return expr;
359 }
360
361 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
362 {
363     statement_t *stat;
364
365     stat = parser_alloc(ctx, size);
366     if(stat) {
367         stat->type = type;
368         stat->next = NULL;
369     }
370
371     return stat;
372 }
373
374 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
375 {
376     call_statement_t *stat;
377
378     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
379     if(!stat)
380         return NULL;
381
382     stat->expr = expr;
383     return &stat->stat;
384 }
385
386 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
387 {
388     assign_statement_t *stat;
389
390     stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
391     if(!stat)
392         return NULL;
393
394     stat->member_expr = left;
395     stat->value_expr = right;
396     return &stat->stat;
397 }
398
399 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
400 {
401     dim_decl_t *decl;
402
403     decl = parser_alloc(ctx, sizeof(*decl));
404     if(!decl)
405         return NULL;
406
407     decl->name = name;
408     decl->next = next;
409     return decl;
410 }
411
412 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
413 {
414     dim_statement_t *stat;
415
416     stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
417     if(!stat)
418         return NULL;
419
420     stat->dim_decls = decls;
421     return &stat->stat;
422 }
423
424 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
425 {
426     elseif_decl_t *decl;
427
428     decl = parser_alloc(ctx, sizeof(*decl));
429     if(!decl)
430         return NULL;
431
432     decl->expr = expr;
433     decl->stat = stat;
434     decl->next = NULL;
435     return decl;
436 }
437
438 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
439         statement_t *else_stat)
440 {
441     if_statement_t *stat;
442
443     stat = new_statement(ctx, STAT_IF, sizeof(*stat));
444     if(!stat)
445         return NULL;
446
447     stat->expr = expr;
448     stat->if_stat = if_stat;
449     stat->elseifs = elseif_decl;
450     stat->else_stat = else_stat;
451     return &stat->stat;
452 }
453
454 void *parser_alloc(parser_ctx_t *ctx, size_t size)
455 {
456     void *ret;
457
458     ret = vbsheap_alloc(&ctx->heap, size);
459     if(!ret)
460         ctx->hres = E_OUTOFMEMORY;
461     return ret;
462 }
463
464 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
465 {
466     ctx->code = ctx->ptr = code;
467     ctx->end = ctx->code + strlenW(ctx->code);
468
469     vbsheap_init(&ctx->heap);
470
471     ctx->parse_complete = FALSE;
472     ctx->hres = S_OK;
473
474     ctx->last_token = tNL;
475     ctx->last_nl = 0;
476     ctx->stats = ctx->stats_tail = NULL;
477     ctx->option_explicit = FALSE;
478
479     parser_parse(ctx);
480
481     if(FAILED(ctx->hres))
482         return ctx->hres;
483     if(!ctx->parse_complete) {
484         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
485         return E_FAIL;
486     }
487
488     return S_OK;
489 }
490
491 void parser_release(parser_ctx_t *ctx)
492 {
493     vbsheap_free(&ctx->heap);
494 }