diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 257379e..f6c68e2 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -688,10 +688,9 @@ namespace chaiscript /** * Dump object info to stdout */ - void dump_object(Boxed_Value o) const + void dump_object(const Boxed_Value &o) const { - Type_Info ti = o.get_type_info(); - std::cout << (ti.is_const()?"const ":"") << get_type_name(ti) << std::endl; + std::cout << (o.is_const()?"const ":"") << type_name(o) << std::endl; } /** @@ -783,7 +782,7 @@ namespace chaiscript return false; } - std::string type_name(Boxed_Value obj) const + std::string type_name(const Boxed_Value &obj) const { return get_type_name(obj.get_type_info()); } diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index aa5ba0e..e04f000 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -587,17 +587,14 @@ namespace chaiscript class dispatch_error : public std::runtime_error { public: - dispatch_error() noexcept - : std::runtime_error("No matching function to dispatch to") + dispatch_error(const std::vector &t_bvs) + : std::runtime_error("Error with function dispatch"), parameters(t_bvs) { } - dispatch_error(bool is_const) noexcept - : std::runtime_error(std::string("No matching function to dispatch to") + (is_const?", parameter is const":"")) - { - } - virtual ~dispatch_error() noexcept {} + + std::vector parameters; }; } @@ -631,7 +628,7 @@ namespace chaiscript ++begin; } - throw exception::dispatch_error(plist.empty()?false:plist[0].is_const()); + throw exception::dispatch_error(plist); } /** diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index f8af5e1..7b5e5ee 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -70,10 +70,23 @@ namespace chaiscript std::string filename; std::vector call_stack; + eval_error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname, + const std::vector &t_parameters, const chaiscript::detail::Dispatch_Engine &t_ss) noexcept : + std::runtime_error(format(t_why, t_where, t_fname, t_parameters, t_ss)), + reason(t_why), start_position(t_where), end_position(t_where), filename(t_fname) + {} + + eval_error(const std::string &t_why, + const std::vector &t_parameters, const chaiscript::detail::Dispatch_Engine &t_ss) noexcept : + std::runtime_error(format(t_why, t_parameters, t_ss)), + reason(t_why) + {} + + eval_error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname) noexcept : std::runtime_error(format(t_why, t_where, t_fname)), reason(t_why), start_position(t_where), end_position(t_where), filename(t_fname) - { } + {} eval_error(const std::string &t_why) noexcept : std::runtime_error("Error: \"" + t_why + "\" "), @@ -83,13 +96,101 @@ namespace chaiscript virtual ~eval_error() noexcept {} private: + static std::string format_why(const std::string &t_why) + { + return "Error: \"" + t_why + "\""; + } + + static std::string format_parameters(const std::vector &t_parameters, + const chaiscript::detail::Dispatch_Engine &t_ss) + { + std::stringstream ss; + ss << "With parameters: ("; + + if (!t_parameters.empty()) + { + std::string paramstr; + + for (const Boxed_Value &bv: t_parameters) + { + paramstr += (bv.is_const()?"const ":""); + paramstr += t_ss.type_name(bv); + paramstr += ", "; + } + + ss << paramstr.substr(0, paramstr.size() - 2); + } + ss << ")"; + + return ss.str(); + } + + static std::string format_filename(const std::string &t_fname) + { + std::stringstream ss; + + if (t_fname != "__EVAL__") + { + ss << "in '" << t_fname << "' "; + } else { + ss << "during evaluation "; + } + + return ss.str(); + } + + static std::string format_location(const File_Position &t_where) + { + std::stringstream ss; + ss << "at (" << t_where.line << ", " << t_where.column << ")"; + return ss.str(); + } + + static std::string format(const std::string &t_why, const File_Position &t_where, const std::string &t_fname, + const std::vector &t_parameters, const chaiscript::detail::Dispatch_Engine &t_ss) + { + std::stringstream ss; + + ss << format_why(t_why); + ss << " "; + + ss << format_parameters(t_parameters, t_ss); + ss << " "; + + ss << format_filename(t_fname); + ss << " "; + + ss << format_location(t_where); + + return ss.str(); + } + + static std::string format(const std::string &t_why, + const std::vector &t_parameters, const chaiscript::detail::Dispatch_Engine &t_ss) + { + std::stringstream ss; + + ss << format_why(t_why); + ss << " "; + + ss << format_parameters(t_parameters, t_ss); + ss << " "; + + return ss.str(); + } + static std::string format(const std::string &t_why, const File_Position &t_where, const std::string &t_fname) { std::stringstream ss; - ss << "Error: \"" << t_why << "\" " << - (t_fname != "__EVAL__" ? ("in '" + t_fname + "' ") : "during evaluation ") << - "at (" << t_where.line << ", " << - t_where.column + ")"; + + ss << format_why(t_why); + ss << " "; + + ss << format_filename(t_fname); + ss << " "; + + ss << format_location(t_where); + return ss.str(); } }; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 3c7cd21..8e2b963 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -67,8 +67,8 @@ namespace chaiscript return t_ss.call_function(t_oper_string, t_lhs, t_rhs); } } - catch(const exception::dispatch_error &){ - throw exception::eval_error("Can not find appropriate '" + t_oper_string + "'"); + catch(const exception::dispatch_error &e){ + throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, t_ss); } } }; @@ -204,7 +204,7 @@ namespace chaiscript } catch(const exception::dispatch_error &e){ t_ss.set_stack(prev_stack); - throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'"); + throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'", e.parameters, t_ss); } catch(detail::Return_Value &rv) { t_ss.set_stack(prev_stack); @@ -242,7 +242,7 @@ namespace chaiscript return (*boxed_cast(this->children[0]->eval(t_ss)))(plb); } catch(const exception::dispatch_error &e){ - throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'"); + throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'", e.parameters, t_ss); } catch(detail::Return_Value &rv) { return rv.retval; @@ -312,19 +312,19 @@ namespace chaiscript try { retval = t_ss.call_function(this->children[1]->text, lhs, retval); } - catch(const exception::dispatch_error &){ - throw exception::eval_error(std::string("Mismatched types in equation") + (lhs.is_const()?", lhs is const.":".")); + catch(const exception::dispatch_error &e){ + throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, t_ss); } } - catch(const exception::dispatch_error &){ - throw exception::eval_error("Can not clone right hand side of equation"); + catch(const exception::dispatch_error &e){ + throw exception::eval_error("Missing clone or copy constructor for right hand side of equation", e.parameters, t_ss); } } else { try { retval = t_ss.call_function(this->children[1]->text, lhs, retval); - } catch(const exception::dispatch_error &){ - throw exception::eval_error("Can not find appropriate '" + this->children[1]->text + "'"); + } catch(const exception::dispatch_error &e){ + throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, t_ss); } } } @@ -439,8 +439,8 @@ namespace chaiscript catch(std::out_of_range &) { throw exception::eval_error("Out of bounds exception"); } - catch(const exception::dispatch_error &){ - throw exception::eval_error("Can not find appropriate array lookup '[]' "); + catch(const exception::dispatch_error &e){ + throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, t_ss ); } } @@ -485,7 +485,7 @@ namespace chaiscript } catch(const exception::dispatch_error &e){ t_ss.set_stack(prev_stack); - throw exception::eval_error(std::string(e.what()) + " for function: " + fun_name); + throw exception::eval_error(std::string(e.what()) + " for function: " + fun_name, e.parameters, t_ss); } catch(detail::Return_Value &rv) { t_ss.set_stack(prev_stack); @@ -503,8 +503,8 @@ namespace chaiscript catch(std::out_of_range &) { throw exception::eval_error("Out of bounds exception"); } - catch(const exception::dispatch_error &){ - throw exception::eval_error("Can not find appropriate array lookup '[]' "); + catch(const exception::dispatch_error &e){ + throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, t_ss); } } } @@ -819,8 +819,8 @@ namespace chaiscript } return const_var(retval); } - catch (const exception::dispatch_error &) { - throw exception::eval_error("Can not find appropriate 'Map()'"); + catch (const exception::dispatch_error &e) { + throw exception::eval_error("Can not find appropriate copy constructor or clone while inserting into Map.", e.parameters, t_ss); } } @@ -896,8 +896,8 @@ namespace chaiscript } else { return t_ss.call_function(this->children[0]->text, bv); } - } catch (const exception::dispatch_error &) { - throw exception::eval_error("Error with prefix operator evaluation: " + children[0]->text); + } catch (const exception::dispatch_error &e) { + throw exception::eval_error("Error with prefix operator evaluation: '" + children[0]->text + "'", e.parameters, t_ss); } } @@ -938,8 +938,8 @@ namespace chaiscript this->children[0]->children[0]->children[0]->eval(t_ss), this->children[0]->children[0]->children[1]->eval(t_ss)); } - catch (const exception::dispatch_error &) { - throw exception::eval_error("Unable to generate range vector"); + catch (const exception::dispatch_error &e) { + throw exception::eval_error("Unable to generate range vector, while calling 'generate_range'", e.parameters, t_ss); } } diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index c176f6e..32aded3 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1587,7 +1587,7 @@ namespace chaiscript throw exception::eval_error("Incomplete expression", File_Position(m_line, m_col), *m_filename); } if (!Char(')')) { - throw exception::eval_error("Missing closing parenthesis", File_Position(m_line, m_col), *m_filename); + throw exception::eval_error("Missing closing parenthesis ')'", File_Position(m_line, m_col), *m_filename); } } return retval; @@ -1605,7 +1605,7 @@ namespace chaiscript retval = true; Container_Arg_List(); if (!Char('}')) { - throw exception::eval_error("Missing closing square bracket", File_Position(m_line, m_col), *m_filename); + throw exception::eval_error("Missing closing brace '}' in container initializer", File_Position(m_line, m_col), *m_filename); } if ((prev_stack_top != m_match_stack.size()) && (m_match_stack.back()->children.size() > 0)) { if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Value_Range) {