Enhance and correct error messages

Backported from C++11 branch.

Conflicts:

	include/chaiscript/dispatchkit/proxy_functions.hpp
	include/chaiscript/language/chaiscript_common.hpp
	include/chaiscript/language/chaiscript_eval.hpp
	include/chaiscript/language/chaiscript_parser.hpp
This commit is contained in:
Jason Turner
2011-09-25 16:46:05 -06:00
parent 3bccf4d977
commit 5a76d98692
5 changed files with 142 additions and 68 deletions

View File

@@ -780,10 +780,9 @@ namespace chaiscript
/** /**
* Dump object info to stdout * 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 << (o.is_const()?"const ":"") << type_name(o) << std::endl;
std::cout << (ti.is_const()?"const ":"") << get_type_name(ti) << std::endl;
} }
/** /**
@@ -875,7 +874,7 @@ namespace chaiscript
return false; 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()); return get_type_name(obj.get_type_info());
} }

View File

@@ -591,17 +591,14 @@ namespace chaiscript
class dispatch_error : public std::runtime_error class dispatch_error : public std::runtime_error
{ {
public: public:
dispatch_error() throw() dispatch_error(const std::vector<Boxed_Value> &t_bvs)
: std::runtime_error("No matching function to dispatch to") : std::runtime_error("Error with function dispatch"), parameters(t_bvs)
{ {
} }
dispatch_error(bool is_const) throw()
: std::runtime_error(std::string("No matching function to dispatch to") + (is_const?", parameter is const":""))
{
}
virtual ~dispatch_error() throw() {} virtual ~dispatch_error() throw() {}
std::vector<Boxed_Value> parameters;
}; };
} }
@@ -635,7 +632,7 @@ namespace chaiscript
++begin; ++begin;
} }
throw exception::dispatch_error(plist.empty()?false:plist[0].is_const()); throw exception::dispatch_error(plist);
} }
/** /**

View File

@@ -71,9 +71,22 @@ namespace chaiscript
std::string filename; std::string filename;
std::vector<AST_NodePtr> call_stack; std::vector<AST_NodePtr> call_stack;
eval_error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname) throw() eval_error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname,
: std::runtime_error(format(t_why, t_where, t_fname)), const std::vector<Boxed_Value> &t_parameters, const chaiscript::detail::Dispatch_Engine &t_ss) :
reason(t_why), start_position(t_where), end_position(t_where), filename(t_fname) 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) :
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) :
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) throw() eval_error(const std::string &t_why) throw()
@@ -89,15 +102,43 @@ namespace chaiscript
return "Error: \"" + 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 (std::vector<Boxed_Value>::const_iterator itr = t_parameters.begin();
itr != t_parameters.end();
++itr)
{
paramstr += (itr->is_const()?"const ":"");
paramstr += t_ss.type_name(*itr);
paramstr += ", ";
}
ss << paramstr.substr(0, paramstr.size() - 2);
}
ss << ")";
return ss.str();
}
static std::string format_filename(const std::string &t_fname) static std::string format_filename(const std::string &t_fname)
{ {
std::stringstream ss; std::stringstream ss;
if (t_fname != "__EVAL__") if (t_fname != "__EVAL__")
{ {
ss << "in '" << t_fname << "' "; ss << "in '" << t_fname << "' ";
} else { } else {
ss << "during evaluation "; ss << "during evaluation ";
} }
return ss.str(); return ss.str();
} }
@@ -108,14 +149,51 @@ namespace chaiscript
return ss.str(); 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) static std::string format(const std::string &t_why, const File_Position &t_where, const std::string &t_fname)
{ {
std::stringstream ss; std::stringstream ss;
ss << format_why(t_why); ss << format_why(t_why);
ss << " "; ss << " ";
ss << format_filename(t_fname); ss << format_filename(t_fname);
ss << " "; ss << " ";
ss << format_location(t_where); ss << format_location(t_where);
return ss.str(); return ss.str();
} }
}; };

View File

@@ -74,8 +74,8 @@ namespace chaiscript
return t_ss.call_function(t_oper_string, t_lhs, t_rhs); return t_ss.call_function(t_oper_string, t_lhs, t_rhs);
} }
} }
catch(const exception::dispatch_error &){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Can not find appropriate '" + t_oper_string + "'"); throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, t_ss);
} }
} }
}; };
@@ -208,7 +208,7 @@ namespace chaiscript
return retval; return retval;
} }
catch(const exception::dispatch_error &e){ 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) { catch(detail::Return_Value &rv) {
return rv.retval; return rv.retval;
@@ -235,7 +235,7 @@ namespace chaiscript
return (*boxed_cast<const Const_Proxy_Function &>(this->children[0]->eval(t_ss)))(plb); return (*boxed_cast<const Const_Proxy_Function &>(this->children[0]->eval(t_ss)))(plb);
} }
catch(const exception::dispatch_error &e){ 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) { catch(detail::Return_Value &rv) {
return rv.retval; return rv.retval;
@@ -294,12 +294,12 @@ namespace chaiscript
try { try {
retval = t_ss.call_function(this->children[1]->text, lhs, retval); retval = t_ss.call_function(this->children[1]->text, lhs, retval);
} }
catch(const exception::dispatch_error &){ catch(const exception::dispatch_error &e){
throw exception::eval_error(std::string("Mismatched types in equation") + (lhs.is_const()?", lhs is const.":".")); throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, t_ss);
} }
} }
catch(const exception::dispatch_error &){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Can not clone right hand side of equation"); throw exception::eval_error("Missing clone or copy constructor for right hand side of equation", e.parameters, t_ss);
} }
} }
else if (this->children[1]->text == ":=") { else if (this->children[1]->text == ":=") {
@@ -312,8 +312,8 @@ namespace chaiscript
else { else {
try { try {
retval = t_ss.call_function(this->children[1]->text, lhs, retval); retval = t_ss.call_function(this->children[1]->text, lhs, retval);
} catch(const exception::dispatch_error &){ } catch(const exception::dispatch_error &e){
throw exception::eval_error("Can not find appropriate '" + this->children[1]->text + "'"); throw exception::eval_error("Unable to find appropriate'" + this->children[1]->text + "' operator.", e.parameters, t_ss);
} }
} }
} }
@@ -431,8 +431,8 @@ namespace chaiscript
catch(std::out_of_range &) { catch(std::out_of_range &) {
throw exception::eval_error("Out of bounds exception"); throw exception::eval_error("Out of bounds exception");
} }
catch(const exception::dispatch_error &){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Can not find appropriate array lookup '[]' "); throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, t_ss );
} }
} }
@@ -475,7 +475,7 @@ namespace chaiscript
retval = t_ss.call_function(fun_name, plb); retval = t_ss.call_function(fun_name, plb);
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
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) { catch(detail::Return_Value &rv) {
retval = rv.retval; retval = rv.retval;
@@ -489,8 +489,8 @@ namespace chaiscript
catch(std::out_of_range &) { catch(std::out_of_range &) {
throw exception::eval_error("Out of bounds exception"); throw exception::eval_error("Out of bounds exception");
} }
catch(const exception::dispatch_error &){ catch(const exception::dispatch_error &e){
throw exception::eval_error("Can not find appropriate array lookup '[]' "); throw exception::eval_error("Can not find appropriate array lookup operator '[]'.", e.parameters, t_ss);
} }
} }
} }
@@ -908,8 +908,8 @@ namespace chaiscript
} }
return const_var(retval); return const_var(retval);
} }
catch (const exception::dispatch_error &) { catch (const exception::dispatch_error &e) {
throw exception::eval_error("Can not find appropriate 'clone' or copy constructor for map elements"); throw exception::eval_error("Can not find appropriate copy constructor or 'clone' while inserting into Map.", e.parameters, t_ss);
} }
} }
@@ -971,8 +971,8 @@ namespace chaiscript
fpp.save_params(params); fpp.save_params(params);
return t_ss.call_function(this->children[0]->text, bv); return t_ss.call_function(this->children[0]->text, bv);
} }
} catch (const exception::dispatch_error &) { } catch (const exception::dispatch_error &e) {
throw exception::eval_error("Error with prefix operator evaluation: " + children[0]->text); throw exception::eval_error("Error with prefix operator evaluation: '" + children[0]->text + "'", e.parameters, t_ss);
} }
} }
@@ -1013,8 +1013,8 @@ namespace chaiscript
this->children[0]->children[0]->children[0]->eval(t_ss), this->children[0]->children[0]->children[0]->eval(t_ss),
this->children[0]->children[0]->children[1]->eval(t_ss)); this->children[0]->children[0]->children[1]->eval(t_ss));
} }
catch (const exception::dispatch_error &) { catch (const exception::dispatch_error &e) {
throw exception::eval_error("Unable to generate range vector"); throw exception::eval_error("Unable to generate range vector, while calling 'generate_range'", e.parameters, t_ss);
} }
} }

View File

@@ -1672,7 +1672,7 @@ namespace chaiscript
throw exception::eval_error("Incomplete expression", File_Position(m_line, m_col), *m_filename); throw exception::eval_error("Incomplete expression", File_Position(m_line, m_col), *m_filename);
} }
if (!Char(')')) { 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; return retval;
@@ -1690,7 +1690,7 @@ namespace chaiscript
retval = true; retval = true;
Container_Arg_List(); Container_Arg_List();
if (!Char(']')) { if (!Char(']')) {
throw exception::eval_error("Missing closing square bracket", File_Position(m_line, m_col), *m_filename); throw exception::eval_error("Missing closing square bracket ']' 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 ((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) { if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Value_Range) {