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
215
216
217
218
219
220
221
222
223
|
#include "openvic-dataloader/v2script/Parser.hpp"
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "SimpleGrammar.hpp"
#include "detail/DetectUtf8.hpp"
#include "detail/Errors.hpp"
#include "detail/LexyReportError.hpp"
#include "detail/NullBuff.hpp"
#include "detail/Warnings.hpp"
#include <lexy/action/parse.hpp>
#include <lexy/encoding.hpp>
#include <lexy/input/buffer.hpp>
#include <lexy/input/file.hpp>
#include <lexy/lexeme.hpp>
#include <lexy/visualize.hpp>
#include <openvic-dataloader/ParseError.hpp>
#include <openvic-dataloader/ParseWarning.hpp>
#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>
using namespace ovdl::v2script;
/// BufferHandler ///
class Parser::BufferHandler {
public:
bool is_valid() const {
return _buffer.size() != 0;
}
std::optional<ParseError> load_buffer(const char* data, std::size_t size) {
_buffer = lexy::buffer(data, size);
return std::nullopt;
}
std::optional<ParseError> load_buffer(const char* start, const char* end) {
_buffer = lexy::buffer(start, end);
return std::nullopt;
}
std::optional<ParseError> load_file(const char* path) {
auto file = lexy::read_file(path);
if (!file) {
return errors::make_no_file_error(path);
}
_buffer = file.buffer();
return std::nullopt;
}
constexpr bool is_exclusive_utf8() const {
return detail::is_utf8_no_ascii(_buffer);
}
template<typename Node, typename ErrorCallback>
std::optional<std::vector<ParseError>> parse(const ErrorCallback& callback) {
auto result = lexy::parse<Node>(_buffer, callback);
if (!result) {
return result.errors();
}
// This is mighty frustrating
_root = std::unique_ptr<ast::Node>(result.value());
return std::nullopt;
}
std::unique_ptr<ast::Node>& get_root() {
return _root;
}
private:
lexy::buffer<lexy::default_encoding> _buffer;
std::unique_ptr<ast::Node> _root;
};
/// BufferHandler ///
Parser::Parser()
: _buffer_handler(std::make_unique<BufferHandler>()),
_error_stream(detail::cnull) {
set_error_log_to_stderr();
}
Parser::Parser(Parser&&) = default;
Parser& Parser::operator=(Parser&& value) = default;
Parser::~Parser() = default;
Parser Parser::from_buffer(const char* data, std::size_t size) {
Parser result;
return std::move(result.load_from_buffer(data, size));
}
Parser Parser::from_buffer(const char* start, const char* end) {
Parser result;
return std::move(result.load_from_buffer(start, end));
}
Parser Parser::from_file(const char* path) {
Parser result;
return std::move(result.load_from_file(path));
}
///
/// @brief Executes a function on _buffer_handler that is expected to load a buffer
///
/// Expected Use:
/// @code {.cpp}
/// _run_load_func(&BufferHandler::<load_function>, <arguments>);
/// @endcode
///
/// @tparam Args
/// @param func
/// @param args
///
template<typename... Args>
inline void Parser::_run_load_func(std::optional<ParseError> (BufferHandler::*func)(Args...), Args... args) {
_warnings.clear();
_errors.clear();
_has_fatal_error = false;
if (auto error = (_buffer_handler.get()->*func)(args...); error) {
_has_fatal_error = error.value().type == ParseError::Type::Fatal;
_errors.push_back(error.value());
_error_stream.get() << "Error: " << _errors.back().message << '\n';
}
}
Parser& Parser::load_from_buffer(const char* data, std::size_t size) {
_run_load_func(&BufferHandler::load_buffer, data, size);
return *this;
}
Parser& Parser::load_from_buffer(const char* start, const char* end) {
_run_load_func(&BufferHandler::load_buffer, start, end);
return *this;
}
Parser& Parser::load_from_file(const char* path) {
_file_path = path;
_run_load_func(&BufferHandler::load_file, path);
return *this;
}
void Parser::set_error_log_to_null() {
set_error_log_to(detail::cnull);
}
void Parser::set_error_log_to_stderr() {
set_error_log_to(std::cerr);
}
void Parser::set_error_log_to_stdout() {
set_error_log_to(std::cout);
}
void Parser::set_error_log_to(std::basic_ostream<char>& stream) {
_error_stream = stream;
}
bool Parser::simple_parse() {
if (!_buffer_handler->is_valid()) {
return false;
}
struct ostream_output_iterator {
std::reference_wrapper<std::ostream> _stream;
auto operator*() const noexcept {
return *this;
}
auto operator++(int) const noexcept {
return *this;
}
ostream_output_iterator& operator=(char c) {
_stream.get().put(c);
return *this;
}
};
if (_buffer_handler->is_exclusive_utf8()) {
_warnings.push_back(warnings::make_utf8_warning(_file_path));
}
auto errors = _buffer_handler->parse<grammar::File>(ovdl::detail::ReporError.path(_file_path).to(ostream_output_iterator { _error_stream }));
if (errors) {
_errors.reserve(errors->size());
for (auto& err : errors.value()) {
_has_fatal_error |= err.type == ParseError::Type::Fatal;
_errors.push_back(err);
}
return false;
}
_file_node.reset(static_cast<ast::FileNode*>(_buffer_handler->get_root().release()));
return true;
}
bool Parser::has_error() const {
return !_errors.empty();
}
bool Parser::has_fatal_error() const {
return _has_fatal_error;
}
bool Parser::has_warning() const {
return !_warnings.empty();
}
const std::vector<ovdl::ParseError>& Parser::get_errors() const {
return _errors;
}
const std::vector<ovdl::ParseWarning>& Parser::get_warnings() const {
return _warnings;
}
const FileNode* Parser::get_file_node() const {
return _file_node.get();
}
|