Enhance and correct error messages

This commit is contained in:
Jason Turner 2011-09-25 16:46:05 -06:00
parent 3329732ceb
commit 702b5fdba1
5 changed files with 137 additions and 40 deletions

View File

@ -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());
}

View File

@ -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<Boxed_Value> &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<Boxed_Value> parameters;
};
}
@ -631,7 +628,7 @@ namespace chaiscript
++begin;
}
throw exception::dispatch_error(plist.empty()?false:plist[0].is_const());
throw exception::dispatch_error(plist);
}
/**

View File

@ -70,10 +70,23 @@ namespace chaiscript
std::string filename;
std::vector<AST_NodePtr> call_stack;
eval_error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname,
const std::vector<Boxed_Value> &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<Boxed_Value> &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<Boxed_Value> &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<Boxed_Value> &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<Boxed_Value> &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();
}
};

View File

@ -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<const Const_Proxy_Function &>(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);
}
}

View File

@ -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) {