Refactor error types

This commit is contained in:
Nikita Lisitsa 2025-12-22 23:50:52 +03:00
parent c3c208b6a3
commit a35add07db
2 changed files with 28 additions and 25 deletions

View file

@ -113,12 +113,24 @@ int main(int argc, char ** argv)
ast::resolve_identifiers(ast);
parsed.push_back(std::move(ast));
}
catch (pslang::parser::parse_error const & error)
catch (pslang::ast::parse_error const & error)
{
std::cerr << "Parse error at " << error.location() << ":\n " << error.what() << std::endl;
print_error_context(argv[arg], error.location());
return EXIT_FAILURE;
}
catch (pslang::ast::type_error const & error)
{
std::cerr << "Type error at " << error.location() << ":\n " << error.what() << std::endl;
print_error_context(argv[arg], error.location());
return EXIT_FAILURE;
}
catch (pslang::ast::invalid_ast_error const & error)
{
std::cerr << "Invalid AST at " << error.location() << ":\n " << error.what() << std::endl;
print_error_context(argv[arg], error.location());
return EXIT_FAILURE;
}
}
if (dump_ast)

View file

@ -8,10 +8,10 @@
namespace pslang::ast
{
struct parse_error
struct error
: std::exception
{
parse_error(std::string message, location location)
error(std::string message, location location)
: message_(std::move(message))
, filename_(location.filename)
, location_(location)
@ -35,31 +35,22 @@ namespace pslang::ast
ast::location location_;
};
struct parse_error
: error
{
using error::error;
};
struct type_error
: error
{
using error::error;
};
struct invalid_ast_error
: std::exception
: error
{
invalid_ast_error(std::string message, location location)
: message_(std::move(message))
, filename_(location.filename)
, location_(location)
{
location_.filename = filename_;
}
char const * what() const noexcept
{
return message_.c_str();
}
ast::location location() const noexcept
{
return location_;
}
private:
std::string message_;
std::string filename_;
ast::location location_;
using error::error;
};