#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)
{
}
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;
+}
#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
-#include <exception>
#include "ilexer.h"
#include "exception.h"
}
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;
}
}
{
}
-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)
{
}
tok_type = typ;
}
-TokenType_T Token::type()
+TokenType_T Token::type() const
{
return tok_type;
}
tok_text = txt;
}
-std::string Token::text()
+std::string Token::text() const
{
return tok_text;
}
tok_line = ln;
}
-int Token::line()
+int Token::line() const
{
return tok_line;
}
tok_col = col;
}
-int Token::column()
+int Token::column() const
{
return tok_col;
}
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
}
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;
}
}
}
else
{
- throw std::exception();
+ Exception ex(-1,-1);
+ ex << "Failed to initialize parser. No lexer was provided.";
+ throw ex;
}
}
}
else
{
- throw std::exception();
+ Exception ex( lookaheadToken(1) );
+ ex << "Unexpected token. Expected " << type << ", received " << lookaheadType(1);
+ throw ex;
}
}