Skip to content

Commit 2bb27e6

Browse files
committed
Remove File's reliance on std::variant for buffer
1 parent 1f30290 commit 2bb27e6

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,12 +39,11 @@ 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:
4645
case Gbk:
47-
return lexy::parse<Node>(buffer<lexy::utf8_char_encoding>(), parse_state(), parse_state().logger().error_callback());
46+
return lexy::parse<Node>(buffer(), parse_state(), parse_state().logger().error_callback());
4847
OVDL_DEFAULT_CASE_UNREACHABLE(Unknown);
4948
}
5049
}();
@@ -211,24 +210,15 @@ const FilePosition Parser::get_error_position(const error::Error* error) const {
211210
return {};
212211
}
213212

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

234224
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
@@ -83,17 +83,17 @@ namespace ovdl::detail {
8383
constexpr virtual buffer_error load_buffer_impl(lexy::buffer<lexy::default_encoding>&& buffer, const char* path = "", std::optional<Encoding> fallback = std::nullopt) = 0;
8484
virtual const char* path_impl() const = 0;
8585

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

9999
template<detail::IsStateType State>
@@ -141,12 +141,9 @@ namespace ovdl::detail {
141141
auto [encoding, is_alone] = encoding_detect::Detector { .default_fallback = fallback.value() }.detect_assess(buffer);
142142
switch (encoding) {
143143
using enum Encoding;
144-
case Ascii: {
145-
generate_state<State, lexy::ascii_encoding>(state, path, std::move(buffer), encoding);
146-
break;
147-
}
144+
case Ascii:
148145
case Utf8: {
149-
generate_state<State, lexy::utf8_char_encoding>(state, path, std::move(buffer), encoding);
146+
generate_state<State>(state, path, std::move(buffer), encoding);
150147
break;
151148
}
152149
case Unknown: {
@@ -211,9 +208,12 @@ namespace ovdl::detail {
211208
return _parse_state;
212209
}
213210

214-
template<typename Encoding>
215-
constexpr const auto& buffer() const {
216-
return _parse_state.file().template get_buffer_as<Encoding>();
211+
constexpr lexy::buffer<lexy::utf8_char_encoding, void>& buffer() {
212+
return _parse_state.file().buffer();
213+
}
214+
215+
constexpr lexy::buffer<lexy::utf8_char_encoding, void> const& buffer() const {
216+
return _parse_state.file().buffer();
217217
}
218218

219219
protected:
@@ -248,9 +248,12 @@ namespace ovdl::detail {
248248
return _parse_state;
249249
}
250250

251-
template<typename Encoding>
252-
constexpr const auto& buffer() const {
253-
return _parse_state.ast().file().template get_buffer_as<Encoding>();
251+
constexpr lexy::buffer<lexy::utf8_char_encoding, void>& buffer() {
252+
return _parse_state.ast().file().buffer();
253+
}
254+
255+
constexpr lexy::buffer<lexy::utf8_char_encoding, void> const& buffer() const {
256+
return _parse_state.ast().file().buffer();
254257
}
255258

256259
protected:

0 commit comments

Comments
 (0)