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