From 3defb6abe3a2b2ee633d9ca602ba7ed95dcd2db2 Mon Sep 17 00:00:00 2001 From: anjiaqi Date: Wed, 6 Aug 2025 17:45:09 +0800 Subject: [PATCH] Fix unexpected CTE without semicolon Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICRM8B Signed-off-by: anjiaqi --- ets2panda/parser/ETSparserExpressions.cpp | 3 ++- .../test/ast/compiler/ets/parser_format.ets | 10 +++---- ...ut_semicolon_before_struct_declaration.ets | 22 ++++++++++++++++ .../annotationDecl_parser_bad_token04.ets | 5 +--- .../test/ast/parser/ets/class_keyword.ets | 11 +++----- .../parser/ets/interface_parser_error_1.ets | 17 ++++-------- ...out_semicolon_before_class_declaration.ets | 26 +++++++++++++++++++ 7 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/without_semicolon_before_struct_declaration.ets create mode 100644 ets2panda/test/runtime/ets/without_semicolon_before_class_declaration.ets diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 45ea49b802..f9568ecaa3 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -309,7 +309,8 @@ ir::Expression *ETSParser::ParseDefaultPrimaryExpression(ExpressionParseFlags fl Lexer()->NextToken(); // eat '.' } - if (Lexer()->GetToken().Type() == lexer::TokenType::KEYW_CLASS || IsStructKeyword()) { + if ((Lexer()->GetToken().Type() == lexer::TokenType::KEYW_CLASS || IsStructKeyword()) && + potentialType->IsBrokenTypeNode()) { Lexer()->NextToken(); // eat 'class' and 'struct' auto *classLiteral = AllocNode(potentialType); ES2PANDA_ASSERT(classLiteral != nullptr); diff --git a/ets2panda/test/ast/compiler/ets/parser_format.ets b/ets2panda/test/ast/compiler/ets/parser_format.ets index a468621ea1..0cfa428977 100644 --- a/ets2panda/test/ast/compiler/ets/parser_format.ets +++ b/ets2panda/test/ast/compiler/ets/parser_format.ets @@ -17,10 +17,8 @@ declaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaare struct MainProg2 { @@Stave } -/* @@? 16:1 Error TypeError: Cannot find type 'declaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaare'. */ -/* @@? 16:1 Error TypeError: Class literal is not yet supported. */ -/* @@? 16:46 Error SyntaxError: Unexpected token 'MainProg2'. */ -/* @@? 16:46 Error TypeError: Unresolved reference MainProg2 */ -/* @@? 16:56 Error SyntaxError: Unexpected token '{'. */ +/* @@? 16:1 Error TypeError: Unresolved reference declaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaare */ +/* @@? 16:39 Error SyntaxError: Unexpected token 'struct'. */ +/* @@? 16:39 Error TypeError: Structs are only used to define UI components, it should be translated at 'plugin after parser' phase. */ /* @@? 17:5 Error SyntaxError: There is no any node to insert at the placeholder position. */ -/* @@? 17:7 Error TypeError: Unresolved reference Stave */ +/* @@? 17:12 Error SyntaxError: Field type annotation expected. */ diff --git a/ets2panda/test/ast/compiler/ets/without_semicolon_before_struct_declaration.ets b/ets2panda/test/ast/compiler/ets/without_semicolon_before_struct_declaration.ets new file mode 100644 index 0000000000..95aac85f2f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/without_semicolon_before_struct_declaration.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +let x: number = 1; +const y: number = x +struct C { + f: number = 1; +} + +/* @@? 18:1 Error TypeError: Structs are only used to define UI components, it should be translated at 'plugin after parser' phase. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_parser_bad_token04.ets b/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_parser_bad_token04.ets index 88cfdb4017..21d016f414 100644 --- a/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_parser_bad_token04.ets +++ b/ets2panda/test/ast/parser/ets/annotations_tests/annotationDecl_parser_bad_token04.ets @@ -19,7 +19,4 @@ import * as Src from "./annotation_export" Src.MyAnno class /* @@ label */A/* @@ label1 */{} -/* @@? 19:1 Error TypeError: Class literal is not yet supported. */ -/* @@? 20:21 Error SyntaxError: Unexpected token 'A'. */ -/* @@? 20:21 Error TypeError: Unresolved reference A */ -/* @@? 20:37 Error SyntaxError: Unexpected token '{'. */ +/* @@? 19:5 Error TypeError: Property 'MyAnno' does not exist on type 'annotation_export' */ diff --git a/ets2panda/test/ast/parser/ets/class_keyword.ets b/ets2panda/test/ast/parser/ets/class_keyword.ets index f34d001a8c..93b3062592 100644 --- a/ets2panda/test/ast/parser/ets/class_keyword.ets +++ b/ets2panda/test/ast/parser/ets/class_keyword.ets @@ -13,13 +13,10 @@ * limitations under the License. */ -final /* @@ label */kngkgj class /* @@ label1 */B /* @@ label2 */{ +final kngkgj class B { } -/* @@@ label Error SyntaxError: Unexpected token 'kngkgj'. */ -/* @@? 16:21 Error TypeError: Cannot find type 'kngkgj'. */ -/* @@? 16:21 Error TypeError: Class literal is not yet supported. */ -/* @@@ label1 Error SyntaxError: Unexpected token 'B'. */ -/* @@? 16:49 Error TypeError: Unresolved reference B */ -/* @@@ label2 Error SyntaxError: Unexpected token '{'. */ +/* @@? 16:7 Error SyntaxError: Unexpected token 'kngkgj'. */ +/* @@? 16:7 Error TypeError: Unresolved reference kngkgj */ +/* @@? 16:14 Error SyntaxError: Unexpected token 'class'. */ diff --git a/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets b/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets index 050fbd6b1c..692ad1e69d 100644 --- a/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets +++ b/ets2panda/test/ast/parser/ets/interface_parser_error_1.ets @@ -47,24 +47,17 @@ function mdin() { let a = new A(); /* @@? 20:16 Error SyntaxError: Unexpected token, expected ','. */ /* @@? 20:16 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ /* @@? 21:1 Error SyntaxError: Identifier expected. */ -/* @@? 22:1 Error TypeError: Class literal is not yet supported. */ -/* @@? 22:1 Error TypeError: Cannot find type 'inter'. */ -/* @@? 24:7 Error SyntaxError: Unexpected token 'A'. */ -/* @@? 24:7 Error TypeError: Unresolved reference A */ -/* @@? 24:9 Error SyntaxError: Unexpected token 'implements'. */ -/* @@? 24:20 Error SyntaxError: Unexpected token 'I'. */ -/* @@? 24:20 Error TypeError: Unresolved reference I */ -/* @@? 24:22 Error SyntaxError: Unexpected token '{'. */ -/* @@? 25:4 Error TypeError: Unresolved reference reanstructor */ -/* @@? 26:19 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 22:1 Error TypeError: Unresolved reference inter */ +/* @@? 24:1 Error SyntaxError: Illegal start of CLASS expression. */ /* @@? 26:19 Error SyntaxError: Unexpected token '�ls'. */ +/* @@? 26:19 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 27:5 Error SyntaxError: Unexpected token 'A'. */ /* @@? 27:7 Error SyntaxError: Unexpected token '{'. */ -/* @@? 28:5 Error SyntaxError: Hard keyword 'constructor' cannot be used as identifier */ /* @@? 28:5 Error SyntaxError: Identifier expected, got 'constructor'. */ +/* @@? 28:5 Error SyntaxError: Hard keyword 'constructor' cannot be used as identifier */ /* @@? 28:16 Error SyntaxError: Invalid annotation name. */ /* @@? 28:19 Error SyntaxError: Annotations are not allowed on this type of declaration. */ /* @@? 29:18 Error SyntaxError: Unexpected token, expected an identifier. */ /* @@? 31:2 Error SyntaxError: Unexpected token '*'. */ /* @@? 32:1 Error SyntaxError: Nested functions are not allowed. */ -/* @@? 71:1 Error SyntaxError: Expected '}', got 'end of stream'. */ +/* @@? 64:1 Error SyntaxError: Expected '}', got 'end of stream'. */ diff --git a/ets2panda/test/runtime/ets/without_semicolon_before_class_declaration.ets b/ets2panda/test/runtime/ets/without_semicolon_before_class_declaration.ets new file mode 100644 index 0000000000..63217805dd --- /dev/null +++ b/ets2panda/test/runtime/ets/without_semicolon_before_class_declaration.ets @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +let x: number = 1; +const y: number = x +class C { + foo(a: Int): Int { + return a; + } +} + +function main(): void { + arktest.assertEQ(new C().foo(1), 1) +} \ No newline at end of file -- Gitee