diff --git a/ets2panda/lexer/scripts/keywords.yaml b/ets2panda/lexer/scripts/keywords.yaml index de7a45f209674d90d71aeedeb1bffe0ec8010011..800f183c3b0f40d6893a4360ae338b457934473b 100644 --- a/ets2panda/lexer/scripts/keywords.yaml +++ b/ets2panda/lexer/scripts/keywords.yaml @@ -68,9 +68,9 @@ keywords: - name: 'bigint' token: KEYW_BIGINT keyword_like: [ets, ts] - flags: [definable_type_name] + flags: [predefined_type] - - name: 'Bigint' + - name: 'BigInt' token: KEYW_BUILTIN_BIGINT keyword_like: [ets] flags: [predefined_type] @@ -79,7 +79,7 @@ keywords: token: KEYW_BOOLEAN keyword: [ets] keyword_like: [js, ts] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Boolean' token: KEYW_BUILTIN_BOOLEAN @@ -94,7 +94,7 @@ keywords: - name: 'byte' token: KEYW_BYTE keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Byte' token: KEYW_BUILTIN_BYTE @@ -114,7 +114,7 @@ keywords: - name: 'char' token: KEYW_CHAR keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Char' token: KEYW_BUILTIN_CHAR @@ -168,7 +168,7 @@ keywords: - name: 'double' token: KEYW_DOUBLE keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Double' token: KEYW_BUILTIN_DOUBLE @@ -229,7 +229,7 @@ keywords: - name: 'float' token: KEYW_FLOAT keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Float' token: KEYW_BUILTIN_FLOAT @@ -319,7 +319,7 @@ keywords: - name: 'int' token: KEYW_INT keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Int' token: KEYW_BUILTIN_INT @@ -350,7 +350,7 @@ keywords: - name: 'long' token: KEYW_LONG keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Long' token: KEYW_BUILTIN_LONG @@ -474,7 +474,7 @@ keywords: - name: 'short' token: KEYW_SHORT keyword: [ets] - flags: [reserved_type_name, definable_type_name] + flags: [reserved_type_name, predefined_type] - name: 'Short' token: KEYW_BUILTIN_SHORT @@ -488,7 +488,7 @@ keywords: - name: 'string' token: KEYW_STRING keyword_like: [ets, ts] - flags: [definable_type_name] + flags: [predefined_type] - name: 'String' token: KEYW_BUILTIN_STRING @@ -498,7 +498,7 @@ keywords: - name: 'struct' token: KEYW_STRUCT keyword_like: [ets] - flags: [definable_type_name] + flags: [reserved_type_name] - name: 'super' token: KEYW_SUPER @@ -584,7 +584,7 @@ keywords: - name: 'void' token: KEYW_VOID keyword: [as, ets, js, ts] - flags: [unary, reserved_type_name, definable_type_name] + flags: [unary, reserved_type_name, predefined_type] - name: 'while' token: KEYW_WHILE diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index d9970074638f4d87b7610d70336eedddf0b3a7ca..7847a5b8999cd2fe40f0282da0925000f4794c06 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -99,6 +99,11 @@ bool ETSParser::IsETSParser() const noexcept return true; } +bool ETSParser::IsValidIdentifierName(const lexer::Token &token) const noexcept +{ + return !token.IsPredefinedType() || util::Helpers::IsStdLib(GetProgram()); +} + std::unique_ptr ETSParser::InitLexer(const SourceFile &sourceFile) { GetProgram()->SetSource(sourceFile); @@ -1078,7 +1083,7 @@ ir::TypeNode *ETSParser::ParseBaseTypeReference(TypeAnnotationParsingOptions *op ir::TypeNode *ETSParser::ParseLiteralIdent(TypeAnnotationParsingOptions *options) { - if (Lexer()->GetToken().IsDefinableTypeName()) { + if (Lexer()->GetToken().IsPredefinedType()) { return GetTypeAnnotationOfPrimitiveType(Lexer()->GetToken().KeywordType(), options); } diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 4e528d725fbac7789f8d80ac635e6d349ebf2aaa..bd8fcb7b1cf5b5fb2e3d2ae40926037f3d34e8af 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -66,6 +66,9 @@ public: } [[nodiscard]] bool IsETSParser() const noexcept override; + + [[nodiscard]] bool IsValidIdentifierName(const lexer::Token &token) const noexcept override; + void AddDirectImportsToDirectExternalSources(const ArenaVector &directImportsFromMainSource, parser::Program *newProg) const; bool CheckDupAndReplace(Program *&oldProg, Program *newProg) const; diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 409e4ef4f6243cd88770cfcd40abca7e363f13dd..b9be71ce60be6a99817c0cc0315ab8f11f87e1a9 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -1334,8 +1334,7 @@ ir::Identifier *ParserImpl::ExpectIdentifier([[maybe_unused]] bool isReference, } auto const &tokenStart = token.Start(); - if (token.IsPredefinedType() && !util::Helpers::IsStdLib(program_) && - ((options & TypeAnnotationParsingOptions::ADD_TYPE_PARAMETER_BINDING) == 0)) { + if (!IsValidIdentifierName(token) && ((options & TypeAnnotationParsingOptions::ADD_TYPE_PARAMETER_BINDING) == 0)) { LogError(diagnostic::PREDEFINED_TYPE_AS_IDENTIFIER, {token.Ident()}, tokenStart); lexer_->NextToken(); return AllocBrokenExpression(tokenStart); diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index ac2c58b6862cc2a0bc2a7a9404dbf072f85d75a6..83ee66ad7bcee45602b6b38df169937e916fe117 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -84,6 +84,11 @@ public: return false; } + [[nodiscard]] virtual bool IsValidIdentifierName([[maybe_unused]] const lexer::Token &token) const noexcept + { + return true; + } + ETSParser *AsETSParser() { ES2PANDA_ASSERT(IsETSParser()); diff --git a/ets2panda/test/ast/parser/ets/InvalidParserImpl.ets b/ets2panda/test/ast/parser/ets/InvalidParserImpl.ets index feca6d13fffc6c08f84198aede31717936a7c022..899de53972fe0f73745efbef8c0b7f314b523183 100644 --- a/ets2panda/test/ast/parser/ets/InvalidParserImpl.ets +++ b/ets2panda/test/ast/parser/ets/InvalidParserImpl.ets @@ -25,6 +25,4 @@ class int {} /* @@? 17:12 Error TypeError: Getter must return a value */ /* @@? 18:5 Error SyntaxError: Getter must not have formal parameters. */ /* @@? 19:1 Error SyntaxError: Setter must have exactly one formal parameter. */ -/* @@? 23:7 Error SyntaxError: Cannot be used as user-defined type. */ -/* @@? 23:7 Error SyntaxError: Hard keyword 'int' cannot be used as identifier */ -/* @@? 23:7 Error SyntaxError: Identifier expected, got 'int'. */ +/* @@? 23:7 Error SyntaxError: int is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/test/ast/parser/ets/ambient_indexer_3.ets b/ets2panda/test/ast/parser/ets/ambient_indexer_3.ets index efd4f356c643e8cf69cad05f4cdb27638295dc55..4f06b3693e3fbd2a9b19aac40a6f7a13b882b374 100644 --- a/ets2panda/test/ast/parser/ets/ambient_indexer_3.ets +++ b/ets2panda/test/ast/parser/ets/ambient_indexer_3.ets @@ -29,4 +29,4 @@ function main() { /* @@? 17:20 Error SyntaxError: Return type of index signature from exported class or interface need to be identifier. */ /* @@? 17:20 Error SyntaxError: Unexpected token ']'. */ /* @@? 17:22 Error SyntaxError: Unexpected token ':'. */ -/* @@? 17:30 Error SyntaxError: Field type annotation expected. */ +/* @@? 17:24 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/test/ast/parser/ets/circular_type_in_alias.ets b/ets2panda/test/ast/parser/ets/circular_type_in_alias.ets index b951ba4ff5a95559f1109f4171310c46b0719362..09a0912afdb078b51efeb8e558a248d3b9c6d2fc 100644 --- a/ets2panda/test/ast/parser/ets/circular_type_in_alias.ets +++ b/ets2panda/test/ast/parser/ets/circular_type_in_alias.ets @@ -29,8 +29,8 @@ type Loop> = { /* @@? 17:23 Error TypeError: Indexed signatures are not allowed. Use arrays instead! */ /* @@? 17:24 Error SyntaxError: Unexpected token ']'. */ /* @@? 17:26 Error SyntaxError: Unexpected token 'extends'. */ -/* @@? 17:34 Error SyntaxError: Unexpected token 'boolean'. */ +/* @@? 17:34 Error SyntaxError: boolean is a predefined type, cannot be used as an identifier */ /* @@? 17:42 Error SyntaxError: Unexpected token '?'. */ /* @@? 17:44 Error SyntaxError: number is a predefined type, cannot be used as an identifier */ /* @@? 17:51 Error SyntaxError: Unexpected token ':'. */ -/* @@? 17:59 Error SyntaxError: Field type annotation expected. */ \ No newline at end of file +/* @@? 17:53 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/test/ast/parser/ets/typenode_clone_comprehensive.ets b/ets2panda/test/ast/parser/ets/typenode_clone_comprehensive.ets index a741c2a82c83d4a6d0084dcdb401c2acf676927f..069c51fb2f2b65c77959c4bb1af9b5001bce86c7 100644 --- a/ets2panda/test/ast/parser/ets/typenode_clone_comprehensive.ets +++ b/ets2panda/test/ast/parser/ets/typenode_clone_comprehensive.ets @@ -94,7 +94,7 @@ declare const test3: ComplexType<'b'>; /* @@? 41:43 Error SyntaxError: Unexpected token ']'. */ /* @@? 41:43 Error SyntaxError: Field type annotation expected. */ /* @@? 41:44 Error SyntaxError: Unexpected token ':'. */ -/* @@? 41:52 Error SyntaxError: Field type annotation expected. */ +/* @@? 41:46 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ /* @@? 44:41 Error TypeError: The `keyof` keyword can only be used for class or interface type. */ /* @@? 44:60 Error SyntaxError: Unexpected token, expected ']'. */ /* @@? 54:34 Error TypeError: Cannot find type 'InvalidMap'. */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_20.ets b/ets2panda/test/ast/parser/ets/user_defined_20.ets index 2826718e9bcbdc71d589e1ce4e54c2731efef3c4..11c00c085ab04a1aaa9a32c9f8aa67f3a774f110 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_20.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_20.ets @@ -17,6 +17,5 @@ struct bigint{ a : string = "15"; } -/* @@? 16:8 Error SyntaxError: Cannot be used as user-defined type. */ +/* @@? 16:8 Error SyntaxError: bigint is a predefined type, cannot be used as an identifier */ /* @@? 16:1 Error TypeError: Structs are only used to define UI components, it should be translated at 'plugin after parser' phase. */ -/* @@? 1:3 Error TypeError: Variable 'bigint' is already defined with different type. */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_21.ets b/ets2panda/test/ast/parser/ets/user_defined_21.ets index 6ed80976387fa0f0f4fe3a265d75d1bcc98ea334..a7715989bf1d418e8d4c0263dca804341ef44563 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_21.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_21.ets @@ -17,5 +17,4 @@ class /* @@ label */bigint{ a : string = "15"; } -/* @@@ label Error SyntaxError: Cannot be used as user-defined type. */ -/* @@? 1:3 Error TypeError: Variable 'bigint' is already defined with different type. */ +/* @@@ label Error SyntaxError: bigint is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_5.ets b/ets2panda/test/ast/parser/ets/user_defined_5.ets index 54076098618908af969d791ce536c4b67d49c011..a20b8680d4ed2d7c4dac1f52b113a95206279b00 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_5.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_5.ets @@ -17,6 +17,4 @@ enum /* @@ label */double { A, B, C } -/* @@@ label Error SyntaxError: Cannot be used as user-defined type. */ -/* @@@ label Error SyntaxError: Identifier expected, got 'double'. */ -/* @@@ label Error SyntaxError: Hard keyword 'double' cannot be used as identifier */ +/* @@@ label Error SyntaxError: double is a predefined type, cannot be used as an identifier */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_7.ets b/ets2panda/test/ast/parser/ets/user_defined_7.ets index 587457df03a00716aa658dc94de604c15c31a1a7..5b9519508f6746008b811c5d0d2d3eefcc2cb2d7 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_7.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_7.ets @@ -17,9 +17,7 @@ interface /* @@ label1 */double { name : string /* @@ label2 */= /* @@ label3 */"" /* @@ label4 */} -/* @@@ label1 Error SyntaxError: Cannot be used as user-defined type. */ -/* @@@ label1 Error SyntaxError: Identifier expected, got 'double'. */ -/* @@@ label1 Error SyntaxError: Hard keyword 'double' cannot be used as identifier */ +/* @@@ label1 Error SyntaxError: double is a predefined type, cannot be used as an identifier */ /* @@@ label2 Error SyntaxError: Interface member initialization is prohibited. */ /* @@@ label3 Error SyntaxError: Unexpected token, expected ','. */ /* @@? 17:51 Error SyntaxError: Unexpected token, expected 'private' or identifier. */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_8.ets b/ets2panda/test/ast/parser/ets/user_defined_8.ets index 95556fc08294ee82fbe20c09051086b5d7fad317..52278742dd3f8ed2e018321cb303a067e5bcf713 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_8.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_8.ets @@ -17,7 +17,5 @@ struct string{ a : string = "15"; } -/* @@? 16:8 Error SyntaxError: Cannot be used as user-defined type. */ +/* @@? 16:8 Error SyntaxError: string is a predefined type, cannot be used as an identifier */ /* @@? 16:1 Error TypeError: Structs are only used to define UI components, it should be translated at 'plugin after parser' phase. */ -/* @@? 1:3 Error TypeError: Variable 'string' is already defined with different type. */ -/* @@? 17:16 Error TypeError: Type '"15"' cannot be assigned to type 'string' */ diff --git a/ets2panda/test/ast/parser/ets/user_defined_9.ets b/ets2panda/test/ast/parser/ets/user_defined_9.ets index fecbee124f116921c19898efd6baa1ed741fd7bf..1170c80f98c44b47ee10471aed582e5cd53019a1 100644 --- a/ets2panda/test/ast/parser/ets/user_defined_9.ets +++ b/ets2panda/test/ast/parser/ets/user_defined_9.ets @@ -17,6 +17,4 @@ class /* @@ label */string{ a : string = "15"; } -/* @@@ label Error SyntaxError: Cannot be used as user-defined type. */ -/* @@? 1:3 Error TypeError: Variable 'string' is already defined with different type. */ -/* @@? 17:16 Error TypeError: Type '"15"' cannot be assigned to type 'string' */ +/* @@@ label Error SyntaxError: string is a predefined type, cannot be used as an identifier */