Skip to content

Commit 85c7859

Browse files
committed
Remove File's reliance on std::variant for buffer
1 parent 6bedcf4 commit 85c7859

File tree

9 files changed

+79
-179
lines changed

9 files changed

+79
-179
lines changed

src/openvic-dataloader/AbstractSyntaxTree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace ovdl {
6060
BasicAbstractSyntaxTree() = default;
6161

6262
explicit BasicAbstractSyntaxTree(file_type&& file)
63-
: AbstractSyntaxTree(file.size() * file.visit_buffer([](auto&& buffer) -> size_t { return sizeof(typename std::decay_t<decltype(buffer)>::char_type); })),
63+
: AbstractSyntaxTree(file.size() * sizeof(typename std::decay_t<decltype(file.buffer())>::char_type)),
6464
_file { std::move(file) } {}
6565

6666
template<typename Encoding, typename MemoryResource = void>

src/openvic-dataloader/DiagnosticLogger.hpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -335,23 +335,22 @@ namespace ovdl {
335335
[[nodiscard]] Writer& annotation(AnnotationKind kind, BasicNodeLocation<LocCharT> loc, format_str<Args...> fmt, Args&&... args) {
336336
std::basic_string<typename decltype(fmt.get())::value_type> output;
337337

338-
_file.visit_buffer([&](auto&& buffer) {
339-
using char_type = typename std::decay_t<decltype(buffer)>::encoding::char_type;
338+
lexy::buffer<lexy::utf8_char_encoding, void> const& buffer = _file.buffer();
339+
using char_type = typename std::decay_t<decltype(buffer)>::encoding::char_type;
340340

341-
BasicNodeLocation<char_type> converted_loc = loc;
341+
BasicNodeLocation<char_type> converted_loc = loc;
342342

343-
auto begin_loc = lexy::get_input_location(buffer, converted_loc.begin());
343+
auto begin_loc = lexy::get_input_location(buffer, converted_loc.begin());
344344

345-
auto stream = _logger.make_callback_stream(output);
346-
auto iter = _logger.make_ostream_iterator(stream);
345+
auto stream = _logger.make_callback_stream(output);
346+
auto iter = _logger.make_ostream_iterator(stream);
347347

348-
lexy_ext::diagnostic_writer _impl { buffer, { lexy::visualize_fancy } };
349-
_impl.write_empty_annotation(iter);
350-
_impl.write_annotation(iter, kind, begin_loc, converted_loc.end(),
351-
[&](auto out, lexy::visualization_options) {
352-
return lexy::_detail::write_str(out, fmt::format(fmt, std::forward<Args>(args)...).c_str());
353-
});
354-
});
348+
lexy_ext::diagnostic_writer _impl { buffer, { lexy::visualize_fancy } };
349+
_impl.write_empty_annotation(iter);
350+
_impl.write_annotation(iter, kind, begin_loc, converted_loc.end(),
351+
[&](auto out, lexy::visualization_options) {
352+
return lexy::_detail::write_str(out, fmt::format(fmt, std::forward<Args>(args)...).c_str());
353+
});
355354

356355
error::Annotation* annotation;
357356
auto message = _logger.intern(output);
@@ -443,11 +442,9 @@ namespace ovdl {
443442

444443
template<std::derived_from<error::Error> T, typename... Args>
445444
void log_with_error(T* error, DiagnosticKind kind, format_str<Args...> fmt, Args&&... args) {
446-
file().visit_buffer(
447-
[&](auto&& buffer) {
448-
lexy_ext::diagnostic_writer impl { buffer };
449-
log_with_impl(impl, error, kind, fmt, std::forward<Args>(args)...);
450-
});
445+
lexy::buffer<lexy::utf8_char_encoding, void> const& buffer = file().buffer();
446+
lexy_ext::diagnostic_writer impl { buffer };
447+
log_with_impl(impl, error, kind, fmt, std::forward<Args>(args)...);
451448
}
452449

453450
template<std::derived_from<error::Error> T, typename... Args>
@@ -483,10 +480,9 @@ namespace ovdl {
483480

484481
Writer result(*this, file(), semantic);
485482

486-
file().visit_buffer([&](auto&& buffer) {
487-
lexy_ext::diagnostic_writer impl { buffer };
488-
log_with_impl(impl, semantic, kind, fmt, std::forward<Args>(args)...);
489-
});
483+
lexy::buffer<lexy::utf8_char_encoding, void> const& buffer = file().buffer();
484+
lexy_ext::diagnostic_writer impl { buffer };
485+
log_with_impl(impl, semantic, kind, fmt, std::forward<Args>(args)...);
490486

491487
if (kind == DiagnosticKind::error) {
492488
_errored = true;

src/openvic-dataloader/File.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const char* File::path() const noexcept {
1515
}
1616

1717
bool File::is_valid() const noexcept {
18-
return _buffer.index() != 0 && !_buffer.valueless_by_exception() && visit_buffer([](auto&& buffer) { return buffer.data() != nullptr; });
18+
return _buffer.data() != nullptr;
1919
}
2020

2121
std::size_t File::size() const noexcept {
22-
return _buffer.index() != 0 && !_buffer.valueless_by_exception() ? _buffer_size : 0;
22+
return _buffer.size();
2323
}

src/openvic-dataloader/File.hpp

Lines changed: 8 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <cassert>
44
#include <concepts> // IWYU pragma: keep
55
#include <type_traits>
6-
#include <variant>
76

87
#include <openvic-dataloader/NodeLocation.hpp>
98
#include <openvic-dataloader/detail/Utility.hpp>
109

10+
#include <lexy/_detail/config.hpp>
1111
#include <lexy/encoding.hpp>
1212
#include <lexy/input/buffer.hpp>
1313

@@ -33,87 +33,17 @@ namespace ovdl {
3333

3434
std::size_t size() const noexcept;
3535

36-
template<typename Encoding, typename MemoryResource = void>
37-
constexpr bool is_buffer() const {
38-
return buffer_ids::type_id<lexy::buffer<Encoding, MemoryResource>>() + 1 == _buffer.index();
36+
lexy::buffer<lexy::utf8_char_encoding, void>& buffer() {
37+
return _buffer;
3938
}
4039

41-
template<typename Encoding, typename MemoryResource = void>
42-
lexy::buffer<Encoding, MemoryResource>* try_get_buffer_as() {
43-
return std::get_if<lexy::buffer<Encoding, MemoryResource>>(&_buffer);
40+
lexy::buffer<lexy::utf8_char_encoding, void> const& buffer() const {
41+
return _buffer;
4442
}
4543

46-
template<typename Encoding, typename MemoryResource = void>
47-
const lexy::buffer<Encoding, MemoryResource>* try_get_buffer_as() const {
48-
return std::get_if<lexy::buffer<Encoding, MemoryResource>>(&_buffer);
49-
}
50-
51-
template<typename Encoding, typename MemoryResource = void>
52-
lexy::buffer<Encoding, MemoryResource>& get_buffer_as() {
53-
assert((is_buffer<Encoding, MemoryResource>()));
54-
return *std::get_if<lexy::buffer<Encoding, MemoryResource>>(&_buffer);
55-
}
56-
57-
template<typename Encoding, typename MemoryResource = void>
58-
const lexy::buffer<Encoding, MemoryResource>& get_buffer_as() const {
59-
assert((is_buffer<Encoding, MemoryResource>()));
60-
return *std::get_if<lexy::buffer<Encoding, MemoryResource>>(&_buffer);
61-
}
62-
63-
#define SWITCH_LIST \
64-
X(1) \
65-
X(2) \
66-
X(3) \
67-
X(4) \
68-
X(5) \
69-
X(6)
70-
71-
#define X(NUM) \
72-
case NUM: \
73-
return visitor(std::get<NUM>(_buffer));
74-
75-
template<typename Visitor>
76-
decltype(auto) visit_buffer(Visitor&& visitor) {
77-
switch (_buffer.index()) {
78-
SWITCH_LIST
79-
case 0: return visitor(lexy::buffer<> {});
80-
default: ovdl::detail::unreachable();
81-
}
82-
}
83-
84-
template<typename Return, typename Visitor>
85-
Return visit_buffer(Visitor&& visitor) {
86-
switch (_buffer.index()) {
87-
SWITCH_LIST
88-
case 0: return visitor(lexy::buffer<> {});
89-
default: ovdl::detail::unreachable();
90-
}
91-
}
92-
93-
template<typename Visitor>
94-
decltype(auto) visit_buffer(Visitor&& visitor) const {
95-
switch (_buffer.index()) {
96-
SWITCH_LIST
97-
case 0: return visitor(lexy::buffer<> {});
98-
default: ovdl::detail::unreachable();
99-
}
100-
}
101-
102-
template<typename Return, typename Visitor>
103-
Return visit_buffer(Visitor&& visitor) const {
104-
switch (_buffer.index()) {
105-
SWITCH_LIST
106-
case 0: return visitor(lexy::buffer<> {});
107-
default: ovdl::detail::unreachable();
108-
}
109-
}
110-
#undef X
111-
#undef SWITCH_LIST
112-
11344
protected:
11445
const char* _path = "";
115-
std::size_t _buffer_size = 0;
116-
detail::type_prepend_t<buffer_ids::variant_type, std::monostate> _buffer;
46+
lexy::buffer<lexy::utf8_char_encoding, void> _buffer;
11747
};
11848

11949
template<typename NodeT>
@@ -122,17 +52,13 @@ namespace ovdl {
12252

12353
BasicFile() = default;
12454

125-
template<typename Encoding, typename MemoryResource = void>
126-
explicit BasicFile(const char* path, lexy::buffer<Encoding, MemoryResource>&& buffer)
55+
explicit BasicFile(const char* path, lexy::buffer<lexy::utf8_char_encoding, void>&& buffer)
12756
: File(path) {
128-
_buffer_size = buffer.size();
12957
_buffer = static_cast<std::remove_reference_t<decltype(buffer)>&&>(buffer);
13058
}
13159

132-
template<typename Encoding, typename MemoryResource = void>
133-
explicit BasicFile(lexy::buffer<Encoding, MemoryResource>&& buffer)
60+
explicit BasicFile(lexy::buffer<lexy::utf8_char_encoding, void>&& buffer)
13461
: File("") {
135-
_buffer_size = buffer.size();
13662
_buffer = static_cast<std::remove_reference_t<decltype(buffer)>&&>(buffer);
13763
}
13864

src/openvic-dataloader/ParseState.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,10 @@ namespace ovdl {
8686
_logger { this->file() },
8787
BasicParseState(encoding) {}
8888

89-
template<typename Encoding, typename MemoryResource = void>
90-
FileParseState(lexy::buffer<Encoding, MemoryResource>&& buffer, detail::Encoding encoding)
89+
FileParseState(lexy::buffer<lexy::utf8_char_encoding, void>&& buffer, detail::Encoding encoding)
9190
: FileParseState(file_type { std::move(buffer) }, encoding) {}
9291

93-
template<typename Encoding, typename MemoryResource = void>
94-
FileParseState(const char* path, lexy::buffer<Encoding, MemoryResource>&& buffer, detail::Encoding encoding)
92+
FileParseState(const char* path, lexy::buffer<lexy::utf8_char_encoding, void>&& buffer, detail::Encoding encoding)
9593
: FileParseState(file_type { path, std::move(buffer) }, encoding) {}
9694

9795
FileParseState(const FileParseState&) = delete;

src/openvic-dataloader/csv/Parser.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ struct Parser::ParseHandler final : detail::BasicFileParseHandler<CsvParseState>
3939
switch (parse_state().encoding()) {
4040
using enum detail::Encoding;
4141
case Ascii:
42-
return lexy::parse<Node>(buffer<lexy::ascii_encoding>(), parse_state(), parse_state().logger().error_callback());
4342
case Utf8:
4443
case Windows1251:
4544
case Windows1252:
46-
return lexy::parse<Node>(buffer<lexy::utf8_char_encoding>(), parse_state(), parse_state().logger().error_callback());
45+
return lexy::parse<Node>(buffer(), parse_state(), parse_state().logger().error_callback());
4746
OVDL_DEFAULT_CASE_UNREACHABLE(Unknown);
4847
}
4948
}();
@@ -210,24 +209,15 @@ const FilePosition Parser::get_error_position(const error::Error* error) const {
210209
return {};
211210
}
212211

213-
// TODO: Remove reinterpret_cast
214-
// WARNING: This almost certainly breaks on utf16 and utf32 encodings, luckily we don't parse in that format
215-
// This is purely to silence the node_location errors because char8_t is useless
216-
#define REINTERPRET_IT(IT) reinterpret_cast<const std::decay_t<decltype(buffer)>::encoding::char_type*>((IT))
217-
218-
return _parse_handler->parse_state().file().visit_buffer(
219-
[&](auto&& buffer) -> FilePosition {
220-
auto loc_begin = lexy::get_input_location(buffer, REINTERPRET_IT(err_location.begin()));
221-
FilePosition result { loc_begin.line_nr(), loc_begin.line_nr(), loc_begin.column_nr(), loc_begin.column_nr() };
222-
if (err_location.begin() < err_location.end()) {
223-
auto loc_end = lexy::get_input_location(buffer, REINTERPRET_IT(err_location.end()), loc_begin.anchor());
224-
result.end_line = loc_end.line_nr();
225-
result.end_column = loc_end.column_nr();
226-
}
227-
return result;
228-
});
229-
230-
#undef REINTERPRET_IT
212+
lexy::buffer<lexy::utf8_char_encoding, void> const& buffer = _parse_handler->buffer();
213+
auto loc_begin = lexy::get_input_location(buffer, err_location.begin());
214+
FilePosition result { loc_begin.line_nr(), loc_begin.line_nr(), loc_begin.column_nr(), loc_begin.column_nr() };
215+
if (err_location.begin() < err_location.end()) {
216+
auto loc_end = lexy::get_input_location(buffer, err_location.end(), loc_begin.anchor());
217+
result.end_line = loc_end.line_nr();
218+
result.end_column = loc_end.column_nr();
219+
}
220+
return result;
231221
}
232222

233223
void Parser::print_errors_to(std::basic_ostream<char>& stream) const {

src/openvic-dataloader/detail/InternalConcepts.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace ovdl::detail {
8989
T t,
9090
const T ct,
9191
typename T::ast_type::file_type&& file,
92-
lexy::buffer<lexy::default_encoding>&& buffer,
92+
lexy::buffer<lexy::utf8_char_encoding>&& buffer,
9393
ovdl::detail::Encoding encoding,
9494
const char* path //
9595
) {
@@ -109,7 +109,7 @@ namespace ovdl::detail {
109109
T t,
110110
const T ct,
111111
typename T::file_type&& file,
112-
lexy::buffer<lexy::default_encoding>&& buffer,
112+
lexy::buffer<lexy::utf8_char_encoding>&& buffer,
113113
ovdl::detail::Encoding encoding,
114114
const char* path //
115115
) {

src/openvic-dataloader/detail/ParseHandler.hpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ namespace ovdl::detail {
8282
constexpr virtual buffer_error load_buffer_impl(lexy::buffer<lexy::default_encoding>&& buffer, const char* path = "", std::optional<Encoding> fallback = std::nullopt) = 0;
8383
virtual const char* path_impl() const = 0;
8484

85-
template<detail::IsStateType State, detail::IsEncoding BufferEncoding>
85+
template<detail::IsStateType State>
8686
static constexpr auto generate_state = [](State* state, const char* path, auto&& buffer, Encoding encoding) {
8787
if (path[0] != '\0') {
8888
*state = {
8989
path,
90-
lexy::buffer<BufferEncoding>(std::move(buffer)),
90+
lexy::buffer<lexy::utf8_char_encoding, void>(std::move(buffer)),
9191
encoding
9292
};
9393
return;
9494
}
95-
*state = { lexy::buffer<BufferEncoding>(std::move(buffer)), encoding };
95+
*state = { lexy::buffer<lexy::utf8_char_encoding, void>(std::move(buffer)), encoding };
9696
};
9797

9898
template<detail::IsStateType State>
@@ -126,12 +126,9 @@ namespace ovdl::detail {
126126
auto [encoding, is_alone] = encoding_detect::Detector { .default_fallback = fallback.value() }.detect_assess(buffer);
127127
switch (encoding) {
128128
using enum Encoding;
129-
case Ascii: {
130-
generate_state<State, lexy::ascii_encoding>(state, path, std::move(buffer), encoding);
131-
break;
132-
}
129+
case Ascii:
133130
case Utf8: {
134-
generate_state<State, lexy::utf8_char_encoding>(state, path, std::move(buffer), encoding);
131+
generate_state<State>(state, path, std::move(buffer), encoding);
135132
break;
136133
}
137134
case Unknown: {
@@ -192,9 +189,12 @@ namespace ovdl::detail {
192189
return _parse_state;
193190
}
194191

195-
template<typename Encoding>
196-
constexpr const auto& buffer() const {
197-
return _parse_state.file().template get_buffer_as<Encoding>();
192+
constexpr lexy::buffer<lexy::utf8_char_encoding, void>& buffer() {
193+
return _parse_state.file().buffer();
194+
}
195+
196+
constexpr lexy::buffer<lexy::utf8_char_encoding, void> const& buffer() const {
197+
return _parse_state.file().buffer();
198198
}
199199

200200
protected:
@@ -229,9 +229,12 @@ namespace ovdl::detail {
229229
return _parse_state;
230230
}
231231

232-
template<typename Encoding>
233-
constexpr const auto& buffer() const {
234-
return _parse_state.ast().file().template get_buffer_as<Encoding>();
232+
constexpr lexy::buffer<lexy::utf8_char_encoding, void>& buffer() {
233+
return _parse_state.ast().file().buffer();
234+
}
235+
236+
constexpr lexy::buffer<lexy::utf8_char_encoding, void> const& buffer() const {
237+
return _parse_state.ast().file().buffer();
235238
}
236239

237240
protected:

0 commit comments

Comments
 (0)