From 458180da5e61887cd9f820e573f307d0a640128d Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sat, 11 May 2024 14:37:50 -0400 Subject: Fix bugs in #37 Fix error handling dropping errors Fix error handling segfaults Improve error messages --- src/openvic-dataloader/csv/Parser.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/openvic-dataloader/csv') diff --git a/src/openvic-dataloader/csv/Parser.cpp b/src/openvic-dataloader/csv/Parser.cpp index 849ea05..361f6ad 100644 --- a/src/openvic-dataloader/csv/Parser.cpp +++ b/src/openvic-dataloader/csv/Parser.cpp @@ -142,8 +142,8 @@ constexpr Parser& Parser::load_from_string(const std::string } template -constexpr Parser& Parser::load_from_file(const char* path) { - _file_path = path; +Parser& Parser::load_from_file(const char* path) { + set_file_path(path); // Type can be deduced?? _run_load_func(std::mem_fn(&ParseHandler::load_file), path); return *this; @@ -224,15 +224,21 @@ void Parser::print_errors_to(std::basic_ostream& stream) const { dryad::visit_tree( error, [&](const error::BufferError* buffer_error) { - stream << buffer_error->message() << '\n'; + stream << "buffer error: " << buffer_error->message() << '\n'; }, [&](const error::ParseError* parse_error) { - stream << parse_error->message() << '\n'; + auto position = get_error_position(parse_error); + std::string pos_str = fmt::format(":{}:{}: ", position.start_line, position.start_column); + stream << _file_path << pos_str << "parse error for '" << parse_error->production_name() << "': " << parse_error->message() << '\n'; }, [&](dryad::child_visitor visitor, const error::Semantic* semantic) { - stream << semantic->message() << '\n'; + auto position = get_error_position(semantic); + std::string pos_str = ": "; + if (!position.is_empty()) { + pos_str = fmt::format(":{}:{}: ", position.start_line, position.start_column); + } + stream << _file_path << pos_str << semantic->message() << '\n'; auto annotations = semantic->annotations(); - if (annotations.empty()) return; for (auto annotation : annotations) { visitor(annotation); } -- cgit v1.2.3-56-ga3b1