aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/ParseState.hpp
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2024-06-18 19:43:20 +0200
committer GitHub <noreply@github.com>2024-06-18 19:43:20 +0200
commit8b623bf4087aa360842ad31145d4ab6946cee9aa (patch)
treef15ebc47d6bf370031af28e4bb4814ae30ef46e1 /src/openvic-dataloader/ParseState.hpp
parent7b521d6023113372cf6b02e562828273c4040f0e (diff)
parentb0c3ba3f91926b0c95625bdbf4aab69269130b13 (diff)
Merge pull request #46 from OpenVicProject/fix/char-detection
Add runtime encoding detection and conversion
Diffstat (limited to 'src/openvic-dataloader/ParseState.hpp')
-rw-r--r--src/openvic-dataloader/ParseState.hpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/openvic-dataloader/ParseState.hpp b/src/openvic-dataloader/ParseState.hpp
new file mode 100644
index 0000000..806829c
--- /dev/null
+++ b/src/openvic-dataloader/ParseState.hpp
@@ -0,0 +1,105 @@
+#pragma once
+
+#include <utility>
+
+#include <openvic-dataloader/detail/Encoding.hpp>
+
+#include <lexy/encoding.hpp>
+#include <lexy/input/buffer.hpp>
+
+#include <dryad/tree.hpp>
+
+#include "DiagnosticLogger.hpp"
+#include "detail/InternalConcepts.hpp"
+
+namespace ovdl {
+ struct BasicParseState {
+ explicit BasicParseState(detail::Encoding encoding = detail::Encoding::Unknown) : _encoding(encoding) {}
+
+ detail::Encoding encoding() const {
+ return _encoding;
+ }
+
+ protected:
+ detail::Encoding _encoding;
+ };
+
+ template<detail::IsAst AstT>
+ struct ParseState : BasicParseState {
+ using ast_type = AstT;
+ using file_type = typename ast_type::file_type;
+ using diagnostic_logger_type = BasicDiagnosticLogger<ParseState>;
+
+ ParseState(typename ast_type::file_type&& file, detail::Encoding encoding)
+ : _ast { std::move(file) },
+ _logger { this->ast().file() },
+ BasicParseState(encoding) {}
+
+ template<typename Encoding, typename MemoryResource = void>
+ ParseState(lexy::buffer<Encoding, MemoryResource>&& buffer, detail::Encoding encoding)
+ : ParseState(typename ast_type::file_type { std::move(buffer) }, encoding) {}
+
+ template<typename Encoding, typename MemoryResource = void>
+ ParseState(const char* path, lexy::buffer<Encoding, MemoryResource>&& buffer, detail::Encoding encoding)
+ : ParseState(typename ast_type::file_type { path, std::move(buffer) }, encoding) {}
+
+ ast_type& ast() {
+ return _ast;
+ }
+
+ const ast_type& ast() const {
+ return _ast;
+ }
+
+ diagnostic_logger_type& logger() {
+ return _logger;
+ }
+
+ const diagnostic_logger_type& logger() const {
+ return _logger;
+ }
+
+ private:
+ ast_type _ast;
+ diagnostic_logger_type _logger;
+ };
+
+ template<detail::IsFile FileT>
+ struct FileParseState : BasicParseState {
+ using file_type = FileT;
+ using diagnostic_logger_type = BasicDiagnosticLogger<FileParseState>;
+
+ FileParseState(file_type&& file, detail::Encoding encoding)
+ : _file { std::move(file) },
+ _logger { this->file() },
+ BasicParseState(encoding) {}
+
+ template<typename Encoding, typename MemoryResource = void>
+ FileParseState(lexy::buffer<Encoding, MemoryResource>&& buffer, detail::Encoding encoding)
+ : FileParseState(file_type { std::move(buffer) }, encoding) {}
+
+ template<typename Encoding, typename MemoryResource = void>
+ FileParseState(const char* path, lexy::buffer<Encoding, MemoryResource>&& buffer, detail::Encoding encoding)
+ : FileParseState(file_type { path, std::move(buffer) }, encoding) {}
+
+ file_type& file() {
+ return _file;
+ }
+
+ const file_type& file() const {
+ return _file;
+ }
+
+ diagnostic_logger_type& logger() {
+ return _logger;
+ }
+
+ const diagnostic_logger_type& logger() const {
+ return _logger;
+ }
+
+ private:
+ file_type _file;
+ diagnostic_logger_type _logger;
+ };
+} \ No newline at end of file