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