1 #ifndef ERROR_H_INCLUDED_ 2 #define ERROR_H_INCLUDED_ 11 #include "kblib/containers.h" 12 #include "kblib/convert.h" 13 #include "kblib/stringops.h" 15 enum class ec : unsigned {
27 invalid_normalization_form,
77 const std::map<ec, std::string> err_strs{
79 {ec::unknown,
"Unknown error."},
80 {ec::internal,
"Internal error."},
81 {ec::no_feature,
"Feature not implemented yet."},
84 {ec::y_internal,
"Unknown load-time error."},
85 {ec::too_many_tnode_args,
"Too many arguments specified for a node."},
86 {ec::invalid_stage_type,
"Invalid replace stage type."},
87 {ec::invalid_normalization_form,
"Invalid normalization form."},
88 {ec::bad_regex,
"Invalid regular expression"},
92 {ec::n_internal,
"Internal error in noderef parser."},
93 {ec::unterminated_noderef,
"Unterminated reference."},
94 {ec::extra_rb,
"Encountered '}' outside reference."},
95 {ec::lb_in_name,
"Encountered '{' inside reference."},
96 {ec::illegal_escape,
"Illegal escape sequence."},
99 {ec::p_internal,
"Internal error in template parser."},
100 {ec::bad_format,
"Invalid argref."},
101 {ec::blank_fname,
"Function names cannot be blank."},
102 {ec::invalid_fname,
"Invalid character in function name."},
103 {ec::call_arg,
"Arguments cannot be used as functions."},
105 {ec::eos,
"Unexpected end-of-input"},
106 {ec::extra_rp,
"Unmatched )."},
107 {ec::extra_lp,
"Unmatched (; or unexpected >."},
108 {ec::extra_p,
"Unexpected top-level |."},
109 {ec::extra_ab,
"unescaped < in template context."},
111 {ec::extra_h,
"encountered unexpected # in argument context."},
112 {ec::extra_c,
"encountered unexpected characters after )."},
113 {ec::early_close,
"blank argref is invalid."},
114 {ec::escape_nostr,
"escape sequence outside of string context."},
115 {ec::overmarked_arg,
"Argrefs at topmost level do not need #."},
117 {ec::invalid_raw,
"Invalid raw string syntax."},
118 {ec::no_function,
"No function by this name exists."},
121 {ec::e_internal,
"Internal error in template evaluator."},
123 "Template function received argument of incorrect type."},
125 "Template function received a list when it expected a single element."},
126 {ec::bad_randspec,
"Bad parameters for a random function."},
127 {ec::invoke_null,
"Invoke requires arguments."},
130 "The first argument to invoke must be a string, not a list."},
132 "A synthesized node cannot be converted to another type."},
133 {ec::unpaired_args,
"Template function expected arguments in sets, but " 134 "could not complete a set."},
136 "Template function received more arguments than it expected."},
137 {ec::not_enough_args,
138 "Template function did not receive all expected arguments."},
140 {ec::e_nonode,
"'node' function's argument does not name a node"},
141 {ec::range_error,
"? or [] index out of range."},
142 {ec::bad_math,
"Invalid mathematical expression."},
143 {ec::m_divzero,
"Division by zero occurred."},
144 {ec::null_var,
"get() of nonexistent variable."},
147 {ec::t_internal,
"Internal error in transformation processor"},
150 inline std::string format_err(ec err) {
151 std::ostringstream out;
152 out << 'E' << std::setw(3) << std::setfill('0') << static_cast<int>(err)
153 <<
": " << kblib::get_or(err_strs, err, err_strs.at(ec::unknown))
160 wordgen_error(std::string desc, std::string context, std::string loc,
162 : l1(std::make_shared<std::string>(std::move(desc))),
163 l2(std::make_shared<std::string>(std::move(context))),
164 l3(std::make_shared<std::string>(std::move(loc))),
165 l4(std::make_shared<std::string>(std::move(info))), c(ec::unknown) {}
166 wordgen_error(ec err, std::string context, std::string loc, std::string info)
167 : l1(std::make_shared<std::string>(format_err(err))),
168 l2(std::make_shared<std::string>(std::move(context))),
169 l3(std::make_shared<std::string>(std::move(loc))),
170 l4(std::make_shared<std::string>(std::move(info))), c(err) {}
171 std::shared_ptr<std::string> l1, l2, l3, l4;
173 ec code()
const {
return c; }
174 std::string what()
const {
return kblib::concat(*l1, *l2, *l3, *l4); }
178 return l.what() == r.what();
182 return l.what() != r.what();
185 [[noreturn]]
inline void parse_error(ec err, std::string_view str,
186 const char* epos, std::string msg =
"") {
187 auto printable = kblib::escapify(str);
188 auto index = kblib::calculate_translated_index(str, epos);
189 throw wordgen_error(err, kblib::concat(
"\"", printable,
"\"\n"),
190 kblib::concat(
" ", kblib::repeat(
' ', index),
"^\n"),
194 [[noreturn]]
inline void parse_error(ec err, std::string_view str,
195 long long epos, std::string msg =
"") {
196 parse_error(err, str, str.data() + epos, std::move(msg));
199 #endif // ERROR_H_INCLUDED_