diff --git a/aot/options.cpp b/aot/options.cpp index fc52aed9d99f8a484503ce2a6203ff28e24b10a5..6b331d041f5fd64d8ca3d285d5348546ccc7140b 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()); diff --git a/parser/ETSparser.cpp b/parser/ETSparser.cpp index 7cd1461c550d34a4fde49613963d4e3ee93c2f2a..367b732ec3ccb3277217122ff51fa82b4b060b69 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 0000000000000000000000000000000000000000..e97108bc34d7578ef30a4d28a10d03600885e863 --- /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 0000000000000000000000000000000000000000..9dd222a122137d5bde5861e8e3c71b62a4a0d8b0 --- /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 0000000000000000000000000000000000000000..fa541eafef2425d853eaefe2e4005af6c7af732f --- /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 0000000000000000000000000000000000000000..0c9c6496d5368dd768dc127505b415697133967c --- /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 5e8032b6fe077aa57fcb70a21290a6f875707481..994be23a5bd7e0f23af249006cc66144ea50677a 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 fc96bb4ef69b15b61e324cf37a3e63cfc14299a5..e4203329bd1ccd31cd3d0e5dadd85903176ed75c 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