aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/detail/ParseHandler.hpp
blob: 28222ae0d656353585034ced8fe7756b02e86cdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma once

#include <cstddef>
#include <optional>
#include <string>
#include <utility>

#include <openvic-dataloader/detail/Concepts.hpp>

#include <lexy/encoding.hpp>
#include <lexy/input/buffer.hpp>
#include <lexy/input/file.hpp>

#include "openvic-dataloader/detail/Encoding.hpp"
#include "openvic-dataloader/detail/Utility.hpp"

#include "detail/BufferError.hpp"
#include "detail/Detect.hpp"
#include "detail/InternalConcepts.hpp"

namespace ovdl::detail {
   struct ParseHandler {
      std::string make_error_from(buffer_error error) const {
         switch (error) {
            using enum ovdl::detail::buffer_error;
            case buffer_is_null:
               return "Buffer could not be loaded.";
            case os_error:
               return "OS file error for '{}'.";
            case file_not_found:
               return "File '{}' not found.";
            case permission_denied:
               return "Read Permission for '{}' denied.";
            default:
               return "";
         }
      }

      constexpr bool is_valid() const {
         return is_valid_impl();
      }

      buffer_error load_buffer_size(const char* data, std::size_t size, std::optional<Encoding> fallback) {
         lexy::buffer<lexy::default_encoding> buffer(data, size);
         if (buffer.data() == nullptr) return buffer_error::buffer_is_null;
         return load_buffer_impl(std::move(buffer), "", fallback);
      }

      buffer_error load_buffer(const char* start, const char* end, std::optional<Encoding> fallback) {
         lexy::buffer<lexy::default_encoding> buffer(start, end);
         if (buffer.data() == nullptr) return buffer_error::buffer_is_null;
         return load_buffer_impl(std::move(buffer), "", fallback);
      }

      buffer_error load_file(const char* path, std::optional<Encoding> fallback) {
         lexy::read_file_result file = lexy::read_file<lexy::default_encoding, lexy::encoding_endianness::bom>(path);

         if (!file) {
            return ovdl::detail::from_underlying<buffer_error>(ovdl::detail::to_underlying(file.error()));
         }

         return load_buffer_impl(std::move(file).buffer(), path, fallback);
      }

      const char* path() const {
         return path_impl();
      }

      static Encoding get_system_fallback() {
         return _system_fallback_encoding.value_or(Encoding::Unknown);
      }

      virtual ~ParseHandler() = default;

   protected:
      constexpr virtual bool is_valid_impl() const = 0;
      constexpr virtual buffer_error load_buffer_impl(lexy::buffer<lexy::default_encoding>&& buffer, const char* path = "", std::optional<Encoding> fallback = std::nullopt) = 0;
      virtual const char* path_impl() const = 0;

      template<detail::IsStateType State, detail::IsEncoding BufferEncoding>
      static constexpr auto generate_state = [](State* state, const char* path, auto&& buffer, Encoding encoding) {
         if (path[0] != '\0') {
            *state = {
               path,
               lexy::buffer<BufferEncoding>(std::move(buffer)),
               encoding
            };
            return;
         }
         *state = { lexy::buffer<BufferEncoding>(std::move(buffer)), encoding };
      };

      template<detail::IsStateType State>
      static void create_state(State* state, const char* path, lexy::buffer<lexy::default_encoding>&& buffer, std::optional<Encoding> fallback) {
         if (!_system_fallback_encoding.has_value()) {
            _detect_system_fallback_encoding();
         }
         bool is_bad_fallback = false;
         if (fallback.has_value()) {
            is_bad_fallback = fallback.value() == Encoding::Ascii || fallback.value() == Encoding::Utf8;
            if (is_bad_fallback)
               fallback = _system_fallback_encoding.value();
         } else {
            fallback = _system_fallback_encoding.value();
         }
         auto [encoding, is_alone] = encoding_detect::Detector { .default_fallback = fallback.value() }.detect_assess(buffer);
         switch (encoding) {
            using enum Encoding;
            case Ascii:
            case Utf8: {
               generate_state<State, lexy::utf8_char_encoding>(state, path, std::move(buffer), encoding);
               break;
            }
            case Unknown:
            case Windows1251:
            case Windows1252: {
               generate_state<State, lexy::default_encoding>(state, path, std::move(buffer), encoding);
               break;
            }
            default:
               ovdl::detail::unreachable();
         }

         if (!is_alone) {
            state->logger().info("encoding type could not be distinguished");
         }

         if (is_bad_fallback) {
            state->logger().warning("fallback encoding cannot be ascii or utf8");
         }

         if (encoding == ovdl::detail::Encoding::Unknown) {
            state->logger().warning("could not detect encoding");
         }
      }

   private:
      inline static std::optional<Encoding> _system_fallback_encoding = std::nullopt;
      static void _detect_system_fallback_encoding();
   };

   template<detail::IsFileParseState ParseState>
   struct BasicFileParseHandler : ParseHandler {
      using parse_state_type = ParseState;

      virtual constexpr bool is_valid_impl() const {
         return _parse_state.file().is_valid();
      }

      constexpr virtual buffer_error load_buffer_impl(lexy::buffer<lexy::default_encoding>&& buffer, const char* path, std::optional<Encoding> fallback) {
         if (buffer.data() == nullptr) {
            _parse_state = {};
            return buffer_error::buffer_is_null;
         }
         create_state(&_parse_state, path, std::move(buffer), fallback);
         return is_valid_impl() ? buffer_error::success : buffer_error::buffer_is_null;
      }

      virtual const char* path_impl() const {
         return _parse_state.file().path();
      }

      parse_state_type& parse_state() {
         return _parse_state;
      }

      const parse_state_type& parse_state() const {
         return _parse_state;
      }

      template<typename Encoding>
      constexpr const auto& buffer() const {
         return _parse_state.file().template get_buffer_as<Encoding>();
      }

   protected:
      parse_state_type _parse_state;
   };

   template<detail::IsParseState ParseState>
   struct BasicStateParseHandler : ParseHandler {
      using parse_state_type = ParseState;

      virtual constexpr bool is_valid_impl() const {
         return _parse_state.ast().file().is_valid();
      }

      constexpr virtual buffer_error load_buffer_impl(lexy::buffer<lexy::default_encoding>&& buffer, const char* path, std::optional<Encoding> fallback) {
         if (buffer.data() == nullptr) return buffer_error::buffer_is_null;
         create_state(&_parse_state, path, std::move(buffer), fallback);
         return is_valid_impl() ? buffer_error::success : buffer_error::buffer_is_null;
      }

      virtual const char* path_impl() const {
         return _parse_state.ast().file().path();
      }

      parse_state_type& parse_state() {
         return _parse_state;
      }

      const parse_state_type& parse_state() const {
         return _parse_state;
      }

      template<typename Encoding>
      constexpr const auto& buffer() const {
         return _parse_state.ast().file().template get_buffer_as<Encoding>();
      }

   protected:
      parse_state_type _parse_state;
   };
}