Circus 0.0.1-alpha
C++ Serialization Framework
Loading...
Searching...
No Matches
circus_error.hpp
Go to the documentation of this file.
1#pragma once
2#include <iostream>
3#include <stack>
4#include <unordered_map>
5
6#include "circus_traits.hpp"
7#include "utils/enum_flag.hpp"
12
13namespace circus::error {
14
20
24#define ERROR_TYPE(NAME) NAME
25#define ERROR_TYPEs \
26 ERROR_TYPE(SYNTAX)
27
28enum CIRCUS_ERROR_TYPES : std::uint64_t {
29 ERROR_TYPEs
30};
31#undef ERROR_TYPE
35#define ERROR_TYPE(NAME) #NAME
36inline const char* REFLECTED_ERROR_MAP[]{
37 ERROR_TYPEs};
38#undef ERROR_TYPE
39
44class parser_error : public std::runtime_error {
45 public:
47 using enum_type = flag_type::enum_type;
49 flag_type type_of;
55 parser_error(flag_type E, const std::string& what) : std::runtime_error(what), type_of(E) {};
56};
57
61class parser_reporter {
63 using underlying_type = std::underlying_type_t<flag_type::enum_type>;
64 std::stack<std::string> error_log_stack;
65 std::stack<std::string> temp;
66
73 static std::string match_and_ret(const underlying_type& from, const underlying_type& to) {
74 return ((from & to) == to) ? REFLECTED_ERROR_MAP[from] : "";
75 };
81 static std::string to_stringized_types(const underlying_type& type) {
82 std::string ret = "";
83 ret += match_and_ret(type, error::SYNTAX);
84 return ret.empty() ? "UNKNOWN" : ret;
85 }
86
87 public:
88 parser_reporter() = default;
89
94 [[nodiscard]] bool empty() const noexcept {
95 return error_log_stack.empty();
96 }
97
103 void report(const flag_type& type, const std::string& message, std::pair<std::size_t, std::size_t> location) noexcept {
104 std::string out = "[CIRCUS][PARSER_ERROR][" + to_stringized_types(type.flags) + "] " + message + " at (" + std::to_string(location.first) + " : " + std::to_string(location.second) + ")";
105 error_log_stack.push(out);
106 }
107
110 void log_errors() noexcept {
111 while (!error_log_stack.empty()) {
112 auto top = error_log_stack.top();
113 std::clog << top << std::endl;
114 temp.push(top);
115 error_log_stack.pop();
116 }
117
118 while (!temp.empty()) {
119 auto top = temp.top();
120 error_log_stack.push(top);
121 temp.pop();
122 }
123 }
124
128 void flush_errors() noexcept {
129 while (!error_log_stack.empty()) {
130 error_log_stack.pop();
131 }
132 };
133
134 ~parser_reporter() = default;
135};
136
143template <typename E>
144 requires std::is_enum_v<E>
145constexpr E operator|(E lhs, E rhs) {
146 using U = std::underlying_type_t<E>;
147 return static_cast<E>(static_cast<U>(lhs) | static_cast<U>(rhs));
148}
149
156template <typename E>
157 requires std::is_enum_v<E>
158constexpr E operator&(E lhs, E rhs) {
159 using U = std::underlying_type_t<E>;
160 return static_cast<E>(static_cast<U>(lhs) & static_cast<U>(rhs));
161}
162
168template <typename E>
169 requires std::is_enum_v<E>
170constexpr E operator~(E val) {
171 using U = std::underlying_type_t<E>;
172 return static_cast<E>(~static_cast<U>(val));
173}
174}; // namespace circus::error
175 // end of ParserErrors group
Provides compile-time traits and C++20 concepts for type introspection used throughout the Circus lib...
Aggregates and manages parser error messages with support for logging and flushing.
Definition circus_error.hpp:61
A utility class for strongly typed enum flags with bitwise operations.
Definition enum_flag.hpp:25
CIRCUS_ERROR_TYPES enum_type
Definition enum_flag.hpp:30
Provides a type-safe wrapper for enum flags with bitwise operations.
parser_error(flag_type E, const std::string &what)
Constructs a parser error with a flag type and descriptive message.
Definition circus_error.hpp:55
void report(const flag_type &type, const std::string &message, std::pair< std::size_t, std::size_t > location) noexcept
Reports an error by adding a formatted message to the error log stack.
Definition circus_error.hpp:103
constexpr E operator|(E lhs, E rhs)
Bitwise OR operator for enum error types.
Definition circus_error.hpp:145
void flush_errors() noexcept
Clears all errors from the error log stack.
Definition circus_error.hpp:128
constexpr E operator~(E val)
Bitwise NOT operator for enum error types.
Definition circus_error.hpp:170
bool empty() const noexcept
Checks if there are any errors logged.
Definition circus_error.hpp:94
flag_type type_of
The type of parser error that occurred.
Definition circus_error.hpp:49
void log_errors() noexcept
Logs all errors to std::clog and preserves the error stack.
Definition circus_error.hpp:110
constexpr E operator&(E lhs, E rhs)
Bitwise AND operator for enum error types.
Definition circus_error.hpp:158