aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/v2script
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2024-05-11 20:37:50 +0200
committer Spartan322 <Megacake1234@gmail.com>2024-05-11 22:27:30 +0200
commit458180da5e61887cd9f820e573f307d0a640128d (patch)
treec0b7aa641b72b5f2650691cba22d17de335622f6 /src/openvic-dataloader/v2script
parent725310939d2b324d79ea4193a72000e21dcc1a2a (diff)
Fix bugs in #37fix/error-handling
Fix error handling dropping errors Fix error handling segfaults Improve error messages
Diffstat (limited to 'src/openvic-dataloader/v2script')
-rw-r--r--src/openvic-dataloader/v2script/DecisionGrammar.hpp2
-rw-r--r--src/openvic-dataloader/v2script/ModifierGrammar.hpp4
-rw-r--r--src/openvic-dataloader/v2script/Parser.cpp23
-rw-r--r--src/openvic-dataloader/v2script/SimpleGrammar.hpp12
4 files changed, 28 insertions, 13 deletions
diff --git a/src/openvic-dataloader/v2script/DecisionGrammar.hpp b/src/openvic-dataloader/v2script/DecisionGrammar.hpp
index 05cb8cc..b5010ff 100644
--- a/src/openvic-dataloader/v2script/DecisionGrammar.hpp
+++ b/src/openvic-dataloader/v2script/DecisionGrammar.hpp
@@ -40,7 +40,7 @@ namespace ovdl::v2script::grammar {
struct DecisionList {
static constexpr auto rule =
- ovdl::dsl::keyword<"political_decisions">(lexy::dsl::inline_<Identifier<StringEscapeOption>>) >>
+ ovdl::dsl::keyword<"political_decisions">(id) >>
(lexy::dsl::equal_sign >> lexy::dsl::curly_bracketed.opt_list(lexy::dsl::p<DecisionStatement>));
static constexpr auto value = lexy::as_list<ast::AssignStatementList>;
diff --git a/src/openvic-dataloader/v2script/ModifierGrammar.hpp b/src/openvic-dataloader/v2script/ModifierGrammar.hpp
index 5b937d5..d6dbb32 100644
--- a/src/openvic-dataloader/v2script/ModifierGrammar.hpp
+++ b/src/openvic-dataloader/v2script/ModifierGrammar.hpp
@@ -15,8 +15,8 @@
#include "detail/dsl.hpp"
namespace ovdl::v2script::grammar {
- constexpr auto modifier_keyword = LEXY_KEYWORD("modifier", lexy::dsl::inline_<Identifier<StringEscapeOption>>);
- constexpr auto factor_keyword = LEXY_KEYWORD("factor", lexy::dsl::inline_<Identifier<StringEscapeOption>>);
+ constexpr auto modifier_keyword = LEXY_KEYWORD("modifier", id);
+ constexpr auto factor_keyword = LEXY_KEYWORD("factor", id);
struct FactorStatement {
static constexpr auto rule = lexy::dsl::position(factor_keyword) >> (lexy::dsl::equal_sign + lexy::dsl::p<Identifier<StringEscapeOption>>);
diff --git a/src/openvic-dataloader/v2script/Parser.cpp b/src/openvic-dataloader/v2script/Parser.cpp
index 29e9e80..a4cad9d 100644
--- a/src/openvic-dataloader/v2script/Parser.cpp
+++ b/src/openvic-dataloader/v2script/Parser.cpp
@@ -1,5 +1,6 @@
#include "openvic-dataloader/v2script/Parser.hpp"
+#include <filesystem>
#include <iostream>
#include <optional>
#include <string>
@@ -149,8 +150,8 @@ 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;
+Parser& Parser::load_from_file(const char* path) {
+ set_file_path(path);
// Type can be deduced??
_run_load_func(std::mem_fn(&ParseHandler::load_file), path);
return *this;
@@ -179,6 +180,7 @@ bool Parser::simple_parse() {
_has_error = _parse_handler->parse_state().logger().errored();
_has_warning = _parse_handler->parse_state().logger().warned();
if (!_parse_handler->root()) {
+ _has_error = true;
_has_fatal_error = true;
if (&_error_stream.get() != &detail::cnull) {
print_errors_to(_error_stream);
@@ -258,8 +260,8 @@ const FileTree* Parser::get_file_node() const {
return _parse_handler->root();
}
-std::string_view Parser::value(const ovdl::v2script::ast::FlatValue& node) const {
- return node.value(_parse_handler->parse_state().ast().symbol_interner());
+std::string_view Parser::value(const ovdl::v2script::ast::FlatValue* node) const {
+ return node->value(_parse_handler->parse_state().ast().symbol_interner());
}
std::string Parser::make_native_string() const {
@@ -319,13 +321,20 @@ void Parser::print_errors_to(std::basic_ostream<char>& stream) const {
dryad::visit_tree(
error,
[&](const error::BufferError* buffer_error) {
- stream << buffer_error->message() << '\n';
+ stream << "buffer error: " << buffer_error->message() << '\n';
},
[&](const error::ParseError* parse_error) {
- stream << parse_error->message() << '\n';
+ auto position = get_error_position(parse_error);
+ std::string pos_str = fmt::format(":{}:{}: ", position.start_line, position.start_column);
+ stream << _file_path << pos_str << "parse error for '" << parse_error->production_name() << "': " << parse_error->message() << '\n';
},
[&](dryad::child_visitor<error::ErrorKind> visitor, const error::Semantic* semantic) {
- stream << semantic->message() << '\n';
+ auto position = get_error_position(semantic);
+ std::string pos_str = ": ";
+ if (!position.is_empty()) {
+ pos_str = fmt::format(":{}:{}: ", position.start_line, position.start_column);
+ }
+ stream << _file_path << pos_str << semantic->message() << '\n';
auto annotations = semantic->annotations();
for (auto annotation : annotations) {
visitor(annotation);
diff --git a/src/openvic-dataloader/v2script/SimpleGrammar.hpp b/src/openvic-dataloader/v2script/SimpleGrammar.hpp
index bd4adaa..731a7f1 100644
--- a/src/openvic-dataloader/v2script/SimpleGrammar.hpp
+++ b/src/openvic-dataloader/v2script/SimpleGrammar.hpp
@@ -5,6 +5,8 @@
#include <lexy/callback.hpp>
#include <lexy/dsl.hpp>
+#include <lexy/dsl/identifier.hpp>
+#include <lexy/dsl/symbol.hpp>
#include "detail/dsl.hpp"
@@ -37,12 +39,14 @@ namespace ovdl::v2script::grammar {
/* REQUIREMENTS: DAT-631 */
static constexpr auto comment_specifier = LEXY_LIT("#") >> lexy::dsl::until(lexy::dsl::newline).or_eof();
+ static constexpr auto ascii = lexy::dsl::ascii::alpha_digit_underscore / LEXY_ASCII_ONE_OF("+:@%&'-.");
+
/* REQUIREMENTS:
* DAT-632
* DAT-635
*/
static constexpr auto windows_1252_data_specifier =
- lexy::dsl::ascii::alpha_digit_underscore / LEXY_ASCII_ONE_OF("+:@%&'-.") /
+ ascii /
lexy::dsl::lit_b<0x8A> / lexy::dsl::lit_b<0x8C> / lexy::dsl::lit_b<0x8E> /
lexy::dsl::lit_b<0x92> / lexy::dsl::lit_b<0x97> / lexy::dsl::lit_b<0x9A> / lexy::dsl::lit_b<0x9C> /
dsl::make_range<0x9E, 0x9F>() /
@@ -74,6 +78,8 @@ namespace ovdl::v2script::grammar {
.map<'r'>('\r')
.map<'t'>('\t');
+ static constexpr auto id = lexy::dsl::identifier(data_char_class);
+
template<ParseOptions Options>
struct SimpleGrammar {
struct StatementListBlock;
@@ -194,14 +200,14 @@ namespace ovdl::v2script::grammar {
template<ovdl::detail::string_literal Keyword, auto Production, auto Value = dsl::default_kw_value<ast::ParseState, ast::IdentifierValue, Keyword>>
using keyword_rule = dsl::keyword_rule<
ast::ParseState,
- Identifier<StringEscapeOption>,
+ id,
ast::AssignStatement,
Keyword, Production, Value>;
template<ovdl::detail::string_literal Keyword, auto Production, auto Value = dsl::default_kw_value<ast::ParseState, ast::IdentifierValue, Keyword>>
using fkeyword_rule = dsl::fkeyword_rule<
ast::ParseState,
- Identifier<StringEscapeOption>,
+ id,
ast::AssignStatement,
Keyword, Production, Value>;