diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 0ede9666e1810073c547e5adf5f41a61acae891c..83603c511a871d649d83ee6bd9dad03bd9618cca 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1118,8 +1118,9 @@ ir::TypeNode *ETSParser::ParseLiteralIdent(TypeAnnotationParsingOptions *options if (Lexer()->GetToken().IsPredefinedType()) { return GetTypeAnnotationOfPrimitiveType(Lexer()->GetToken().KeywordType(), options); } - - if (Lexer()->TryEatTokenFromKeywordType(lexer::TokenType::KEYW_KEYOF)) { + + if ((((*options) & TypeAnnotationParsingOptions::IGNORE_KEYW_KEYOF) == 0) && + Lexer()->TryEatTokenFromKeywordType(lexer::TokenType::KEYW_KEYOF)) { auto keyofOptions = *options | TypeAnnotationParsingOptions::REPORT_ERROR; auto *typeAnnotation = ParseTypeAnnotationNoPreferParam(&keyofOptions); ES2PANDA_ASSERT(typeAnnotation != nullptr); diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 4fd573b45d684582cb2e7e7ad1b1865270dbcfc5..657ae8b0799b0a2b7bb79118696e5da7f02efa9a 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -305,9 +305,9 @@ ir::Expression *ETSParser::ParseDefaultPrimaryExpression(ExpressionParseFlags fl { auto startLoc = Lexer()->GetToken().Start(); auto savedPos = Lexer()->Save(); - TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::POTENTIAL_CLASS_LITERAL | - TypeAnnotationParsingOptions::IGNORE_FUNCTION_TYPE | - TypeAnnotationParsingOptions::DISALLOW_UNION; + TypeAnnotationParsingOptions options = + TypeAnnotationParsingOptions::POTENTIAL_CLASS_LITERAL | TypeAnnotationParsingOptions::IGNORE_FUNCTION_TYPE | + TypeAnnotationParsingOptions::DISALLOW_UNION | TypeAnnotationParsingOptions::IGNORE_KEYW_KEYOF; ir::TypeNode *potentialType = ParseTypeAnnotation(&options); if (potentialType != nullptr) { diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index d0238cef36ff4b495b3e6d9895561b7308db3631..1be1b162f43a07a4674db57aa6f0244a8f1c02c6 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -66,7 +66,8 @@ enum class TypeAnnotationParsingOptions : uint32_t { POTENTIAL_NEW_ARRAY = 1U << 16U, ANNOTATION_NOT_ALLOW = 1U << 17U, INSTANCEOF = 1U << 18U, - TYPE_ALIAS_CONTEXT = 1U << 19U + TYPE_ALIAS_CONTEXT = 1U << 19U, + IGNORE_KEYW_KEYOF = 1U << 20U }; enum class ParseListOptions : uint32_t { diff --git a/ets2panda/test/ast/parser/ets/soft_keyword_keyof.ets b/ets2panda/test/ast/parser/ets/soft_keyword_keyof.ets new file mode 100644 index 0000000000000000000000000000000000000000..a577d1efa467a77215fd3d268948baaa7e50757a --- /dev/null +++ b/ets2panda/test/ast/parser/ets/soft_keyword_keyof.ets @@ -0,0 +1,60 @@ +/* + * 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 keyof = 0; +keyof = 1 + +class A { + keyof = 1 +} + +class B { + readonly keyof = 1 +} + +interface I{ + test(keyof: String): String; +} + +class C implements I{ + test(keyof: String): String{ + return keyof; + } +} + +abstract class AD { + readonly keyof = 1 + abstract test(keyof: String): String; +} + +class D extends AD{ + test(keyof: String): String{ + return keyof; + } +} + +function main(): void{ + arktest.assertEQ(keyof, 1) + let ca = new A() + ca.keyof = 2 + arktest.assertEQ(ca.keyof, 2) + let cb = new B() + arktest.assertEQ(cb.keyof, 1) + let cc = new C() + arktest.assertEQ(cc.test("test"), "test") + let cd = new D() + arktest.assertEQ(cd.test("test"), "test") + arktest.assertEQ(cd.keyof, 1) +}