From a35add07db622ff991339ac0dda0014252bca6c1 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 22 Dec 2025 23:50:52 +0300 Subject: [PATCH] Refactor error types --- apps/interpreter/source/main.cpp | 14 +++++++++- libs/ast/include/pslang/ast/error.hpp | 39 +++++++++++---------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/apps/interpreter/source/main.cpp b/apps/interpreter/source/main.cpp index b57f41a..845d776 100644 --- a/apps/interpreter/source/main.cpp +++ b/apps/interpreter/source/main.cpp @@ -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) diff --git a/libs/ast/include/pslang/ast/error.hpp b/libs/ast/include/pslang/ast/error.hpp index e264d67..e0caeec 100644 --- a/libs/ast/include/pslang/ast/error.hpp +++ b/libs/ast/include/pslang/ast/error.hpp @@ -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 invalid_ast_error - : std::exception + struct parse_error + : error { - invalid_ast_error(std::string message, location location) - : message_(std::move(message)) - , filename_(location.filename) - , location_(location) - { - location_.filename = filename_; - } + using error::error; + }; - char const * what() const noexcept - { - return message_.c_str(); - } + struct type_error + : error + { + using error::error; + }; - ast::location location() const noexcept - { - return location_; - } - - private: - std::string message_; - std::string filename_; - ast::location location_; + struct invalid_ast_error + : error + { + using error::error; };