aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/v2script/Parser.cpp
blob: 3386e482bc261fb191a343a42daf6a60ae465d44 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "openvic-dataloader/v2script/Parser.hpp"

#include <functional>
#include <iostream>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include <openvic-dataloader/ParseError.hpp>
#include <openvic-dataloader/ParseWarning.hpp>
#include <openvic-dataloader/detail/Concepts.hpp>
#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>
#include <openvic-dataloader/v2script/NodeLocationMap.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 "detail/BasicBufferHandler.hpp"
#include "detail/DetectUtf8.hpp"
#include "detail/Errors.hpp"
#include "detail/LexyReportError.hpp"
#include "detail/NullBuff.hpp"
#include "detail/OStreamOutputIterator.hpp"
#include "detail/Warnings.hpp"
#include "v2script/DecisionGrammar.hpp"
#include "v2script/EventGrammar.hpp"
#include "v2script/LuaDefinesGrammar.hpp"
#include "v2script/SimpleGrammar.hpp"

using namespace ovdl;
using namespace ovdl::v2script;

///   BufferHandler ///

class Parser::BufferHandler final : public detail::BasicBufferHandler<> {
public:
   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;
   }

   ast::NodeLocationMap<decltype(_buffer)>& get_location_map() {
      return _location_map;
   }

private:
   friend class ::ovdl::v2script::ast::Node;
   std::unique_ptr<ast::Node> _root;
   ast::NodeLocationMap<decltype(_buffer)> _location_map;
};

/// BufferHandler ///

Parser::Parser()
   : _buffer_handler(std::make_unique<BufferHandler>()) {
   set_error_log_to_stderr();
}

Parser::Parser(Parser&&) = default;
Parser& Parser::operator=(Parser&&) = 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_string(const std::string_view string) {
   Parser result;
   return std::move(result.load_from_string(string));
}

Parser Parser::from_file(const char* path) {
   Parser result;
   return std::move(result.load_from_file(path));
}

Parser Parser::from_file(const std::filesystem::path& 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 Type
/// @tparam Args
/// @param func
/// @param args
///
template<typename... Args>
constexpr void Parser::_run_load_func(detail::LoadCallback<BufferHandler, Args...> auto func, Args... args) {
   _warnings.clear();
   _errors.clear();
   _has_fatal_error = false;
   if (auto error = func(_buffer_handler.get(), std::forward<Args>(args)...); error) {
      _has_fatal_error = error.value().type == ParseError::Type::Fatal;
      _errors.push_back(error.value());
      _error_stream.get() << "Error: " << _errors.back().message << '\n';
   }
}

constexpr Parser& Parser::load_from_buffer(const char* data, std::size_t size) {
   // Type can't be deduced?
   _run_load_func(std::mem_fn(&BufferHandler::load_buffer_size), data, size);
   return *this;
}

constexpr Parser& Parser::load_from_buffer(const char* start, const char* end) {
   // Type can't be deduced?
   _run_load_func(std::mem_fn(&BufferHandler::load_buffer), start, end);
   return *this;
}

constexpr Parser& Parser::load_from_string(const std::string_view string) {
   return load_from_buffer(string.data(), string.size());
}

constexpr Parser& Parser::load_from_file(const char* path) {
   _file_path = path;
   // Type can be deduced??
   _run_load_func(std::mem_fn(&BufferHandler::load_file), path);
   return *this;
}

Parser& Parser::load_from_file(const std::filesystem::path& path) {
   return load_from_file(path.string().c_str());
}

constexpr Parser& Parser::load_from_file(const detail::Has_c_str auto& path) {
   return load_from_file(path.c_str());
}

/* REQUIREMENTS:
 * DAT-23
 * DAT-26
 * DAT-28
 * DAT-29
 */
bool Parser::simple_parse() {
   if (!_buffer_handler->is_valid()) {
      return false;
   }

   if (_buffer_handler->is_exclusive_utf8()) {
      _warnings.push_back(warnings::make_utf8_warning(_file_path));
   }

   auto errors = _buffer_handler->parse<grammar::File<grammar::NoStringEscapeOption>>(ovdl::detail::ReporError.path(_file_path).to(detail::OStreamOutputIterator { _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::event_parse() {
   if (!_buffer_handler->is_valid()) {
      return false;
   }

   if (_buffer_handler->is_exclusive_utf8()) {
      _warnings.push_back(warnings::make_utf8_warning(_file_path));
   }

   auto errors = _buffer_handler->parse<grammar::EventFile>(ovdl::detail::ReporError.path(_file_path).to(detail::OStreamOutputIterator { _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::decision_parse() {
   if (!_buffer_handler->is_valid()) {
      return false;
   }

   if (_buffer_handler->is_exclusive_utf8()) {
      _warnings.push_back(warnings::make_utf8_warning(_file_path));
   }

   auto errors = _buffer_handler->parse<grammar::DecisionFile>(ovdl::detail::ReporError.path(_file_path).to(detail::OStreamOutputIterator { _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::lua_defines_parse() {
   if (!_buffer_handler->is_valid()) {
      return false;
   }

   if (_buffer_handler->is_exclusive_utf8()) {
      _warnings.push_back(warnings::make_utf8_warning(_file_path));
   }

   auto errors = _buffer_handler->parse<lua::grammar::File<>>(ovdl::detail::ReporError.path(_file_path).to(detail::OStreamOutputIterator { _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;
}

const FileNode* Parser::get_file_node() const {
   return _file_node.get();
}

void Parser::generate_node_location_map() {
   _buffer_handler->get_location_map().clear();
   _buffer_handler->get_location_map().generate_location_map(_buffer_handler->get_buffer(), get_file_node());
}

const ast::Node::line_col Parser::get_node_begin(const ast::NodeCPtr node) const {
   if (!node) return { 0, 0 };
   return node->get_begin_line_col(*this);
}

const ast::Node::line_col Parser::get_node_end(const ast::NodeCPtr node) const {
   if (!node) return { 0, 0 };
   return node->get_end_line_col(*this);
}

const ast::Node::line_col ast::Node::get_begin_line_col(const Parser& parser) const {
   if (!parser._buffer_handler->is_valid() || parser._buffer_handler->_location_map.empty()) return {};
   line_col result;
   auto [itr, range_end] = parser._buffer_handler->_location_map.equal_range(this);
   if (itr != range_end) {
      result.line = itr->second.line_nr();
      result.column = itr->second.column_nr();
   }
   // Standard doesn't really guarantee the direction of the range's sequence, but only GCC goes backwards
   // TODO: DON'T USE STANDARD UNORDERED_MULTIMAP
#if defined(__GNUC__) && !defined(__clang__)
   itr++;
   if (itr != range_end) {
      result.line = itr->second.line_nr();
      result.column = itr->second.column_nr();
   }
#endif
   return result;
}

const ast::Node::line_col ast::Node::get_end_line_col(const Parser& parser) const {
   if (!parser._buffer_handler->is_valid() || parser._buffer_handler->_location_map.empty()) return {};
   line_col result;
   auto [itr, range_end] = parser._buffer_handler->_location_map.equal_range(this);
   if (itr != range_end) {
      result.line = itr->second.line_nr();
      result.column = itr->second.column_nr();
   }
   // Standard doesn't really guarantee the direction of the range's sequence, but only GCC goes backwards
   // TODO: DON'T USE STANDARD UNORDERED_MULTIMAP
#if defined(__GNUC__) && !defined(__clang__)
   return result;
#endif
   itr++;
   if (itr != range_end) {
      result.line = itr->second.line_nr();
      result.column = itr->second.column_nr();
   }
   return result;
}