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 --- include/openvic-dataloader/DiagnosticLogger.hpp | 55 ++++++++++++++----------- 1 file changed, 30 insertions(+), 25 deletions(-) (limited to 'include/openvic-dataloader/DiagnosticLogger.hpp') diff --git a/include/openvic-dataloader/DiagnosticLogger.hpp b/include/openvic-dataloader/DiagnosticLogger.hpp index 1fa2784..bd8f9cc 100644 --- a/include/openvic-dataloader/DiagnosticLogger.hpp +++ b/include/openvic-dataloader/DiagnosticLogger.hpp @@ -11,9 +11,14 @@ #include #include #include +#include #include #include +#include +#include +#include +#include #include #include #include @@ -26,18 +31,14 @@ #include -#include "openvic-dataloader/detail/CallbackOStream.hpp" -#include "openvic-dataloader/detail/utility/ErrorRange.hpp" -#include "openvic-dataloader/detail/utility/Utility.hpp" - #include namespace ovdl { - struct DiagnosticLogger { + struct DiagnosticLogger : SymbolIntern { using AnnotationKind = lexy_ext::annotation_kind; using DiagnosticKind = lexy_ext::diagnostic_kind; - using error_range = detail::error_range; + using error_range = detail::error_range; explicit operator bool() const; bool errored() const; @@ -57,28 +58,36 @@ namespace ovdl { using Reader = lexy::input_reader; error::Error* result; + std::string production_name = context.production(); + auto left_strip = production_name.find_first_of('<'); + if (left_strip != std::string::npos) { + auto right_strip = production_name.find_first_of('>', left_strip); + if (right_strip != std::string::npos) { + production_name.erase(left_strip, right_strip - left_strip + 1); + } + } + + auto production = _logger.intern_cstr(production_name); if constexpr (std::is_same_v) { auto string = lexy::_detail::make_literal_lexeme(error.string(), error.length()); - NodeLocation loc = NodeLocation::make_from(string.begin(), string.end()); + NodeLocation loc = NodeLocation::make_from(context.position(), error.position() - 1); auto message = _logger.intern_cstr(fmt::format("expected '{}'", string.data())); - result = _logger.template create(loc, message, context.production()); + result = _logger.template create(loc, message, production); } else if constexpr (std::is_same_v) { auto string = lexy::_detail::make_literal_lexeme(error.string(), error.length()); - NodeLocation loc = NodeLocation::make_from(string.begin(), string.end()); + NodeLocation loc = NodeLocation::make_from(context.position(), error.position() - 1); auto message = _logger.intern_cstr(fmt::format("expected keyword '{}'", string.data())); - result = _logger.template create(loc, message, context.production()); + result = _logger.template create(loc, message, production); } else if constexpr (std::is_same_v) { auto message = _logger.intern_cstr(fmt::format("expected {}", error.name())); - result = _logger.template create(error.position(), message, context.production()); + result = _logger.template create(error.position(), message, production); } else { NodeLocation loc = NodeLocation::make_from(error.begin(), error.end()); auto message = _logger.intern_cstr(error.message()); - result = _logger.template create(loc, message, context.production()); + result = _logger.template create(loc, message, production); } - if constexpr (requires { _logger.insert(result); }) { - _logger.insert(result); - } + _logger.insert(result); _count++; } @@ -119,12 +128,12 @@ namespace ovdl { dryad::node_map _map; dryad::tree _tree; - struct SymbolId; - using index_type = std::uint32_t; - using symbol_type = dryad::symbol; - using symbol_interner_type = dryad::symbol_interner; symbol_interner_type _symbol_interner; + void insert(error::Error* root) { + _tree.root()->insert_back(root); + } + public: symbol_type intern(const char* str, std::size_t length) { return _symbol_interner.intern(str, length); @@ -282,10 +291,10 @@ namespace ovdl { auto message = _logger.intern_cstr(output); switch (kind) { case AnnotationKind::primary: - _logger.create(loc, message); + annotation = _logger.create(loc, message); break; case AnnotationKind::secondary: - _logger.create(loc, message); + annotation = _logger.create(loc, message); break; default: detail::unreachable(); } @@ -381,10 +390,6 @@ namespace ovdl { } private: - void insert(error::Error* root) { - _tree.root()->insert_back(root); - } - const file_type* _file; }; } \ No newline at end of file -- cgit v1.2.3-56-ga3b1