From: Matthias Braun Date: Wed, 31 Jan 2007 16:23:09 +0000 (+0000) Subject: improve error handling in lisp parser X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=78d47dc66f5ada145de0700b84cefb7334befe73;p=supertux.git improve error handling in lisp parser SVN-Revision: 4756 --- diff --git a/src/lisp/parser.cpp b/src/lisp/parser.cpp index cfc30e8f6..526d57a3c 100644 --- a/src/lisp/parser.cpp +++ b/src/lisp/parser.cpp @@ -63,6 +63,7 @@ Parser::parse(const std::string& filename) IFileStreambuf ins(filename); std::istream in(&ins); + this->filename = filename; if(!in.good()) { std::stringstream msg; msg << "Parser problem: Couldn't open file '" << filename << "'."; @@ -94,22 +95,25 @@ Parser::parse(std::istream& stream) return result; } +void +Parser::parse_error(const char* msg) +{ + std::stringstream emsg; + emsg << "Parse Error at '" << filename << "' line " << lexer->getLineNumber() + << ": " << msg; + throw std::runtime_error(emsg.str()); +} + Lisp* Parser::read() { Lisp* result; switch(token) { case Lexer::TOKEN_EOF: { - std::stringstream msg; - msg << "Parse Error at line " << lexer->getLineNumber() << ": " - << "Unexpected EOF."; - throw std::runtime_error(msg.str()); + parse_error("Unexpected EOF."); } case Lexer::TOKEN_CLOSE_PAREN: { - std::stringstream msg; - msg << "Parse Error at line " << lexer->getLineNumber() << ": " - << "Unexpected ')'."; - throw std::runtime_error(msg.str()); + parse_error("Unexpected ')'."); } case Lexer::TOKEN_OPEN_PAREN: { result = new Lisp(Lisp::TYPE_CONS); @@ -126,7 +130,7 @@ Parser::read() // evaluate translation function (_ str) in place here token = lexer->getNextToken(); if(token != Lexer::TOKEN_STRING) - throw std::runtime_error("Expected string after '(_'"); + parse_error("Expected string after '(_'"); result = new Lisp(Lisp::TYPE_STRING); if(dictionary) { @@ -140,7 +144,7 @@ Parser::read() } token = lexer->getNextToken(); if(token != Lexer::TOKEN_CLOSE_PAREN) - throw std::runtime_error("Expected ')' after '(_ string'"); + parse_error("Expected ')' after '(_ string'"); break; } diff --git a/src/lisp/parser.hpp b/src/lisp/parser.hpp index fc808b18e..5f0ad0f4e 100644 --- a/src/lisp/parser.hpp +++ b/src/lisp/parser.hpp @@ -43,9 +43,11 @@ public: Lisp* parse(std::istream& stream); private: + void parse_error(const char* msg); Lisp* read(); Lexer* lexer; + std::string filename; TinyGetText::DictionaryManager* dictionary_manager; TinyGetText::Dictionary* dictionary; Lexer::TokenType token;