From aed493322b432a5a4659c709f8ce76e8d9b03dff Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 16 Jul 2009 13:24:15 +0000 Subject: [PATCH] Clean up exception story so that there is only one exception type and one thing the user needs to catch --- include/chaiscript/chaiscript.hpp | 36 ++---- .../chaiscript/language/chaiscript_engine.hpp | 2 +- .../chaiscript/language/chaiscript_parser.hpp | 112 +++++++++--------- src/main.cpp | 22 ---- 4 files changed, 69 insertions(+), 103 deletions(-) diff --git a/include/chaiscript/chaiscript.hpp b/include/chaiscript/chaiscript.hpp index 956f39e..3733e19 100644 --- a/include/chaiscript/chaiscript.hpp +++ b/include/chaiscript/chaiscript.hpp @@ -83,41 +83,29 @@ namespace chaiscript }; /** - * Errors generated inside the parser + * Errors generated during parsing or evaluation */ - struct Parse_Error : public std::runtime_error { + struct Eval_Error : public std::runtime_error { std::string reason; File_Position position; const char *filename; - Parse_Error(const std::string &why, const File_Position &where, const char *fname) : - std::runtime_error("Parse error: \"" + why + "\" in '" - + std::string(fname) + "' at: (" + boost::lexical_cast(where.line+1) + ", " + - boost::lexical_cast(where.column) + ")"), + Eval_Error(const std::string &why, const File_Position &where, const char *fname) : + std::runtime_error("Error: \"" + why + "\" " + + (std::string(fname) != "__EVAL__" ? ("in '" + std::string(fname) + "' ") : "during evaluation ") + + + "at (" + boost::lexical_cast(where.line) + ", " + + boost::lexical_cast(where.column) + ")"), reason(why), position(where), filename(fname) { } - Parse_Error(const std::string &why, const TokenPtr &where) - : std::runtime_error("Parse error: \"" + why + "\" in '" - + where->filename + "' line: " + boost::lexical_cast(where->start.line+1)), + Eval_Error(const std::string &why, const TokenPtr &where) + : std::runtime_error("Error: \"" + why + "\" " + + (std::string(where->filename) != "__EVAL__" ? ("in '" + std::string(where->filename) + "' ") : "during evaluation ") + + "at (" + boost::lexical_cast(where->start.line) + ", " + + boost::lexical_cast(where->start.column) + ")"), reason(why), position(where->start), filename(where->filename) { } - virtual ~Parse_Error() throw() {} - }; - - /** - * Errors generated inside the evaluator - */ - struct Eval_Error : public std::runtime_error { - std::string reason; - TokenPtr location; - - Eval_Error(const std::string &why, const TokenPtr where) - : std::runtime_error("Eval error: \"" + why + "\" in '" - + where->filename + "' line: " + boost::lexical_cast(where->start.line+1)), - reason(why), location(where) { } - virtual ~Eval_Error() throw() {} }; diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 4dbed53..0fbab8f 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -88,7 +88,7 @@ namespace chaiscript engine.register_function(boost::shared_ptr( new dispatchkit::Dynamic_Proxy_Function(boost::bind(&ChaiScript_System::eval, boost::ref(*this), _1), 1)), "eval"); - evaluate_string(chaiscript_prelude); + evaluate_string(chaiscript_prelude, "standard prelude"); } /** diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 37af242..2e958eb 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -301,7 +301,7 @@ namespace chaiscript ++col; } else { - throw Parse_Error("Unclosed quoted string", File_Position(line, col), filename); + throw Eval_Error("Unclosed quoted string", File_Position(line, col), filename); } } return retval; @@ -343,7 +343,7 @@ namespace chaiscript case ('t') : match.push_back('\t'); break; case ('\'') : match.push_back('\''); break; case ('\"') : match.push_back('\"'); break; - default: throw Parse_Error("Unknown escaped sequence in string", File_Position(prev_line, prev_col), filename); + default: throw Eval_Error("Unknown escaped sequence in string", File_Position(prev_line, prev_col), filename); } } else { @@ -392,7 +392,7 @@ namespace chaiscript ++col; } else { - throw Parse_Error("Unclosed single-quoted string", File_Position(line, col), filename); + throw Eval_Error("Unclosed single-quoted string", File_Position(line, col), filename); } } return retval; @@ -434,7 +434,7 @@ namespace chaiscript case ('t') : match.push_back('\t'); break; case ('\'') : match.push_back('\''); break; case ('\"') : match.push_back('\"'); break; - default: throw Parse_Error("Unknown escaped sequence in string", File_Position(prev_line, prev_col), filename); + default: throw Eval_Error("Unknown escaped sequence in string", File_Position(prev_line, prev_col), filename); } } else { @@ -694,7 +694,7 @@ namespace chaiscript if (Char(',')) { do { if (!Equation()) { - throw Parse_Error("Unexpected value in parameter list", match_stack.back()); + throw Eval_Error("Unexpected value in parameter list", match_stack.back()); } } while (retval && Char(',')); } @@ -721,7 +721,7 @@ namespace chaiscript if (Char(',')) { do { if (!Map_Pair()) { - throw Parse_Error("Unexpected value in container", match_stack.back()); + throw Eval_Error("Unexpected value in container", match_stack.back()); } } while (retval && Char(',')); } @@ -746,14 +746,14 @@ namespace chaiscript if (Char('(')) { Arg_List(); if (!Char(')')) { - throw Parse_Error("Incomplete anonymous function", File_Position(line, col), filename); + throw Eval_Error("Incomplete anonymous function", File_Position(line, col), filename); } } while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete anonymous function", File_Position(line, col), filename); + throw Eval_Error("Incomplete anonymous function", File_Position(line, col), filename); } build_match(Token_Type::Lambda, prev_stack_top); @@ -784,13 +784,13 @@ namespace chaiscript retval = true; if (!Id(true)) { - throw Parse_Error("Missing function name in definition", File_Position(line, col), filename); + throw Eval_Error("Missing function name in definition", File_Position(line, col), filename); } if (Char('(')) { Arg_List(); if (!Char(')')) { - throw Parse_Error("Incomplete function definition", File_Position(line, col), filename); + throw Eval_Error("Incomplete function definition", File_Position(line, col), filename); } } @@ -798,13 +798,13 @@ namespace chaiscript if (Char(':')) { if (!Expression()) { - throw Parse_Error("Missing guard expression for function", File_Position(line, col), filename); + throw Eval_Error("Missing guard expression for function", File_Position(line, col), filename); } } while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete function definition", File_Position(line, col), filename); + throw Eval_Error("Incomplete function definition", File_Position(line, col), filename); } build_match(Token_Type::Def, prev_stack_top); @@ -829,17 +829,17 @@ namespace chaiscript retval = true; if (!Char('(')) { - throw Parse_Error("Incomplete 'if' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'if' expression", File_Position(line, col), filename); } if (!(Expression() && Char(')'))) { - throw Parse_Error("Incomplete 'if' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'if' expression", File_Position(line, col), filename); } while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete 'if' block", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'if' block", File_Position(line, col), filename); } bool has_matches = true; @@ -848,17 +848,17 @@ namespace chaiscript has_matches = false; if (Keyword("elseif", true)) { if (!Char('(')) { - throw Parse_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); } if (!(Expression() && Char(')'))) { - throw Parse_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'elseif' expression", File_Position(line, col), filename); } while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete 'elseif' block", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'elseif' block", File_Position(line, col), filename); } has_matches = true; } @@ -866,7 +866,7 @@ namespace chaiscript while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete 'else' block", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'else' block", File_Position(line, col), filename); } has_matches = true; } @@ -890,17 +890,17 @@ namespace chaiscript retval = true; if (!Char('(')) { - throw Parse_Error("Incomplete 'while' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'while' expression", File_Position(line, col), filename); } if (!(Expression() && Char(')'))) { - throw Parse_Error("Incomplete 'while' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'while' expression", File_Position(line, col), filename); } while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete 'while' block", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'while' block", File_Position(line, col), filename); } build_match(Token_Type::While, prev_stack_top); @@ -919,7 +919,7 @@ namespace chaiscript return true; } else { - throw Parse_Error("Incomplete conditions in 'for' loop", File_Position(line, col), filename); + throw Eval_Error("Incomplete conditions in 'for' loop", File_Position(line, col), filename); } } @@ -935,17 +935,17 @@ namespace chaiscript retval = true; if (!Char('(')) { - throw Parse_Error("Incomplete 'for' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'for' expression", File_Position(line, col), filename); } if (!(For_Guards() && Char(')'))) { - throw Parse_Error("Incomplete 'for' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'for' expression", File_Position(line, col), filename); } while (Eol()); if (!Block()) { - throw Parse_Error("Incomplete 'for' block", File_Position(line, col), filename); + throw Eval_Error("Incomplete 'for' block", File_Position(line, col), filename); } build_match(Token_Type::For, prev_stack_top); @@ -967,7 +967,7 @@ namespace chaiscript Statements(); if (!Char('}')) { - throw Parse_Error("Incomplete block", File_Position(line, col), filename); + throw Eval_Error("Incomplete block", File_Position(line, col), filename); } build_match(Token_Type::Block, prev_stack_top); @@ -1031,7 +1031,7 @@ namespace chaiscript Arg_List(); if (!Char(')')) { - throw Parse_Error("Incomplete function call", File_Position(line, col), filename); + throw Eval_Error("Incomplete function call", File_Position(line, col), filename); } build_match(Token_Type::Fun_Call, prev_stack_top); @@ -1040,7 +1040,7 @@ namespace chaiscript has_more = true; if (!(Expression() && Char(']'))) { - throw Parse_Error("Incomplete array access", File_Position(line, col), filename); + throw Eval_Error("Incomplete array access", File_Position(line, col), filename); } build_match(Token_Type::Array_Call, prev_stack_top); @@ -1063,7 +1063,7 @@ namespace chaiscript retval = true; if (!Id(true)) { - throw Parse_Error("Incomplete variable declaration", File_Position(line, col), filename); + throw Eval_Error("Incomplete variable declaration", File_Position(line, col), filename); } build_match(Token_Type::Var_Decl, prev_stack_top); @@ -1081,10 +1081,10 @@ namespace chaiscript if (Char('(')) { retval = true; if (!Expression()) { - throw Parse_Error("Incomplete expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete expression", File_Position(line, col), filename); } if (!Char(')')) { - throw Parse_Error("Missing closing parenthesis", File_Position(line, col), filename); + throw Eval_Error("Missing closing parenthesis", File_Position(line, col), filename); } } return retval; @@ -1102,7 +1102,7 @@ namespace chaiscript retval = true; Container_Arg_List(); if (!Char(']')) { - throw Parse_Error("Missing closing square bracket", File_Position(line, col), filename); + throw Eval_Error("Missing closing square bracket", File_Position(line, col), filename); } if ((prev_stack_top != match_stack.size()) && (match_stack.back()->children.size() > 0)) { if (match_stack.back()->children[0]->identifier == Token_Type::Value_Range) { @@ -1144,7 +1144,7 @@ namespace chaiscript while ((input_pos != input_end) && (*input_pos != '`')) { if (Eol()) { - throw Parse_Error("Carriage return in identifier literal", File_Position(line, col), filename); + throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename); } else { ++input_pos; @@ -1153,10 +1153,10 @@ namespace chaiscript } if (start == input_pos) { - throw Parse_Error("Missing contents of identifier literal", File_Position(line, col), filename); + throw Eval_Error("Missing contents of identifier literal", File_Position(line, col), filename); } else if (input_pos == input_end) { - throw Parse_Error("Incomplete identifier literal", File_Position(line, col), filename); + throw Eval_Error("Incomplete identifier literal", File_Position(line, col), filename); } ++col; @@ -1182,7 +1182,7 @@ namespace chaiscript retval = true; if (!Dot_Access()) { - throw Parse_Error("Incomplete negation expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete negation expression", File_Position(line, col), filename); } build_match(Token_Type::Negate, prev_stack_top); @@ -1191,7 +1191,7 @@ namespace chaiscript retval = true; if (!Dot_Access()) { - throw Parse_Error("Incomplete '!' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete '!' expression", File_Position(line, col), filename); } build_match(Token_Type::Not, prev_stack_top); @@ -1200,7 +1200,7 @@ namespace chaiscript retval = true; if (!Dot_Access()) { - throw Parse_Error("Incomplete '++' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete '++' expression", File_Position(line, col), filename); } build_match(Token_Type::Prefix, prev_stack_top); @@ -1209,7 +1209,7 @@ namespace chaiscript retval = true; if (!Dot_Access()) { - throw Parse_Error("Incomplete '--' expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete '--' expression", File_Position(line, col), filename); } build_match(Token_Type::Prefix, prev_stack_top); @@ -1244,7 +1244,7 @@ namespace chaiscript if (Symbol(">=", true) || Symbol(">", true) || Symbol("<=", true) || Symbol("<", true) || Symbol("==", true) || Symbol("!=", true)) { do { if (!Additive()) { - throw Parse_Error("Incomplete comparison expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete comparison expression", File_Position(line, col), filename); } } while (retval && (Symbol(">=", true) || Symbol(">", true) || Symbol("<=", true) || Symbol("<", true) || Symbol("==", true) || Symbol("!=", true))); @@ -1268,7 +1268,7 @@ namespace chaiscript if (Symbol("+", true) || Symbol("-", true)) { do { if (!Multiplicative()) { - throw Parse_Error("Incomplete math expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete math expression", File_Position(line, col), filename); } } while (retval && (Symbol("+", true) || Symbol("-", true))); @@ -1292,7 +1292,7 @@ namespace chaiscript if (Symbol("*", true) || Symbol("/", true) || Symbol("%", true)) { do { if (!Dot_Access()) { - throw Parse_Error("Incomplete math expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete math expression", File_Position(line, col), filename); } } while (retval && (Symbol("*", true) || Symbol("/", true) || Symbol("%", true))); @@ -1316,7 +1316,7 @@ namespace chaiscript if (Symbol(".")) { do { if (!Value()) { - throw Parse_Error("Incomplete dot notation", File_Position(line, col), filename); + throw Eval_Error("Incomplete dot notation", File_Position(line, col), filename); } } while (retval && Symbol(".")); @@ -1340,7 +1340,7 @@ namespace chaiscript if (Symbol("&&", true) || Symbol("||", true)) { do { if (!Comparison()) { - throw Parse_Error("Incomplete expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete expression", File_Position(line, col), filename); } } while (retval && (Symbol("&&", true) || Symbol("||", true))); @@ -1364,7 +1364,7 @@ namespace chaiscript if (Symbol(":")) { do { if (!Expression()) { - throw Parse_Error("Incomplete map pair", File_Position(line, col), filename); + throw Eval_Error("Incomplete map pair", File_Position(line, col), filename); } } while (retval && Symbol(":")); @@ -1389,7 +1389,7 @@ namespace chaiscript if (Symbol("..")) { retval = true; if (!Expression()) { - throw Parse_Error("Incomplete value range", File_Position(line, col), filename); + throw Eval_Error("Incomplete value range", File_Position(line, col), filename); } build_match(Token_Type::Value_Range, prev_stack_top); @@ -1418,7 +1418,7 @@ namespace chaiscript retval = true; if (Symbol("=", true) || Symbol(":=", true) || Symbol("+=", true) || Symbol("-=", true) || Symbol("*=", true) || Symbol("/=", true)) { if (!Equation()) { - throw Parse_Error("Incomplete equation", match_stack.back()); + throw Eval_Error("Incomplete equation", match_stack.back()); } build_match(Token_Type::Equation, prev_stack_top); @@ -1441,7 +1441,7 @@ namespace chaiscript has_more = false; if (Def()) { if (!saw_eol) { - throw Parse_Error("Two function definitions missing line separator", match_stack.back()); + throw Eval_Error("Two function definitions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1449,7 +1449,7 @@ namespace chaiscript } else if (If()) { if (!saw_eol) { - throw Parse_Error("Two function definitions missing line separator", match_stack.back()); + throw Eval_Error("Two function definitions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1457,7 +1457,7 @@ namespace chaiscript } else if (While()) { if (!saw_eol) { - throw Parse_Error("Two function definitions missing line separator", match_stack.back()); + throw Eval_Error("Two function definitions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1465,7 +1465,7 @@ namespace chaiscript } else if (For()) { if (!saw_eol) { - throw Parse_Error("Two function definitions missing line separator", match_stack.back()); + throw Eval_Error("Two function definitions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1473,7 +1473,7 @@ namespace chaiscript } else if (Return()) { if (!saw_eol) { - throw Parse_Error("Two expressions missing line separator", match_stack.back()); + throw Eval_Error("Two expressions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1481,7 +1481,7 @@ namespace chaiscript } else if (Break()) { if (!saw_eol) { - throw Parse_Error("Two expressions missing line separator", match_stack.back()); + throw Eval_Error("Two expressions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1489,7 +1489,7 @@ namespace chaiscript } else if (Equation()) { if (!saw_eol) { - throw Parse_Error("Two expressions missing line separator", match_stack.back()); + throw Eval_Error("Two expressions missing line separator", match_stack.back()); } has_more = true; retval = true; @@ -1525,7 +1525,7 @@ namespace chaiscript if (Statements()) { if (input_pos != input_end) { - throw Parse_Error("Unparsed input", File_Position(line, col), fname); + throw Eval_Error("Unparsed input", File_Position(line, col), fname); } else { build_match(Token_Type::File, 0); diff --git a/src/main.cpp b/src/main.cpp index 0e0bd6f..b62aa7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,12 +44,6 @@ int main(int argc, char *argv[]) { } } } - catch (chaiscript::Parse_Error &pe) { - std::cout << pe.reason << " in " << pe.filename << " at " << pe.position.line << ", " << pe.position.column << std::endl; - } - catch (chaiscript::Eval_Error &ee) { - std::cout << ee.reason << std::endl; - } catch (std::exception &e) { std::cout << e.what() << std::endl; } @@ -65,22 +59,6 @@ int main(int argc, char *argv[]) { try { dispatchkit::Boxed_Value val = chai.evaluate_file(argv[i]); } - catch (chaiscript::Parse_Error &pe) { - if (filename != std::string("__EVAL__")) { - std::cout << pe.reason << " in " << pe.filename << " at " << pe.position.line << ", " << pe.position.column << std::endl; - } - else { - std::cout << pe.reason << std::endl; - } - } - catch (chaiscript::Eval_Error &ee) { - if (filename != std::string("__EVAL__")) { - std::cout << ee.reason << " in '" << ee.location->filename << "' at " << ee.location->start.line << ", " << ee.location->start.column << std::endl; - } - else { - std::cout << ee.reason << std::endl; - } - } catch (std::exception &e) { std::cout << e.what() << std::endl; }