diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index bd8fcb7b1cf5b5fb2e3d2ae40926037f3d34e8af..6f530b731ac1334ded098e8f0642e9dbff3be494 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -270,6 +270,7 @@ private: ir::TypeNode *ParseFunctionType(TypeAnnotationParsingOptions *options, ir::TSTypeParameterDeclaration *typeParamDecl = nullptr); ir::TypeNode *ParseETSTupleType(TypeAnnotationParsingOptions *options); + ir::TypeNode *CreateErrorForWrongArrayType(lexer::SourcePosition &startPos); ir::TypeNode *ParseTsArrayType(ir::TypeNode *typeNode, TypeAnnotationParsingOptions *options); bool ParseTriplePeriod(bool spreadTypePresent); std::string PrimitiveTypeToName(ir::PrimitiveType type) const; diff --git a/ets2panda/parser/ETSparserTypes.cpp b/ets2panda/parser/ETSparserTypes.cpp index 209397b1dfd8a24599c3ead32b37462f3269c994..6c56ffe26501a77ae24a5989366565b57cc1a0c1 100644 --- a/ets2panda/parser/ETSparserTypes.cpp +++ b/ets2panda/parser/ETSparserTypes.cpp @@ -503,6 +503,18 @@ ir::TypeNode *ETSParser::ParseThisType(TypeAnnotationParsingOptions *options) return thisType; } +ir::TypeNode *ETSParser::CreateErrorForWrongArrayType(lexer::SourcePosition &startPos) +{ + if (Lexer()->GetToken().Type() != lexer::TokenType::LITERAL_STRING) { + LogExpectedToken(lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET); + } else { + Lexer()->NextToken(); // eat string lteral + Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET); + LogError(diagnostic::INDEXED_ACCESS_TYPE, {}, startPos); + } + return AllocBrokenType({startPos, Lexer()->GetToken().End()}); +} + ir::TypeNode *ETSParser::ParseTsArrayType(ir::TypeNode *typeNode, TypeAnnotationParsingOptions *options) { while (Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_SQUARE_BRACKET) { @@ -516,8 +528,7 @@ ir::TypeNode *ETSParser::ParseTsArrayType(ir::TypeNode *typeNode, TypeAnnotation if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET) { if ((*options & TypeAnnotationParsingOptions::REPORT_ERROR) != 0) { - LogExpectedToken(lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET); - return AllocBrokenType({Lexer()->GetToken().Start(), Lexer()->GetToken().End()}); + return CreateErrorForWrongArrayType(startPos); } return nullptr; } diff --git a/ets2panda/test/ast/compiler/ets/readonly/readonly_array.ets b/ets2panda/test/ast/compiler/ets/readonly/readonly_array.ets index 9df99d0e45be9a3cb4f45fa3819aa2f858b710d1..bace18b65dc77cca02417040a7ac92699f76e59d 100644 --- a/ets2panda/test/ast/compiler/ets/readonly/readonly_array.ets +++ b/ets2panda/test/ast/compiler/ets/readonly/readonly_array.ets @@ -15,7 +15,5 @@ let a: readonly int [""]; -/* @@? 16:22 Error SyntaxError: Unexpected token, expected ']'. */ -/* @@? 16:22 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */ -/* @@? 16:22 Error SyntaxError: Unexpected token ']'. */ -/* @@? 16:24 Error SyntaxError: Unexpected token ']'. */ +/* @@? 16:21 Error SyntaxError: Indexed access types are not supported, use type name instead! */ +/* @@? 16:25 Error SyntaxError: 'readonly' type modifier is only permitted on resizable array and tuple types. */ \ No newline at end of file diff --git a/ets2panda/test/ast/parser/ets/indexed_access_type_1.ets b/ets2panda/test/ast/parser/ets/indexed_access_type_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..e61fe1d3fab1458a696e6e22bb016527c4ecfc77 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/indexed_access_type_1.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +type Point = /* @@ label1 */{x: number = 0; y: number = 0} +type N = Point/* @@ label2 */["x"] + +/* @@@ label1 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ +/* @@@ label2 Error SyntaxError: Indexed access types are not supported, use type name instead! */ diff --git a/ets2panda/test/ast/parser/ets/indexed_access_type_2.ets b/ets2panda/test/ast/parser/ets/indexed_access_type_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..479a44a0de92857745272bd7e96e12be612a355d --- /dev/null +++ b/ets2panda/test/ast/parser/ets/indexed_access_type_2.ets @@ -0,0 +1,19 @@ +/* + * 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. + */ + +class Point {x: number = 0; y: number = 0} +type N = Point/* @@ label */["x"] + +/* @@@ label Error SyntaxError: Indexed access types are not supported, use type name instead! */ diff --git a/ets2panda/test/ast/parser/ets/invalid_type_assignment.ets b/ets2panda/test/ast/parser/ets/invalid_type_assignment.ets index 763dbf30f6cef41fb6af13babf263fcc9ff70b47..814b8ad67da037693c17f0a3aeb06aef2da51219 100644 --- a/ets2panda/test/ast/parser/ets/invalid_type_assignment.ets +++ b/ets2panda/test/ast/parser/ets/invalid_type_assignment.ets @@ -21,6 +21,4 @@ flags: [dynamic-ast] type AxeX = Point['x']; /* @@? 20:18 Error SyntaxError: Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly! */ -/* @@? 21:23 Error SyntaxError: Unexpected token, expected ']'. */ -/* @@? 21:23 Error SyntaxError: Unexpected token ']'. */ -/* @@? 21:26 Error SyntaxError: Unexpected token ']'. */ \ No newline at end of file +/* @@? 21:22 Error SyntaxError: Indexed access types are not supported, use type name instead! */ \ No newline at end of file diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index bfc4c833fee12b73b0133ec6f9476fbc1b61fa0b..527dc3abaeaa66c16c81ba2a379e282b1febd78d 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -464,6 +464,10 @@ syntax: id: 234 message: "Property or signature expected." +- name: INDEXED_ACCESS_TYPE + id: 49888 + message: "Indexed access types are not supported, use type name instead!" + - name: INDEX_MISSING_IDENTIFIER id: 21 message: "Return type of index signature from exported class or interface need to be identifier."