Initial commit
[minnow.git] / src / minnow / lexer.hpp
blob84cad995284e0202329db6fc4f358ee67e15d103
1 #ifndef LEXER_HPP
2 #define LEXER_HPP
4 #include <stdexcept>
5 #include <sstream>
6 #include <string>
7 #include <vector>
9 class TokenType {
10 public:
11 enum Type { Id, Num, Char, String, Def, Bool, Symbol, EOL};
14 struct FilePos {
15 std::string filename;
16 unsigned int lineNumber, colStart, colEnd;
18 FilePos& operator=( const FilePos& newPos ) {
19 filename = newPos.filename;
20 lineNumber = newPos.lineNumber;
21 colStart = newPos.colStart;
22 colEnd = newPos.colEnd;
24 return *this;
28 struct Token {
29 TokenType::Type tokenType;
30 std::string data;
31 FilePos pos;
33 Token () { }
34 Token(TokenType::Type tType, std::string tData) : tokenType(tType), data(tData) { }
35 Token(TokenType::Type tType, std::string tData, std::string tFilename, int tLineNumber, int tColStart, int tColEnd) :
36 tokenType(tType), data(tData) {
37 pos.filename = tFilename;
38 pos.lineNumber = tLineNumber;
39 pos.colStart = tColStart;
40 pos.colEnd = tColEnd;
44 class CompilerException : public std::runtime_error {
45 public:
46 CompilerException(const std::string &msg, const int lineNumber, const int colStart, const int colEnd, const std::string &filename)
47 : std::runtime_error(build_message(msg, lineNumber, colStart, colEnd, filename)) {
50 CompilerException(const std::string &msg, const Token *token)
51 : std::runtime_error(build_message(msg, token->pos.lineNumber, token->pos.colStart, token->pos.colEnd, token->pos.filename)) {
54 CompilerException(const std::string &msg, const FilePos &pos)
55 : std::runtime_error(build_message(msg, pos.lineNumber, pos.colStart, pos.colEnd, pos.filename)) {
58 CompilerException(const std::string &msg)
59 : std::runtime_error(msg) {
62 std::string build_message(const std::string &msg, const int lineNumber, const int colStart, const int colEnd, const std::string &filename) const {
63 std::ostringstream msg_builder;
65 msg_builder << msg << " at line " << lineNumber << " column " << colStart << " in '" << filename << "'";
67 return msg_builder.str();
71 std::vector<Token*> tokenize(std::string sourceText, std::string filename);
73 #endif //LEXER_HPP