From d56834a2c36b7f984616c08679f35e086a4421a7 Mon Sep 17 00:00:00 2001 From: Tamas Toth Date: Wed, 15 Feb 2023 12:25:00 +0100 Subject: [PATCH 1/2] Informative error message add for unsupported expressions in global scope. Signed-off-by: Tamas Toth --- parser/ETSparser.cpp | 28 +++++++++++- .../ets/global_scope_boolean-expected.txt | 1 + test/parser/ets/global_scope_boolean.ets | 17 ++++++++ .../ets/global_scope_string-expected.txt | 1 + test/parser/ets/global_scope_string.ets | 17 ++++++++ util/helpers.cpp | 43 ++++++++++++++++++- util/helpers.h | 2 + 7 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 test/parser/ets/global_scope_boolean-expected.txt create mode 100644 test/parser/ets/global_scope_boolean.ets create mode 100644 test/parser/ets/global_scope_string-expected.txt create mode 100644 test/parser/ets/global_scope_string.ets diff --git a/parser/ETSparser.cpp b/parser/ETSparser.cpp index 7cd1461c5..367b732ec 100644 --- a/parser/ETSparser.cpp +++ b/parser/ETSparser.cpp @@ -1109,7 +1109,8 @@ ir::Statement *ETSParser::ParseTypeDeclaration(bool allowStatic) auto modifiers = ir::ClassDefinitionModifiers::ID_REQUIRED | ir::ClassDefinitionModifiers::CLASS_DECL; - switch (Lexer()->GetToken().Type()) { + auto tokenType = Lexer()->GetToken().Type(); + switch (tokenType) { case lexer::TokenType::KEYW_STATIC: { if (!allowStatic) { ThrowUnexpectedToken(Lexer()->GetToken().Type()); @@ -1157,6 +1158,31 @@ ir::Statement *ETSParser::ParseTypeDeclaration(bool allowStatic) } [[fallthrough]]; } + case lexer::TokenType::LITERAL_NUMBER: + case lexer::TokenType::LITERAL_NULL: + case lexer::TokenType::LITERAL_STRING: + case lexer::TokenType::LITERAL_FALSE: + case lexer::TokenType::LITERAL_TRUE: + case lexer::TokenType::LITERAL_CHAR: { + std::wstring_convert, char16_t> convert; + std::string errMsg("Cannot used in global scope '"); + + std::string text = tokenType == lexer::TokenType::LITERAL_CHAR + ? convert.to_bytes(Lexer()->GetToken().Utf16()) + : Lexer()->GetToken().Ident().Mutf8(); + + if ((Lexer()->GetToken().Flags() & lexer::TokenFlags::HAS_ESCAPE) == 0) { + errMsg.append(text); + } else { + errMsg.append(util::Helpers::RegexpToString(text)); + } + + errMsg.append("'"); + ThrowSyntaxError(errMsg.c_str()); + } + case lexer::TokenType::LITERAL_REGEXP: { + ThrowSyntaxError("literal regexp error"); + } default: { ThrowUnexpectedToken(Lexer()->GetToken().Type()); } diff --git a/test/parser/ets/global_scope_boolean-expected.txt b/test/parser/ets/global_scope_boolean-expected.txt new file mode 100644 index 000000000..e97108bc3 --- /dev/null +++ b/test/parser/ets/global_scope_boolean-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot used in global scope 'true' [global_scope_boolean.ets:16:1] diff --git a/test/parser/ets/global_scope_boolean.ets b/test/parser/ets/global_scope_boolean.ets new file mode 100644 index 000000000..9dd222a12 --- /dev/null +++ b/test/parser/ets/global_scope_boolean.ets @@ -0,0 +1,17 @@ +/* + * 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. + */ + +true + diff --git a/test/parser/ets/global_scope_string-expected.txt b/test/parser/ets/global_scope_string-expected.txt new file mode 100644 index 000000000..fa541eafe --- /dev/null +++ b/test/parser/ets/global_scope_string-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot used in global scope 'a\bcde\fg' [global_scope_string.ets:16:1] diff --git a/test/parser/ets/global_scope_string.ets b/test/parser/ets/global_scope_string.ets new file mode 100644 index 000000000..0c9c6496d --- /dev/null +++ b/test/parser/ets/global_scope_string.ets @@ -0,0 +1,17 @@ +/* + * 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. + */ + +"a\bcde\fg" + diff --git a/util/helpers.cpp b/util/helpers.cpp index 5e8032b6f..994be23a5 100644 --- a/util/helpers.cpp +++ b/util/helpers.cpp @@ -39,6 +39,7 @@ #include "plugins/ecmascript/es2panda/ir/ts/tsInterfaceDeclaration.h" #include "plugins/ecmascript/es2panda/ir/ts/tsEnumDeclaration.h" #include "plugins/ecmascript/es2panda/ir/module/importDeclaration.h" +#include "plugins/ecmascript/es2panda/lexer/token/letters.h" namespace panda::es2panda::util { // Helpers @@ -577,4 +578,44 @@ std::tuple Helpers::ParamName(ArenaAllocator *allocator, return {Helpers::ToStringView(allocator, index), true}; } -} // namespace panda::es2panda::util + +std::string Helpers::RegexpToString(const std::string &str) +{ + std::string ret = ""; + + for (unsigned int i = 0; i != str.length(); i++) { + switch (str.at(i)) { + case lexer::LEX_CHAR_BS: { + ret.append("\\b"); + break; + } + case lexer::LEX_CHAR_TAB: { + ret.append("\\t"); + break; + } + case lexer::LEX_CHAR_LF: { + ret.append("\\n"); + break; + } + case lexer::LEX_CHAR_VT: { + ret.append("\\v"); + break; + } + case lexer::LEX_CHAR_FF: { + ret.append("\\f"); + break; + } + case lexer::LEX_CHAR_CR: { + ret.append("\\r"); + break; + } + default: { + ret += str.at(i); + break; + } + } + } + + return ret; +} +} // namespace panda::es2panda::util \ No newline at end of file diff --git a/util/helpers.h b/util/helpers.h index fc96bb4ef..e4203329b 100644 --- a/util/helpers.h +++ b/util/helpers.h @@ -100,6 +100,8 @@ public: } static const uint32_t INVALID_INDEX = 4294967295L; + + static std::string RegexpToString(const std::string &str); }; template -- Gitee From 623ecbe52407e95b7dff3f9160a41ac5eb93e8f0 Mon Sep 17 00:00:00 2001 From: Tamas Toth Date: Wed, 22 Feb 2023 10:38:46 +0100 Subject: [PATCH 2/2] When use --module in ets add error. Signed-off-by: Tamas Toth --- aot/options.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/aot/options.cpp b/aot/options.cpp index fc52aed9d..6b331d041 100644 --- a/aot/options.cpp +++ b/aot/options.cpp @@ -43,7 +43,7 @@ bool Options::Parse(int argc, const char **argv) // parser panda::PandArg inputExtension("extension", "", "Parse the input as the given extension (options: js | ts | as | ets)"); - panda::PandArg opModule("module", false, "Parse the input as module"); + panda::PandArg opModule("module", false, "Parse the input as module (only on js support)"); panda::PandArg opParseOnly("parse-only", false, "Parse the input only"); panda::PandArg opDumpAst("dump-ast", false, "Dump the parsed AST"); @@ -103,6 +103,11 @@ bool Options::Parse(int argc, const char **argv) return false; } + if (inputExtension.GetValue() == "ets" && opModule.GetValue()) { + errorMsg_ = "Error: --module not supported in ets."; + return false; + } + sourceFile_ = inputFile.GetValue(); std::ifstream inputStream(sourceFile_.c_str()); -- Gitee