made the token type less repetitive
This commit is contained in:
@ -1,60 +1,50 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
enum class TokenType {
|
namespace lexer {
|
||||||
ILLEGAL,
|
|
||||||
EOF_,
|
|
||||||
|
|
||||||
IDENT,
|
// X-macro list of token types and their string representations
|
||||||
INT,
|
#define TOKEN_LIST \
|
||||||
|
X(ILLEGAL, "ILLEGAL") \
|
||||||
|
X(EOF_, "EOF") \
|
||||||
|
X(IDENT, "IDENT") \
|
||||||
|
X(INT, "INT") \
|
||||||
|
X(ASSIGN, "=") \
|
||||||
|
X(PLUS, "+") \
|
||||||
|
X(COMMA, ",") \
|
||||||
|
X(SEMICOLON, ";") \
|
||||||
|
X(LPAREN, "(") \
|
||||||
|
X(RPAREN, ")") \
|
||||||
|
X(LBRACE, "{") \
|
||||||
|
X(RBRACE, "}") \
|
||||||
|
X(FUNCTION, "FUNCTION") \
|
||||||
|
X(LET, "LET")
|
||||||
|
|
||||||
ASSIGN,
|
// Define the TokenType enum using the X-macro
|
||||||
PLUS,
|
enum class TokenType {
|
||||||
|
#define X(name, str) name,
|
||||||
|
TOKEN_LIST
|
||||||
|
#undef X
|
||||||
|
};
|
||||||
|
|
||||||
COMMA,
|
// Array mapping enum values to their string representations
|
||||||
SEMICOLON,
|
static constexpr std::
|
||||||
|
array<std::string_view, static_cast<size_t>(TokenType::LET) + 1>
|
||||||
|
tokenTypeStrings = {
|
||||||
|
#define X(name, str) str,
|
||||||
|
TOKEN_LIST
|
||||||
|
#undef X
|
||||||
|
};
|
||||||
|
|
||||||
LPAREN,
|
// Stream insertion operator using the lookup array
|
||||||
RPAREN,
|
inline std::ostream& operator<<(std::ostream& os, TokenType type) {
|
||||||
LBRACE,
|
auto idx = static_cast<size_t>(type);
|
||||||
RBRACE,
|
if (idx < tokenTypeStrings.size())
|
||||||
|
return os << tokenTypeStrings[idx];
|
||||||
FUNCTION,
|
|
||||||
LET
|
|
||||||
};
|
|
||||||
|
|
||||||
inline std::ostream& operator<<(std::ostream& os, TokenType tt) {
|
|
||||||
switch (tt) {
|
|
||||||
case TokenType::ILLEGAL:
|
|
||||||
return os << "ILLEGAL";
|
|
||||||
case TokenType::EOF_:
|
|
||||||
return os << "EOF";
|
|
||||||
case TokenType::IDENT:
|
|
||||||
return os << "IDENT";
|
|
||||||
case TokenType::INT:
|
|
||||||
return os << "INT";
|
|
||||||
case TokenType::ASSIGN:
|
|
||||||
return os << "=";
|
|
||||||
case TokenType::PLUS:
|
|
||||||
return os << "+";
|
|
||||||
case TokenType::COMMA:
|
|
||||||
return os << ",";
|
|
||||||
case TokenType::SEMICOLON:
|
|
||||||
return os << ";";
|
|
||||||
case TokenType::LPAREN:
|
|
||||||
return os << "(";
|
|
||||||
case TokenType::RPAREN:
|
|
||||||
return os << ")";
|
|
||||||
case TokenType::LBRACE:
|
|
||||||
return os << "{";
|
|
||||||
case TokenType::RBRACE:
|
|
||||||
return os << "}";
|
|
||||||
case TokenType::FUNCTION:
|
|
||||||
return os << "FUNCTION";
|
|
||||||
case TokenType::LET:
|
|
||||||
return os << "LET";
|
|
||||||
default:
|
|
||||||
return os << "Unknown";
|
return os << "Unknown";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
} // namespace lexer
|
||||||
|
Reference in New Issue
Block a user