diff options
author | Spartan322 <Megacake1234@gmail.com> | 2024-05-11 20:37:50 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2024-05-11 22:27:30 +0200 |
commit | 458180da5e61887cd9f820e573f307d0a640128d (patch) | |
tree | c0b7aa641b72b5f2650691cba22d17de335622f6 /include/openvic-dataloader | |
parent | 725310939d2b324d79ea4193a72000e21dcc1a2a (diff) |
Fix bugs in #37fix/error-handling
Fix error handling dropping errors
Fix error handling segfaults
Improve error messages
Diffstat (limited to 'include/openvic-dataloader')
-rw-r--r-- | include/openvic-dataloader/AbstractSyntaxTree.hpp | 8 | ||||
-rw-r--r-- | include/openvic-dataloader/DiagnosticLogger.hpp | 55 | ||||
-rw-r--r-- | include/openvic-dataloader/Error.hpp | 13 | ||||
-rw-r--r-- | include/openvic-dataloader/Parser.hpp | 2 | ||||
-rw-r--r-- | include/openvic-dataloader/csv/Parser.hpp | 4 | ||||
-rw-r--r-- | include/openvic-dataloader/detail/SymbolIntern.hpp | 14 | ||||
-rw-r--r-- | include/openvic-dataloader/detail/utility/ErrorRange.hpp | 5 | ||||
-rw-r--r-- | include/openvic-dataloader/v2script/Parser.hpp | 6 |
8 files changed, 64 insertions, 43 deletions
diff --git a/include/openvic-dataloader/AbstractSyntaxTree.hpp b/include/openvic-dataloader/AbstractSyntaxTree.hpp index db81eeb..c6453e3 100644 --- a/include/openvic-dataloader/AbstractSyntaxTree.hpp +++ b/include/openvic-dataloader/AbstractSyntaxTree.hpp @@ -7,6 +7,7 @@ #include <openvic-dataloader/File.hpp> #include <openvic-dataloader/NodeLocation.hpp> +#include <openvic-dataloader/detail/SymbolIntern.hpp> #include <openvic-dataloader/detail/utility/Utility.hpp> #include <dryad/node.hpp> @@ -17,12 +18,7 @@ #include <fmt/core.h> namespace ovdl { - struct AbstractSyntaxTree { - struct SymbolId; - using index_type = std::uint32_t; - using symbol_type = dryad::symbol<SymbolId, index_type>; - using symbol_interner_type = dryad::symbol_interner<SymbolId, char, index_type>; - + struct AbstractSyntaxTree : SymbolIntern { symbol_type intern(const char* str, std::size_t length); symbol_type intern(std::string_view str); const char* intern_cstr(const char* str, std::size_t length); 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 <openvic-dataloader/Error.hpp> #include <openvic-dataloader/File.hpp> #include <openvic-dataloader/NodeLocation.hpp> +#include <openvic-dataloader/detail/CallbackOStream.hpp> #include <openvic-dataloader/detail/LexyReportError.hpp> #include <openvic-dataloader/detail/OStreamOutputIterator.hpp> +#include <openvic-dataloader/detail/SymbolIntern.hpp> +#include <openvic-dataloader/detail/utility/ErrorRange.hpp> +#include <openvic-dataloader/detail/utility/Utility.hpp> +#include <lexy/error.hpp> #include <lexy/input/base.hpp> #include <lexy/input/buffer.hpp> #include <lexy/visualize.hpp> @@ -26,18 +31,14 @@ #include <fmt/core.h> -#include "openvic-dataloader/detail/CallbackOStream.hpp" -#include "openvic-dataloader/detail/utility/ErrorRange.hpp" -#include "openvic-dataloader/detail/utility/Utility.hpp" - #include <lexy_ext/report_error.hpp> 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<error::Root>; explicit operator bool() const; bool errored() const; @@ -57,28 +58,36 @@ namespace ovdl { using Reader = lexy::input_reader<Input>; 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<Tag, lexy::expected_literal>) { auto string = lexy::_detail::make_literal_lexeme<typename Reader::encoding>(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<error::ExpectedLiteral>(loc, message, context.production()); + result = _logger.template create<error::ExpectedLiteral>(loc, message, production); } else if constexpr (std::is_same_v<Tag, lexy::expected_keyword>) { auto string = lexy::_detail::make_literal_lexeme<typename Reader::encoding>(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<error::ExpectedKeyword>(loc, message, context.production()); + result = _logger.template create<error::ExpectedKeyword>(loc, message, production); } else if constexpr (std::is_same_v<Tag, lexy::expected_char_class>) { auto message = _logger.intern_cstr(fmt::format("expected {}", error.name())); - result = _logger.template create<error::ExpectedCharClass>(error.position(), message, context.production()); + result = _logger.template create<error::ExpectedCharClass>(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<error::GenericParseError>(loc, message, context.production()); + result = _logger.template create<error::GenericParseError>(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<const error::Error, NodeLocation> _map; dryad::tree<error::Root> _tree; - struct SymbolId; - using index_type = std::uint32_t; - using symbol_type = dryad::symbol<SymbolId, index_type>; - using symbol_interner_type = dryad::symbol_interner<SymbolId, char, index_type>; 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<error::PrimaryAnnotation>(loc, message); + annotation = _logger.create<error::PrimaryAnnotation>(loc, message); break; case AnnotationKind::secondary: - _logger.create<error::SecondaryAnnotation>(loc, message); + annotation = _logger.create<error::SecondaryAnnotation>(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 diff --git a/include/openvic-dataloader/Error.hpp b/include/openvic-dataloader/Error.hpp index fd3254d..726079c 100644 --- a/include/openvic-dataloader/Error.hpp +++ b/include/openvic-dataloader/Error.hpp @@ -59,7 +59,7 @@ namespace ovdl::error { } struct Error : dryad::abstract_node_all<ErrorKind> { - const char* message() const { return _message; } + std::string_view message() const { return _message; } protected: DRYAD_ABSTRACT_NODE_CTOR(Error); @@ -79,7 +79,7 @@ namespace ovdl::error { struct Root : dryad::basic_node<ErrorKind::Root, dryad::container_node<Error>> { explicit Root(dryad::node_ctor ctor) : node_base(ctor) {} - DRYAD_CHILD_NODE_RANGE_GETTER(Error, errors, nullptr, this); + DRYAD_CHILD_NODE_RANGE_GETTER(Error, errors, nullptr, this->node_after(_last)); void insert_back(Error* error) { insert_child_after(_last, error); @@ -99,7 +99,7 @@ namespace ovdl::error { }; struct ParseError : dryad::abstract_node_range<Error, ErrorKind::FirstParseError, ErrorKind::LastParseError> { - const char* production_name() const { return _production_name; } + std::string_view production_name() const { return _production_name; } protected: explicit ParseError(dryad::node_ctor ctor, @@ -126,7 +126,7 @@ namespace ovdl::error { using GenericParseError = _ParseError_t<ErrorKind::GenericParseError>; struct Semantic : dryad::abstract_node_range<dryad::container_node<Error>, ErrorKind::FirstSemantic, ErrorKind::LastSemantic> { - DRYAD_CHILD_NODE_RANGE_GETTER(Annotation, annotations, nullptr, this); + DRYAD_CHILD_NODE_RANGE_GETTER(Annotation, annotations, nullptr, this->node_after(_last_annotation)); void push_back(Annotation* annotation); void push_back(AnnotationList p_annotations); @@ -146,6 +146,9 @@ namespace ovdl::error { insert_child_list_after(nullptr, annotations); _set_message(message); }; + + private: + Error* _last_annotation; }; template<ErrorKind NodeKind> @@ -187,9 +190,11 @@ namespace ovdl::error { inline void Semantic::push_back(Annotation* annotation) { insert_child_after(annotations().end().deref(), annotation); + _last_annotation = annotation; } inline void Semantic::push_back(AnnotationList p_annotations) { insert_child_list_after(annotations().end().deref(), p_annotations); + _last_annotation = *p_annotations.end(); } }
\ No newline at end of file diff --git a/include/openvic-dataloader/Parser.hpp b/include/openvic-dataloader/Parser.hpp index c1f266b..b885f3d 100644 --- a/include/openvic-dataloader/Parser.hpp +++ b/include/openvic-dataloader/Parser.hpp @@ -22,6 +22,8 @@ namespace ovdl::detail { std::string_view get_file_path() const; protected: + void set_file_path(std::string_view path); + std::reference_wrapper<std::ostream> _error_stream; std::string _file_path; bool _has_fatal_error = false; diff --git a/include/openvic-dataloader/csv/Parser.hpp b/include/openvic-dataloader/csv/Parser.hpp index ccf732a..06e7251 100644 --- a/include/openvic-dataloader/csv/Parser.hpp +++ b/include/openvic-dataloader/csv/Parser.hpp @@ -31,7 +31,7 @@ namespace ovdl::csv { constexpr Parser& load_from_buffer(const char* data, std::size_t size); constexpr Parser& load_from_buffer(const char* start, const char* end); constexpr Parser& load_from_string(const std::string_view string); - constexpr Parser& load_from_file(const char* path); + Parser& load_from_file(const char* path); Parser& load_from_file(const std::filesystem::path& path); constexpr Parser& load_from_file(const detail::HasCstr auto& path) { @@ -42,7 +42,7 @@ namespace ovdl::csv { const std::vector<csv::LineObject>& get_lines() const; - using error_range = ovdl::detail::error_range; + using error_range = ovdl::detail::error_range<error::Root>; Parser::error_range get_errors() const; const FilePosition get_error_position(const error::Error* error) const; diff --git a/include/openvic-dataloader/detail/SymbolIntern.hpp b/include/openvic-dataloader/detail/SymbolIntern.hpp new file mode 100644 index 0000000..8755887 --- /dev/null +++ b/include/openvic-dataloader/detail/SymbolIntern.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include <cstdint> + +#include <dryad/symbol.hpp> + +namespace ovdl { + struct SymbolIntern { + struct SymbolId; + using index_type = std::uint32_t; + using symbol_type = dryad::symbol<SymbolId, index_type>; + using symbol_interner_type = dryad::symbol_interner<SymbolId, char, index_type>; + }; +}
\ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/ErrorRange.hpp b/include/openvic-dataloader/detail/utility/ErrorRange.hpp index a427f6c..7d5ca13 100644 --- a/include/openvic-dataloader/detail/utility/ErrorRange.hpp +++ b/include/openvic-dataloader/detail/utility/ErrorRange.hpp @@ -2,10 +2,9 @@ #include <utility> -#include <openvic-dataloader/Error.hpp> - #include <dryad/node.hpp> namespace ovdl::detail { - using error_range = decltype(std::declval<const error::Root*>()->errors()); + template<typename ErrorRoot> + using error_range = decltype(std::declval<const ErrorRoot*>()->errors()); }
\ No newline at end of file diff --git a/include/openvic-dataloader/v2script/Parser.hpp b/include/openvic-dataloader/v2script/Parser.hpp index cef1faf..ea42aa2 100644 --- a/include/openvic-dataloader/v2script/Parser.hpp +++ b/include/openvic-dataloader/v2script/Parser.hpp @@ -33,7 +33,7 @@ namespace ovdl::v2script { constexpr Parser& load_from_buffer(const char* data, std::size_t size); constexpr Parser& load_from_buffer(const char* start, const char* end); constexpr Parser& load_from_string(const std::string_view string); - constexpr Parser& load_from_file(const char* path); + Parser& load_from_file(const char* path); Parser& load_from_file(const std::filesystem::path& path); constexpr Parser& load_from_file(const detail::HasCstr auto& path) { @@ -47,14 +47,14 @@ namespace ovdl::v2script { const FileTree* get_file_node() const; - std::string_view value(const ovdl::v2script::ast::FlatValue& node) const; + std::string_view value(const ovdl::v2script::ast::FlatValue* node) const; std::string make_native_string() const; std::string make_list_string() const; const FilePosition get_position(const ast::Node* node) const; - using error_range = ovdl::detail::error_range; + using error_range = ovdl::detail::error_range<error::Root>; Parser::error_range get_errors() const; const FilePosition get_error_position(const error::Error* error) const; |