vbscript: Added interp_equal 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 expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
39 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
40 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
41 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
42
43 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
44
45 static statement_t *new_call_statement(parser_ctx_t*,member_expression_t*);
46
47 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
48
49 %}
50
51 %pure_parser
52 %start Program
53
54 %union {
55     const WCHAR *string;
56     statement_t *statement;
57     expression_t *expression;
58     member_expression_t *member;
59     BOOL bool;
60 }
61
62 %token tEOF tNL tREM tEMPTYBRACKETS
63 %token tTRUE tFALSE
64 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
65 %token tIS tLTEQ tGTEQ tMOD
66 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET
67 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
68 %token tWHILE tWEND tDO tLOOP tUNTIL
69 %token tBYREF tBYVAL
70 %token tOPTION tEXPLICIT
71 %token tSTOP
72 %token tNOTHING tEMPTY tNULL
73 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
74 %token tERROR tNEXT tON tRESUME tGOTO
75 %token <string> tIdentifier tString
76
77 %type <statement> Statement StatementNl
78 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression
79 %type <expression> ConcatExpression
80 %type <expression> NotExpression
81 %type <member> MemberExpression
82 %type <expression> Arguments_opt ArgumentList_opt ArgumentList
83 %type <bool> OptionExplicit_opt
84
85 %%
86
87 Program
88     : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }
89
90 OptionExplicit_opt
91     : /* empty */                { $$ = FALSE; }
92     | tOPTION tEXPLICIT tNL      { $$ = TRUE; }
93
94 SourceElements
95     : /* empty */
96     | SourceElements StatementNl    { source_add_statement(ctx, $2); }
97
98 StatementNl
99     : Statement tNL                 { $$ = $1; }
100
101 Statement
102     : MemberExpression ArgumentList_opt     { $1->args = $2; $$ = new_call_statement(ctx, $1); CHECK_ERROR; }
103     | tCALL MemberExpression Arguments_opt  { $2->args = $3; $$ = new_call_statement(ctx, $2); CHECK_ERROR; }
104
105 MemberExpression
106     : tIdentifier                   { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
107     /* FIXME: MemberExpressionArgs '.' tIdentifier */
108
109 Arguments_opt
110     : EmptyBrackets_opt             { $$ = NULL; }
111     | '(' ArgumentList ')'          { $$ = $2; }
112
113 ArgumentList_opt
114     : EmptyBrackets_opt             { $$ = NULL; }
115     | ArgumentList                  { $$ = $1; }
116
117 ArgumentList
118     : Expression                    { $$ = $1; }
119     | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
120
121 EmptyBrackets_opt
122     : /* empty */
123     | tEMPTYBRACKETS
124
125 Expression
126     : NotExpression                 { $$ = $1; }
127
128 NotExpression
129     : EqualityExpression            { $$ = $1; }
130     | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
131
132 EqualityExpression
133     : ConcatExpression                          { $$ = $1; }
134     | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
135
136 ConcatExpression
137     : LiteralExpression /* FIXME */ { $$ = $1; }
138     | PrimaryExpression /* FIXME */ { $$ = $1; }
139
140 LiteralExpression
141     : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
142     | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
143     | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
144
145 PrimaryExpression
146     : '(' Expression ')'            { $$ = $2; }
147
148 %%
149
150 static int parser_error(const char *str)
151 {
152     return 0;
153 }
154
155 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
156 {
157     if(ctx->stats) {
158         ctx->stats_tail->next = stat;
159         ctx->stats_tail = stat;
160     }else {
161         ctx->stats = ctx->stats_tail = stat;
162     }
163 }
164
165 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
166 {
167     ctx->parse_complete = TRUE;
168     ctx->option_explicit = option_explicit;
169 }
170
171 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, unsigned size)
172 {
173     expression_t *expr;
174
175     expr = parser_alloc(ctx, size ? size : sizeof(*expr));
176     if(expr) {
177         expr->type = type;
178         expr->next = NULL;
179     }
180
181     return expr;
182 }
183
184 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
185 {
186     bool_expression_t *expr;
187
188     expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
189     if(!expr)
190         return NULL;
191
192     expr->value = value;
193     return &expr->expr;
194 }
195
196 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
197 {
198     string_expression_t *expr;
199
200     expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
201     if(!expr)
202         return NULL;
203
204     expr->value = value;
205     return &expr->expr;
206 }
207
208 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
209 {
210     unary_expression_t *expr;
211
212     expr = new_expression(ctx, type, sizeof(*expr));
213     if(!expr)
214         return NULL;
215
216     expr->subexpr = subexpr;
217     return &expr->expr;
218 }
219
220 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
221 {
222     binary_expression_t *expr;
223
224     expr = new_expression(ctx, type, sizeof(*expr));
225     if(!expr)
226         return NULL;
227
228     expr->left = left;
229     expr->right = right;
230     return &expr->expr;
231 }
232
233 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
234 {
235     member_expression_t *expr;
236
237     expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
238     if(!expr)
239         return NULL;
240
241     expr->obj_expr = obj_expr;
242     expr->identifier = identifier;
243     expr->args = NULL;
244     return expr;
245 }
246
247 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, unsigned size)
248 {
249     statement_t *stat;
250
251     stat = parser_alloc(ctx, size);
252     if(stat) {
253         stat->type = type;
254         stat->next = NULL;
255     }
256
257     return stat;
258 }
259
260 static statement_t *new_call_statement(parser_ctx_t *ctx, member_expression_t *expr)
261 {
262     call_statement_t *stat;
263
264     stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
265     if(!stat)
266         return NULL;
267
268     stat->expr = expr;
269     return &stat->stat;
270 }
271
272 void *parser_alloc(parser_ctx_t *ctx, size_t size)
273 {
274     void *ret;
275
276     /* FIXME: leaks! */
277     ret = heap_alloc(size);
278     if(!ret)
279         ctx->hres = E_OUTOFMEMORY;
280     return ret;
281 }
282
283 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code)
284 {
285     ctx->code = ctx->ptr = code;
286     ctx->end = ctx->code + strlenW(ctx->code);
287
288     ctx->parse_complete = FALSE;
289     ctx->hres = S_OK;
290
291     ctx->last_token = tNL;
292     ctx->last_nl = 0;
293     ctx->stats = ctx->stats_tail = NULL;
294     ctx->option_explicit = FALSE;
295
296     parser_parse(ctx);
297
298     if(FAILED(ctx->hres))
299         return ctx->hres;
300     if(!ctx->parse_complete) {
301         FIXME("parser failed on parsing %s\n", debugstr_w(ctx->ptr));
302         return E_FAIL;
303     }
304
305     return S_OK;
306 }