Skip to content

Commit

Permalink
Update error code
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Jan 13, 2024
1 parent 6fc2d27 commit 31aef35
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
39 changes: 23 additions & 16 deletions include/papilio/script/interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,28 +417,32 @@ inline constexpr bool is_variable_storable_v = is_variable_storable<T>::value;

// ^^^ variable ^^^ / vvv interpreter vvv

PAPILIO_EXPORT enum class script_error_code : int
{
no_error = 0,
end_of_string = 1,
invalid_field_name,
invalid_condition,
invalid_index,
invalid_attribute,
invalid_operator,
invalid_string,
unclosed_brace,

unknown_error = -1,
};

[[nodiscard]]
std::string to_string(script_error_code ec);

std::ostream& operator<<(std::ostream& os, script_error_code ec);

PAPILIO_EXPORT class interpreter_base
{
public:
static constexpr char32_t script_start = U'$';
static constexpr char32_t condition_end = U':';

enum class script_error_code : int
{
end_of_string = 1,
invalid_field_name,
invalid_condition,
invalid_index,
invalid_attribute,
invalid_operator,
invalid_string,
unclosed_brace
};

friend std::string to_string(script_error_code ec);

friend std::ostream& operator<<(std::ostream& os, script_error_code ec);

class script_error : public format_error
{
public:
Expand All @@ -455,6 +459,9 @@ PAPILIO_EXPORT class interpreter_base
script_error_code m_ec;
};

[[nodiscard]]
static script_error make_error(script_error_code ec);

protected:
[[noreturn]]
static void throw_end_of_string();
Expand Down
25 changes: 18 additions & 7 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace papilio::script
{
std::string to_string(interpreter_base::script_error_code ec)
static const char* to_cstr(script_error_code ec) noexcept
{
using enum interpreter_base::script_error_code;
using enum script_error_code;

#define PAPILIO_SCRIPT_ERR(code, msg) \
case code: return msg

switch(ec)
{
PAPILIO_SCRIPT_ERR(no_error, "no error");
PAPILIO_SCRIPT_ERR(end_of_string, "end of string");
PAPILIO_SCRIPT_ERR(invalid_field_name, "invalid field name");
PAPILIO_SCRIPT_ERR(invalid_condition, "invalid condition");
Expand All @@ -21,26 +22,36 @@ std::string to_string(interpreter_base::script_error_code ec)
PAPILIO_SCRIPT_ERR(unclosed_brace, "unclosed brace");

[[unlikely]] default:
throw std::invalid_argument("invalid error code");
PAPILIO_SCRIPT_ERR(unknown_error, "unknown error");
}
}

std::ostream& operator<<(std::ostream& os, interpreter_base::script_error_code ec)
std::string to_string(script_error_code ec)
{
os << to_string(ec);
return to_cstr(ec);
}

std::ostream& operator<<(std::ostream& os, script_error_code ec)
{
os << to_cstr(ec);
return os;
}

interpreter_base::script_error::script_error(script_error_code ec)
: format_error(to_string(ec)), m_ec(ec) {}

interpreter_base::script_error interpreter_base::make_error(script_error_code ec)
{
return script_error(ec);
}

void interpreter_base::throw_end_of_string()
{
throw script_error(script_error_code::end_of_string);
throw make_error(script_error_code::end_of_string);
}

void interpreter_base::throw_error(script_error_code ec)
{
throw script_error(ec);
throw make_error(ec);
}
} // namespace papilio::script
2 changes: 1 addition & 1 deletion test/test_script_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ auto get_err(papilio::format_string<Args...> fmt, Args&&... args)
TEST(interpreter, exception)
{
using namespace papilio;
using enum script::interpreter_base::script_error_code;
using enum script::script_error_code;
using test_script_interpreter::get_err;

EXPECT_EQ(get_err("{").error_code(), end_of_string);
Expand Down

0 comments on commit 31aef35

Please sign in to comment.