include: Add IConfigAviMux definition.
[wine/multimedia.git] / dlls / vbscript / parser.y
blob6e1eb9542fa486a55ebbda9b3974edaac7338b08
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*,BOOL,dim_list_t*);
61 static dim_list_t *new_dim(parser_ctx_t*,unsigned,dim_list_t*);
62 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,expression_t*,statement_t*);
63 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
64 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
65 static const_decl_t *new_const_decl(parser_ctx_t*,const WCHAR*,expression_t*);
66 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_t*,case_clausule_t*);
68 static class_decl_t *new_class_decl(parser_ctx_t*);
69 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
70 static class_decl_t *add_variant_prop(parser_ctx_t*,class_decl_t*,const WCHAR*,unsigned);
72 static statement_t *link_statements(statement_t*,statement_t*);
74 static const WCHAR propertyW[] = {'p','r','o','p','e','r','t','y',0};
76 #define STORAGE_IS_PRIVATE 1
77 #define STORAGE_IS_DEFAULT 2
79 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
83 %lex-param { parser_ctx_t *ctx }
84 %parse-param { parser_ctx_t *ctx }
85 %pure-parser
86 %start Program
88 %union {
89 const WCHAR *string;
90 statement_t *statement;
91 expression_t *expression;
92 member_expression_t *member;
93 elseif_decl_t *elseif;
94 dim_decl_t *dim_decl;
95 dim_list_t *dim_list;
96 function_decl_t *func_decl;
97 arg_decl_t *arg_decl;
98 class_decl_t *class_decl;
99 const_decl_t *const_decl;
100 case_clausule_t *case_clausule;
101 unsigned uint;
102 LONG lng;
103 BOOL bool;
104 double dbl;
107 %token tEOF tNL tREM tEMPTYBRACKETS
108 %token tTRUE tFALSE
109 %token tNOT tAND tOR tXOR tEQV tIMP tNEQ
110 %token tIS tLTEQ tGTEQ tMOD
111 %token tCALL tDIM tSUB tFUNCTION tPROPERTY tGET tLET tCONST
112 %token tIF tELSE tELSEIF tEND tTHEN tEXIT
113 %token tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tSTEP tEACH tIN
114 %token tSELECT tCASE
115 %token tBYREF tBYVAL
116 %token tOPTION tEXPLICIT
117 %token tSTOP
118 %token tNOTHING tEMPTY tNULL
119 %token tCLASS tSET tNEW tPUBLIC tPRIVATE tDEFAULT tME
120 %token tERROR tNEXT tON tRESUME tGOTO
121 %token <string> tIdentifier tString
122 %token <lng> tLong tShort
123 %token <dbl> tDouble
125 %type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt IfStatement Else_opt
126 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression
127 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
128 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression
129 %type <expression> ConstExpression NumericLiteralExpression
130 %type <member> MemberExpression
131 %type <expression> Arguments_opt ArgumentList_opt Step_opt ExpressionList
132 %type <bool> OptionExplicit_opt DoType
133 %type <arg_decl> ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
134 %type <func_decl> FunctionDecl PropertyDecl
135 %type <elseif> ElseIfs_opt ElseIfs ElseIf
136 %type <class_decl> ClassDeclaration ClassBody
137 %type <uint> Storage Storage_opt IntegerValue
138 %type <dim_decl> DimDeclList DimDecl
139 %type <dim_list> DimList
140 %type <const_decl> ConstDecl ConstDeclList
141 %type <string> Identifier
142 %type <case_clausule> CaseClausules
146 Program
147 : OptionExplicit_opt SourceElements tEOF { parse_complete(ctx, $1); }
149 OptionExplicit_opt
150 : /* empty */ { $$ = FALSE; }
151 | tOPTION tEXPLICIT tNL { $$ = TRUE; }
153 SourceElements
154 : /* empty */
155 | SourceElements StatementNl { source_add_statement(ctx, $2); }
156 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
158 StatementsNl_opt
159 : /* empty */ { $$ = NULL; }
160 | StatementsNl { $$ = $1; }
162 StatementsNl
163 : StatementNl { $$ = $1; }
164 | StatementNl StatementsNl { $$ = link_statements($1, $2); }
166 StatementNl
167 : Statement tNL { $$ = $1; }
169 Statement
170 : ':' { $$ = NULL; }
171 | ':' Statement { $$ = $2; }
172 | SimpleStatement { $$ = $1; }
173 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
174 | SimpleStatement ':' { $$ = $1; }
176 SimpleStatement
177 : MemberExpression ArgumentList_opt { $1->args = $2; $$ = new_call_statement(ctx, FALSE, $1); CHECK_ERROR; }
178 | tCALL MemberExpression Arguments_opt { $2->args = $3; $$ = new_call_statement(ctx, TRUE, $2); CHECK_ERROR; }
179 | MemberExpression Arguments_opt '=' Expression
180 { $1->args = $2; $$ = new_assign_statement(ctx, $1, $4); CHECK_ERROR; }
181 | tDIM DimDeclList { $$ = new_dim_statement(ctx, $2); CHECK_ERROR; }
182 | IfStatement { $$ = $1; }
183 | tWHILE Expression tNL StatementsNl_opt tWEND
184 { $$ = new_while_statement(ctx, STAT_WHILE, $2, $4); CHECK_ERROR; }
185 | tDO DoType Expression tNL StatementsNl_opt tLOOP
186 { $$ = new_while_statement(ctx, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
187 CHECK_ERROR; }
188 | tDO tNL StatementsNl_opt tLOOP DoType Expression
189 { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
190 CHECK_ERROR; }
191 | tDO tNL StatementsNl_opt tLOOP { $$ = new_while_statement(ctx, STAT_DOWHILE, NULL, $3); CHECK_ERROR; }
192 | FunctionDecl { $$ = new_function_statement(ctx, $1); CHECK_ERROR; }
193 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0); CHECK_ERROR; }
194 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0); CHECK_ERROR; }
195 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0); CHECK_ERROR; }
196 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0); CHECK_ERROR; }
197 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0); CHECK_ERROR; }
198 | tSET MemberExpression Arguments_opt '=' Expression
199 { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
200 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
201 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
202 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, FALSE); CHECK_ERROR; }
203 | tCONST ConstDeclList { $$ = new_const_statement(ctx, $2); CHECK_ERROR; }
204 | tFOR Identifier '=' Expression tTO Expression Step_opt tNL StatementsNl_opt tNEXT
205 { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
206 | tFOR tEACH Identifier tIN Expression tNL StatementsNl_opt tNEXT
207 { $$ = new_foreach_statement(ctx, $3, $5, $7); }
208 | tSELECT tCASE Expression tNL CaseClausules tEND tSELECT
209 { $$ = new_select_statement(ctx, $3, $5); }
211 MemberExpression
212 : Identifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
213 | CallExpression '.' Identifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
215 DimDeclList
216 : DimDecl { $$ = $1; }
217 | DimDecl ',' DimDeclList { $1->next = $3; $$ = $1; }
219 DimDecl
220 : Identifier { $$ = new_dim_decl(ctx, $1, FALSE, NULL); CHECK_ERROR; }
221 | Identifier '(' DimList ')' { $$ = new_dim_decl(ctx, $1, TRUE, $3); CHECK_ERROR; }
222 | Identifier tEMPTYBRACKETS { $$ = new_dim_decl(ctx, $1, TRUE, NULL); CHECK_ERROR; }
224 DimList
225 : IntegerValue { $$ = new_dim(ctx, $1, NULL); }
226 | IntegerValue ',' DimList { $$ = new_dim(ctx, $1, $3); }
228 ConstDeclList
229 : ConstDecl { $$ = $1; }
230 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
232 ConstDecl
233 : Identifier '=' ConstExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
235 ConstExpression
236 : LiteralExpression { $$ = $1; }
237 | '-' NumericLiteralExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
239 DoType
240 : tWHILE { $$ = TRUE; }
241 | tUNTIL { $$ = FALSE; }
243 Step_opt
244 : /* empty */ { $$ = NULL;}
245 | tSTEP Expression { $$ = $2; }
247 IfStatement
248 : tIF Expression tTHEN tNL StatementsNl_opt ElseIfs_opt Else_opt tEND tIF
249 { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
250 | tIF Expression tTHEN Statement { $$ = new_if_statement(ctx, $2, $4, NULL, NULL); CHECK_ERROR; }
251 | tIF Expression tTHEN Statement tELSE Statement EndIf_opt
252 { $$ = new_if_statement(ctx, $2, $4, NULL, $6); CHECK_ERROR; }
254 EndIf_opt
255 : /* empty */
256 | tEND tIF
258 ElseIfs_opt
259 : /* empty */ { $$ = NULL; }
260 | ElseIfs { $$ = $1; }
262 ElseIfs
263 : ElseIf { $$ = $1; }
264 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
266 ElseIf
267 : tELSEIF Expression tTHEN tNL StatementsNl_opt
268 { $$ = new_elseif_decl(ctx, $2, $5); }
270 Else_opt
271 : /* empty */ { $$ = NULL; }
272 | tELSE tNL StatementsNl_opt { $$ = $3; }
274 CaseClausules
275 : /* empty */ { $$ = NULL; }
276 | tCASE tELSE tNL StatementsNl { $$ = new_case_clausule(ctx, NULL, $4, NULL); }
277 | tCASE ExpressionList tNL StatementsNl_opt CaseClausules
278 { $$ = new_case_clausule(ctx, $2, $4, $5); }
280 Arguments_opt
281 : EmptyBrackets_opt { $$ = NULL; }
282 | '(' ExpressionList ')' { $$ = $2; }
284 ArgumentList_opt
285 : EmptyBrackets_opt { $$ = NULL; }
286 | ExpressionList { $$ = $1; }
288 EmptyBrackets_opt
289 : /* empty */
290 | tEMPTYBRACKETS
292 ExpressionList
293 : Expression { $$ = $1; }
294 | Expression ',' ExpressionList { $1->next = $3; $$ = $1; }
296 Expression
297 : EqvExpression { $$ = $1; }
298 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
300 EqvExpression
301 : XorExpression { $$ = $1; }
302 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
304 XorExpression
305 : OrExpression { $$ = $1; }
306 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
308 OrExpression
309 : AndExpression { $$ = $1; }
310 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
312 AndExpression
313 : NotExpression { $$ = $1; }
314 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
316 NotExpression
317 : EqualityExpression { $$ = $1; }
318 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
320 EqualityExpression
321 : ConcatExpression { $$ = $1; }
322 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
323 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
324 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
325 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
326 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
327 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
328 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
330 ConcatExpression
331 : AdditiveExpression { $$ = $1; }
332 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
334 AdditiveExpression
335 : ModExpression { $$ = $1; }
336 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
337 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
339 ModExpression
340 : IntdivExpression { $$ = $1; }
341 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
343 IntdivExpression
344 : MultiplicativeExpression { $$ = $1; }
345 | IntdivExpression '\\' MultiplicativeExpression
346 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
348 MultiplicativeExpression
349 : ExpExpression { $$ = $1; }
350 | MultiplicativeExpression '*' ExpExpression
351 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
352 | MultiplicativeExpression '/' ExpExpression
353 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
355 ExpExpression
356 : UnaryExpression { $$ = $1; }
357 | ExpExpression '^' UnaryExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
359 UnaryExpression
360 : LiteralExpression { $$ = $1; }
361 | CallExpression { $$ = $1; }
362 | tNEW Identifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
363 | '-' UnaryExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
365 CallExpression
366 : PrimaryExpression { $$ = $1; }
367 | MemberExpression Arguments_opt { $1->args = $2; $$ = &$1->expr; }
369 LiteralExpression
370 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
371 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
372 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
373 | NumericLiteralExpression { $$ = $1; }
374 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
375 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
376 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
378 NumericLiteralExpression
379 : tShort { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
380 | '0' { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
381 | tLong { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
382 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
384 IntegerValue
385 : tShort { $$ = $1; }
386 | '0' { $$ = 0; }
387 | tLong { $$ = $1; }
389 PrimaryExpression
390 : '(' Expression ')' { $$ = new_unary_expression(ctx, EXPR_BRACKETS, $2); }
391 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
393 ClassDeclaration
394 : tCLASS Identifier tNL ClassBody tEND tCLASS tNL { $4->name = $2; $$ = $4; }
396 ClassBody
397 : /* empty */ { $$ = new_class_decl(ctx); }
398 | FunctionDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
399 | Storage tIdentifier tNL ClassBody { $$ = add_variant_prop(ctx, $4, $2, $1); CHECK_ERROR; }
400 | PropertyDecl tNL ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
402 PropertyDecl
403 : Storage_opt tPROPERTY tGET tIdentifier EmptyBrackets_opt tNL StatementsNl_opt tEND tPROPERTY
404 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, NULL, $7); CHECK_ERROR; }
405 | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
406 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
407 | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' tNL StatementsNl_opt tEND tPROPERTY
408 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
410 FunctionDecl
411 : Storage_opt tSUB Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tSUB
412 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
413 | Storage_opt tFUNCTION Identifier ArgumentsDecl_opt tNL StatementsNl_opt tEND tFUNCTION
414 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
416 Storage_opt
417 : /* empty*/ { $$ = 0; }
418 | Storage { $$ = $1; }
420 Storage
421 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
422 | tPUBLIC { $$ = 0; }
423 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
425 ArgumentsDecl_opt
426 : EmptyBrackets_opt { $$ = NULL; }
427 | '(' ArgumentDeclList ')' { $$ = $2; }
429 ArgumentDeclList
430 : ArgumentDecl { $$ = $1; }
431 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
433 ArgumentDecl
434 : Identifier { $$ = new_argument_decl(ctx, $1, TRUE); }
435 | tBYREF Identifier { $$ = new_argument_decl(ctx, $2, TRUE); }
436 | tBYVAL Identifier { $$ = new_argument_decl(ctx, $2, FALSE); }
438 /* 'property' may be both keyword and identifier, depending on context */
439 Identifier
440 : tIdentifier { $$ = $1; }
441 | tPROPERTY { $$ = propertyW; }
444 static int parser_error(parser_ctx_t *ctx, const char *str)
446 return 0;
449 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
451 if(!stat)
452 return;
454 if(ctx->stats) {
455 ctx->stats_tail->next = stat;
456 ctx->stats_tail = stat;
457 }else {
458 ctx->stats = ctx->stats_tail = stat;
462 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
464 class_decl->next = ctx->class_decls;
465 ctx->class_decls = class_decl;
468 static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
470 ctx->parse_complete = TRUE;
471 ctx->option_explicit = option_explicit;
474 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
476 expression_t *expr;
478 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
479 if(expr) {
480 expr->type = type;
481 expr->next = NULL;
484 return expr;
487 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
489 bool_expression_t *expr;
491 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
492 if(!expr)
493 return NULL;
495 expr->value = value;
496 return &expr->expr;
499 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
501 string_expression_t *expr;
503 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
504 if(!expr)
505 return NULL;
507 expr->value = value;
508 return &expr->expr;
511 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
513 int_expression_t *expr;
515 expr = new_expression(ctx, type, sizeof(*expr));
516 if(!expr)
517 return NULL;
519 expr->value = value;
520 return &expr->expr;
523 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
525 double_expression_t *expr;
527 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
528 if(!expr)
529 return NULL;
531 expr->value = value;
532 return &expr->expr;
535 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
537 unary_expression_t *expr;
539 expr = new_expression(ctx, type, sizeof(*expr));
540 if(!expr)
541 return NULL;
543 expr->subexpr = subexpr;
544 return &expr->expr;
547 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
549 binary_expression_t *expr;
551 expr = new_expression(ctx, type, sizeof(*expr));
552 if(!expr)
553 return NULL;
555 expr->left = left;
556 expr->right = right;
557 return &expr->expr;
560 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
562 member_expression_t *expr;
564 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
565 if(!expr)
566 return NULL;
568 expr->obj_expr = obj_expr;
569 expr->identifier = identifier;
570 expr->args = NULL;
571 return expr;
574 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
576 string_expression_t *expr;
578 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
579 if(!expr)
580 return NULL;
582 expr->value = identifier;
583 return &expr->expr;
586 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
588 statement_t *stat;
590 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
591 if(stat) {
592 stat->type = type;
593 stat->next = NULL;
596 return stat;
599 static statement_t *new_call_statement(parser_ctx_t *ctx, BOOL is_strict, member_expression_t *expr)
601 call_statement_t *stat;
603 stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
604 if(!stat)
605 return NULL;
607 stat->expr = expr;
608 stat->is_strict = is_strict;
609 return &stat->stat;
612 static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
614 assign_statement_t *stat;
616 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
617 if(!stat)
618 return NULL;
620 stat->member_expr = left;
621 stat->value_expr = right;
622 return &stat->stat;
625 static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
627 assign_statement_t *stat;
629 stat = new_statement(ctx, STAT_SET, sizeof(*stat));
630 if(!stat)
631 return NULL;
633 stat->member_expr = left;
634 stat->value_expr = right;
635 return &stat->stat;
638 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL is_array, dim_list_t *dims)
640 dim_decl_t *decl;
642 decl = parser_alloc(ctx, sizeof(*decl));
643 if(!decl)
644 return NULL;
646 decl->name = name;
647 decl->is_array = is_array;
648 decl->dims = dims;
649 decl->next = NULL;
650 return decl;
653 static dim_list_t *new_dim(parser_ctx_t *ctx, unsigned val, dim_list_t *next)
655 dim_list_t *ret;
657 ret = parser_alloc(ctx, sizeof(*ret));
658 if(!ret)
659 return NULL;
661 ret->val = val;
662 ret->next = next;
663 return ret;
666 static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
668 dim_statement_t *stat;
670 stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
671 if(!stat)
672 return NULL;
674 stat->dim_decls = decls;
675 return &stat->stat;
678 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
680 elseif_decl_t *decl;
682 decl = parser_alloc(ctx, sizeof(*decl));
683 if(!decl)
684 return NULL;
686 decl->expr = expr;
687 decl->stat = stat;
688 decl->next = NULL;
689 return decl;
692 static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
694 while_statement_t *stat;
696 stat = new_statement(ctx, type, sizeof(*stat));
697 if(!stat)
698 return NULL;
700 stat->expr = expr;
701 stat->body = body;
702 return &stat->stat;
705 static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
706 expression_t *to_expr, expression_t *step_expr, statement_t *body)
708 forto_statement_t *stat;
710 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
711 if(!stat)
712 return NULL;
714 stat->identifier = identifier;
715 stat->from_expr = from_expr;
716 stat->to_expr = to_expr;
717 stat->step_expr = step_expr;
718 stat->body = body;
719 return &stat->stat;
722 static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
723 statement_t *body)
725 foreach_statement_t *stat;
727 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
728 if(!stat)
729 return NULL;
731 stat->identifier = identifier;
732 stat->group_expr = group_expr;
733 stat->body = body;
734 return &stat->stat;
737 static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
738 statement_t *else_stat)
740 if_statement_t *stat;
742 stat = new_statement(ctx, STAT_IF, sizeof(*stat));
743 if(!stat)
744 return NULL;
746 stat->expr = expr;
747 stat->if_stat = if_stat;
748 stat->elseifs = elseif_decl;
749 stat->else_stat = else_stat;
750 return &stat->stat;
753 static statement_t *new_select_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_clausules)
755 select_statement_t *stat;
757 stat = new_statement(ctx, STAT_SELECT, sizeof(*stat));
758 if(!stat)
759 return NULL;
761 stat->expr = expr;
762 stat->case_clausules = case_clausules;
763 return &stat->stat;
766 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_t *stat, case_clausule_t *next)
768 case_clausule_t *ret;
770 ret = parser_alloc(ctx, sizeof(*ret));
771 if(!ret)
772 return NULL;
774 ret->expr = expr;
775 ret->stat = stat;
776 ret->next = next;
777 return ret;
780 static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
782 onerror_statement_t *stat;
784 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
785 if(!stat)
786 return NULL;
788 stat->resume_next = resume_next;
789 return &stat->stat;
792 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
794 arg_decl_t *arg_decl;
796 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
797 if(!arg_decl)
798 return NULL;
800 arg_decl->name = name;
801 arg_decl->by_ref = by_ref;
802 arg_decl->next = NULL;
803 return arg_decl;
806 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
807 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
809 function_decl_t *decl;
811 if(storage_flags & STORAGE_IS_DEFAULT) {
812 if(type == FUNC_PROPGET) {
813 type = FUNC_DEFGET;
814 }else {
815 FIXME("Invalid default property\n");
816 ctx->hres = E_FAIL;
817 return NULL;
821 decl = parser_alloc(ctx, sizeof(*decl));
822 if(!decl)
823 return NULL;
825 decl->name = name;
826 decl->type = type;
827 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
828 decl->args = arg_decl;
829 decl->body = body;
830 decl->next = NULL;
831 decl->next_prop_func = NULL;
832 return decl;
835 static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
837 function_statement_t *stat;
839 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
840 if(!stat)
841 return NULL;
843 stat->func_decl = decl;
844 return &stat->stat;
847 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
849 class_decl_t *class_decl;
851 class_decl = parser_alloc(ctx, sizeof(*class_decl));
852 if(!class_decl)
853 return NULL;
855 class_decl->funcs = NULL;
856 class_decl->props = NULL;
857 class_decl->next = NULL;
858 return class_decl;
861 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
863 function_decl_t *iter;
865 for(iter = class_decl->funcs; iter; iter = iter->next) {
866 if(!strcmpiW(iter->name, decl->name)) {
867 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
868 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
869 ctx->hres = E_FAIL;
870 return NULL;
873 while(1) {
874 if(iter->type == decl->type) {
875 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
876 ctx->hres = E_FAIL;
877 return NULL;
879 if(!iter->next_prop_func)
880 break;
881 iter = iter->next_prop_func;
884 iter->next_prop_func = decl;
885 return class_decl;
889 decl->next = class_decl->funcs;
890 class_decl->funcs = decl;
891 return class_decl;
894 static class_decl_t *add_variant_prop(parser_ctx_t *ctx, class_decl_t *class_decl, const WCHAR *identifier, unsigned storage_flags)
896 class_prop_decl_t *prop;
898 if(storage_flags & STORAGE_IS_DEFAULT) {
899 FIXME("variant prop van't be default value\n");
900 ctx->hres = E_FAIL;
901 return NULL;
904 prop = parser_alloc(ctx, sizeof(*prop));
905 if(!prop)
906 return NULL;
908 prop->name = identifier;
909 prop->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
910 prop->next = class_decl->props;
911 class_decl->props = prop;
912 return class_decl;
915 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
917 const_decl_t *decl;
919 decl = parser_alloc(ctx, sizeof(*decl));
920 if(!decl)
921 return NULL;
923 decl->name = name;
924 decl->value_expr = expr;
925 decl->next = NULL;
926 return decl;
929 static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
931 const_statement_t *stat;
933 stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
934 if(!stat)
935 return NULL;
937 stat->decls = decls;
938 return &stat->stat;
941 static statement_t *link_statements(statement_t *head, statement_t *tail)
943 statement_t *iter;
945 for(iter = head; iter->next; iter = iter->next);
946 iter->next = tail;
948 return head;
951 void *parser_alloc(parser_ctx_t *ctx, size_t size)
953 void *ret;
955 ret = heap_pool_alloc(&ctx->heap, size);
956 if(!ret)
957 ctx->hres = E_OUTOFMEMORY;
958 return ret;
961 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter)
963 const WCHAR html_delimiterW[] = {'<','/','s','c','r','i','p','t','>',0};
965 ctx->code = ctx->ptr = code;
966 ctx->end = ctx->code + strlenW(ctx->code);
968 heap_pool_init(&ctx->heap);
970 ctx->parse_complete = FALSE;
971 ctx->hres = S_OK;
973 ctx->last_token = tNL;
974 ctx->last_nl = 0;
975 ctx->stats = ctx->stats_tail = NULL;
976 ctx->class_decls = NULL;
977 ctx->option_explicit = FALSE;
978 ctx->is_html = delimiter && !strcmpiW(delimiter, html_delimiterW);
980 parser_parse(ctx);
982 if(FAILED(ctx->hres))
983 return ctx->hres;
984 if(!ctx->parse_complete) {
985 FIXME("parser failed around %s\n", debugstr_w(ctx->code+20 > ctx->ptr ? ctx->code : ctx->ptr-20));
986 return E_FAIL;
989 return S_OK;
992 void parser_release(parser_ctx_t *ctx)
994 heap_pool_free(&ctx->heap);