]> git.mdlowis.com Git - archive/parse-utils.git/commitdiff
Cleaned up Exception interface and marked Token methods as const
authorMike D. Lowis <mike@mdlowis.com>
Tue, 13 Mar 2012 16:53:07 +0000 (12:53 -0400)
committerMike D. Lowis <mike@mdlowis.com>
Tue, 13 Mar 2012 16:53:07 +0000 (12:53 -0400)
source/exception/exception.cpp
source/exception/exception.h
source/lexer/ilexer.cpp
source/lexer/token/token.cpp
source/lexer/token/token.h
source/parser/btparser/btparser.cpp
source/parser/llkparser/llkparser.cpp

index f95f7ce8d6c79e6b1b01f8fa18c8f028c0ffc091..c0c47cb1b2e6f3a14e7120bd0665059f6ee6df77 100644 (file)
@@ -1,6 +1,10 @@
 #include <sstream>
 #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;
+}
index 93679d29713095189303cd0dcc2b2e467148a2a9..2c1b9b1f92cdc15d5fc08854f8d9b86798c8a8de 100644 (file)
@@ -1,21 +1,26 @@
 #ifndef EXCEPTION_H
-#define EXCEPTION_H 
+#define EXCEPTION_H
 
 #include <exception>
 #include <string>
+#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
index 66288f705228071d66e03dc108c8994149b89775..bd6c3331005a008a93925e5b06d81a3258d6629c 100644 (file)
@@ -1,4 +1,3 @@
-#include <exception>
 #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;
     }
 }
index ae8c2459299bd6a765f60e47bdf6dc6395d918ab..8a4008d79c1cfb0df746e111cb217269f2f937f5 100644 (file)
@@ -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;
 }
index 8c5f0b697d643cd6a64f149c1d1f0837b3afd075..7550bd43c08f6a072e201c4aa5cb0dc8ba6c08cd 100644 (file)
@@ -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
index a9138483d81c3ec48bef5a3362b1b7e84618deef..3bf3e3c56ad52f25432d9dda74a76c5396ee2bdf 100644 (file)
@@ -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;
     }
 }
index 8412bc3adafc373807aaebf2b8174ef84b1027c4..ded1fd134872a9ea57769ef4125c1f71797915a5 100644 (file)
@@ -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;
     }
 }