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