http.sys: Keep connection sockets open after sending a 400 response.
[wine.git] / dlls / vbscript / parser.y
blob25b5ac513f347d0c7dcdd68b37351950d8e0d06a
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(unsigned*,parser_ctx_t*,const char*);
30 static void handle_isexpression_script(parser_ctx_t *ctx, expression_t *expr);
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_date_expression(parser_ctx_t*,DATE);
38 static expression_t *new_string_expression(parser_ctx_t*,const WCHAR*);
39 static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
40 static expression_t *new_double_expression(parser_ctx_t*,double);
41 static expression_t *new_unary_expression(parser_ctx_t*,expression_type_t,expression_t*);
42 static expression_t *new_binary_expression(parser_ctx_t*,expression_type_t,expression_t*,expression_t*);
43 static expression_t *new_new_expression(parser_ctx_t*,const WCHAR*);
45 static member_expression_t *new_member_expression(parser_ctx_t*,expression_t*,const WCHAR*);
46 static call_expression_t *new_call_expression(parser_ctx_t*,expression_t*,expression_t*);
47 static call_expression_t *make_call_expression(parser_ctx_t*,expression_t*,expression_t*);
49 static void *new_statement(parser_ctx_t*,statement_type_t,size_t,unsigned);
50 static statement_t *new_call_statement(parser_ctx_t*,unsigned,expression_t*);
51 static statement_t *new_assign_statement(parser_ctx_t*,unsigned,expression_t*,expression_t*);
52 static statement_t *new_set_statement(parser_ctx_t*,unsigned,expression_t*,expression_t*);
53 static statement_t *new_dim_statement(parser_ctx_t*,unsigned,dim_decl_t*);
54 static statement_t *new_redim_statement(parser_ctx_t*,unsigned,BOOL,redim_decl_t*);
55 static statement_t *new_while_statement(parser_ctx_t*,unsigned,statement_type_t,expression_t*,statement_t*);
56 static statement_t *new_forto_statement(parser_ctx_t*,unsigned,const WCHAR*,expression_t*,expression_t*,expression_t*,statement_t*);
57 static statement_t *new_foreach_statement(parser_ctx_t*,unsigned,const WCHAR*,expression_t*,statement_t*);
58 static statement_t *new_if_statement(parser_ctx_t*,unsigned,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
59 static statement_t *new_function_statement(parser_ctx_t*,unsigned,function_decl_t*);
60 static statement_t *new_onerror_statement(parser_ctx_t*,unsigned,BOOL);
61 static statement_t *new_const_statement(parser_ctx_t*,unsigned,const_decl_t*);
62 static statement_t *new_select_statement(parser_ctx_t*,unsigned,expression_t*,case_clausule_t*);
63 static statement_t *new_with_statement(parser_ctx_t*,unsigned,expression_t*,statement_t*);
65 static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,BOOL,dim_list_t*);
66 static dim_list_t *new_dim(parser_ctx_t*,unsigned,dim_list_t*);
67 static redim_decl_t *new_redim_decl(parser_ctx_t*,const WCHAR*,expression_t*);
68 static elseif_decl_t *new_elseif_decl(parser_ctx_t*,unsigned,expression_t*,statement_t*);
69 static function_decl_t *new_function_decl(parser_ctx_t*,const WCHAR*,function_type_t,unsigned,arg_decl_t*,statement_t*);
70 static arg_decl_t *new_argument_decl(parser_ctx_t*,const WCHAR*,BOOL);
71 static const_decl_t *new_const_decl(parser_ctx_t*,const WCHAR*,expression_t*);
72 static case_clausule_t *new_case_clausule(parser_ctx_t*,expression_t*,statement_t*,case_clausule_t*);
74 static class_decl_t *new_class_decl(parser_ctx_t*);
75 static class_decl_t *add_class_function(parser_ctx_t*,class_decl_t*,function_decl_t*);
76 static class_decl_t *add_dim_prop(parser_ctx_t*,class_decl_t*,dim_decl_t*,unsigned);
78 static statement_t *link_statements(statement_t*,statement_t*);
80 #define STORAGE_IS_PRIVATE 1
81 #define STORAGE_IS_DEFAULT 2
83 #define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT
85 #define PARSER_LTYPE unsigned
86 #define YYLLOC_DEFAULT(Cur, Rhs, N) Cur = YYRHSLOC((Rhs), (N) ? 1 : 0)
90 %lex-param { parser_ctx_t *ctx }
91 %parse-param { parser_ctx_t *ctx }
92 %define api.prefix {parser_}
93 %define api.pure
94 %start Program
96 %union {
97 const WCHAR *string;
98 statement_t *statement;
99 expression_t *expression;
100 member_expression_t *member;
101 elseif_decl_t *elseif;
102 dim_decl_t *dim_decl;
103 dim_list_t *dim_list;
104 redim_decl_t *redim_decl;
105 function_decl_t *func_decl;
106 arg_decl_t *arg_decl;
107 class_decl_t *class_decl;
108 const_decl_t *const_decl;
109 case_clausule_t *case_clausule;
110 unsigned uint;
111 LONG integer;
112 BOOL boolean;
113 double dbl;
114 DATE date;
117 %token tEXPRESSION tNL tEMPTYBRACKETS tEXPRLBRACKET
118 %token tLTEQ tGTEQ tNEQ
119 %token tSTOP tME tREM tDOT
120 %token <string> tTRUE tFALSE
121 %token <string> tNOT tAND tOR tXOR tEQV tIMP
122 %token <string> tIS tMOD
123 %token <string> tCALL tSUB tFUNCTION tGET tLET tCONST
124 %token <string> tDIM tREDIM tPRESERVE
125 %token <string> tIF tELSE tELSEIF tEND tTHEN tEXIT
126 %token <string> tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tEACH tIN
127 %token <string> tSELECT tCASE tWITH
128 %token <string> tBYREF tBYVAL
129 %token <string> tOPTION
130 %token <string> tNOTHING tEMPTY tNULL
131 %token <string> tCLASS tSET tNEW tPUBLIC tPRIVATE
132 %token <string> tNEXT tON tRESUME tGOTO
133 %token <string> tIdentifier tString
134 %token <string> tDEFAULT tERROR tEXPLICIT tPROPERTY tSTEP
135 %token <integer> tInt
136 %token <dbl> tDouble
137 %token <date> tDate
139 %type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt BodyStatements IfStatement Else_opt
140 %type <statement> GlobalDimDeclaration
141 %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression ExpressionNl_opt
142 %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression
143 %type <expression> NotExpression UnaryExpression AndExpression OrExpression XorExpression EqvExpression SignExpression
144 %type <expression> ConstExpression NumericLiteralExpression
145 %type <member> MemberExpression
146 %type <expression> Arguments ArgumentList ArgumentList_opt Step_opt ExpressionList
147 %type <boolean> DoType Preserve_opt
148 %type <arg_decl> ArgumentsDecl ArgumentsDecl_opt ArgumentDeclList ArgumentDecl
149 %type <func_decl> FunctionDecl PropertyDecl
150 %type <elseif> ElseIfs_opt ElseIfs ElseIf
151 %type <class_decl> ClassDeclaration ClassBody
152 %type <uint> Storage Storage_opt IntegerValue
153 %type <dim_decl> DimDeclList DimDecl
154 %type <dim_list> DimList
155 %type <redim_decl> ReDimDeclList ReDimDecl
156 %type <const_decl> ConstDecl ConstDeclList
157 %type <string> Identifier
158 %type <case_clausule> CaseClausules
162 Program
163 : OptionExplicit_opt SourceElements
164 | tEXPRESSION ExpressionNl_opt { handle_isexpression_script(ctx, $2); }
166 OptionExplicit_opt
167 : /* empty */
168 | tOPTION tEXPLICIT StSep { ctx->option_explicit = TRUE; }
170 SourceElements
171 : /* empty */
172 | SourceElements GlobalDimDeclaration StSep
173 { source_add_statement(ctx, $2); }
174 | SourceElements StatementNl { source_add_statement(ctx, $2); }
175 | SourceElements ClassDeclaration { source_add_class(ctx, $2); }
177 GlobalDimDeclaration
178 : tPRIVATE tCONST ConstDeclList { $$ = new_const_statement(ctx, @$, $3); CHECK_ERROR; }
179 | tPUBLIC tCONST ConstDeclList { $$ = new_const_statement(ctx, @$, $3); CHECK_ERROR; }
180 | tPRIVATE DimDeclList { $$ = new_dim_statement(ctx, @$, $2); CHECK_ERROR; }
181 | tPUBLIC DimDeclList { $$ = new_dim_statement(ctx, @$, $2); CHECK_ERROR; }
183 ExpressionNl_opt
184 : /* empty */ { $$ = NULL; }
185 | Expression tNL { $$ = $1; }
187 BodyStatements
188 : /* empty */ { $$ = NULL; }
189 | Statement { $$ = $1; }
190 | StatementNl BodyStatements { $$ = link_statements($1, $2); }
192 StatementsNl_opt
193 : /* empty */ { $$ = NULL; }
194 | StatementsNl { $$ = $1; }
196 StatementsNl
197 : SimpleStatement StSep { $$ = $1; }
198 | SimpleStatement StSep StatementsNl { $$ = link_statements($1, $3); }
200 StatementNl
201 : Statement tNL { $$ = $1; }
203 Statement
204 : ':' { $$ = NULL; }
205 | ':' Statement { $$ = $2; }
206 | SimpleStatement { $$ = $1; }
207 | SimpleStatement ':' Statement { $1->next = $3; $$ = $1; }
208 | SimpleStatement ':' { $$ = $1; }
210 SimpleStatement
211 : CallExpression ArgumentList_opt { call_expression_t *call_expr = make_call_expression(ctx, $1, $2); CHECK_ERROR;
212 $$ = new_call_statement(ctx, @$, &call_expr->expr); CHECK_ERROR; };
213 | tCALL UnaryExpression { $$ = new_call_statement(ctx, @$, $2); CHECK_ERROR; }
214 | CallExpression '=' Expression
215 { $$ = new_assign_statement(ctx, @$, $1, $3); CHECK_ERROR; }
216 | tDIM DimDeclList { $$ = new_dim_statement(ctx, @$, $2); CHECK_ERROR; }
217 | tREDIM Preserve_opt ReDimDeclList { $$ = new_redim_statement(ctx, @$, $2, $3); CHECK_ERROR; }
218 | IfStatement { $$ = $1; }
219 | tWHILE Expression StSep StatementsNl_opt tWEND
220 { $$ = new_while_statement(ctx, @$, STAT_WHILE, $2, $4); CHECK_ERROR; }
221 | tDO DoType Expression StSep StatementsNl_opt tLOOP
222 { $$ = new_while_statement(ctx, @$, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
223 CHECK_ERROR; }
224 | tDO StSep StatementsNl_opt tLOOP DoType Expression
225 { $$ = new_while_statement(ctx, @4, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
226 CHECK_ERROR; }
227 | tDO StSep StatementsNl_opt tLOOP { $$ = new_while_statement(ctx, @$, STAT_DOWHILE, NULL, $3); CHECK_ERROR; }
228 | FunctionDecl { $$ = new_function_statement(ctx, @$, $1); CHECK_ERROR; }
229 | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0, @$); CHECK_ERROR; }
230 | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0, @$); CHECK_ERROR; }
231 | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0, @$); CHECK_ERROR; }
232 | tEXIT tPROPERTY { $$ = new_statement(ctx, STAT_EXITPROP, 0, @$); CHECK_ERROR; }
233 | tEXIT tSUB { $$ = new_statement(ctx, STAT_EXITSUB, 0, @$); CHECK_ERROR; }
234 | tSET CallExpression '=' Expression { $$ = new_set_statement(ctx, @$, $2, $4); CHECK_ERROR; }
235 | tSTOP { $$ = new_statement(ctx, STAT_STOP, 0, @$); CHECK_ERROR; }
236 | tON tERROR tRESUME tNEXT { $$ = new_onerror_statement(ctx, @$, TRUE); CHECK_ERROR; }
237 | tON tERROR tGOTO '0' { $$ = new_onerror_statement(ctx, @$, FALSE); CHECK_ERROR; }
238 | tCONST ConstDeclList { $$ = new_const_statement(ctx, @$, $2); CHECK_ERROR; }
239 | tFOR Identifier '=' Expression tTO Expression Step_opt StSep StatementsNl_opt tNEXT
240 { $$ = new_forto_statement(ctx, @$, $2, $4, $6, $7, $9); CHECK_ERROR; }
241 | tFOR tEACH Identifier tIN Expression StSep StatementsNl_opt tNEXT
242 { $$ = new_foreach_statement(ctx, @$, $3, $5, $7); }
243 | tSELECT tCASE Expression StSep CaseClausules tEND tSELECT
244 { $$ = new_select_statement(ctx, @$, $3, $5); }
245 | tWITH Expression StSep StatementsNl_opt tEND tWITH
246 { $$ = new_with_statement(ctx, @$, $2, $4); }
248 MemberExpression
249 : Identifier { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
250 | CallExpression '.' tIdentifier { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
251 | tDOT tIdentifier { expression_t *dot_expr = new_expression(ctx, EXPR_DOT, sizeof(*dot_expr)); CHECK_ERROR;
252 $$ = new_member_expression(ctx, dot_expr, $2); CHECK_ERROR; }
254 Preserve_opt
255 : /* empty */ { $$ = FALSE; }
256 | tPRESERVE { $$ = TRUE; }
258 ReDimDecl
259 : tIdentifier '(' ArgumentList ')' { $$ = new_redim_decl(ctx, $1, $3); CHECK_ERROR; }
261 ReDimDeclList
262 : ReDimDecl { $$ = $1; }
263 | ReDimDecl ',' ReDimDeclList { $1->next = $3; $$ = $1; }
265 DimDeclList
266 : DimDecl { $$ = $1; }
267 | DimDecl ',' DimDeclList { $1->next = $3; $$ = $1; }
269 DimDecl
270 : Identifier { $$ = new_dim_decl(ctx, $1, FALSE, NULL); CHECK_ERROR; }
271 | Identifier '(' DimList ')' { $$ = new_dim_decl(ctx, $1, TRUE, $3); CHECK_ERROR; }
272 | Identifier tEMPTYBRACKETS { $$ = new_dim_decl(ctx, $1, TRUE, NULL); CHECK_ERROR; }
274 DimList
275 : IntegerValue { $$ = new_dim(ctx, $1, NULL); }
276 | IntegerValue ',' DimList { $$ = new_dim(ctx, $1, $3); }
278 ConstDeclList
279 : ConstDecl { $$ = $1; }
280 | ConstDecl ',' ConstDeclList { $1->next = $3; $$ = $1; }
282 ConstDecl
283 : Identifier '=' ConstExpression { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }
285 ConstExpression
286 : LiteralExpression { $$ = $1; }
287 | '-' NumericLiteralExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
289 DoType
290 : tWHILE { $$ = TRUE; }
291 | tUNTIL { $$ = FALSE; }
293 Step_opt
294 : /* empty */ { $$ = NULL;}
295 | tSTEP Expression { $$ = $2; }
297 IfStatement
298 : tIF Expression tTHEN tNL StatementsNl_opt ElseIfs_opt Else_opt tEND tIF
299 { $$ = new_if_statement(ctx, @$, $2, $5, $6, $7); CHECK_ERROR; }
300 | tIF Expression tTHEN Statement EndIf_opt { $$ = new_if_statement(ctx, @$, $2, $4, NULL, NULL); CHECK_ERROR; }
301 | tIF Expression tTHEN Statement tELSE Statement EndIf_opt
302 { $$ = new_if_statement(ctx, @$, $2, $4, NULL, $6); CHECK_ERROR; }
304 EndIf_opt
305 : /* empty */
306 | tEND tIF
308 ElseIfs_opt
309 : /* empty */ { $$ = NULL; }
310 | ElseIfs { $$ = $1; }
312 ElseIfs
313 : ElseIf { $$ = $1; }
314 | ElseIf ElseIfs { $1->next = $2; $$ = $1; }
316 ElseIf
317 : tELSEIF Expression tTHEN StSep_opt StatementsNl_opt
318 { $$ = new_elseif_decl(ctx, @$, $2, $5); }
320 Else_opt
321 : /* empty */ { $$ = NULL; }
322 | tELSE StSep_opt StatementsNl_opt { $$ = $3; }
324 CaseClausules
325 : /* empty */ { $$ = NULL; }
326 | tCASE tELSE StSep_opt StatementsNl_opt { $$ = new_case_clausule(ctx, NULL, $4, NULL); }
327 | tCASE ExpressionList StSep_opt StatementsNl_opt CaseClausules { $$ = new_case_clausule(ctx, $2, $4, $5); }
329 Arguments
330 : tEMPTYBRACKETS { $$ = NULL; }
331 | '(' ArgumentList ')' { $$ = $2; }
333 ArgumentList_opt
334 : /* empty */ { $$ = NULL; }
335 | ArgumentList { $$ = $1; }
337 ArgumentList
338 : Expression { $$ = $1; }
339 | Expression ',' ArgumentList { $1->next = $3; $$ = $1; }
340 | ',' ArgumentList { $$ = new_expression(ctx, EXPR_NOARG, 0); CHECK_ERROR; $$->next = $2; }
342 EmptyBrackets_opt
343 : /* empty */
344 | tEMPTYBRACKETS
346 ExpressionList
347 : Expression { $$ = $1; }
348 | Expression ',' ExpressionList { $1->next = $3; $$ = $1; }
350 Expression
351 : EqvExpression { $$ = $1; }
352 | Expression tIMP EqvExpression { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }
354 EqvExpression
355 : XorExpression { $$ = $1; }
356 | EqvExpression tEQV XorExpression { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }
358 XorExpression
359 : OrExpression { $$ = $1; }
360 | XorExpression tXOR OrExpression { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
362 OrExpression
363 : AndExpression { $$ = $1; }
364 | OrExpression tOR AndExpression { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
366 AndExpression
367 : NotExpression { $$ = $1; }
368 | AndExpression tAND NotExpression { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
370 NotExpression
371 : EqualityExpression { $$ = $1; }
372 | tNOT NotExpression { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }
374 EqualityExpression
375 : ConcatExpression { $$ = $1; }
376 | EqualityExpression '=' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
377 | EqualityExpression tNEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
378 | EqualityExpression '>' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
379 | EqualityExpression '<' ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
380 | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
381 | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
382 | EqualityExpression tIS ConcatExpression { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
384 ConcatExpression
385 : AdditiveExpression { $$ = $1; }
386 | ConcatExpression '&' AdditiveExpression { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }
388 AdditiveExpression
389 : ModExpression { $$ = $1; }
390 | AdditiveExpression '+' ModExpression { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
391 | AdditiveExpression '-' ModExpression { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }
393 ModExpression
394 : IntdivExpression { $$ = $1; }
395 | ModExpression tMOD IntdivExpression { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }
397 IntdivExpression
398 : MultiplicativeExpression { $$ = $1; }
399 | IntdivExpression '\\' MultiplicativeExpression
400 { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
402 MultiplicativeExpression
403 : ExpExpression { $$ = $1; }
404 | MultiplicativeExpression '*' ExpExpression
405 { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
406 | MultiplicativeExpression '/' ExpExpression
407 { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }
409 ExpExpression
410 : SignExpression { $$ = $1; }
411 | ExpExpression '^' SignExpression { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
413 SignExpression
414 : UnaryExpression { $$ = $1; }
415 | '-' SignExpression { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
416 | '+' SignExpression { $$ = $2; }
418 UnaryExpression
419 : LiteralExpression { $$ = $1; }
420 | CallExpression { $$ = $1; }
421 | tNEW Identifier { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
423 CallExpression
424 : PrimaryExpression { $$ = $1; }
425 | MemberExpression { $$ = &$1->expr; }
426 | CallExpression Arguments { call_expression_t *expr = new_call_expression(ctx, $1, $2); CHECK_ERROR;
427 $$ = &expr->expr; }
429 LiteralExpression
430 : tTRUE { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
431 | tFALSE { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
432 | tString { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
433 | tDate { $$ = new_date_expression(ctx, $1); CHECK_ERROR; }
434 | NumericLiteralExpression { $$ = $1; }
435 | tEMPTY { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
436 | tNULL { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
437 | tNOTHING { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
439 NumericLiteralExpression
440 : '0' { $$ = new_long_expression(ctx, EXPR_INT, 0); CHECK_ERROR; }
441 | tInt { $$ = new_long_expression(ctx, EXPR_INT, $1); CHECK_ERROR; }
442 | tDouble { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }
444 IntegerValue
445 : '0' { $$ = 0; }
446 | tInt { $$ = $1; }
448 PrimaryExpression
449 : tEXPRLBRACKET Expression ')' { $$ = new_unary_expression(ctx, EXPR_BRACKETS, $2); }
450 | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
452 ClassDeclaration
453 : tCLASS Identifier StSep ClassBody tEND tCLASS StSep { $4->name = $2; $$ = $4; }
455 ClassBody
456 : /* empty */ { $$ = new_class_decl(ctx); }
457 | FunctionDecl { $$ = add_class_function(ctx, new_class_decl(ctx), $1); CHECK_ERROR; }
458 | FunctionDecl StSep ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
459 /* FIXME: We should use DimDecl here to support arrays, but that conflicts with PropertyDecl. */
460 | Storage tIdentifier { dim_decl_t *dim_decl = new_dim_decl(ctx, $2, FALSE, NULL); CHECK_ERROR;
461 $$ = add_dim_prop(ctx, new_class_decl(ctx), dim_decl, $1); CHECK_ERROR; }
462 | Storage tIdentifier StSep ClassBody { dim_decl_t *dim_decl = new_dim_decl(ctx, $2, FALSE, NULL); CHECK_ERROR;
463 $$ = add_dim_prop(ctx, $4, dim_decl, $1); CHECK_ERROR; }
464 | tDIM DimDecl { $$ = add_dim_prop(ctx, new_class_decl(ctx), $2, 0); CHECK_ERROR; }
465 | tDIM DimDecl StSep ClassBody { $$ = add_dim_prop(ctx, $4, $2, 0); CHECK_ERROR; }
466 | PropertyDecl { $$ = add_class_function(ctx, new_class_decl(ctx), $1); CHECK_ERROR; }
467 | PropertyDecl StSep ClassBody { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
469 PropertyDecl
470 : Storage_opt tPROPERTY tGET Identifier ArgumentsDecl_opt StSep BodyStatements tEND tPROPERTY
471 { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, $5, $7); CHECK_ERROR; }
472 | Storage_opt tPROPERTY tLET Identifier '(' ArgumentDeclList ')' StSep BodyStatements tEND tPROPERTY
473 { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
474 | Storage_opt tPROPERTY tSET Identifier '(' ArgumentDeclList ')' StSep BodyStatements tEND tPROPERTY
475 { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
477 FunctionDecl
478 : Storage_opt tSUB Identifier StSep BodyStatements tEND tSUB
479 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, NULL, $5); CHECK_ERROR; }
480 | Storage_opt tSUB Identifier ArgumentsDecl Nl_opt BodyStatements tEND tSUB
481 { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
482 | Storage_opt tFUNCTION Identifier StSep BodyStatements tEND tFUNCTION
483 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, NULL, $5); CHECK_ERROR; }
484 | Storage_opt tFUNCTION Identifier ArgumentsDecl Nl_opt BodyStatements tEND tFUNCTION
485 { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }
487 Storage_opt
488 : /* empty*/ { $$ = 0; }
489 | Storage { $$ = $1; }
491 Storage
492 : tPUBLIC tDEFAULT { $$ = STORAGE_IS_DEFAULT; }
493 | tPUBLIC { $$ = 0; }
494 | tPRIVATE { $$ = STORAGE_IS_PRIVATE; }
496 ArgumentsDecl_opt
497 : /* empty*/ { $$ = 0; }
498 | ArgumentsDecl { $$ = $1; }
500 ArgumentsDecl
501 : tEMPTYBRACKETS { $$ = NULL; }
502 | '(' ArgumentDeclList ')' { $$ = $2; }
504 ArgumentDeclList
505 : ArgumentDecl { $$ = $1; }
506 | ArgumentDecl ',' ArgumentDeclList { $1->next = $3; $$ = $1; }
508 ArgumentDecl
509 : Identifier EmptyBrackets_opt { $$ = new_argument_decl(ctx, $1, TRUE); }
510 | tBYREF Identifier EmptyBrackets_opt { $$ = new_argument_decl(ctx, $2, TRUE); }
511 | tBYVAL Identifier EmptyBrackets_opt { $$ = new_argument_decl(ctx, $2, FALSE); }
513 /* these keywords may also be an identifier, depending on context */
514 Identifier
515 : tIdentifier { $$ = $1; }
516 | tDEFAULT { ctx->last_token = tIdentifier; $$ = $1; }
517 | tERROR { ctx->last_token = tIdentifier; $$ = $1; }
518 | tEXPLICIT { ctx->last_token = tIdentifier; $$ = $1; }
519 | tPROPERTY { ctx->last_token = tIdentifier; $$ = $1; }
520 | tSTEP { ctx->last_token = tIdentifier; $$ = $1; }
522 StSep_opt
523 : /* empty */
524 | StSep
526 Nl_opt
527 : /* empty */
528 | tNL Nl_opt
530 /* Most statements accept both new line and ':' as separators */
531 StSep
532 : tNL
533 | ':'
534 | tNL StSep
535 | ':' StSep
539 static int parser_error(unsigned *loc, parser_ctx_t *ctx, const char *str)
541 if(ctx->error_loc == -1)
542 ctx->error_loc = *loc;
543 if(ctx->hres == S_OK) {
544 FIXME("%s: %s\n", debugstr_w(ctx->code + *loc), debugstr_a(str));
545 ctx->hres = E_FAIL;
546 }else {
547 WARN("%s: %08lx\n", debugstr_w(ctx->code + *loc), ctx->hres);
549 return 0;
552 static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
554 if(!stat)
555 return;
557 /* concatenate both linked lists */
558 if(ctx->stats) {
559 ctx->stats_tail->next = stat;
560 ctx->stats_tail = stat;
561 }else {
562 ctx->stats = ctx->stats_tail = stat;
564 /* find new tail */
565 while(ctx->stats_tail->next) {
566 ctx->stats_tail=ctx->stats_tail->next;
570 static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
572 class_decl->next = ctx->class_decls;
573 ctx->class_decls = class_decl;
576 static void handle_isexpression_script(parser_ctx_t *ctx, expression_t *expr)
578 retval_statement_t *stat;
580 if(!expr)
581 return;
583 stat = new_statement(ctx, STAT_RETVAL, sizeof(*stat), 0);
584 if(!stat)
585 return;
587 stat->expr = expr;
588 ctx->stats = &stat->stat;
591 static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
593 expression_t *expr;
595 expr = parser_alloc(ctx, size ? size : sizeof(*expr));
596 if(expr) {
597 expr->type = type;
598 expr->next = NULL;
601 return expr;
604 static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
606 bool_expression_t *expr;
608 expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
609 if(!expr)
610 return NULL;
612 expr->value = value;
613 return &expr->expr;
616 static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
618 string_expression_t *expr;
620 expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
621 if(!expr)
622 return NULL;
624 expr->value = value;
625 return &expr->expr;
628 static expression_t *new_date_expression(parser_ctx_t *ctx, DATE value)
630 date_expression_t *expr;
632 expr = new_expression(ctx, EXPR_DATE, sizeof(*expr));
633 if(!expr)
634 return NULL;
636 expr->value = value;
637 return &expr->expr;
640 static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
642 int_expression_t *expr;
644 expr = new_expression(ctx, type, sizeof(*expr));
645 if(!expr)
646 return NULL;
648 expr->value = value;
649 return &expr->expr;
652 static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
654 double_expression_t *expr;
656 expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
657 if(!expr)
658 return NULL;
660 expr->value = value;
661 return &expr->expr;
664 static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
666 unary_expression_t *expr;
668 expr = new_expression(ctx, type, sizeof(*expr));
669 if(!expr)
670 return NULL;
672 expr->subexpr = subexpr;
673 return &expr->expr;
676 static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
678 binary_expression_t *expr;
680 expr = new_expression(ctx, type, sizeof(*expr));
681 if(!expr)
682 return NULL;
684 expr->left = left;
685 expr->right = right;
686 return &expr->expr;
689 static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
691 member_expression_t *expr;
693 expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
694 if(!expr)
695 return NULL;
697 expr->obj_expr = obj_expr;
698 expr->identifier = identifier;
699 return expr;
702 static call_expression_t *new_call_expression(parser_ctx_t *ctx, expression_t *expr, expression_t *arguments)
704 call_expression_t *call_expr;
706 call_expr = new_expression(ctx, EXPR_CALL, sizeof(*call_expr));
707 if(!call_expr)
708 return NULL;
710 call_expr->call_expr = expr;
711 call_expr->args = arguments;
712 return call_expr;
715 static call_expression_t *make_call_expression(parser_ctx_t *ctx, expression_t *callee_expr, expression_t *arguments)
717 call_expression_t *call_expr;
719 if(callee_expr->type == EXPR_MEMBER)
720 return new_call_expression(ctx, callee_expr, arguments);
721 if(callee_expr->type != EXPR_CALL) {
722 FIXME("Unhandled for expr type %u\n", callee_expr->type);
723 ctx->hres = E_FAIL;
724 return NULL;
726 call_expr = (call_expression_t*)callee_expr;
727 if(!call_expr->args) {
728 call_expr->args = arguments;
729 return call_expr;
732 if(call_expr->args->next) {
733 FIXME("Invalid syntax: invalid use of parentheses for arguments\n");
734 ctx->hres = E_FAIL;
735 return NULL;
738 call_expr->args = new_unary_expression(ctx, EXPR_BRACKETS, call_expr->args);
739 if(!call_expr->args)
740 return NULL;
741 if(!arguments)
742 return call_expr;
744 if(arguments->type != EXPR_NOARG) {
745 FIXME("Invalid syntax: missing comma\n");
746 ctx->hres = E_FAIL;
747 return NULL;
750 call_expr->args->next = arguments->next;
751 return call_expr;
754 static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
756 string_expression_t *expr;
758 expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
759 if(!expr)
760 return NULL;
762 expr->value = identifier;
763 return &expr->expr;
766 static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size, unsigned loc)
768 statement_t *stat;
770 stat = parser_alloc(ctx, size ? size : sizeof(*stat));
771 if(stat) {
772 stat->type = type;
773 stat->loc = loc;
774 stat->next = NULL;
777 return stat;
780 static statement_t *new_call_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr)
782 call_expression_t *call_expr = NULL;
783 call_statement_t *stat;
785 stat = new_statement(ctx, STAT_CALL, sizeof(*stat), loc);
786 if(!stat)
787 return NULL;
789 switch(expr->type) {
790 case EXPR_MEMBER:
791 call_expr = new_call_expression(ctx, expr, NULL);
792 break;
793 case EXPR_CALL:
794 call_expr = (call_expression_t*)expr;
795 break;
796 default:
797 FIXME("Unsupported expr type %u\n", expr->type);
798 ctx->hres = E_NOTIMPL;
800 if(!call_expr)
801 return NULL;
803 stat->expr = call_expr;
804 return &stat->stat;
807 static statement_t *new_assign_statement(parser_ctx_t *ctx, unsigned loc, expression_t *left, expression_t *right)
809 assign_statement_t *stat;
811 stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat), loc);
812 if(!stat)
813 return NULL;
815 stat->left_expr = left;
816 stat->value_expr = right;
818 return &stat->stat;
821 static statement_t *new_set_statement(parser_ctx_t *ctx, unsigned loc, expression_t *left, expression_t *right)
823 assign_statement_t *stat;
825 stat = new_statement(ctx, STAT_SET, sizeof(*stat), loc);
826 if(!stat)
827 return NULL;
829 stat->left_expr = left;
830 stat->value_expr = right;
832 return &stat->stat;
835 static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL is_array, dim_list_t *dims)
837 dim_decl_t *decl;
839 decl = parser_alloc(ctx, sizeof(*decl));
840 if(!decl)
841 return NULL;
843 decl->name = name;
844 decl->is_array = is_array;
845 decl->dims = dims;
846 decl->next = NULL;
847 return decl;
850 static dim_list_t *new_dim(parser_ctx_t *ctx, unsigned val, dim_list_t *next)
852 dim_list_t *ret;
854 ret = parser_alloc(ctx, sizeof(*ret));
855 if(!ret)
856 return NULL;
858 ret->val = val;
859 ret->next = next;
860 return ret;
863 static statement_t *new_dim_statement(parser_ctx_t *ctx, unsigned loc, dim_decl_t *decls)
865 dim_statement_t *stat;
867 stat = new_statement(ctx, STAT_DIM, sizeof(*stat), loc);
868 if(!stat)
869 return NULL;
871 stat->dim_decls = decls;
872 return &stat->stat;
875 static redim_decl_t *new_redim_decl(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *dims)
877 redim_decl_t *decl;
879 decl = parser_alloc(ctx, sizeof(*decl));
880 if(!decl)
881 return NULL;
883 decl->identifier = identifier;
884 decl->dims = dims;
885 decl->next = NULL;
886 return decl;
889 static statement_t *new_redim_statement(parser_ctx_t *ctx, unsigned loc, BOOL preserve, redim_decl_t *decls)
891 redim_statement_t *stat;
893 stat = new_statement(ctx, STAT_REDIM, sizeof(*stat), loc);
894 if(!stat)
895 return NULL;
897 stat->preserve = preserve;
898 stat->redim_decls = decls;
899 return &stat->stat;
902 static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_t *stat)
904 elseif_decl_t *decl;
906 decl = parser_alloc(ctx, sizeof(*decl));
907 if(!decl)
908 return NULL;
910 decl->expr = expr;
911 decl->stat = stat;
912 decl->loc = loc;
913 decl->next = NULL;
914 return decl;
917 static statement_t *new_while_statement(parser_ctx_t *ctx, unsigned loc, statement_type_t type, expression_t *expr, statement_t *body)
919 while_statement_t *stat;
921 stat = new_statement(ctx, type, sizeof(*stat), loc);
922 if(!stat)
923 return NULL;
925 stat->expr = expr;
926 stat->body = body;
927 return &stat->stat;
930 static statement_t *new_forto_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier, expression_t *from_expr,
931 expression_t *to_expr, expression_t *step_expr, statement_t *body)
933 forto_statement_t *stat;
935 stat = new_statement(ctx, STAT_FORTO, sizeof(*stat), loc);
936 if(!stat)
937 return NULL;
939 stat->identifier = identifier;
940 stat->from_expr = from_expr;
941 stat->to_expr = to_expr;
942 stat->step_expr = step_expr;
943 stat->body = body;
944 return &stat->stat;
947 static statement_t *new_foreach_statement(parser_ctx_t *ctx, unsigned loc, const WCHAR *identifier, expression_t *group_expr,
948 statement_t *body)
950 foreach_statement_t *stat;
952 stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat), loc);
953 if(!stat)
954 return NULL;
956 stat->identifier = identifier;
957 stat->group_expr = group_expr;
958 stat->body = body;
959 return &stat->stat;
962 static statement_t *new_if_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
963 statement_t *else_stat)
965 if_statement_t *stat;
967 stat = new_statement(ctx, STAT_IF, sizeof(*stat), loc);
968 if(!stat)
969 return NULL;
971 stat->expr = expr;
972 stat->if_stat = if_stat;
973 stat->elseifs = elseif_decl;
974 stat->else_stat = else_stat;
975 return &stat->stat;
978 static statement_t *new_select_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, case_clausule_t *case_clausules)
980 select_statement_t *stat;
982 stat = new_statement(ctx, STAT_SELECT, sizeof(*stat), loc);
983 if(!stat)
984 return NULL;
986 stat->expr = expr;
987 stat->case_clausules = case_clausules;
988 return &stat->stat;
991 static statement_t *new_with_statement(parser_ctx_t *ctx, unsigned loc, expression_t *expr, statement_t *body)
993 with_statement_t *stat;
995 stat = new_statement(ctx, STAT_WITH, sizeof(*stat), loc);
996 if(!stat)
997 return NULL;
999 stat->expr = expr;
1000 stat->body = body;
1001 return &stat->stat;
1004 static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_t *stat, case_clausule_t *next)
1006 case_clausule_t *ret;
1008 ret = parser_alloc(ctx, sizeof(*ret));
1009 if(!ret)
1010 return NULL;
1012 ret->expr = expr;
1013 ret->stat = stat;
1014 ret->next = next;
1015 return ret;
1018 static statement_t *new_onerror_statement(parser_ctx_t *ctx, unsigned loc, BOOL resume_next)
1020 onerror_statement_t *stat;
1022 stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat), loc);
1023 if(!stat)
1024 return NULL;
1026 stat->resume_next = resume_next;
1027 return &stat->stat;
1030 static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
1032 arg_decl_t *arg_decl;
1034 arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
1035 if(!arg_decl)
1036 return NULL;
1038 arg_decl->name = name;
1039 arg_decl->by_ref = by_ref;
1040 arg_decl->next = NULL;
1041 return arg_decl;
1044 static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
1045 unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
1047 function_decl_t *decl;
1048 BOOL is_default = FALSE;
1050 if(storage_flags & STORAGE_IS_DEFAULT) {
1051 if(type == FUNC_PROPGET || type == FUNC_FUNCTION || type == FUNC_SUB) {
1052 is_default = TRUE;
1053 }else {
1054 FIXME("Invalid default property\n");
1055 ctx->hres = E_FAIL;
1056 return NULL;
1060 decl = parser_alloc(ctx, sizeof(*decl));
1061 if(!decl)
1062 return NULL;
1064 decl->name = name;
1065 decl->type = type;
1066 decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
1067 decl->is_default = is_default;
1068 decl->args = arg_decl;
1069 decl->body = body;
1070 decl->next = NULL;
1071 decl->next_prop_func = NULL;
1072 return decl;
1075 static statement_t *new_function_statement(parser_ctx_t *ctx, unsigned loc, function_decl_t *decl)
1077 function_statement_t *stat;
1079 stat = new_statement(ctx, STAT_FUNC, sizeof(*stat), loc);
1080 if(!stat)
1081 return NULL;
1083 stat->func_decl = decl;
1084 return &stat->stat;
1087 static class_decl_t *new_class_decl(parser_ctx_t *ctx)
1089 class_decl_t *class_decl;
1091 class_decl = parser_alloc(ctx, sizeof(*class_decl));
1092 if(!class_decl)
1093 return NULL;
1095 class_decl->funcs = NULL;
1096 class_decl->props = NULL;
1097 class_decl->next = NULL;
1098 return class_decl;
1101 static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
1103 function_decl_t *iter;
1105 for(iter = class_decl->funcs; iter; iter = iter->next) {
1106 if(!wcsicmp(iter->name, decl->name)) {
1107 if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
1108 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
1109 ctx->hres = E_FAIL;
1110 return NULL;
1113 while(1) {
1114 if(iter->type == decl->type) {
1115 FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
1116 ctx->hres = E_FAIL;
1117 return NULL;
1119 if(!iter->next_prop_func)
1120 break;
1121 iter = iter->next_prop_func;
1124 iter->next_prop_func = decl;
1125 return class_decl;
1129 decl->next = class_decl->funcs;
1130 class_decl->funcs = decl;
1131 return class_decl;
1134 static class_decl_t *add_dim_prop(parser_ctx_t *ctx, class_decl_t *class_decl, dim_decl_t *dim_decl, unsigned storage_flags)
1136 if(storage_flags & STORAGE_IS_DEFAULT) {
1137 FIXME("variant prop can't be default value\n");
1138 ctx->hres = E_FAIL;
1139 return NULL;
1142 dim_decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
1143 dim_decl->next = class_decl->props;
1144 class_decl->props = dim_decl;
1145 return class_decl;
1148 static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
1150 const_decl_t *decl;
1152 decl = parser_alloc(ctx, sizeof(*decl));
1153 if(!decl)
1154 return NULL;
1156 decl->name = name;
1157 decl->value_expr = expr;
1158 decl->next = NULL;
1159 return decl;
1162 static statement_t *new_const_statement(parser_ctx_t *ctx, unsigned loc, const_decl_t *decls)
1164 const_statement_t *stat;
1166 stat = new_statement(ctx, STAT_CONST, sizeof(*stat), loc);
1167 if(!stat)
1168 return NULL;
1170 stat->decls = decls;
1171 return &stat->stat;
1174 static statement_t *link_statements(statement_t *head, statement_t *tail)
1176 statement_t *iter;
1178 if (!head) return tail;
1180 for(iter = head; iter->next; iter = iter->next);
1181 iter->next = tail;
1183 return head;
1186 void *parser_alloc(parser_ctx_t *ctx, size_t size)
1188 void *ret;
1190 ret = heap_pool_alloc(&ctx->heap, size);
1191 if(!ret)
1192 ctx->hres = E_OUTOFMEMORY;
1193 return ret;
1196 HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter, DWORD flags)
1198 ctx->code = ctx->ptr = code;
1199 ctx->end = ctx->code + lstrlenW(ctx->code);
1201 heap_pool_init(&ctx->heap);
1203 ctx->hres = S_OK;
1204 ctx->error_loc = -1;
1205 ctx->last_token = tNL;
1206 ctx->last_nl = 0;
1207 ctx->stats = ctx->stats_tail = NULL;
1208 ctx->class_decls = NULL;
1209 ctx->option_explicit = FALSE;
1210 ctx->is_html = delimiter && !wcsicmp(delimiter, L"</script>");
1212 if(flags & SCRIPTTEXT_ISEXPRESSION)
1213 ctx->last_token = tEXPRESSION;
1215 parser_parse(ctx);
1217 return ctx->hres;
1220 void parser_release(parser_ctx_t *ctx)
1222 heap_pool_free(&ctx->heap);