gdiplus/tests: Use BOOL type where appropriate.
[wine.git] / dlls / vbscript / parser.y
blob3c54fd88ba862b581997f9fc03901e6e409b56a4
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
21 #include "vbscript.h"
22 #include "parse.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 static int parser_error(parser_ctx_t *,const char*);
30 static void parse_complete(parser_ctx_t*,BOOL);
32 static void source_add_statement(parser_ctx_t*,statement_t*);
33 static void source_add_class(parser_ctx_t*,class_decl_t*);
35 static void *new_expression(parser_ctx_t*,expression_type_t,size_t);
36 static expression_t *new_bool_expression(parser_ctx_t*,VARIANT_BOOL);
37 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
38 static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
39 static expression_t *new_double_expression(parser_ctx_t*,double);
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 static expression_t *new_new_expression(parser_ctx_t*,const WCHAR*);
44 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
46 static void *new_statement(parser_ctx_t*,statement_type_t,size_t);
47 static statement_t *new_call_statement(parser_ctx_t*,BOOL,member_expression_t*);
48 static statement_t *new_assign_statement(parser_ctx_t*,member_expression_t*,expression_t*);
49 static statement_t *new_set_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_while_statement(parser_ctx_t*,statement_type_t,expression_t*,statement_t*);
52 static statement_t *new_forto_statement(parser_ctx_t*,const WCHAR*,expression_t*,expression_t*,expression_t*,statement_t*);
53 static statement_t *new_foreach_statement(parser_ctx_t*,const WCHAR*,expression_t*,statement_t*);
54 static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
55 static statement_t *new_function_statement(parser_ctx_t*,function_decl_t*);
56 static statement_t *new_onerror_statement(parser_ctx_t*,BOOL);
57 static statement_t *new_const_statement(parser_ctx_t*,const_decl_t*);
58 static statement_t *new_select_statement(parser_ctx_t*,expression_t*,case_clausule_t*);
60 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,dim_decl_t*);
61 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
62 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
63 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
64 static const_decl_t *new_const_decl(parser_ctx_t*,const WCHAR*,expression_t*);
65 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_t*,case_clausule_t*);
67 static class_decl_t *new_class_decl(parser_ctx_t*);
68 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
69 static class_decl_t *add_variant_prop(parser_ctx_t*,class_decl_t*,const WCHAR*,unsigned);
71 static statement_t *link_statements(statement_t*,statement_t*);
73 static const WCHAR propertyW[] = {'p','r','o','p','e','r','t','y',0};
75 #define STORAGE_IS_PRIVATE 1
76 #define STORAGE_IS_DEFAULT 2
78 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
82 %lex-param { parser_ctx_t *ctx }
83 %parse-param { parser_ctx_t *ctx }
84 %pure-parser
85 %start Program
87 %union {
88 const WCHAR *string;
89 statement_t *statement;
90 expression_t *expression;
91 member_expression_t *member;
92 elseif_decl_t *elseif;
93 dim_decl_t *dim_decl;
94 function_decl_t *func_decl;
95 arg_decl_t *arg_decl;
96 class_decl_t *class_decl;
97 const_decl_t *const_decl;
98 case_clausule_t *case_clausule;
99 unsigned uint;
100 LONG lng;
101 BOOL bool;
102 double dbl;
105 %token tEOF tNL tREM tEMPTYBRACKETS
106 %token tTRUE tFALSE
107 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
108 %token tIS tLTEQ tGTEQ tMOD
109 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET tCONST
110 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
111 %token tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tSTEP tEACH tIN
112 %token tSELECT tCASE
113 %token tBYREF tBYVAL
114 %token tOPTION tEXPLICIT
115 %token tSTOP
116 %token tNOTHING tEMPTY tNULL
117 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
118 %token tERROR tNEXT tON tRESUME tGOTO
119 %token <string> tIdentifier tString
120 %token <lng> tLong tShort
121 %token <dbl> tDouble
123 %type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
124 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
125 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
126 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
127 %type <expression> ConstExpression NumericLiteralExpression
128 %type <member> MemberExpression
129 %type <expression> Arguments_opt ArgumentList_opt Step_opt ExpressionList
130 %type <bool> OptionExplicit_opt DoType
131 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
132 %type <func_decl> FunctionDecl PropertyDecl
133 %type <elseif> ElseIfs_opt ElseIfs ElseIf
134 %type <class_decl> ClassDeclaration ClassBody
135 %type <uint> Storage Storage_opt
136 %type <dim_decl> DimDeclList
137 %type <const_decl> ConstDecl ConstDeclList
138 %type <string> Identifier
139 %type <case_clausule> CaseClausules
143 Program
144 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
146 OptionExplicit_opt
147 : /* empty */ { $$ = FALSE; }
148 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
150 SourceElements
151 : /* empty */
152 | SourceElements StatementNl { source_add_statement(ctx, $2); }
153 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
155 StatementsNl_opt
156 : /* empty */ { $$ = NULL; }
157 | StatementsNl { $$ = $1; }
159 StatementsNl
160 : StatementNl { $$ = $1; }
161 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
163 StatementNl
164 : Statement tNL { $$ = $1; }
166 Statement
167 : ':' { $$ = NULL; }
168 | ':' Statement { $$ = $2; }
169 | SimpleStatement { $$ = $1; }
170 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
171 | SimpleStatement ':' { $$ = $1; }
173 SimpleStatement
174 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, FALSE, $1); CHECK_ERROR; }
175 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, TRUE, $2); CHECK_ERROR; }
176 | MemberExpression Arguments_opt '=' Expression
177 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
178 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
179 | IfStatement { $$ = $1; }
180 | tWHILE Expression tNL StatementsNl_opt tWEND
181 { $$ = new_while_statement(ctx, STAT_WHILE, $2, $4); CHECK_ERROR; }
182 | tDO DoType Expression tNL StatementsNl_opt tLOOP
183 { $$ = new_while_statement(ctx, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
184 CHECK_ERROR; }
185 | tDO tNL StatementsNl_opt tLOOP DoType Expression
186 { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
187 CHECK_ERROR; }
188 | tDO tNL StatementsNl_opt tLOOP { $$ = new_while_statement(ctx, STAT_DOWHILE, NULL, $3); CHECK_ERROR; }
189 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
190 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0); CHECK_ERROR; }
191 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
192 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
193 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
194 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
195 | tSET MemberExpression Arguments_opt '=' Expression
196 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
197 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
198 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
199 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
200 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
201 | tFOR Identifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
202 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
203 | tFOR tEACH Identifier tIN Expression tNL StatementsNl_opt tNEXT
204 { $$ = new_foreach_statement(ctx, $3, $5, $7); }
205 | tSELECT tCASE Expression tNL CaseClausules tEND tSELECT
206 { $$ = new_select_statement(ctx, $3, $5); }
208 MemberExpression
209 : Identifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
210 | CallExpression '.' Identifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
212 DimDeclList /* FIXME: Support arrays */
213 : Identifier { $$ = new_dim_decl(ctx, $1, NULL); CHECK_ERROR; }
214 | Identifier ',' DimDeclList { $$ = new_dim_decl(ctx, $1, $3); CHECK_ERROR; }
216 ConstDeclList
217 : ConstDecl { $$ = $1; }
218 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
220 ConstDecl
221 : Identifier '=' ConstExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
223 ConstExpression
224 : LiteralExpression { $$ = $1; }
225 | '-' NumericLiteralExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
227 DoType
228 : tWHILE { $$ = TRUE; }
229 | tUNTIL { $$ = FALSE; }
231 Step_opt
232 : /* empty */ { $$ = NULL;}
233 | tSTEP Expression { $$ = $2; }
235 IfStatement
236 : tIF Expression tTHEN tNL StatementsNl_opt ElseIfs_opt Else_opt tEND tIF
237 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
238 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
239 | tIF Expression tTHEN Statement tELSE Statement EndIf_opt
240 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
242 EndIf_opt
243 : /* empty */
244 | tEND tIF
246 ElseIfs_opt
247 : /* empty */ { $$ = NULL; }
248 | ElseIfs { $$ = $1; }
250 ElseIfs
251 : ElseIf { $$ = $1; }
252 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
254 ElseIf
255 : tELSEIF Expression tTHEN tNL StatementsNl_opt
256 { $$ = new_elseif_decl(ctx, $2, $5); }
258 Else_opt
259 : /* empty */ { $$ = NULL; }
260 | tELSE tNL StatementsNl_opt { $$ = $3; }
262 CaseClausules
263 : /* empty */ { $$ = NULL; }
264 | tCASE tELSE tNL StatementsNl { $$ = new_case_clausule(ctx, NULL, $4, NULL); }
265 | tCASE ExpressionList tNL StatementsNl_opt CaseClausules
266 { $$ = new_case_clausule(ctx, $2, $4, $5); }
268 Arguments_opt
269 : EmptyBrackets_opt { $$ = NULL; }
270 | '(' ExpressionList ')' { $$ = $2; }
272 ArgumentList_opt
273 : EmptyBrackets_opt { $$ = NULL; }
274 | ExpressionList { $$ = $1; }
276 EmptyBrackets_opt
277 : /* empty */
278 | tEMPTYBRACKETS
280 ExpressionList
281 : Expression { $$ = $1; }
282 | Expression ',' ExpressionList { $1->next = $3; $$ = $1; }
284 Expression
285 : EqvExpression { $$ = $1; }
286 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
288 EqvExpression
289 : XorExpression { $$ = $1; }
290 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
292 XorExpression
293 : OrExpression { $$ = $1; }
294 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
296 OrExpression
297 : AndExpression { $$ = $1; }
298 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
300 AndExpression
301 : NotExpression { $$ = $1; }
302 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
304 NotExpression
305 : EqualityExpression { $$ = $1; }
306 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
308 EqualityExpression
309 : ConcatExpression { $$ = $1; }
310 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
311 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
312 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
313 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
314 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
315 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
316 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
318 ConcatExpression
319 : AdditiveExpression { $$ = $1; }
320 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
322 AdditiveExpression
323 : ModExpression { $$ = $1; }
324 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
325 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
327 ModExpression
328 : IntdivExpression { $$ = $1; }
329 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
331 IntdivExpression
332 : MultiplicativeExpression { $$ = $1; }
333 | IntdivExpression '\\' MultiplicativeExpression
334 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
336 MultiplicativeExpression
337 : ExpExpression { $$ = $1; }
338 | MultiplicativeExpression '*' ExpExpression
339 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
340 | MultiplicativeExpression '/' ExpExpression
341 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
343 ExpExpression
344 : UnaryExpression { $$ = $1; }
345 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
347 UnaryExpression
348 : LiteralExpression { $$ = $1; }
349 | CallExpression { $$ = $1; }
350 | tNEW Identifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
351 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
353 CallExpression
354 : PrimaryExpression { $$ = $1; }
355 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
357 LiteralExpression
358 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
359 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
360 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
361 | NumericLiteralExpression { $$ = $1; }
362 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
363 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
364 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
366 NumericLiteralExpression
367 : tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
368 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
369 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
370 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
373 PrimaryExpression
374 : '(' Expression ')' { $$ = new_unary_expression(ctx, EXPR_BRACKETS, $2); }
375 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
377 ClassDeclaration
378 : tCLASS Identifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
380 ClassBody
381 : /* empty */ { $$ = new_class_decl(ctx); }
382 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
383 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
384 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
386 PropertyDecl
387 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
388 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
389 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
390 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
391 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
392 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
394 FunctionDecl
395 : Storage_opt tSUB Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
396 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
397 | Storage_opt tFUNCTION Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
398 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
400 Storage_opt
401 : /* empty*/ { $$ = 0; }
402 | Storage { $$ = $1; }
404 Storage
405 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
406 | tPUBLIC { $$ = 0; }
407 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
409 ArgumentsDecl_opt
410 : EmptyBrackets_opt { $$ = NULL; }
411 | '(' ArgumentDeclList ')' { $$ = $2; }
413 ArgumentDeclList
414 : ArgumentDecl { $$ = $1; }
415 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
417 ArgumentDecl
418 : Identifier { $$ = new_argument_decl(ctx, $1, TRUE); }
419 | tBYREF Identifier { $$ = new_argument_decl(ctx, $2, TRUE); }
420 | tBYVAL Identifier { $$ = new_argument_decl(ctx, $2, FALSE); }
422 /* 'property' may be both keyword and identifier, depending on context */
423 Identifier
424 : tIdentifier { $$ = $1; }
425 | tPROPERTY { $$ = propertyW; }
428 static int parser_error(parser_ctx_t *ctx, const char *str)
430 return 0;
433 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
435 if(!stat)
436 return;
438 if(ctx->stats) {
439 ctx->stats_tail->next = stat;
440 ctx->stats_tail = stat;
441 }else {
442 ctx->stats = ctx->stats_tail = stat;
446 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
448 class_decl->next = ctx->class_decls;
449 ctx->class_decls = class_decl;
452 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
454 ctx->parse_complete = TRUE;
455 ctx->option_explicit = option_explicit;
458 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
460 expression_t *expr;
462 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
463 if(expr) {
464 expr->type = type;
465 expr->next = NULL;
468 return expr;
471 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
473 bool_expression_t *expr;
475 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
476 if(!expr)
477 return NULL;
479 expr->value = value;
480 return &expr->expr;
483 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
485 string_expression_t *expr;
487 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
488 if(!expr)
489 return NULL;
491 expr->value = value;
492 return &expr->expr;
495 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
497 int_expression_t *expr;
499 expr = new_expression(ctx, type, sizeof(*expr));
500 if(!expr)
501 return NULL;
503 expr->value = value;
504 return &expr->expr;
507 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
509 double_expression_t *expr;
511 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
512 if(!expr)
513 return NULL;
515 expr->value = value;
516 return &expr->expr;
519 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
521 unary_expression_t *expr;
523 expr = new_expression(ctx, type, sizeof(*expr));
524 if(!expr)
525 return NULL;
527 expr->subexpr = subexpr;
528 return &expr->expr;
531 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
533 binary_expression_t *expr;
535 expr = new_expression(ctx, type, sizeof(*expr));
536 if(!expr)
537 return NULL;
539 expr->left = left;
540 expr->right = right;
541 return &expr->expr;
544 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
546 member_expression_t *expr;
548 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
549 if(!expr)
550 return NULL;
552 expr->obj_expr = obj_expr;
553 expr->identifier = identifier;
554 expr->args = NULL;
555 return expr;
558 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
560 string_expression_t *expr;
562 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
563 if(!expr)
564 return NULL;
566 expr->value = identifier;
567 return &expr->expr;
570 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
572 statement_t *stat;
574 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
575 if(stat) {
576 stat->type = type;
577 stat->next = NULL;
580 return stat;
583 static statement_t *new_call_statement(parser_ctx_t *ctx, BOOL is_strict, member_expression_t *expr)
585 call_statement_t *stat;
587 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
588 if(!stat)
589 return NULL;
591 stat->expr = expr;
592 stat->is_strict = is_strict;
593 return &stat->stat;
596 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
598 assign_statement_t *stat;
600 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
601 if(!stat)
602 return NULL;
604 stat->member_expr = left;
605 stat->value_expr = right;
606 return &stat->stat;
609 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
611 assign_statement_t *stat;
613 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
614 if(!stat)
615 return NULL;
617 stat->member_expr = left;
618 stat->value_expr = right;
619 return &stat->stat;
622 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, dim_decl_t *next)
624 dim_decl_t *decl;
626 decl = parser_alloc(ctx, sizeof(*decl));
627 if(!decl)
628 return NULL;
630 decl->name = name;
631 decl->next = next;
632 return decl;
635 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
637 dim_statement_t *stat;
639 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
640 if(!stat)
641 return NULL;
643 stat->dim_decls = decls;
644 return &stat->stat;
647 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
649 elseif_decl_t *decl;
651 decl = parser_alloc(ctx, sizeof(*decl));
652 if(!decl)
653 return NULL;
655 decl->expr = expr;
656 decl->stat = stat;
657 decl->next = NULL;
658 return decl;
661 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
663 while_statement_t *stat;
665 stat = new_statement(ctx, type, sizeof(*stat));
666 if(!stat)
667 return NULL;
669 stat->expr = expr;
670 stat->body = body;
671 return &stat->stat;
674 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
675 expression_t *to_expr, expression_t *step_expr, statement_t *body)
677 forto_statement_t *stat;
679 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
680 if(!stat)
681 return NULL;
683 stat->identifier = identifier;
684 stat->from_expr = from_expr;
685 stat->to_expr = to_expr;
686 stat->step_expr = step_expr;
687 stat->body = body;
688 return &stat->stat;
691 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
692 statement_t *body)
694 foreach_statement_t *stat;
696 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
697 if(!stat)
698 return NULL;
700 stat->identifier = identifier;
701 stat->group_expr = group_expr;
702 stat->body = body;
703 return &stat->stat;
706 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
707 statement_t *else_stat)
709 if_statement_t *stat;
711 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
712 if(!stat)
713 return NULL;
715 stat->expr = expr;
716 stat->if_stat = if_stat;
717 stat->elseifs = elseif_decl;
718 stat->else_stat = else_stat;
719 return &stat->stat;
722 static statement_t *new_select_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_clausules)
724 select_statement_t *stat;
726 stat = new_statement(ctx, STAT_SELECT, sizeof(*stat));
727 if(!stat)
728 return NULL;
730 stat->expr = expr;
731 stat->case_clausules = case_clausules;
732 return &stat->stat;
735 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_t *stat, case_clausule_t *next)
737 case_clausule_t *ret;
739 ret = parser_alloc(ctx, sizeof(*ret));
740 if(!ret)
741 return NULL;
743 ret->expr = expr;
744 ret->stat = stat;
745 ret->next = next;
746 return ret;
749 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
751 onerror_statement_t *stat;
753 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
754 if(!stat)
755 return NULL;
757 stat->resume_next = resume_next;
758 return &stat->stat;
761 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
763 arg_decl_t *arg_decl;
765 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
766 if(!arg_decl)
767 return NULL;
769 arg_decl->name = name;
770 arg_decl->by_ref = by_ref;
771 arg_decl->next = NULL;
772 return arg_decl;
775 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
776 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
778 function_decl_t *decl;
780 if(storage_flags & STORAGE_IS_DEFAULT) {
781 if(type == FUNC_PROPGET) {
782 type = FUNC_DEFGET;
783 }else {
784 FIXME("Invalid default property\n");
785 ctx->hres = E_FAIL;
786 return NULL;
790 decl = parser_alloc(ctx, sizeof(*decl));
791 if(!decl)
792 return NULL;
794 decl->name = name;
795 decl->type = type;
796 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
797 decl->args = arg_decl;
798 decl->body = body;
799 decl->next = NULL;
800 decl->next_prop_func = NULL;
801 return decl;
804 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
806 function_statement_t *stat;
808 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
809 if(!stat)
810 return NULL;
812 stat->func_decl = decl;
813 return &stat->stat;
816 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
818 class_decl_t *class_decl;
820 class_decl = parser_alloc(ctx, sizeof(*class_decl));
821 if(!class_decl)
822 return NULL;
824 class_decl->funcs = NULL;
825 class_decl->props = NULL;
826 class_decl->next = NULL;
827 return class_decl;
830 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
832 function_decl_t *iter;
834 for(iter = class_decl->funcs; iter; iter = iter->next) {
835 if(!strcmpiW(iter->name, decl->name)) {
836 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
837 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
838 ctx->hres = E_FAIL;
839 return NULL;
842 while(1) {
843 if(iter->type == decl->type) {
844 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
845 ctx->hres = E_FAIL;
846 return NULL;
848 if(!iter->next_prop_func)
849 break;
850 iter = iter->next_prop_func;
853 iter->next_prop_func = decl;
854 return class_decl;
858 decl->next = class_decl->funcs;
859 class_decl->funcs = decl;
860 return class_decl;
863 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
865 class_prop_decl_t *prop;
867 if(storage_flags & STORAGE_IS_DEFAULT) {
868 FIXME("variant prop van't be default value\n");
869 ctx->hres = E_FAIL;
870 return NULL;
873 prop = parser_alloc(ctx, sizeof(*prop));
874 if(!prop)
875 return NULL;
877 prop->name = identifier;
878 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
879 prop->next = class_decl->props;
880 class_decl->props = prop;
881 return class_decl;
884 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
886 const_decl_t *decl;
888 decl = parser_alloc(ctx, sizeof(*decl));
889 if(!decl)
890 return NULL;
892 decl->name = name;
893 decl->value_expr = expr;
894 decl->next = NULL;
895 return decl;
898 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
900 const_statement_t *stat;
902 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
903 if(!stat)
904 return NULL;
906 stat->decls = decls;
907 return &stat->stat;
910 static statement_t *link_statements(statement_t *head, statement_t *tail)
912 statement_t *iter;
914 for(iter = head; iter->next; iter = iter->next);
915 iter->next = tail;
917 return head;
920 void *parser_alloc(parser_ctx_t *ctx, size_t size)
922 void *ret;
924 ret = heap_pool_alloc(&ctx->heap, size);
925 if(!ret)
926 ctx->hres = E_OUTOFMEMORY;
927 return ret;
930 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter)
932 const WCHAR html_delimiterW[] = {'<','/','s','c','r','i','p','t','>',0};
934 ctx->code = ctx->ptr = code;
935 ctx->end = ctx->code + strlenW(ctx->code);
937 heap_pool_init(&ctx->heap);
939 ctx->parse_complete = FALSE;
940 ctx->hres = S_OK;
942 ctx->last_token = tNL;
943 ctx->last_nl = 0;
944 ctx->stats = ctx->stats_tail = NULL;
945 ctx->class_decls = NULL;
946 ctx->option_explicit = FALSE;
947 ctx->is_html = delimiter && !strcmpiW(delimiter, html_delimiterW);
949 parser_parse(ctx);
951 if(FAILED(ctx->hres))
952 return ctx->hres;
953 if(!ctx->parse_complete) {
954 FIXME("parser failed around %s\n", debugstr_w(ctx->code+20 > ctx->ptr ? ctx->code : ctx->ptr-20));
955 return E_FAIL;
958 return S_OK;
961 void parser_release(parser_ctx_t *ctx)
963 heap_pool_free(&ctx->heap);