From: Francis De Brabandere <francisdb@gmail.com> A ClassDeclaration was only accepted as a standalone top-level SourceElement, so a class that followed another statement on the same line (e.g. Dim x : Class C) failed to parse, and a duplicate class name was reported at the first declaration rather than the later one. Make a class one of the units a global line is built from, registered in source order, so such a line parses and a redefinition is reported at the later declaration as native does. Because a class is only valid at global scope it is reachable only there, not from the shared statement grammar used by procedure and control-flow bodies, so a class anywhere else is reported as a syntax error at its location. --- dlls/vbscript/parser.y | 31 +++++++++++++++++++++++++------ dlls/vbscript/tests/run.c | 21 ++++++++------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/dlls/vbscript/parser.y b/dlls/vbscript/parser.y index ec67cde2215..9425a1f2aed 100644 --- a/dlls/vbscript/parser.y +++ b/dlls/vbscript/parser.y @@ -149,7 +149,7 @@ static statement_t *link_statements(statement_t*,statement_t*); %right tNOT %left '=' tNEQ '>' '<' tGTEQ tLTEQ tIS -%type <statement> Statement SimpleStatement StatementNl StatementsNl StatementsNl_opt BodyStatements IfStatement Else_opt +%type <statement> Statement SimpleStatement StatementNl GlobalUnit StatementsNl StatementsNl_opt BodyStatements IfStatement Else_opt %type <statement> GlobalDimDeclaration StatementsBody StatementsBody_opt %type <expression> Expression LiteralExpression PrimaryExpression EqualityExpression CallExpression ExpressionNl_opt %type <expression> ConcatExpression AdditiveExpression ModExpression IntdivExpression MultiplicativeExpression ExpExpression @@ -185,8 +185,17 @@ SourceElements : /* empty */ | SourceElements GlobalDimDeclaration StSep { source_add_statement(ctx, $2); } - | SourceElements StatementNl { source_add_statement(ctx, $2); } - | SourceElements ClassDeclaration { source_add_class(ctx, $2); } + | SourceElements GlobalUnit StSep { source_add_statement(ctx, $2); } + +/* A global line is a StSep-separated sequence of units; a unit may be a Class + declaration. Classes are therefore reachable only at script global scope, not + from the shared SimpleStatement used by every body, so a Class anywhere else + is rejected as a syntax error (see the tCLASS rule in SimpleStatement), while + still being allowed after another statement, e.g. Dim x : Class C. A Class is + not a statement, so it is registered directly and yields no statement. */ +GlobalUnit + : SimpleStatement { $$ = $1; } + | ClassDeclaration { source_add_class(ctx, $1); $$ = NULL; } GlobalDimDeclaration : tPRIVATE tCONST ConstDeclList { $$ = new_const_statement(ctx, @$, $3); CHECK_ERROR; } @@ -267,6 +276,12 @@ SimpleStatement | tDO StSep StatementsNl_opt error { ctx->hres = MAKE_VBSERROR(VBSE_EXPECTED_LOOP); YYABORT; } | tDO error { ctx->hres = MAKE_VBSERROR(VBSE_EXPECTED_WHILE_UNTIL_EOS); YYABORT; } | FunctionDecl { $$ = new_function_statement(ctx, @$, $1); CHECK_ERROR; } + | tCLASS { /* A Class is only valid at global scope (see GlobalUnit). The real + ClassDeclaration wins the shift there, so this rule only matches in a + body, where it reports the syntax error native reports. It is needed: + without it, error recovery surfaces a different, context-dependent + error code instead of the plain syntax error. */ + ctx->error_loc = @1; ctx->hres = MAKE_VBSERROR(VBSE_SYNTAX_ERROR); YYABORT; } | tEXIT tDO { $$ = new_statement(ctx, STAT_EXITDO, 0, @2); CHECK_ERROR; } | tEXIT tFOR { $$ = new_statement(ctx, STAT_EXITFOR, 0, @2); CHECK_ERROR; } | tEXIT tFUNCTION { $$ = new_statement(ctx, STAT_EXITFUNC, 0, @2); CHECK_ERROR; } @@ -574,7 +589,7 @@ PrimaryExpression | tME { $$ = new_expression(ctx, EXPR_ME, 0); CHECK_ERROR; } ClassDeclaration - : tCLASS Identifier StSep ClassBody tEND tCLASS StSep { $4->name = $2; $4->loc = @2; $$ = $4; } + : tCLASS Identifier StSep ClassBody tEND tCLASS { $4->name = $2; $4->loc = @2; $$ = $4; } | tCLASS Identifier tEND tCLASS { ctx->error_loc = @3; ctx->hres = MAKE_VBSERROR(VBSE_EXPECTED_STATEMENT); YYABORT; } | tCLASS Identifier StSep ClassBody tEND error { ctx->hres = MAKE_VBSERROR(VBSE_EXPECTED_CLASS); YYABORT; } @@ -733,8 +748,12 @@ static void source_add_statement(parser_ctx_t *ctx, statement_t *stat) 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; + class_decl_t **iter; + + /* Append to keep classes in source order, so a redefinition is reported at the later declaration. */ + class_decl->next = NULL; + for(iter = &ctx->class_decls; *iter; iter = &(*iter)->next); + *iter = class_decl; } static void handle_isexpression_script(parser_ctx_t *ctx, expression_t *expr) diff --git a/dlls/vbscript/tests/run.c b/dlls/vbscript/tests/run.c index 2d43651af84..30a099d1a5f 100644 --- a/dlls/vbscript/tests/run.c +++ b/dlls/vbscript/tests/run.c @@ -3681,20 +3681,18 @@ static void test_class_decl_scope(void) BOOL expect_ok; /* whether the script should compile */ USHORT error_code; /* expected error number when it should not */ ULONG error_line; /* expected 0-based error line when it should not */ - BOOL todo; } tests[] = { /* A Class declaration may follow another statement separated by ':'. */ - { L"Dim x : Class C\nPublic v\nEnd Class\n", TRUE, 0, 0, TRUE }, + { L"Dim x : Class C\nPublic v\nEnd Class\n", TRUE }, /* A Class declared inside a procedure body is rejected. */ - { L"Sub S\nClass C\nEnd Class\nEnd Sub\n", FALSE, 1002, 1, TRUE }, + { L"Sub S\nClass C\nEnd Class\nEnd Sub\n", FALSE, 1002, 1 }, /* A Class declared inside a control-flow block is rejected. */ - { L"If True Then\nClass C\nEnd Class\nEnd If\n", FALSE, 1002, 1, TRUE }, + { L"If True Then\nClass C\nEnd Class\nEnd If\n", FALSE, 1002, 1 }, /* A duplicate Class name is reported at the later declaration. */ - { L"Class C\nEnd Class\nDim x : Class C\nEnd Class\n", FALSE, 1041, 2, TRUE }, + { L"Class C\nEnd Class\nDim x : Class C\nEnd Class\n", FALSE, 1041, 2 }, }; HRESULT hres; unsigned i; - BOOL pass; for (i = 0; i < ARRAY_SIZE(tests); i++) { error_line = ~0; @@ -3705,14 +3703,11 @@ static void test_class_decl_scope(void) CLEAR_CALLED(OnScriptError); if (tests[i].expect_ok) - pass = hres == S_OK; + ok(hres == S_OK, "[%u] %s: hres=%08lx\n", i, wine_dbgstr_w(tests[i].src), hres); else - pass = FAILED(hres) && error_code == tests[i].error_code - && error_line == tests[i].error_line; - - todo_wine_if(tests[i].todo) - ok(pass, "[%u] %s: hres=%08lx code=%u line=%lu\n", i, wine_dbgstr_w(tests[i].src), - hres, error_code, error_line); + ok(FAILED(hres) && error_code == tests[i].error_code && error_line == tests[i].error_line, + "[%u] %s: hres=%08lx code=%u line=%lu\n", i, wine_dbgstr_w(tests[i].src), + hres, error_code, error_line); } } -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/10897