diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index a78f3e1ad6e113215d15f1cfc2ece7331dcafdc5..22c37aac453a2d9712e53b5afa9e9fa77efcdd67 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -4657,16 +4657,24 @@ void ETSParser::CheckDeclare() Lexer()->NextToken(); // eat 'declare' switch (Lexer()->GetToken().KeywordType()) { - case lexer::TokenType::KEYW_LET: + case lexer::TokenType::KEYW_ABSTRACT: + case lexer::TokenType::KEYW_CLASS: case lexer::TokenType::KEYW_CONST: + case lexer::TokenType::KEYW_ENUM: case lexer::TokenType::KEYW_FUNCTION: - case lexer::TokenType::KEYW_CLASS: + case lexer::TokenType::KEYW_INTERFACE: + case lexer::TokenType::KEYW_LET: case lexer::TokenType::KEYW_NAMESPACE: - case lexer::TokenType::KEYW_ENUM: - case lexer::TokenType::KEYW_ABSTRACT: - case lexer::TokenType::KEYW_INTERFACE: { + case lexer::TokenType::KEYW_TYPE: { return; } + case lexer::TokenType::LITERAL_IDENT: { + if (Lexer()->GetToken().Ident().Is("namespace")) { + ThrowSyntaxError("Ambient namespace declarations are not yet implemented"); + } + + [[fallthrough]]; + } default: { ThrowSyntaxError("Unexpected token."); } diff --git a/ets2panda/test/compiler/ets/declare_type_alias-expected.txt b/ets2panda/test/compiler/ets/declare_type_alias-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..53484c2cca3c000420c0ea8ff9ba142fcf028f74 --- /dev/null +++ b/ets2panda/test/compiler/ets/declare_type_alias-expected.txt @@ -0,0 +1,250 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "KPointer", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "typeAnnotation": { + "type": "ETSUnionType", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 16, + "column": 34 + }, + "end": { + "line": 16, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 38 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 17, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/declare_type_alias.ets b/ets2panda/test/compiler/ets/declare_type_alias.ets new file mode 100644 index 0000000000000000000000000000000000000000..64ac7d066d5662e61983c27420d4d5d9fcde502a --- /dev/null +++ b/ets2panda/test/compiler/ets/declare_type_alias.ets @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 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. + */ + +declare type KPointer = number | int;