From f54599839bb91b6219b4c0a5063eed0380b880a5 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Tue, 13 Mar 2012 12:53:07 -0400 Subject: [PATCH] Cleaned up Exception interface and marked Token methods as const --- source/exception/exception.cpp | 21 +++++++++++++++++++-- source/exception/exception.h | 27 ++++++++++++++++----------- source/lexer/ilexer.cpp | 5 +---- source/lexer/token/token.cpp | 10 +++++----- source/lexer/token/token.h | 13 +++++-------- source/parser/btparser/btparser.cpp | 7 ++----- source/parser/llkparser/llkparser.cpp | 8 ++++++-- 7 files changed, 54 insertions(+), 37 deletions(-) diff --git a/source/exception/exception.cpp b/source/exception/exception.cpp index f95f7ce..c0c47cb 100644 --- a/source/exception/exception.cpp +++ b/source/exception/exception.cpp @@ -1,6 +1,10 @@ #include #include "exception.h" +Exception::Exception(const Token& tok) throw() : std::exception(), ex_line(tok.line()), ex_column(tok.column()) +{ +} + Exception::Exception(int line, int column) throw() : std::exception(), ex_line(line), ex_column(column) { } @@ -13,13 +17,26 @@ const char* Exception::what() const throw() return oss.str().c_str(); } -void Exception::setMessage(std::string msg) throw() +void Exception::message(const std::string& msg) throw() { ex_msg = msg; } -std::string& Exception::message(void) throw() +const std::string& Exception::message(void) const throw() { return ex_msg; } +Exception& operator<< (Exception& ex, const std::string& rhs) +{ + ex.ex_msg.append(rhs); + return ex; +} + +Exception& operator<< (Exception& ex, unsigned int rhs) +{ + std::stringstream ss; + ss << rhs; + ex.ex_msg.append( ss.str() ); + return ex; +} diff --git a/source/exception/exception.h b/source/exception/exception.h index 93679d2..2c1b9b1 100644 --- a/source/exception/exception.h +++ b/source/exception/exception.h @@ -1,21 +1,26 @@ #ifndef EXCEPTION_H -#define EXCEPTION_H +#define EXCEPTION_H #include #include +#include "token.h" class Exception : public std::exception { - protected: - int ex_line; - int ex_column; - std::string ex_msg; - public: - Exception(int line, int column) throw(); - virtual ~Exception() throw() {}; - virtual const char* what() const throw(); - void setMessage(std::string msg) throw(); - std::string& message(void) throw(); + protected: + int ex_line; + int ex_column; + std::string ex_msg; + public: + Exception(const Token& tok) throw(); + Exception(int line, int column) throw(); + virtual ~Exception() throw() {}; + virtual const char* what() const throw(); + void message(const std::string& msg) throw(); + const std::string& message(void) const throw(); + + friend Exception& operator<< (Exception& ex, const std::string& rhs); + friend Exception& operator<< (Exception& ex, unsigned int rhs); }; #endif diff --git a/source/lexer/ilexer.cpp b/source/lexer/ilexer.cpp index 66288f7..bd6c333 100644 --- a/source/lexer/ilexer.cpp +++ b/source/lexer/ilexer.cpp @@ -1,4 +1,3 @@ -#include #include "ilexer.h" #include "exception.h" @@ -45,10 +44,8 @@ void ILexer::match(char x) { } else { - ostringstream oss; - oss << "Unexpected character. Expected " << x << ", received " << current << "."; Exception ex(line,column); - ex.setMessage(oss.str()); + ex << "Unexpected character. Expected " << x << ", received " << current << "."; throw ex; } } diff --git a/source/lexer/token/token.cpp b/source/lexer/token/token.cpp index ae8c245..8a4008d 100644 --- a/source/lexer/token/token.cpp +++ b/source/lexer/token/token.cpp @@ -5,7 +5,7 @@ Token::Token() : tok_type(EOF), tok_text(""), tok_line(-1), tok_col(-1) { } -Token::Token(TokenType_T ttype, std::string ttext, int line, int col) : tok_type(ttype), tok_text(ttext), tok_line(line), tok_col(col) +Token::Token(TokenType_T ttype, const std::string& ttext, int line, int col) : tok_type(ttype), tok_text(ttext), tok_line(line), tok_col(col) { } @@ -18,7 +18,7 @@ void Token::type(TokenType_T typ) tok_type = typ; } -TokenType_T Token::type() +TokenType_T Token::type() const { return tok_type; } @@ -28,7 +28,7 @@ void Token::text(std::string txt) tok_text = txt; } -std::string Token::text() +std::string Token::text() const { return tok_text; } @@ -38,7 +38,7 @@ void Token::line(int ln) tok_line = ln; } -int Token::line() +int Token::line() const { return tok_line; } @@ -48,7 +48,7 @@ void Token::column(int col) tok_col = col; } -int Token::column() +int Token::column() const { return tok_col; } diff --git a/source/lexer/token/token.h b/source/lexer/token/token.h index 8c5f0b6..7550bd4 100644 --- a/source/lexer/token/token.h +++ b/source/lexer/token/token.h @@ -15,19 +15,16 @@ class Token public: Token(); Token(TokenType_T ttype, int line, int col); - Token(TokenType_T ttype, std::string ttext, int line, int col); + Token(TokenType_T ttype, const std::string& ttext, int line, int col); void type(TokenType_T typ); - TokenType_T type(); - + TokenType_T type() const; void text(std::string txt); - std::string text(); - + std::string text() const; void line(int ln); - int line(); - + int line() const; void column(int col); - int column(); + int column() const; }; #endif diff --git a/source/parser/btparser/btparser.cpp b/source/parser/btparser/btparser.cpp index a913848..3bf3e3c 100644 --- a/source/parser/btparser/btparser.cpp +++ b/source/parser/btparser/btparser.cpp @@ -49,11 +49,8 @@ void BTParser::match(TokenType_T type) } else { - Token& tok = lookaheadToken(1); - ostringstream oss; - oss << "Unexpected token type. Expected " << type << ", received " << tok.type() << "."; - Exception ex( tok.line(), tok.column() ); - ex.setMessage(oss.str()); + Exception ex( lookaheadToken(1) ); + ex << "Unexpected token type. Expected " << type << ", received " << lookaheadToken(1).type() << "."; throw ex; } } diff --git a/source/parser/llkparser/llkparser.cpp b/source/parser/llkparser/llkparser.cpp index 8412bc3..ded1fd1 100644 --- a/source/parser/llkparser/llkparser.cpp +++ b/source/parser/llkparser/llkparser.cpp @@ -9,7 +9,9 @@ LLKParser::LLKParser(int k_val, ILexer* lxer) : k(k_val), next(0), lexer(lxer) } else { - throw std::exception(); + Exception ex(-1,-1); + ex << "Failed to initialize parser. No lexer was provided."; + throw ex; } } @@ -38,7 +40,9 @@ void LLKParser::match(TokenType_T type) } else { - throw std::exception(); + Exception ex( lookaheadToken(1) ); + ex << "Unexpected token. Expected " << type << ", received " << lookaheadType(1); + throw ex; } } -- 2.52.0