Skip to content

Commit 0e51e57

Browse files
committed
Add Error::parseErrorType()
1 parent 810c446 commit 0e51e57

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

liblangutil/Exceptions.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@
2929
using namespace solidity;
3030
using namespace solidity::langutil;
3131

32+
std::map<Error::Type, std::string> const Error::m_errorTypeNames = {
33+
{Error::Type::IOError, "IOError"},
34+
{Error::Type::FatalError, "FatalError"},
35+
{Error::Type::JSONError, "JSONError"},
36+
{Error::Type::InternalCompilerError, "InternalCompilerError"},
37+
{Error::Type::CompilerError, "CompilerError"},
38+
{Error::Type::Exception, "Exception"},
39+
{Error::Type::CodeGenerationError, "CodeGenerationError"},
40+
{Error::Type::DeclarationError, "DeclarationError"},
41+
{Error::Type::DocstringParsingError, "DocstringParsingError"},
42+
{Error::Type::ParserError, "ParserError"},
43+
{Error::Type::SyntaxError, "SyntaxError"},
44+
{Error::Type::TypeError, "TypeError"},
45+
{Error::Type::UnimplementedFeatureError, "UnimplementedFeatureError"},
46+
{Error::Type::YulException, "YulException"},
47+
{Error::Type::SMTLogicException, "SMTLogicException"},
48+
{Error::Type::Warning, "Warning"},
49+
{Error::Type::Info, "Info"},
50+
};
51+
3252
Error::Error(
3353
ErrorId _errorId, Error::Type _type,
3454
std::string const& _description,

liblangutil/Exceptions.h

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
#include <boost/preprocessor/facilities/overload.hpp>
3434
#include <boost/algorithm/string/case_conv.hpp>
3535

36+
#include <optional>
3637
#include <string>
3738
#include <utility>
38-
#include <vector>
39-
#include <memory>
4039
#include <variant>
40+
#include <vector>
4141

4242
namespace solidity::langutil
4343
{
@@ -269,27 +269,17 @@ class Error: virtual public util::Exception
269269

270270
static std::string formatErrorType(Type _type)
271271
{
272-
switch (_type)
273-
{
274-
case Type::IOError: return "IOError";
275-
case Type::FatalError: return "FatalError";
276-
case Type::JSONError: return "JSONError";
277-
case Type::InternalCompilerError: return "InternalCompilerError";
278-
case Type::CompilerError: return "CompilerError";
279-
case Type::Exception: return "Exception";
280-
case Type::CodeGenerationError: return "CodeGenerationError";
281-
case Type::DeclarationError: return "DeclarationError";
282-
case Type::DocstringParsingError: return "DocstringParsingError";
283-
case Type::ParserError: return "ParserError";
284-
case Type::SyntaxError: return "SyntaxError";
285-
case Type::TypeError: return "TypeError";
286-
case Type::UnimplementedFeatureError: return "UnimplementedFeatureError";
287-
case Type::YulException: return "YulException";
288-
case Type::SMTLogicException: return "SMTLogicException";
289-
case Type::Warning: return "Warning";
290-
case Type::Info: return "Info";
291-
}
292-
util::unreachable();
272+
return m_errorTypeNames.at(_type);
273+
}
274+
275+
static std::optional<Type> parseErrorType(std::string _name)
276+
{
277+
static std::map<std::string, Error::Type> const m_errorTypesByName = util::invertMap(m_errorTypeNames);
278+
279+
if (m_errorTypesByName.count(_name) == 0)
280+
return std::nullopt;
281+
282+
return m_errorTypesByName.at(_name);
293283
}
294284

295285
static std::string formatTypeOrSeverity(std::variant<Error::Type, Error::Severity> _typeOrSeverity)
@@ -309,6 +299,8 @@ class Error: virtual public util::Exception
309299
private:
310300
ErrorId m_errorId;
311301
Type m_type;
302+
303+
static std::map<Type, std::string> const m_errorTypeNames;
312304
};
313305

314306
}

0 commit comments

Comments
 (0)