From 688cbf0334db3d2da0dbb0d759ada47d4ec6fc70 Mon Sep 17 00:00:00 2001 From: Sergey Chernykh Date: Wed, 19 Feb 2025 16:19:18 +0800 Subject: [PATCH] Create dummy import source Description: * Create dummy import source with *ERROR_LITERL*, if there is no normal one * Add check to not log *ERROR_LITERAL* in diagnostic_messages Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBNDPZ Signed-off-by: Sergey Chernykh --- ets2panda/parser/ETSparser.cpp | 13 ++++--------- ets2panda/parser/parserImpl.cpp | 4 +++- ets2panda/varbinder/ETSBinder.cpp | 5 +++++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index af5e8fe7d5..0e7f067ca1 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1035,7 +1035,9 @@ ir::ImportSource *ETSParser::ParseSourceFromClause(bool requireFrom) if (Lexer()->GetToken().Type() != lexer::TokenType::LITERAL_STRING) { LogExpectedToken(lexer::TokenType::LITERAL_STRING); - return nullptr; // Error processing. + // Try to create DUMMY import source as error placeholder + auto errorLiteral = AllocNode(ERROR_LITERAL); + return Allocator()->New(errorLiteral, errorLiteral, Language(Language::Id::ETS), false); } ES2PANDA_ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_STRING); @@ -1065,11 +1067,6 @@ ir::Statement *ETSParser::ParseImportDeclarationHelper(lexer::SourcePosition sta ir::ImportKinds importKind) { auto const importSource = ParseSourceFromClause(true); - if (importSource == nullptr) { - // Error is logged inside ParseSourceFromClause - return AllocBrokenStatement(); // Error processing. - } - const auto endLocDef = importSource->Source()->End(); auto *const importDeclaration = AllocNode(importSource, std::move(specifiers), importKind); @@ -1108,9 +1105,7 @@ ArenaVector ETSParser::ParseImportDeclarations() auto pos = Lexer()->Save(); if (!specifiers.empty()) { auto *const importDecl = ParseImportDeclarationHelper(startLoc, specifiers, importKind); - if (!importDecl->IsBrokenStatement()) { - statements.push_back(importDecl->AsETSImportDeclaration()); - } + statements.push_back(importDecl->AsETSImportDeclaration()); } if (!defaultSpecifiers.empty()) { diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 4c4411ab8c..b5755a6692 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -1280,7 +1280,9 @@ void ParserImpl::LogUnexpectedToken(lexer::TokenType tokenType) void ParserImpl::LogUnexpectedToken(lexer::Token const &token) { - LogSyntaxError(std::string(UNEXPECTED_TOKEN).append(token.ToString()).append("'.")); + if (token.ToString() != ERROR_LITERAL) { + LogError(diagnostic::UNEXPECTED_TOKEN_PARAM, {token.ToString()}); + } } void ParserImpl::LogExpectedToken(lexer::TokenType tokenType) diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 3889f09cb8..893a6b075f 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -921,6 +921,11 @@ varbinder::Variable *ETSBinder::FindStaticBinding(const ArenaVector ETSBinder::GetExternalProgram(const util::StringView &sourceName, const ir::StringLiteral *importPath) { + if (sourceName == ERROR_LITERAL) { + // avoid logging rediculus messages, there must be a syntax error + ASSERT(GetContext()->diagnosticEngine->IsAnyError()); + return ArenaVector(Allocator()->Adapter()); + } // NOTE: quick fix to make sure not to look for the global program among the external sources if (sourceName.Compare(globalRecordTable_.Program()->AbsoluteName()) == 0) { ArenaVector mainModule(Allocator()->Adapter()); -- Gitee