From fc37b4eeb63e6f42e6a6a5bfe85878c288fd69c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 29 Sep 2023 22:07:44 +0200 Subject: [PATCH] Add Error::parseErrorType() --- liblangutil/Exceptions.cpp | 20 ++++++++++++++++++++ liblangutil/Exceptions.h | 37 +++++++++++++++---------------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/liblangutil/Exceptions.cpp b/liblangutil/Exceptions.cpp index 118720b74..108acd121 100644 --- a/liblangutil/Exceptions.cpp +++ b/liblangutil/Exceptions.cpp @@ -29,6 +29,26 @@ using namespace solidity; using namespace solidity::langutil; +std::map const Error::m_errorTypeNames = { + {Error::Type::IOError, "IOError"}, + {Error::Type::FatalError, "FatalError"}, + {Error::Type::JSONError, "JSONError"}, + {Error::Type::InternalCompilerError, "InternalCompilerError"}, + {Error::Type::CompilerError, "CompilerError"}, + {Error::Type::Exception, "Exception"}, + {Error::Type::CodeGenerationError, "CodeGenerationError"}, + {Error::Type::DeclarationError, "DeclarationError"}, + {Error::Type::DocstringParsingError, "DocstringParsingError"}, + {Error::Type::ParserError, "ParserError"}, + {Error::Type::SyntaxError, "SyntaxError"}, + {Error::Type::TypeError, "TypeError"}, + {Error::Type::UnimplementedFeatureError, "UnimplementedFeatureError"}, + {Error::Type::YulException, "YulException"}, + {Error::Type::SMTLogicException, "SMTLogicException"}, + {Error::Type::Warning, "Warning"}, + {Error::Type::Info, "Info"}, +}; + Error::Error( ErrorId _errorId, Error::Type _type, std::string const& _description, diff --git a/liblangutil/Exceptions.h b/liblangutil/Exceptions.h index 65362df31..72ad0c12b 100644 --- a/liblangutil/Exceptions.h +++ b/liblangutil/Exceptions.h @@ -33,10 +33,11 @@ #include #include +#include +#include #include #include #include -#include #include namespace solidity::langutil @@ -269,27 +270,17 @@ public: static std::string formatErrorType(Type _type) { - switch (_type) - { - case Type::IOError: return "IOError"; - case Type::FatalError: return "FatalError"; - case Type::JSONError: return "JSONError"; - case Type::InternalCompilerError: return "InternalCompilerError"; - case Type::CompilerError: return "CompilerError"; - case Type::Exception: return "Exception"; - case Type::CodeGenerationError: return "CodeGenerationError"; - case Type::DeclarationError: return "DeclarationError"; - case Type::DocstringParsingError: return "DocstringParsingError"; - case Type::ParserError: return "ParserError"; - case Type::SyntaxError: return "SyntaxError"; - case Type::TypeError: return "TypeError"; - case Type::UnimplementedFeatureError: return "UnimplementedFeatureError"; - case Type::YulException: return "YulException"; - case Type::SMTLogicException: return "SMTLogicException"; - case Type::Warning: return "Warning"; - case Type::Info: return "Info"; - } - util::unreachable(); + return m_errorTypeNames.at(_type); + } + + static std::optional parseErrorType(std::string _name) + { + static std::map const m_errorTypesByName = util::invertMap(m_errorTypeNames); + + if (m_errorTypesByName.count(_name) == 0) + return std::nullopt; + + return m_errorTypesByName.at(_name); } static std::string formatTypeOrSeverity(std::variant _typeOrSeverity) @@ -309,6 +300,8 @@ public: private: ErrorId m_errorId; Type m_type; + + static std::map const m_errorTypeNames; }; }