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
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_
}
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
;
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
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
163 : OptionExplicit_opt SourceElements
164 | tEXPRESSION ExpressionNl_opt
{ handle_isexpression_script
(ctx
, $2); }
168 | tOPTION tEXPLICIT StSep
{ ctx
->option_explicit
= TRUE
; }
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); }
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
; }
184 : /* empty */ { $$
= NULL
; }
185 | Expression tNL
{ $$
= $1; }
188 : /* empty */ { $$
= NULL
; }
189 | Statement
{ $$
= $1; }
190 | StatementNl BodyStatements
{ $$
= link_statements
($1, $2); }
193 : /* empty */ { $$
= NULL
; }
194 | StatementsNl
{ $$
= $1; }
197 : SimpleStatement StSep
{ $$
= $1; }
198 | SimpleStatement StSep StatementsNl
{ $$
= link_statements
($1, $3); }
201 : Statement tNL
{ $$
= $1; }
205 |
':' Statement
{ $$
= $2; }
206 | SimpleStatement
{ $$
= $1; }
207 | SimpleStatement
':' Statement
{ $1->next
= $3; $$
= $1; }
208 | SimpleStatement
':' { $$
= $1; }
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);
224 | tDO StSep StatementsNl_opt tLOOP DoType Expression
225 { $$
= new_while_statement
(ctx
, @
4, $5 ? STAT_DOWHILE
: STAT_DOUNTIL
, $6, $3);
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); }
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
; }
255 : /* empty */ { $$
= FALSE
; }
256 | tPRESERVE
{ $$
= TRUE
; }
259 : tIdentifier
'(' ArgumentList
')' { $$
= new_redim_decl
(ctx
, $1, $3); CHECK_ERROR
; }
262 : ReDimDecl
{ $$
= $1; }
263 | ReDimDecl
',' ReDimDeclList
{ $1->next
= $3; $$
= $1; }
266 : DimDecl
{ $$
= $1; }
267 | DimDecl
',' DimDeclList
{ $1->next
= $3; $$
= $1; }
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
; }
275 : IntegerValue
{ $$
= new_dim
(ctx
, $1, NULL
); }
276 | IntegerValue
',' DimList
{ $$
= new_dim
(ctx
, $1, $3); }
279 : ConstDecl
{ $$
= $1; }
280 | ConstDecl
',' ConstDeclList
{ $1->next
= $3; $$
= $1; }
283 : Identifier
'=' ConstExpression
{ $$
= new_const_decl
(ctx
, $1, $3); CHECK_ERROR
; }
286 : LiteralExpression
{ $$
= $1; }
287 |
'-' NumericLiteralExpression
{ $$
= new_unary_expression
(ctx
, EXPR_NEG
, $2); CHECK_ERROR
; }
290 : tWHILE
{ $$
= TRUE
; }
291 | tUNTIL
{ $$
= FALSE
; }
294 : /* empty */ { $$
= NULL
;}
295 | tSTEP Expression
{ $$
= $2; }
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
; }
309 : /* empty */ { $$
= NULL
; }
310 | ElseIfs
{ $$
= $1; }
313 : ElseIf
{ $$
= $1; }
314 | ElseIf ElseIfs
{ $1->next
= $2; $$
= $1; }
317 : tELSEIF Expression tTHEN StSep_opt StatementsNl_opt
318 { $$
= new_elseif_decl
(ctx
, @$
, $2, $5); }
321 : /* empty */ { $$
= NULL
; }
322 | tELSE StSep_opt StatementsNl_opt
{ $$
= $3; }
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); }
330 : tEMPTYBRACKETS
{ $$
= NULL
; }
331 |
'(' ArgumentList
')' { $$
= $2; }
334 : /* empty */ { $$
= NULL
; }
335 | ArgumentList
{ $$
= $1; }
338 : Expression
{ $$
= $1; }
339 | Expression
',' ArgumentList
{ $1->next
= $3; $$
= $1; }
340 |
',' ArgumentList
{ $$
= new_expression
(ctx
, EXPR_NOARG
, 0); CHECK_ERROR
; $$
->next
= $2; }
347 : Expression
{ $$
= $1; }
348 | Expression
',' ExpressionList
{ $1->next
= $3; $$
= $1; }
351 : EqvExpression
{ $$
= $1; }
352 | Expression tIMP EqvExpression
{ $$
= new_binary_expression
(ctx
, EXPR_IMP
, $1, $3); CHECK_ERROR
; }
355 : XorExpression
{ $$
= $1; }
356 | EqvExpression tEQV XorExpression
{ $$
= new_binary_expression
(ctx
, EXPR_EQV
, $1, $3); CHECK_ERROR
; }
359 : OrExpression
{ $$
= $1; }
360 | XorExpression tXOR OrExpression
{ $$
= new_binary_expression
(ctx
, EXPR_XOR
, $1, $3); CHECK_ERROR
; }
363 : AndExpression
{ $$
= $1; }
364 | OrExpression tOR AndExpression
{ $$
= new_binary_expression
(ctx
, EXPR_OR
, $1, $3); CHECK_ERROR
; }
367 : NotExpression
{ $$
= $1; }
368 | AndExpression tAND NotExpression
{ $$
= new_binary_expression
(ctx
, EXPR_AND
, $1, $3); CHECK_ERROR
; }
371 : EqualityExpression
{ $$
= $1; }
372 | tNOT NotExpression
{ $$
= new_unary_expression
(ctx
, EXPR_NOT
, $2); CHECK_ERROR
; }
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
; }
385 : AdditiveExpression
{ $$
= $1; }
386 | ConcatExpression
'&' AdditiveExpression
{ $$
= new_binary_expression
(ctx
, EXPR_CONCAT
, $1, $3); CHECK_ERROR
; }
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
; }
394 : IntdivExpression
{ $$
= $1; }
395 | ModExpression tMOD IntdivExpression
{ $$
= new_binary_expression
(ctx
, EXPR_MOD
, $1, $3); CHECK_ERROR
; }
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
; }
410 : SignExpression
{ $$
= $1; }
411 | ExpExpression
'^' SignExpression
{ $$
= new_binary_expression
(ctx
, EXPR_EXP
, $1, $3); CHECK_ERROR
; }
414 : UnaryExpression
{ $$
= $1; }
415 |
'-' SignExpression
{ $$
= new_unary_expression
(ctx
, EXPR_NEG
, $2); CHECK_ERROR
; }
416 |
'+' SignExpression
{ $$
= $2; }
419 : LiteralExpression
{ $$
= $1; }
420 | CallExpression
{ $$
= $1; }
421 | tNEW Identifier
{ $$
= new_new_expression
(ctx
, $2); CHECK_ERROR
; }
424 : PrimaryExpression
{ $$
= $1; }
425 | MemberExpression
{ $$
= &$1->expr
; }
426 | CallExpression Arguments
{ call_expression_t
*expr
= new_call_expression
(ctx
, $1, $2); CHECK_ERROR
;
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
; }
449 : tEXPRLBRACKET Expression
')' { $$
= new_unary_expression
(ctx
, EXPR_BRACKETS
, $2); }
450 | tME
{ $$
= new_expression
(ctx
, EXPR_ME
, 0); CHECK_ERROR
; }
453 : tCLASS Identifier StSep ClassBody tEND tCLASS StSep
{ $4->name
= $2; $$
= $4; }
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
; }
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
; }
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
; }
488 : /* empty*/ { $$
= 0; }
489 | Storage
{ $$
= $1; }
492 : tPUBLIC tDEFAULT
{ $$
= STORAGE_IS_DEFAULT
; }
493 | tPUBLIC
{ $$
= 0; }
494 | tPRIVATE
{ $$
= STORAGE_IS_PRIVATE
; }
497 : /* empty*/ { $$
= 0; }
498 | ArgumentsDecl
{ $$
= $1; }
501 : tEMPTYBRACKETS
{ $$
= NULL
; }
502 |
'(' ArgumentDeclList
')' { $$
= $2; }
505 : ArgumentDecl
{ $$
= $1; }
506 | ArgumentDecl
',' ArgumentDeclList
{ $1->next
= $3; $$
= $1; }
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 */
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; }
530 /* Most statements accept both new line and ':' as separators */
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
));
547 WARN
("%s: %08lx\n", debugstr_w
(ctx
->code
+ *loc
), ctx
->hres
);
552 static void source_add_statement
(parser_ctx_t
*ctx
, statement_t
*stat
)
557 /* concatenate both linked lists */
559 ctx
->stats_tail
->next
= stat
;
560 ctx
->stats_tail
= stat
;
562 ctx
->stats
= ctx
->stats_tail
= stat
;
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
;
583 stat
= new_statement
(ctx
, STAT_RETVAL
, sizeof
(*stat
), 0);
588 ctx
->stats
= &stat
->stat
;
591 static void *new_expression
(parser_ctx_t
*ctx
, expression_type_t type
, size_t size
)
595 expr
= parser_alloc
(ctx
, size ? size
: sizeof
(*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
));
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
));
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
));
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
));
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
));
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
));
672 expr
->subexpr
= subexpr
;
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
));
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
));
697 expr
->obj_expr
= obj_expr
;
698 expr
->identifier
= identifier
;
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
));
710 call_expr
->call_expr
= expr
;
711 call_expr
->args
= arguments
;
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
);
726 call_expr
= (call_expression_t
*)callee_expr
;
727 if
(!call_expr
->args
) {
728 call_expr
->args
= arguments
;
732 if
(call_expr
->args
->next
) {
733 FIXME
("Invalid syntax: invalid use of parentheses for arguments\n");
738 call_expr
->args
= new_unary_expression
(ctx
, EXPR_BRACKETS
, call_expr
->args
);
744 if
(arguments
->type
!= EXPR_NOARG
) {
745 FIXME
("Invalid syntax: missing comma\n");
750 call_expr
->args
->next
= arguments
->next
;
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
));
762 expr
->value
= identifier
;
766 static void *new_statement
(parser_ctx_t
*ctx
, statement_type_t type
, size_t size
, unsigned loc
)
770 stat
= parser_alloc
(ctx
, size ? size
: sizeof
(*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
);
791 call_expr
= new_call_expression
(ctx
, expr
, NULL
);
794 call_expr
= (call_expression_t
*)expr
;
797 FIXME
("Unsupported expr type %u\n", expr
->type
);
798 ctx
->hres
= E_NOTIMPL
;
803 stat
->expr
= call_expr
;
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
);
815 stat
->left_expr
= left
;
816 stat
->value_expr
= right
;
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
);
829 stat
->left_expr
= left
;
830 stat
->value_expr
= right
;
835 static dim_decl_t
*new_dim_decl
(parser_ctx_t
*ctx
, const WCHAR
*name
, BOOL is_array
, dim_list_t
*dims
)
839 decl
= parser_alloc
(ctx
, sizeof
(*decl
));
844 decl
->is_array
= is_array
;
850 static dim_list_t
*new_dim
(parser_ctx_t
*ctx
, unsigned val
, dim_list_t
*next
)
854 ret
= parser_alloc
(ctx
, sizeof
(*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
);
871 stat
->dim_decls
= decls
;
875 static redim_decl_t
*new_redim_decl
(parser_ctx_t
*ctx
, const WCHAR
*identifier
, expression_t
*dims
)
879 decl
= parser_alloc
(ctx
, sizeof
(*decl
));
883 decl
->identifier
= identifier
;
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
);
897 stat
->preserve
= preserve
;
898 stat
->redim_decls
= decls
;
902 static elseif_decl_t
*new_elseif_decl
(parser_ctx_t
*ctx
, unsigned loc
, expression_t
*expr
, statement_t
*stat
)
906 decl
= parser_alloc
(ctx
, sizeof
(*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
);
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
);
939 stat
->identifier
= identifier
;
940 stat
->from_expr
= from_expr
;
941 stat
->to_expr
= to_expr
;
942 stat
->step_expr
= step_expr
;
947 static statement_t
*new_foreach_statement
(parser_ctx_t
*ctx
, unsigned loc
, const WCHAR
*identifier
, expression_t
*group_expr
,
950 foreach_statement_t
*stat
;
952 stat
= new_statement
(ctx
, STAT_FOREACH
, sizeof
(*stat
), loc
);
956 stat
->identifier
= identifier
;
957 stat
->group_expr
= group_expr
;
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
);
972 stat
->if_stat
= if_stat
;
973 stat
->elseifs
= elseif_decl
;
974 stat
->else_stat
= else_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
);
987 stat
->case_clausules
= case_clausules
;
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
);
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
));
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
);
1026 stat
->resume_next
= resume_next
;
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
));
1038 arg_decl
->name
= name
;
1039 arg_decl
->by_ref
= by_ref
;
1040 arg_decl
->next
= NULL
;
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
) {
1054 FIXME
("Invalid default property\n");
1060 decl
= parser_alloc
(ctx
, sizeof
(*decl
));
1066 decl
->is_public
= !(storage_flags
& STORAGE_IS_PRIVATE
);
1067 decl
->is_default
= is_default
;
1068 decl
->args
= arg_decl
;
1071 decl
->next_prop_func
= NULL
;
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
);
1083 stat
->func_decl
= decl
;
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
));
1095 class_decl
->funcs
= NULL
;
1096 class_decl
->props
= NULL
;
1097 class_decl
->next
= NULL
;
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
));
1114 if
(iter
->type
== decl
->type
) {
1115 FIXME
("Redefinition of %s::%s\n", debugstr_w
(class_decl
->name
), debugstr_w
(decl
->name
));
1119 if
(!iter
->next_prop_func
)
1121 iter
= iter
->next_prop_func
;
1124 iter
->next_prop_func
= decl
;
1129 decl
->next
= class_decl
->funcs
;
1130 class_decl
->funcs
= 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");
1142 dim_decl
->is_public
= !(storage_flags
& STORAGE_IS_PRIVATE
);
1143 dim_decl
->next
= class_decl
->props
;
1144 class_decl
->props
= dim_decl
;
1148 static const_decl_t
*new_const_decl
(parser_ctx_t
*ctx
, const WCHAR
*name
, expression_t
*expr
)
1152 decl
= parser_alloc
(ctx
, sizeof
(*decl
));
1157 decl
->value_expr
= expr
;
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
);
1170 stat
->decls
= decls
;
1174 static statement_t
*link_statements
(statement_t
*head
, statement_t
*tail
)
1178 if
(!head
) return tail
;
1180 for
(iter
= head
; iter
->next
; iter
= iter
->next
);
1186 void *parser_alloc
(parser_ctx_t
*ctx
, size_t size
)
1190 ret
= heap_pool_alloc
(&ctx
->heap
, size
);
1192 ctx
->hres
= E_OUTOFMEMORY
;
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
);
1204 ctx
->error_loc
= -1;
1205 ctx
->last_token
= tNL
;
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
;
1220 void parser_release
(parser_ctx_t
*ctx
)
1222 heap_pool_free
(&ctx
->heap
);