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 | 25 ++++++++++++++++++++----- dlls/vbscript/tests/run.c | 21 ++++++++------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/dlls/vbscript/parser.y b/dlls/vbscript/parser.y index ec67cde2215..fea607a8d63 100644 --- a/dlls/vbscript/parser.y +++ b/dlls/vbscript/parser.y @@ -185,8 +185,13 @@ 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 SimpleStatement StSep { source_add_statement(ctx, $2); } + | SourceElements ClassDeclaration StSep { source_add_class(ctx, $2); } + +/* A Class declaration is reachable only here 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. */ GlobalDimDeclaration : tPRIVATE tCONST ConstDeclList { $$ = new_const_statement(ctx, @$, $3); CHECK_ERROR; } @@ -267,6 +272,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 SourceElements). 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 +585,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 +744,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 55b7e6b0f4c..990cf70c1e3 100644 --- a/dlls/vbscript/tests/run.c +++ b/dlls/vbscript/tests/run.c @@ -3681,16 +3681,14 @@ 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[] = { - { L"Dim x : Class C\nPublic v\nEnd Class\n", TRUE, 0, 0, TRUE }, - { L"Sub S\nClass C\nEnd Class\nEnd Sub\n", FALSE, 1002, 1, TRUE }, - { L"If True Then\nClass C\nEnd Class\nEnd If\n", FALSE, 1002, 1, TRUE }, - { L"Class C\nEnd Class\nDim x : Class C\nEnd Class\n", FALSE, 1041, 2, TRUE }, + { L"Dim x : Class C\nPublic v\nEnd Class\n", TRUE }, + { L"Sub S\nClass C\nEnd Class\nEnd Sub\n", FALSE, 1002, 1 }, + { L"If True Then\nClass C\nEnd Class\nEnd If\n", FALSE, 1002, 1 }, + { 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; @@ -3701,14 +3699,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