vbscript: Add missing error handling in interp_jmp_false.
[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                           { $$ = $1; }
214     | ExpExpression '^' UnaryExpression         { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
215
216 UnaryExpression
217     : LiteralExpression             { $$ = $1; }
218     | CallExpression                { $$ = $1; }
219     | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
220
221 CallExpression
222     : PrimaryExpression                 { $$ = $1; }
223     | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
224
225 LiteralExpression
226     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
227     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
228     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
229     | tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
230     | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
231     | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
232     | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
233     | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
234     | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
235
236 PrimaryExpression
237     : '(' Expression ')'            { $$ = $2; }
238
239 %%
240
241 static int parser_error(const char *str)
242 {
243     return 0;
244 }
245
246 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
247 {
248     if(ctx->stats) {
249         ctx->stats_tail->next = stat;
250         ctx->stats_tail = stat;
251     }else {
252         ctx->stats = ctx->stats_tail = stat;
253     }
254 }
255
256 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
257 {
258     ctx->parse_complete = TRUE;
259     ctx->option_explicit = option_explicit;
260 }
261
262 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
263 {
264     expression_t *expr;
265
266     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
267     if(expr) {
268         expr->type = type;
269         expr->next = NULL;
270     }
271
272     return expr;
273 }
274
275 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
276 {
277     bool_expression_t *expr;
278
279     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
280     if(!expr)
281         return NULL;
282
283     expr->value = value;
284     return &expr->expr;
285 }
286
287 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
288 {
289     string_expression_t *expr;
290
291     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
292     if(!expr)
293         return NULL;
294
295     expr->value = value;
296     return &expr->expr;
297 }
298
299 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
300 {
301     int_expression_t *expr;
302
303     expr = new_expression(ctx, type, sizeof(*expr));
304     if(!expr)
305         return NULL;
306
307     expr->value = value;
308     return &expr->expr;
309 }
310
311 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
312 {
313     double_expression_t *expr;
314
315     expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
316     if(!expr)
317         return NULL;
318
319     expr->value = value;
320     return &expr->expr;
321 }
322
323 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
324 {
325     unary_expression_t *expr;
326
327     expr = new_expression(ctx, type, sizeof(*expr));
328     if(!expr)
329         return NULL;
330
331     expr->subexpr = subexpr;
332     return &expr->expr;
333 }
334
335 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
336 {
337     binary_expression_t *expr;
338
339     expr = new_expression(ctx, type, sizeof(*expr));
340     if(!expr)
341         return NULL;
342
343     expr->left = left;
344     expr->right = right;
345     return &expr->expr;
346 }
347
348 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
349 {
350     member_expression_t *expr;
351
352     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
353     if(!expr)
354         return NULL;
355
356     expr->obj_expr = obj_expr;
357     expr->identifier = identifier;
358     expr->args = NULL;
359     return expr;
360 }
361
362 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
363 {
364     statement_t *stat;
365
366     stat = parser_alloc(ctx, size);
367     if(stat) {
368         stat->type = type;
369         stat->next = NULL;
370     }
371
372     return stat;
373 }
374
375 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
376 {
377     call_statement_t *stat;
378
379     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
380     if(!stat)
381         return NULL;
382
383     stat->expr = expr;
384     return &stat->stat;
385 }
386
387 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
388 {
389     assign_statement_t *stat;
390
391     stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
392     if(!stat)
393         return NULL;
394
395     stat->member_expr = left;
396     stat->value_expr = right;
397     return &stat->stat;
398 }
399
400 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
401 {
402     dim_decl_t *decl;
403
404     decl = parser_alloc(ctx, sizeof(*decl));
405     if(!decl)
406         return NULL;
407
408     decl->name = name;
409     decl->next = next;
410     return decl;
411 }
412
413 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
414 {
415     dim_statement_t *stat;
416
417     stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
418     if(!stat)
419         return NULL;
420
421     stat->dim_decls = decls;
422     return &stat->stat;
423 }
424
425 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
426 {
427     elseif_decl_t *decl;
428
429     decl = parser_alloc(ctx, sizeof(*decl));
430     if(!decl)
431         return NULL;
432
433     decl->expr = expr;
434     decl->stat = stat;
435     decl->next = NULL;
436     return decl;
437 }
438
439 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
440         statement_t *else_stat)
441 {
442     if_statement_t *stat;
443
444     stat = new_statement(ctx, STAT_IF, sizeof(*stat));
445     if(!stat)
446         return NULL;
447
448     stat->expr = expr;
449     stat->if_stat = if_stat;
450     stat->elseifs = elseif_decl;
451     stat->else_stat = else_stat;
452     return &stat->stat;
453 }
454
455 void *parser_alloc(parser_ctx_t *ctx, size_t size)
456 {
457     void *ret;
458
459     ret = vbsheap_alloc(&ctx->heap, size);
460     if(!ret)
461         ctx->hres = E_OUTOFMEMORY;
462     return ret;
463 }
464
465 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
466 {
467     ctx->code = ctx->ptr = code;
468     ctx->end = ctx->code + strlenW(ctx->code);
469
470     vbsheap_init(&ctx->heap);
471
472     ctx->parse_complete = FALSE;
473     ctx->hres = S_OK;
474
475     ctx->last_token = tNL;
476     ctx->last_nl = 0;
477     ctx->stats = ctx->stats_tail = NULL;
478     ctx->option_explicit = FALSE;
479
480     parser_parse(ctx);
481
482     if(FAILED(ctx->hres))
483         return ctx->hres;
484     if(!ctx->parse_complete) {
485         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
486         return E_FAIL;
487     }
488
489     return S_OK;
490 }
491
492 void parser_release(parser_ctx_t *ctx)
493 {
494     vbsheap_free(&ctx->heap);
495 }