parser.y 36.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2011 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

%{

#include "vbscript.h"
#include "parse.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(vbscript);

28
static int parser_error(parser_ctx_t *,const char*);
29

30
static void parse_complete(parser_ctx_t*,BOOL);
31

32
static void source_add_statement(parser_ctx_t*,statement_t*);
33
static void source_add_class(parser_ctx_t*,class_decl_t*);
34

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 39
static expression_t *new_long_expression(parser_ctx_t*,expression_type_t,LONG);
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*);
43

44 45
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 55
static statement_t *new_if_statement(parser_ctx_t*,expression_t*,statement_t*,elseif_decl_t*,statement_t*);
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*);
59

60 61
static dim_decl_t *new_dim_decl(parser_ctx_t*,const WCHAR*,BOOL,dim_list_t*);
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*);
67

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_dim_prop(parser_ctx_t*,class_decl_t*,dim_decl_t*,unsigned);
71

72 73
static statement_t *link_statements(statement_t*,statement_t*);

74 75 76
#define STORAGE_IS_PRIVATE    1
#define STORAGE_IS_DEFAULT    2

77 78
#define CHECK_ERROR if(((parser_ctx_t*)ctx)->hres != S_OK) YYABORT

79 80
%}

81 82 83
%lex-param { parser_ctx_t *ctx }
%parse-param { parser_ctx_t *ctx }
%pure-parser
84 85 86 87
%start Program

%union {
    const WCHAR *string;
88
    statement_t *statement;
89
    expression_t *expression;
90
    member_expression_t *member;
91
    elseif_decl_t *elseif;
92
    dim_decl_t *dim_decl;
93
    dim_list_t *dim_list;
94 95
    function_decl_t *func_decl;
    arg_decl_t *arg_decl;
96
    class_decl_t *class_decl;
97
    const_decl_t *const_decl;
98
    case_clausule_t *case_clausule;
99
    unsigned uint;
100
    LONG lng;
101
    BOOL boolean;
102
    double dbl;
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
%token tEOF tNL tEMPTYBRACKETS
%token tLTEQ tGTEQ tNEQ
%token tSTOP tME tREM
%token <string> tTRUE tFALSE
%token <string> tNOT tAND tOR tXOR tEQV tIMP
%token <string> tIS tMOD
%token <string> tCALL tDIM tSUB tFUNCTION tGET tLET tCONST
%token <string> tIF tELSE tELSEIF tEND tTHEN tEXIT
%token <string> tWHILE tWEND tDO tLOOP tUNTIL tFOR tTO tEACH tIN
%token <string> tSELECT tCASE
%token <string> tBYREF tBYVAL
%token <string> tOPTION
%token <string> tNOTHING tEMPTY tNULL
%token <string> tCLASS tSET tNEW tPUBLIC tPRIVATE
%token <string> tNEXT tON tRESUME tGOTO
120
%token <string> tIdentifier tString
121
%token <string> tDEFAULT tERROR tEXPLICIT tPROPERTY tSTEP
122 123
%token <lng> tLong tShort
%token <dbl> tDouble
124

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 ArgumentList_opt Step_opt ExpressionList
132
%type <boolean> 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 138 139
%type <uint> Storage Storage_opt IntegerValue
%type <dim_decl> DimDeclList DimDecl
%type <dim_list> DimList
140
%type <const_decl> ConstDecl ConstDeclList
141
%type <string> Identifier DotIdentifier
142
%type <case_clausule> CaseClausules
143

144 145
%%

146
Program
147 148 149 150
    : OptionExplicit_opt SourceElements tEOF    { parse_complete(ctx, $1); }

OptionExplicit_opt
    : /* empty */                { $$ = FALSE; }
151
    | tOPTION tEXPLICIT StSep    { $$ = TRUE; }
152 153 154

SourceElements
    : /* empty */
155 156
    | SourceElements StatementNl            { source_add_statement(ctx, $2); }
    | SourceElements ClassDeclaration       { source_add_class(ctx, $2); }
157

158 159 160 161
StatementsNl_opt
    : /* empty */                           { $$ = NULL; }
    | StatementsNl                          { $$ = $1; }

162 163
StatementsNl
    : StatementNl                           { $$ = $1; }
164
    | StatementNl StatementsNl              { $$ = link_statements($1, $2); }
165

166
StatementNl
167
    : Statement tNL                         { $$ = $1; }
168 169

Statement
170 171 172 173 174 175 176
    : ':'                                   { $$ = NULL; }
    | ':' Statement                         { $$ = $2; }
    | SimpleStatement                       { $$ = $1; }
    | SimpleStatement ':' Statement         { $1->next = $3; $$ = $1; }
    | SimpleStatement ':'                   { $$ = $1; }

SimpleStatement
177 178
    : MemberExpression ArgumentList_opt     { $1->args = $2; $$ = new_call_statement(ctx, FALSE, $1); CHECK_ERROR; }
    | tCALL MemberExpression Arguments_opt  { $2->args = $3; $$ = new_call_statement(ctx, TRUE, $2); CHECK_ERROR; }
179 180
    | MemberExpression Arguments_opt '=' Expression
                                            { $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 StSep StatementsNl_opt tWEND
184
                                            { $$ = new_while_statement(ctx, STAT_WHILE, $2, $4); CHECK_ERROR; }
185
    | tDO DoType Expression StSep StatementsNl_opt tLOOP
186 187
                                            { $$ = new_while_statement(ctx, $2 ? STAT_WHILELOOP : STAT_UNTIL, $3, $5);
                                              CHECK_ERROR; }
188
    | tDO StSep StatementsNl_opt tLOOP DoType Expression
189 190
                                            { $$ = new_while_statement(ctx, $5 ? STAT_DOWHILE : STAT_DOUNTIL, $6, $3);
                                              CHECK_ERROR; }
191
    | tDO StSep 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 199
    | tSET MemberExpression Arguments_opt '=' Expression
                                            { $2->args = $3; $$ = new_set_statement(ctx, $2, $5); CHECK_ERROR; }
200
    | tSTOP                                 { $$ = new_statement(ctx, STAT_STOP, 0); CHECK_ERROR; }
201 202
    | tON tERROR tRESUME tNEXT              { $$ = new_onerror_statement(ctx, TRUE); CHECK_ERROR; }
    | 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 StSep StatementsNl_opt tNEXT
205
                                            { $$ = new_forto_statement(ctx, $2, $4, $6, $7, $9); CHECK_ERROR; }
206
    | tFOR tEACH Identifier tIN Expression StSep StatementsNl_opt tNEXT
207
                                            { $$ = new_foreach_statement(ctx, $3, $5, $7); }
208
    | tSELECT tCASE Expression StSep CaseClausules tEND tSELECT
209
                                            { $$ = new_select_statement(ctx, $3, $5); }
210 211

MemberExpression
212
    : Identifier                            { $$ = new_member_expression(ctx, NULL, $1); CHECK_ERROR; }
213
    | CallExpression '.' DotIdentifier      { $$ = new_member_expression(ctx, $1, $3); CHECK_ERROR; }
214

215 216 217 218 219 220 221 222 223 224 225 226
DimDeclList
    : DimDecl                               { $$ = $1; }
    | DimDecl ',' DimDeclList               { $1->next = $3; $$ = $1; }

DimDecl
    : Identifier                            { $$ = new_dim_decl(ctx, $1, FALSE, NULL); CHECK_ERROR; }
    | Identifier '(' DimList ')'            { $$ = new_dim_decl(ctx, $1, TRUE, $3); CHECK_ERROR; }
    | Identifier tEMPTYBRACKETS             { $$ = new_dim_decl(ctx, $1, TRUE, NULL); CHECK_ERROR; }

DimList
    : IntegerValue                          { $$ = new_dim(ctx, $1, NULL); }
    | IntegerValue ',' DimList              { $$ = new_dim(ctx, $1, $3); }
227

228 229 230 231 232
ConstDeclList
    : ConstDecl                             { $$ = $1; }
    | ConstDecl ',' ConstDeclList           { $1->next = $3; $$ = $1; }

ConstDecl
233 234 235 236 237
    : Identifier '=' ConstExpression        { $$ = new_const_decl(ctx, $1, $3); CHECK_ERROR; }

ConstExpression
    : LiteralExpression                     { $$ = $1; }
    | '-' NumericLiteralExpression          { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
238

239 240 241 242
DoType
    : tWHILE        { $$ = TRUE; }
    | tUNTIL        { $$ = FALSE; }

243 244 245 246
Step_opt
    : /* empty */                           { $$ = NULL;}
    | tSTEP Expression                      { $$ = $2; }

247
IfStatement
248
    : tIF Expression tTHEN tNL StatementsNl_opt ElseIfs_opt Else_opt tEND tIF
249 250
                                               { $$ = new_if_statement(ctx, $2, $5, $6, $7); CHECK_ERROR; }
    | tIF Expression tTHEN Statement EndIf_opt { $$ = 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; }
253

254 255 256 257
EndIf_opt
    : /* empty */
    | tEND tIF

258 259 260 261 262 263 264 265 266
ElseIfs_opt
    : /* empty */                           { $$ = NULL; }
    | ElseIfs                               { $$ = $1; }

ElseIfs
    : ElseIf                                { $$ = $1; }
    | ElseIf ElseIfs                        { $1->next = $2; $$ = $1; }

ElseIf
267
    : tELSEIF Expression tTHEN tNL StatementsNl_opt
268 269 270 271
                                            { $$ = new_elseif_decl(ctx, $2, $5); }

Else_opt
    : /* empty */                           { $$ = NULL; }
272
    | tELSE tNL StatementsNl_opt            { $$ = $3; }
273

274 275
CaseClausules
    : /* empty */                          { $$ = NULL; }
276 277
    | tCASE tELSE StSep StatementsNl       { $$ = new_case_clausule(ctx, NULL, $4, NULL); }
    | tCASE ExpressionList StSep StatementsNl_opt CaseClausules
278 279
                                           { $$ = new_case_clausule(ctx, $2, $4, $5); }

280
Arguments_opt
281
    : EmptyBrackets_opt             { $$ = NULL; }
282
    | '(' ArgumentList ')'          { $$ = $2; }
283 284

ArgumentList_opt
285
    : EmptyBrackets_opt             { $$ = NULL; }
286 287 288 289 290 291
    | ArgumentList                  { $$ = $1; }

ArgumentList
    : Expression                    { $$ = $1; }
    | Expression ',' ArgumentList   { $1->next = $3; $$ = $1; }
    | ',' ArgumentList              { $$ = new_expression(ctx, EXPR_NOARG, 0); CHECK_ERROR; $$->next = $2; }
292

293 294 295 296
EmptyBrackets_opt
    : /* empty */
    | tEMPTYBRACKETS

297 298 299 300
ExpressionList
    : Expression                    { $$ = $1; }
    | Expression ',' ExpressionList { $1->next = $3; $$ = $1; }

301
Expression
302 303 304 305 306 307 308 309
    : EqvExpression                             { $$ = $1; }
    | Expression tIMP EqvExpression             { $$ = new_binary_expression(ctx, EXPR_IMP, $1, $3); CHECK_ERROR; }

EqvExpression
    : XorExpression                             { $$ = $1; }
    | EqvExpression tEQV XorExpression          { $$ = new_binary_expression(ctx, EXPR_EQV, $1, $3); CHECK_ERROR; }

XorExpression
310
    : OrExpression                              { $$ = $1; }
311
    | XorExpression tXOR OrExpression           { $$ = new_binary_expression(ctx, EXPR_XOR, $1, $3); CHECK_ERROR; }
312 313

OrExpression
314
    : AndExpression                             { $$ = $1; }
315
    | OrExpression tOR AndExpression            { $$ = new_binary_expression(ctx, EXPR_OR, $1, $3); CHECK_ERROR; }
316 317 318 319

AndExpression
    : NotExpression                             { $$ = $1; }
    | AndExpression tAND NotExpression          { $$ = new_binary_expression(ctx, EXPR_AND, $1, $3); CHECK_ERROR; }
320 321

NotExpression
322 323 324 325 326 327
    : EqualityExpression            { $$ = $1; }
    | tNOT NotExpression            { $$ = new_unary_expression(ctx, EXPR_NOT, $2); CHECK_ERROR; }

EqualityExpression
    : ConcatExpression                          { $$ = $1; }
    | EqualityExpression '=' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_EQUAL, $1, $3); CHECK_ERROR; }
328
    | EqualityExpression tNEQ ConcatExpression  { $$ = new_binary_expression(ctx, EXPR_NEQUAL, $1, $3); CHECK_ERROR; }
329 330 331 332
    | EqualityExpression '>' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_GT, $1, $3); CHECK_ERROR; }
    | EqualityExpression '<' ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_LT, $1, $3); CHECK_ERROR; }
    | EqualityExpression tGTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_GTEQ, $1, $3); CHECK_ERROR; }
    | EqualityExpression tLTEQ ConcatExpression { $$ = new_binary_expression(ctx, EXPR_LTEQ, $1, $3); CHECK_ERROR; }
333
    | EqualityExpression tIS ConcatExpression   { $$ = new_binary_expression(ctx, EXPR_IS, $1, $3); CHECK_ERROR; }
334 335

ConcatExpression
336 337 338 339
    : AdditiveExpression                        { $$ = $1; }
    | ConcatExpression '&' AdditiveExpression   { $$ = new_binary_expression(ctx, EXPR_CONCAT, $1, $3); CHECK_ERROR; }

AdditiveExpression
340 341 342 343 344
    : ModExpression                             { $$ = $1; }
    | AdditiveExpression '+' ModExpression      { $$ = new_binary_expression(ctx, EXPR_ADD, $1, $3); CHECK_ERROR; }
    | AdditiveExpression '-' ModExpression      { $$ = new_binary_expression(ctx, EXPR_SUB, $1, $3); CHECK_ERROR; }

ModExpression
345 346 347 348
    : IntdivExpression                          { $$ = $1; }
    | ModExpression tMOD IntdivExpression       { $$ = new_binary_expression(ctx, EXPR_MOD, $1, $3); CHECK_ERROR; }

IntdivExpression
349 350 351
    : MultiplicativeExpression                  { $$ = $1; }
    | IntdivExpression '\\' MultiplicativeExpression
                                                { $$ = new_binary_expression(ctx, EXPR_IDIV, $1, $3); CHECK_ERROR; }
352

353
MultiplicativeExpression
354 355 356 357 358 359 360
    : ExpExpression                             { $$ = $1; }
    | MultiplicativeExpression '*' ExpExpression
                                                { $$ = new_binary_expression(ctx, EXPR_MUL, $1, $3); CHECK_ERROR; }
    | MultiplicativeExpression '/' ExpExpression
                                                { $$ = new_binary_expression(ctx, EXPR_DIV, $1, $3); CHECK_ERROR; }

ExpExpression
361 362
    : UnaryExpression                           { $$ = $1; }
    | ExpExpression '^' UnaryExpression         { $$ = new_binary_expression(ctx, EXPR_EXP, $1, $3); CHECK_ERROR; }
363 364 365 366

UnaryExpression
    : LiteralExpression             { $$ = $1; }
    | CallExpression                { $$ = $1; }
367
    | tNEW Identifier               { $$ = new_new_expression(ctx, $2); CHECK_ERROR; }
368
    | '-' UnaryExpression           { $$ = new_unary_expression(ctx, EXPR_NEG, $2); CHECK_ERROR; }
369 370 371 372

CallExpression
    : PrimaryExpression                 { $$ = $1; }
    | MemberExpression Arguments_opt    { $1->args = $2; $$ = &$1->expr; }
373 374 375 376

LiteralExpression
    : tTRUE                         { $$ = new_bool_expression(ctx, VARIANT_TRUE); CHECK_ERROR; }
    | tFALSE                        { $$ = new_bool_expression(ctx, VARIANT_FALSE); CHECK_ERROR; }
377
    | tString                       { $$ = new_string_expression(ctx, $1); CHECK_ERROR; }
378
    | NumericLiteralExpression      { $$ = $1; }
379
    | tEMPTY                        { $$ = new_expression(ctx, EXPR_EMPTY, 0); CHECK_ERROR; }
380
    | tNULL                         { $$ = new_expression(ctx, EXPR_NULL, 0); CHECK_ERROR; }
381
    | tNOTHING                      { $$ = new_expression(ctx, EXPR_NOTHING, 0); CHECK_ERROR; }
382

383 384 385 386 387 388
NumericLiteralExpression
    : tShort                        { $$ = new_long_expression(ctx, EXPR_USHORT, $1); CHECK_ERROR; }
    | '0'                           { $$ = new_long_expression(ctx, EXPR_USHORT, 0); CHECK_ERROR; }
    | tLong                         { $$ = new_long_expression(ctx, EXPR_ULONG, $1); CHECK_ERROR; }
    | tDouble                       { $$ = new_double_expression(ctx, $1); CHECK_ERROR; }

389 390 391 392
IntegerValue
    : tShort                        { $$ = $1; }
    | '0'                           { $$ = 0; }
    | tLong                         { $$ = $1; }
393

394
PrimaryExpression
395
    : '(' Expression ')'            { $$ = new_unary_expression(ctx, EXPR_BRACKETS, $2); }
396
    | tME                           { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; }
397

398
ClassDeclaration
399
    : tCLASS Identifier StSep ClassBody tEND tCLASS StSep       { $4->name = $2; $$ = $4; }
400 401

ClassBody
402 403
    : /* empty */                                 { $$ = new_class_decl(ctx); }
    | FunctionDecl StSep ClassBody                { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
404
    /* FIXME: We should use DimDecl here to support arrays, but that conflicts with PropertyDecl. */
405
    | Storage tIdentifier StSep ClassBody         { dim_decl_t *dim_decl = new_dim_decl(ctx, $2, FALSE, NULL); CHECK_ERROR;
406
                                                  $$ = add_dim_prop(ctx, $4, dim_decl, $1); CHECK_ERROR; }
407 408
    | tDIM DimDecl StSep ClassBody                { $$ = add_dim_prop(ctx, $4, $2, 0); CHECK_ERROR; }
    | PropertyDecl StSep ClassBody                { $$ = add_class_function(ctx, $3, $1); CHECK_ERROR; }
409 410

PropertyDecl
411
    : Storage_opt tPROPERTY tGET tIdentifier ArgumentsDecl_opt StSep StatementsNl_opt tEND tPROPERTY
412
                                    { $$ = new_function_decl(ctx, $4, FUNC_PROPGET, $1, $5, $7); CHECK_ERROR; }
413
    | Storage_opt tPROPERTY tLET tIdentifier '(' ArgumentDecl ')' StSep StatementsNl_opt tEND tPROPERTY
414
                                    { $$ = new_function_decl(ctx, $4, FUNC_PROPLET, $1, $6, $9); CHECK_ERROR; }
415
    | Storage_opt tPROPERTY tSET tIdentifier '(' ArgumentDecl ')' StSep StatementsNl_opt tEND tPROPERTY
416
                                    { $$ = new_function_decl(ctx, $4, FUNC_PROPSET, $1, $6, $9); CHECK_ERROR; }
417

418
FunctionDecl
419
    : Storage_opt tSUB Identifier ArgumentsDecl_opt StSep StatementsNl_opt tEND tSUB
420
                                    { $$ = new_function_decl(ctx, $3, FUNC_SUB, $1, $4, $6); CHECK_ERROR; }
421
    | Storage_opt tFUNCTION Identifier ArgumentsDecl_opt StSep StatementsNl_opt tEND tFUNCTION
422 423 424 425 426 427 428 429 430 431
                                    { $$ = new_function_decl(ctx, $3, FUNC_FUNCTION, $1, $4, $6); CHECK_ERROR; }

Storage_opt
    : /* empty*/                    { $$ = 0; }
    | Storage                       { $$ = $1; }

Storage
    : tPUBLIC tDEFAULT              { $$ = STORAGE_IS_DEFAULT; }
    | tPUBLIC                       { $$ = 0; }
    | tPRIVATE                      { $$ = STORAGE_IS_PRIVATE; }
432 433 434 435 436 437 438 439 440 441

ArgumentsDecl_opt
    : EmptyBrackets_opt                         { $$ = NULL; }
    | '(' ArgumentDeclList ')'                  { $$ = $2; }

ArgumentDeclList
    : ArgumentDecl                              { $$ = $1; }
    | ArgumentDecl ',' ArgumentDeclList         { $1->next = $3; $$ = $1; }

ArgumentDecl
442 443 444
    : Identifier EmptyBrackets_opt              { $$ = new_argument_decl(ctx, $1, TRUE); }
    | tBYREF Identifier EmptyBrackets_opt       { $$ = new_argument_decl(ctx, $2, TRUE); }
    | tBYVAL Identifier EmptyBrackets_opt       { $$ = new_argument_decl(ctx, $2, FALSE); }
445

446
/* these keywords may also be an identifier, depending on context */
447 448
Identifier
    : tIdentifier    { $$ = $1; }
449 450 451 452 453
    | tDEFAULT       { $$ = $1; }
    | tERROR         { $$ = $1; }
    | tEXPLICIT      { $$ = $1; }
    | tPROPERTY      { $$ = $1; }
    | tSTEP          { $$ = $1; }
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
/* most keywords can be an identifier after a dot */
DotIdentifier
    : Identifier     { $$ = $1; }
    | tTRUE          { $$ = $1; }
    | tFALSE         { $$ = $1; }
    | tNOT           { $$ = $1; }
    | tAND           { $$ = $1; }
    | tOR            { $$ = $1; }
    | tXOR           { $$ = $1; }
    | tEQV           { $$ = $1; }
    | tIMP           { $$ = $1; }
    | tIS            { $$ = $1; }
    | tMOD           { $$ = $1; }
    | tCALL          { $$ = $1; }
    | tDIM           { $$ = $1; }
    | tSUB           { $$ = $1; }
    | tFUNCTION      { $$ = $1; }
    | tGET           { $$ = $1; }
    | tLET           { $$ = $1; }
    | tCONST         { $$ = $1; }
    | tIF            { $$ = $1; }
    | tELSE          { $$ = $1; }
    | tELSEIF        { $$ = $1; }
    | tEND           { $$ = $1; }
    | tTHEN          { $$ = $1; }
    | tEXIT          { $$ = $1; }
    | tWHILE         { $$ = $1; }
    | tWEND          { $$ = $1; }
    | tDO            { $$ = $1; }
    | tLOOP          { $$ = $1; }
    | tUNTIL         { $$ = $1; }
    | tFOR           { $$ = $1; }
    | tTO            { $$ = $1; }
    | tEACH          { $$ = $1; }
    | tIN            { $$ = $1; }
    | tSELECT        { $$ = $1; }
    | tCASE          { $$ = $1; }
    | tBYREF         { $$ = $1; }
    | tBYVAL         { $$ = $1; }
    | tOPTION        { $$ = $1; }
    | tNOTHING       { $$ = $1; }
    | tEMPTY         { $$ = $1; }
    | tNULL          { $$ = $1; }
    | tCLASS         { $$ = $1; }
    | tSET           { $$ = $1; }
    | tNEW           { $$ = $1; }
    | tPUBLIC        { $$ = $1; }
    | tPRIVATE       { $$ = $1; }
    | tNEXT          { $$ = $1; }
    | tON            { $$ = $1; }
    | tRESUME        { $$ = $1; }
    | tGOTO          { $$ = $1; }

508
/* Most statements accept both new line and ':' as separators */
509 510 511
StSep
    : tNL
    | ':'
512 513
    | tNL StSep
    | ':' StSep
514

515 516
%%

517
static int parser_error(parser_ctx_t *ctx, const char *str)
518 519 520 521
{
    return 0;
}

522 523
static void source_add_statement(parser_ctx_t *ctx, statement_t *stat)
{
524 525 526
    if(!stat)
        return;

527 528 529 530 531 532 533 534
    if(ctx->stats) {
        ctx->stats_tail->next = stat;
        ctx->stats_tail = stat;
    }else {
        ctx->stats = ctx->stats_tail = stat;
    }
}

535 536 537 538 539 540
static void source_add_class(parser_ctx_t *ctx, class_decl_t *class_decl)
{
    class_decl->next = ctx->class_decls;
    ctx->class_decls = class_decl;
}

541
static void parse_complete(parser_ctx_t *ctx, BOOL option_explicit)
542 543
{
    ctx->parse_complete = TRUE;
544
    ctx->option_explicit = option_explicit;
545 546
}

547
static void *new_expression(parser_ctx_t *ctx, expression_type_t type, size_t size)
548 549 550 551 552 553 554 555 556 557 558 559
{
    expression_t *expr;

    expr = parser_alloc(ctx, size ? size : sizeof(*expr));
    if(expr) {
        expr->type = type;
        expr->next = NULL;
    }

    return expr;
}

560 561 562 563 564 565 566 567 568 569 570 571
static expression_t *new_bool_expression(parser_ctx_t *ctx, VARIANT_BOOL value)
{
    bool_expression_t *expr;

    expr = new_expression(ctx, EXPR_BOOL, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->value = value;
    return &expr->expr;
}

572 573 574 575 576 577 578 579 580 581 582 583
static expression_t *new_string_expression(parser_ctx_t *ctx, const WCHAR *value)
{
    string_expression_t *expr;

    expr = new_expression(ctx, EXPR_STRING, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->value = value;
    return &expr->expr;
}

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static expression_t *new_long_expression(parser_ctx_t *ctx, expression_type_t type, LONG value)
{
    int_expression_t *expr;

    expr = new_expression(ctx, type, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->value = value;
    return &expr->expr;
}

static expression_t *new_double_expression(parser_ctx_t *ctx, double value)
{
    double_expression_t *expr;

    expr = new_expression(ctx, EXPR_DOUBLE, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->value = value;
    return &expr->expr;
}

608 609 610 611 612 613 614 615 616 617 618 619
static expression_t *new_unary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *subexpr)
{
    unary_expression_t *expr;

    expr = new_expression(ctx, type, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->subexpr = subexpr;
    return &expr->expr;
}

620 621 622 623 624 625 626 627 628 629 630 631 632
static expression_t *new_binary_expression(parser_ctx_t *ctx, expression_type_t type, expression_t *left, expression_t *right)
{
    binary_expression_t *expr;

    expr = new_expression(ctx, type, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->left = left;
    expr->right = right;
    return &expr->expr;
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646
static member_expression_t *new_member_expression(parser_ctx_t *ctx, expression_t *obj_expr, const WCHAR *identifier)
{
    member_expression_t *expr;

    expr = new_expression(ctx, EXPR_MEMBER, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->obj_expr = obj_expr;
    expr->identifier = identifier;
    expr->args = NULL;
    return expr;
}

647 648 649 650 651 652 653 654 655 656 657 658
static expression_t *new_new_expression(parser_ctx_t *ctx, const WCHAR *identifier)
{
    string_expression_t *expr;

    expr = new_expression(ctx, EXPR_NEW, sizeof(*expr));
    if(!expr)
        return NULL;

    expr->value = identifier;
    return &expr->expr;
}

659
static void *new_statement(parser_ctx_t *ctx, statement_type_t type, size_t size)
660 661 662
{
    statement_t *stat;

663
    stat = parser_alloc(ctx, size ? size : sizeof(*stat));
664 665 666 667 668 669 670 671
    if(stat) {
        stat->type = type;
        stat->next = NULL;
    }

    return stat;
}

672
static statement_t *new_call_statement(parser_ctx_t *ctx, BOOL is_strict, member_expression_t *expr)
673 674 675 676 677 678 679 680
{
    call_statement_t *stat;

    stat = new_statement(ctx, STAT_CALL, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->expr = expr;
681
    stat->is_strict = is_strict;
682 683 684
    return &stat->stat;
}

685 686 687 688 689 690 691 692 693 694 695 696 697
static statement_t *new_assign_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
{
    assign_statement_t *stat;

    stat = new_statement(ctx, STAT_ASSIGN, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->member_expr = left;
    stat->value_expr = right;
    return &stat->stat;
}

698 699 700 701 702 703 704 705 706 707 708 709 710
static statement_t *new_set_statement(parser_ctx_t *ctx, member_expression_t *left, expression_t *right)
{
    assign_statement_t *stat;

    stat = new_statement(ctx, STAT_SET, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->member_expr = left;
    stat->value_expr = right;
    return &stat->stat;
}

711
static dim_decl_t *new_dim_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL is_array, dim_list_t *dims)
712 713 714 715 716 717 718 719
{
    dim_decl_t *decl;

    decl = parser_alloc(ctx, sizeof(*decl));
    if(!decl)
        return NULL;

    decl->name = name;
720 721 722
    decl->is_array = is_array;
    decl->dims = dims;
    decl->next = NULL;
723 724 725
    return decl;
}

726 727 728 729 730 731 732 733 734 735 736 737 738
static dim_list_t *new_dim(parser_ctx_t *ctx, unsigned val, dim_list_t *next)
{
    dim_list_t *ret;

    ret = parser_alloc(ctx, sizeof(*ret));
    if(!ret)
        return NULL;

    ret->val = val;
    ret->next = next;
    return ret;
}

739 740 741 742 743 744 745 746 747 748 749 750
static statement_t *new_dim_statement(parser_ctx_t *ctx, dim_decl_t *decls)
{
    dim_statement_t *stat;

    stat = new_statement(ctx, STAT_DIM, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->dim_decls = decls;
    return &stat->stat;
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764
static elseif_decl_t *new_elseif_decl(parser_ctx_t *ctx, expression_t *expr, statement_t *stat)
{
    elseif_decl_t *decl;

    decl = parser_alloc(ctx, sizeof(*decl));
    if(!decl)
        return NULL;

    decl->expr = expr;
    decl->stat = stat;
    decl->next = NULL;
    return decl;
}

765 766 767 768 769 770 771 772 773 774 775 776 777
static statement_t *new_while_statement(parser_ctx_t *ctx, statement_type_t type, expression_t *expr, statement_t *body)
{
    while_statement_t *stat;

    stat = new_statement(ctx, type, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->expr = expr;
    stat->body = body;
    return &stat->stat;
}

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
static statement_t *new_forto_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *from_expr,
        expression_t *to_expr, expression_t *step_expr, statement_t *body)
{
    forto_statement_t *stat;

    stat = new_statement(ctx, STAT_FORTO, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->identifier = identifier;
    stat->from_expr = from_expr;
    stat->to_expr = to_expr;
    stat->step_expr = step_expr;
    stat->body = body;
    return &stat->stat;
}

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
static statement_t *new_foreach_statement(parser_ctx_t *ctx, const WCHAR *identifier, expression_t *group_expr,
        statement_t *body)
{
    foreach_statement_t *stat;

    stat = new_statement(ctx, STAT_FOREACH, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->identifier = identifier;
    stat->group_expr = group_expr;
    stat->body = body;
    return &stat->stat;
}

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
static statement_t *new_if_statement(parser_ctx_t *ctx, expression_t *expr, statement_t *if_stat, elseif_decl_t *elseif_decl,
        statement_t *else_stat)
{
    if_statement_t *stat;

    stat = new_statement(ctx, STAT_IF, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->expr = expr;
    stat->if_stat = if_stat;
    stat->elseifs = elseif_decl;
    stat->else_stat = else_stat;
    return &stat->stat;
}

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
static statement_t *new_select_statement(parser_ctx_t *ctx, expression_t *expr, case_clausule_t *case_clausules)
{
    select_statement_t *stat;

    stat = new_statement(ctx, STAT_SELECT, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->expr = expr;
    stat->case_clausules = case_clausules;
    return &stat->stat;
}

static case_clausule_t *new_case_clausule(parser_ctx_t *ctx, expression_t *expr, statement_t *stat, case_clausule_t *next)
{
    case_clausule_t *ret;

    ret = parser_alloc(ctx, sizeof(*ret));
    if(!ret)
        return NULL;

    ret->expr = expr;
    ret->stat = stat;
    ret->next = next;
    return ret;
}

853 854 855 856 857 858 859 860 861 862 863 864
static statement_t *new_onerror_statement(parser_ctx_t *ctx, BOOL resume_next)
{
    onerror_statement_t *stat;

    stat = new_statement(ctx, STAT_ONERROR, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->resume_next = resume_next;
    return &stat->stat;
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
static arg_decl_t *new_argument_decl(parser_ctx_t *ctx, const WCHAR *name, BOOL by_ref)
{
    arg_decl_t *arg_decl;

    arg_decl = parser_alloc(ctx, sizeof(*arg_decl));
    if(!arg_decl)
        return NULL;

    arg_decl->name = name;
    arg_decl->by_ref = by_ref;
    arg_decl->next = NULL;
    return arg_decl;
}

static function_decl_t *new_function_decl(parser_ctx_t *ctx, const WCHAR *name, function_type_t type,
880
        unsigned storage_flags, arg_decl_t *arg_decl, statement_t *body)
881 882 883
{
    function_decl_t *decl;

884
    if(storage_flags & STORAGE_IS_DEFAULT) {
885
        if(type == FUNC_PROPGET) {
886
            type = FUNC_DEFGET;
887 888 889 890 891
        }else {
            FIXME("Invalid default property\n");
            ctx->hres = E_FAIL;
            return NULL;
        }
892 893
    }

894 895 896 897 898 899
    decl = parser_alloc(ctx, sizeof(*decl));
    if(!decl)
        return NULL;

    decl->name = name;
    decl->type = type;
900
    decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
901 902
    decl->args = arg_decl;
    decl->body = body;
903
    decl->next = NULL;
904
    decl->next_prop_func = NULL;
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    return decl;
}

static statement_t *new_function_statement(parser_ctx_t *ctx, function_decl_t *decl)
{
    function_statement_t *stat;

    stat = new_statement(ctx, STAT_FUNC, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->func_decl = decl;
    return &stat->stat;
}

920 921 922 923 924 925 926 927
static class_decl_t *new_class_decl(parser_ctx_t *ctx)
{
    class_decl_t *class_decl;

    class_decl = parser_alloc(ctx, sizeof(*class_decl));
    if(!class_decl)
        return NULL;

928
    class_decl->funcs = NULL;
929
    class_decl->props = NULL;
930 931 932 933
    class_decl->next = NULL;
    return class_decl;
}

934 935 936 937 938 939 940 941 942 943 944
static class_decl_t *add_class_function(parser_ctx_t *ctx, class_decl_t *class_decl, function_decl_t *decl)
{
    function_decl_t *iter;

    for(iter = class_decl->funcs; iter; iter = iter->next) {
        if(!strcmpiW(iter->name, decl->name)) {
            if(decl->type == FUNC_SUB || decl->type == FUNC_FUNCTION) {
                FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
                ctx->hres = E_FAIL;
                return NULL;
            }
945 946 947 948 949 950 951 952 953 954 955 956 957 958

            while(1) {
                if(iter->type == decl->type) {
                    FIXME("Redefinition of %s::%s\n", debugstr_w(class_decl->name), debugstr_w(decl->name));
                    ctx->hres = E_FAIL;
                    return NULL;
                }
                if(!iter->next_prop_func)
                    break;
                iter = iter->next_prop_func;
            }

            iter->next_prop_func = decl;
            return class_decl;
959 960 961 962 963 964 965 966
        }
    }

    decl->next = class_decl->funcs;
    class_decl->funcs = decl;
    return class_decl;
}

967
static class_decl_t *add_dim_prop(parser_ctx_t *ctx, class_decl_t *class_decl, dim_decl_t *dim_decl, unsigned storage_flags)
968 969 970 971 972 973 974
{
    if(storage_flags & STORAGE_IS_DEFAULT) {
        FIXME("variant prop van't be default value\n");
        ctx->hres = E_FAIL;
        return NULL;
    }

975 976 977
    dim_decl->is_public = !(storage_flags & STORAGE_IS_PRIVATE);
    dim_decl->next = class_decl->props;
    class_decl->props = dim_decl;
978 979 980
    return class_decl;
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
static const_decl_t *new_const_decl(parser_ctx_t *ctx, const WCHAR *name, expression_t *expr)
{
    const_decl_t *decl;

    decl = parser_alloc(ctx, sizeof(*decl));
    if(!decl)
        return NULL;

    decl->name = name;
    decl->value_expr = expr;
    decl->next = NULL;
    return decl;
}

static statement_t *new_const_statement(parser_ctx_t *ctx, const_decl_t *decls)
{
    const_statement_t *stat;

    stat = new_statement(ctx, STAT_CONST, sizeof(*stat));
    if(!stat)
        return NULL;

    stat->decls = decls;
    return &stat->stat;
}

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
static statement_t *link_statements(statement_t *head, statement_t *tail)
{
    statement_t *iter;

    for(iter = head; iter->next; iter = iter->next);
    iter->next = tail;

    return head;
}

1017 1018 1019 1020
void *parser_alloc(parser_ctx_t *ctx, size_t size)
{
    void *ret;

1021
    ret = heap_pool_alloc(&ctx->heap, size);
1022 1023 1024 1025 1026
    if(!ret)
        ctx->hres = E_OUTOFMEMORY;
    return ret;
}

1027
HRESULT parse_script(parser_ctx_t *ctx, const WCHAR *code, const WCHAR *delimiter)
1028
{
1029
    static const WCHAR html_delimiterW[] = {'<','/','s','c','r','i','p','t','>',0};
1030

1031 1032 1033
    ctx->code = ctx->ptr = code;
    ctx->end = ctx->code + strlenW(ctx->code);

1034
    heap_pool_init(&ctx->heap);
1035

1036 1037 1038
    ctx->parse_complete = FALSE;
    ctx->hres = S_OK;

1039 1040
    ctx->last_token = tNL;
    ctx->last_nl = 0;
1041
    ctx->stats = ctx->stats_tail = NULL;
1042
    ctx->class_decls = NULL;
1043
    ctx->option_explicit = FALSE;
1044
    ctx->is_html = delimiter && !strcmpiW(delimiter, html_delimiterW);
1045

1046 1047 1048 1049 1050
    parser_parse(ctx);

    if(FAILED(ctx->hres))
        return ctx->hres;
    if(!ctx->parse_complete) {
1051
        FIXME("parser failed around %s\n", debugstr_w(ctx->code+20 > ctx->ptr ? ctx->code : ctx->ptr-20));
1052 1053 1054 1055 1056
        return E_FAIL;
    }

    return S_OK;
}
1057 1058 1059

void parser_release(parser_ctx_t *ctx)
{
1060
    heap_pool_free(&ctx->heap);
1061
}