From 757114a3c5b748567b42f273c7b78ca039ae983c Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Tue, 28 Nov 2023 05:09:26 -0500 Subject: Add `deps/dryad` -> https://github.com/Spartan322/dryad Add `deps/fmt` -> https://github.com/fmtlib/fmt Add `deps/range-v3` -> https://github.com/ericniebler/range-v3 Improve parser error and warning support Update .clang-format Update `deps/SCsub` --- include/openvic-dataloader/detail/BasicParser.hpp | 37 ------- .../openvic-dataloader/detail/CallbackOStream.hpp | 31 +++--- include/openvic-dataloader/detail/ClassExport.hpp | 9 -- include/openvic-dataloader/detail/Concepts.hpp | 21 ---- .../detail/LexyFwdDeclaration.hpp | 8 ++ .../openvic-dataloader/detail/LexyReportError.hpp | 107 +++++++++++++++++++++ .../detail/OStreamOutputIterator.hpp | 22 +++++ .../detail/OptionalConstexpr.hpp | 9 -- include/openvic-dataloader/detail/PointerHash.hpp | 23 ----- include/openvic-dataloader/detail/SelfType.hpp | 28 ------ include/openvic-dataloader/detail/TypeName.hpp | 52 ---------- .../openvic-dataloader/detail/VectorConstexpr.hpp | 9 -- .../openvic-dataloader/detail/utility/Concepts.hpp | 45 +++++++++ .../detail/utility/Constexpr.hpp | 15 +++ .../detail/utility/ErrorRange.hpp | 11 +++ .../detail/utility/PointerHash.hpp | 23 +++++ .../openvic-dataloader/detail/utility/SelfType.hpp | 28 ++++++ .../openvic-dataloader/detail/utility/TypeName.hpp | 52 ++++++++++ .../openvic-dataloader/detail/utility/Utility.hpp | 38 ++++++++ 19 files changed, 368 insertions(+), 200 deletions(-) delete mode 100644 include/openvic-dataloader/detail/BasicParser.hpp delete mode 100644 include/openvic-dataloader/detail/ClassExport.hpp delete mode 100644 include/openvic-dataloader/detail/Concepts.hpp create mode 100644 include/openvic-dataloader/detail/LexyFwdDeclaration.hpp create mode 100644 include/openvic-dataloader/detail/LexyReportError.hpp create mode 100644 include/openvic-dataloader/detail/OStreamOutputIterator.hpp delete mode 100644 include/openvic-dataloader/detail/OptionalConstexpr.hpp delete mode 100644 include/openvic-dataloader/detail/PointerHash.hpp delete mode 100644 include/openvic-dataloader/detail/SelfType.hpp delete mode 100644 include/openvic-dataloader/detail/TypeName.hpp delete mode 100644 include/openvic-dataloader/detail/VectorConstexpr.hpp create mode 100644 include/openvic-dataloader/detail/utility/Concepts.hpp create mode 100644 include/openvic-dataloader/detail/utility/Constexpr.hpp create mode 100644 include/openvic-dataloader/detail/utility/ErrorRange.hpp create mode 100644 include/openvic-dataloader/detail/utility/PointerHash.hpp create mode 100644 include/openvic-dataloader/detail/utility/SelfType.hpp create mode 100644 include/openvic-dataloader/detail/utility/TypeName.hpp create mode 100644 include/openvic-dataloader/detail/utility/Utility.hpp (limited to 'include/openvic-dataloader/detail') diff --git a/include/openvic-dataloader/detail/BasicParser.hpp b/include/openvic-dataloader/detail/BasicParser.hpp deleted file mode 100644 index 7524bb5..0000000 --- a/include/openvic-dataloader/detail/BasicParser.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include - -#include -#include -#include - -namespace ovdl::detail { - class BasicParser { - public: - BasicParser(); - - void set_error_log_to_null(); - void set_error_log_to_stderr(); - void set_error_log_to_stdout(); - void set_error_log_to(std::basic_ostream& stream); - - bool has_error() const; - bool has_fatal_error() const; - bool has_warning() const; - - const std::vector& get_errors() const; - const std::vector& get_warnings() const; - std::string_view get_file_path() const; - - protected: - std::vector _errors; - std::vector _warnings; - - std::reference_wrapper _error_stream; - std::string _file_path; - bool _has_fatal_error = false; - }; -} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/CallbackOStream.hpp b/include/openvic-dataloader/detail/CallbackOStream.hpp index 641d53f..f7cfc4a 100644 --- a/include/openvic-dataloader/detail/CallbackOStream.hpp +++ b/include/openvic-dataloader/detail/CallbackOStream.hpp @@ -6,10 +6,10 @@ #include namespace ovdl::detail { - template> - class BasicCallbackStreamBuffer : public std::basic_streambuf { + template> + class BasicCallbackStreamBuffer : public std::basic_streambuf { public: - using base_type = std::basic_streambuf; + using base_type = std::basic_streambuf; using callback_type = Callback; using char_type = typename base_type::char_type; using int_type = typename base_type::int_type; @@ -29,11 +29,12 @@ namespace ovdl::detail { }; int_type overflow(int_type ch) override { + auto c = static_cast(ch); if constexpr (std::is_same_v) { - _callback(&ch, 1, _user_data); + _callback(&c, 1, _user_data); return 1; } else { - return _callback(&ch, 1, _user_data); // returns the number of characters successfully written. + return _callback(&c, 1, _user_data); // returns the number of characters successfully written. } } @@ -64,22 +65,28 @@ namespace ovdl::detail { CallbackWStreamBuffer(Callback cb, void* user_data = nullptr) : base_type(cb, user_data) {} }; - template> - class BasicCallbackStream : public std::basic_ostream { + template> + class BasicCallbackStream : public std::basic_ostream { public: - using base_type = std::basic_ostream; + using base_type = std::basic_ostream; BasicCallbackStream(Callback cb, void* user_data = nullptr) : m_sbuf(cb, user_data), - std::basic_ios(&m_sbuf), - std::basic_ostream(&m_sbuf) { - std::basic_ios::init(&m_sbuf); + std::basic_ios(&m_sbuf), + std::basic_ostream(&m_sbuf) { + std::basic_ios::init(&m_sbuf); } private: - BasicCallbackStreamBuffer m_sbuf; + BasicCallbackStreamBuffer m_sbuf; }; + template + auto make_callback_stream(auto&& cb, void* user_data = nullptr) { + using Callback = std::decay_t; + return BasicCallbackStream { std::forward(cb), user_data }; + } + template class CallbackStream : public BasicCallbackStream { public: diff --git a/include/openvic-dataloader/detail/ClassExport.hpp b/include/openvic-dataloader/detail/ClassExport.hpp deleted file mode 100644 index 27098ed..0000000 --- a/include/openvic-dataloader/detail/ClassExport.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#ifdef _MSC_VER -#define OVDL_EXPORT __declspec(dllexport) -#elif defined(__GNUC__) -#define OVDL_EXPORT __attribute__((visibility("default"))) -#else -#define OVDL_EXPORT -#endif \ No newline at end of file diff --git a/include/openvic-dataloader/detail/Concepts.hpp b/include/openvic-dataloader/detail/Concepts.hpp deleted file mode 100644 index 3ca210c..0000000 --- a/include/openvic-dataloader/detail/Concepts.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -namespace ovdl::detail { - template - concept LoadCallback = - requires(T t, Self* self, Args... args) { - { t(self, std::forward(args)...) } -> std::same_as>; - }; - - template - concept Has_c_str = - requires(T t) { - { t.c_str() } -> std::same_as; - }; -} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/LexyFwdDeclaration.hpp b/include/openvic-dataloader/detail/LexyFwdDeclaration.hpp new file mode 100644 index 0000000..554c88d --- /dev/null +++ b/include/openvic-dataloader/detail/LexyFwdDeclaration.hpp @@ -0,0 +1,8 @@ +#pragma once + +namespace lexy { + struct default_encoding; + + template + struct buffer; +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/LexyReportError.hpp b/include/openvic-dataloader/detail/LexyReportError.hpp new file mode 100644 index 0000000..3c32bd1 --- /dev/null +++ b/include/openvic-dataloader/detail/LexyReportError.hpp @@ -0,0 +1,107 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "openvic-dataloader/detail/utility/Concepts.hpp" + +#include + +namespace ovdl::detail { + template + struct _ReportError { + OutputIterator _iter; + lexy::visualization_options _opts; + const char* _path; + + struct _sink { + OutputIterator _iter; + lexy::visualization_options _opts; + const char* _path; + std::size_t _count; + std::vector _errors; + + using return_type = std::vector; + + template + void operator()(const lexy::error_context& context, const lexy::error& error) { + _iter = lexy_ext::_detail::write_error(_iter, context, error, _opts, _path); + ++_count; + + // Convert the context location and error location into line/column information. + auto context_location = lexy::get_input_location(context.input(), context.position()); + auto location = lexy::get_input_location(context.input(), error.position(), context_location.anchor()); + + std::basic_stringstream message; + + // Write the main annotation. + if constexpr (std::is_same_v) { + auto string = lexy::_detail::make_literal_lexeme(error.string(), error.length()); + + message << "expected '" << string.data() << '\''; + } else if constexpr (std::is_same_v) { + auto string = lexy::_detail::make_literal_lexeme(error.string(), error.length()); + + message << "expected keyword '" << string.data() << '\''; + } else if constexpr (std::is_same_v) { + message << "expected " << error.name(); + } else { + message << error.message(); + } + + _errors.push_back( + ParseError { + ParseError::Type::Fatal, // TODO: distinguish recoverable errors from fatal errors + std::move(message.str()), + 0, // TODO: implement proper error codes + ParseData { + context.production(), + context_location.line_nr(), + context_location.column_nr(), + }, + location.line_nr(), + location.column_nr(), + }); + } + + return_type finish() && { + if (_count != 0) + *_iter++ = '\n'; + return _errors; + } + }; + constexpr auto sink() const { + return _sink { _iter, _opts, _path, 0 }; + } + + /// Specifies a path that will be printed alongside the diagnostic. + constexpr _ReportError path(const char* path) const { + return { _iter, _opts, path }; + } + + constexpr _ReportError path(const detail::HasCstr auto& path_object) const { + return path(path_object.c_str()); + } + + /// Specifies an output iterator where the errors are written to. + template + constexpr _ReportError to(OI out) const { + return { out, _opts, _path }; + } + + /// Overrides visualization options. + constexpr _ReportError opts(lexy::visualization_options opts) const { + return { _iter, opts, _path }; + } + }; + + constexpr auto ReporError = _ReportError {}; +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/OStreamOutputIterator.hpp b/include/openvic-dataloader/detail/OStreamOutputIterator.hpp new file mode 100644 index 0000000..8f120c7 --- /dev/null +++ b/include/openvic-dataloader/detail/OStreamOutputIterator.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +namespace ovdl::detail { + struct OStreamOutputIterator { + std::reference_wrapper _stream; + + auto operator*() const noexcept { + return *this; + } + auto operator++(int) const noexcept { + return *this; + } + + OStreamOutputIterator& operator=(char c) { + _stream.get().put(c); + return *this; + } + }; +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/OptionalConstexpr.hpp b/include/openvic-dataloader/detail/OptionalConstexpr.hpp deleted file mode 100644 index bcb12a7..0000000 --- a/include/openvic-dataloader/detail/OptionalConstexpr.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -// THANK YOU APPLE FOR YOUR UTTER DISREGARD FOR C++20 - -#if __cpp_lib_optional >= 202106L -#define OVDL_OPTIONAL_CONSTEXPR constexpr -#else -#define OVDL_OPTIONAL_CONSTEXPR inline -#endif \ No newline at end of file diff --git a/include/openvic-dataloader/detail/PointerHash.hpp b/include/openvic-dataloader/detail/PointerHash.hpp deleted file mode 100644 index c0d28bc..0000000 --- a/include/openvic-dataloader/detail/PointerHash.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include - -namespace ovdl::detail { - /* hash any pointer */ - template - struct PointerHash { - using type = T; - using ptr_type = T*; - using const_type = const T; - using const_ptr_type = const T*; - using const_ptr_const_type = const const_ptr_type; - constexpr std::size_t operator()(const_ptr_const_type pointer) const { - auto addr = reinterpret_cast(pointer); -#if SIZE_MAX < UINTPTR_MAX - /* size_t is not large enough to hold the pointer’s memory address */ - addr %= SIZE_MAX; /* truncate the address so it is small enough to fit in a size_t */ -#endif - return addr; - } - }; -} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/SelfType.hpp b/include/openvic-dataloader/detail/SelfType.hpp deleted file mode 100644 index 5209700..0000000 --- a/include/openvic-dataloader/detail/SelfType.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include - -namespace ovdl::detail { -#if !defined(_MSC_VER) -#pragma GCC diagnostic push -#pragma clang diagnostic ignored "-Wunknown-warning-option" -#pragma GCC diagnostic ignored "-Wnon-template-friend" -#endif - template - struct Reader { - friend auto adl_GetSelfType(Reader); - }; - - template - struct Writer { - friend auto adl_GetSelfType(Reader) { return U {}; } - }; -#if !defined(_MSC_VER) -#pragma GCC diagnostic pop -#endif - - inline void adl_GetSelfType() {} - - template - using Read = std::remove_pointer_t {}))>; -} diff --git a/include/openvic-dataloader/detail/TypeName.hpp b/include/openvic-dataloader/detail/TypeName.hpp deleted file mode 100644 index 1a34a0f..0000000 --- a/include/openvic-dataloader/detail/TypeName.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace ovdl::detail { - - template - constexpr auto substring_as_array(std::string_view str, std::index_sequence) { - return std::array { str[Idxs]... }; - } - - template - constexpr auto type_name_array() { -#if defined(__clang__) - constexpr auto prefix = std::string_view { "[T = " }; - constexpr auto suffix = std::string_view { "]" }; - constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; -#elif defined(__GNUC__) - constexpr auto prefix = std::string_view { "with T = " }; - constexpr auto suffix = std::string_view { "]" }; - constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; -#elif defined(_MSC_VER) - constexpr auto prefix = std::string_view { "type_name_array<" }; - constexpr auto suffix = std::string_view { ">(void)" }; - constexpr auto function = std::string_view { __FUNCSIG__ }; -#else -#error Unsupported compiler -#endif - - constexpr auto start = function.find(prefix) + prefix.size(); - constexpr auto end = function.rfind(suffix); - - static_assert(start < end); - - constexpr auto name = function.substr(start, (end - start)); - return substring_as_array(name, std::make_index_sequence {}); - } - - template - struct type_name_holder { - static inline constexpr auto value = type_name_array(); - }; - - template - constexpr auto type_name() -> std::string_view { - constexpr auto& value = type_name_holder::value; - return std::string_view { value.data(), value.size() }; - } -} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/VectorConstexpr.hpp b/include/openvic-dataloader/detail/VectorConstexpr.hpp deleted file mode 100644 index 7e7fa34..0000000 --- a/include/openvic-dataloader/detail/VectorConstexpr.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -// THANK YOU APPLE FOR YOUR UTTER DISREGARD FOR C++20 - -#if __cpp_lib_constexpr_vector >= 201907L -#define OVDL_VECTOR_CONSTEXPR constexpr -#else -#define OVDL_VECTOR_CONSTEXPR inline -#endif \ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/Concepts.hpp b/include/openvic-dataloader/detail/utility/Concepts.hpp new file mode 100644 index 0000000..0ba91cc --- /dev/null +++ b/include/openvic-dataloader/detail/utility/Concepts.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include + +namespace ovdl { + struct NodeLocation; + struct File; + namespace detail { + enum class buffer_error : std::uint8_t; + } +} + +namespace ovdl::detail { + template + concept any_of = (std::same_as || ...); + + template + concept HasCstr = + requires(T t) { + { t.c_str() } -> std::same_as; + }; + + template + concept HasPath = requires(T& t) { + { t.path() } -> std::same_as; + }; + + template + concept LoadCallback = + requires(T&& t, Self&& self, Args&&... args) { + { std::invoke(std::forward(t), std::forward(self), std::forward(args)...) } -> std::same_as; + }; + + template + concept IsEncoding = requires(T t) { + typename T::char_type; + typename T::int_type; + { T::template is_secondary_char_type() } -> std::same_as; + { T::eof() } -> std::same_as; + { T::to_int_type(typename T::char_type {}) } -> std::same_as; + }; +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/Constexpr.hpp b/include/openvic-dataloader/detail/utility/Constexpr.hpp new file mode 100644 index 0000000..49479c5 --- /dev/null +++ b/include/openvic-dataloader/detail/utility/Constexpr.hpp @@ -0,0 +1,15 @@ +#pragma once + +// THANK YOU APPLE FOR YOUR UTTER DISREGARD FOR C++20 + +#if __cpp_lib_optional >= 202106L +#define OVDL_OPTIONAL_CONSTEXPR constexpr +#else +#define OVDL_OPTIONAL_CONSTEXPR inline +#endif + +#if __cpp_lib_constexpr_vector >= 201907L +#define OVDL_VECTOR_CONSTEXPR constexpr +#else +#define OVDL_VECTOR_CONSTEXPR inline +#endif \ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/ErrorRange.hpp b/include/openvic-dataloader/detail/utility/ErrorRange.hpp new file mode 100644 index 0000000..a427f6c --- /dev/null +++ b/include/openvic-dataloader/detail/utility/ErrorRange.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +#include + +#include + +namespace ovdl::detail { + using error_range = decltype(std::declval()->errors()); +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/PointerHash.hpp b/include/openvic-dataloader/detail/utility/PointerHash.hpp new file mode 100644 index 0000000..c0d28bc --- /dev/null +++ b/include/openvic-dataloader/detail/utility/PointerHash.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace ovdl::detail { + /* hash any pointer */ + template + struct PointerHash { + using type = T; + using ptr_type = T*; + using const_type = const T; + using const_ptr_type = const T*; + using const_ptr_const_type = const const_ptr_type; + constexpr std::size_t operator()(const_ptr_const_type pointer) const { + auto addr = reinterpret_cast(pointer); +#if SIZE_MAX < UINTPTR_MAX + /* size_t is not large enough to hold the pointer’s memory address */ + addr %= SIZE_MAX; /* truncate the address so it is small enough to fit in a size_t */ +#endif + return addr; + } + }; +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/SelfType.hpp b/include/openvic-dataloader/detail/utility/SelfType.hpp new file mode 100644 index 0000000..5209700 --- /dev/null +++ b/include/openvic-dataloader/detail/utility/SelfType.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +namespace ovdl::detail { +#if !defined(_MSC_VER) +#pragma GCC diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wnon-template-friend" +#endif + template + struct Reader { + friend auto adl_GetSelfType(Reader); + }; + + template + struct Writer { + friend auto adl_GetSelfType(Reader) { return U {}; } + }; +#if !defined(_MSC_VER) +#pragma GCC diagnostic pop +#endif + + inline void adl_GetSelfType() {} + + template + using Read = std::remove_pointer_t {}))>; +} diff --git a/include/openvic-dataloader/detail/utility/TypeName.hpp b/include/openvic-dataloader/detail/utility/TypeName.hpp new file mode 100644 index 0000000..1a34a0f --- /dev/null +++ b/include/openvic-dataloader/detail/utility/TypeName.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include +#include + +namespace ovdl::detail { + + template + constexpr auto substring_as_array(std::string_view str, std::index_sequence) { + return std::array { str[Idxs]... }; + } + + template + constexpr auto type_name_array() { +#if defined(__clang__) + constexpr auto prefix = std::string_view { "[T = " }; + constexpr auto suffix = std::string_view { "]" }; + constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; +#elif defined(__GNUC__) + constexpr auto prefix = std::string_view { "with T = " }; + constexpr auto suffix = std::string_view { "]" }; + constexpr auto function = std::string_view { __PRETTY_FUNCTION__ }; +#elif defined(_MSC_VER) + constexpr auto prefix = std::string_view { "type_name_array<" }; + constexpr auto suffix = std::string_view { ">(void)" }; + constexpr auto function = std::string_view { __FUNCSIG__ }; +#else +#error Unsupported compiler +#endif + + constexpr auto start = function.find(prefix) + prefix.size(); + constexpr auto end = function.rfind(suffix); + + static_assert(start < end); + + constexpr auto name = function.substr(start, (end - start)); + return substring_as_array(name, std::make_index_sequence {}); + } + + template + struct type_name_holder { + static inline constexpr auto value = type_name_array(); + }; + + template + constexpr auto type_name() -> std::string_view { + constexpr auto& value = type_name_holder::value; + return std::string_view { value.data(), value.size() }; + } +} \ No newline at end of file diff --git a/include/openvic-dataloader/detail/utility/Utility.hpp b/include/openvic-dataloader/detail/utility/Utility.hpp new file mode 100644 index 0000000..138a029 --- /dev/null +++ b/include/openvic-dataloader/detail/utility/Utility.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include "openvic-dataloader/detail/utility/TypeName.hpp" + +namespace ovdl::detail { + [[noreturn]] inline void unreachable() { + // Uses compiler specific extensions if possible. + // Even if no extension is used, undefined behavior is still raised by + // an empty function body and the noreturn attribute. +#ifdef __GNUC__ // GCC, Clang, ICC + __builtin_unreachable(); +#elif defined(_MSC_VER) // MSVC + __assume(false); +#endif + } + + template + constexpr std::string_view get_kind_name() { + constexpr auto name = type_name(); + + return name; + } + + template + requires std::is_enum_v + constexpr std::underlying_type_t to_underlying(EnumT e) { + return static_cast>(e); + } + + template + requires std::is_enum_v + constexpr EnumT from_underlying(std::underlying_type_t ut) { + return static_cast(ut); + } +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1