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); ast::resolve_identifiers(ast);
parsed.push_back(std::move(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; std::cerr << "Parse error at " << error.location() << ":\n " << error.what() << std::endl;
print_error_context(argv[arg], error.location()); print_error_context(argv[arg], error.location());
return EXIT_FAILURE; 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) if (dump_ast)

View file

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