#ifndef __TOKEN_H #define __TOKEN_H #define FOREACH_TOKEN(FUNCTION) \ FUNCTION(LEFT_PAREN) \ FUNCTION(RIGHT_PAREN) \ FUNCTION(LEFT_BRACE) \ FUNCTION(RIGHT_BRACE) \ FUNCTION(COMMA) \ FUNCTION(DOT) \ FUNCTION(MINUS) \ FUNCTION(PLUS) \ FUNCTION(SEMICOLON) \ FUNCTION(SLASH) \ FUNCTION(STAR) \ FUNCTION(BANG) \ FUNCTION(BANG_EQUAL) \ FUNCTION(EQUAL) \ FUNCTION(EQUAL_EQUAL) \ FUNCTION(GREATER) \ FUNCTION(GREATER_EQUAL) \ FUNCTION(LESS) \ FUNCTION(LESS_EQUAL) \ FUNCTION(IDENTIFIER) \ FUNCTION(STRING) \ FUNCTION(NUMBER) \ FUNCTION(AND) \ FUNCTION(CLASS) \ FUNCTION(ELSE) \ FUNCTION(FUN) \ FUNCTION(FOR) \ FUNCTION(IF) \ FUNCTION(NIL) \ FUNCTION(OR) \ FUNCTION(PRINT) \ FUNCTION(RETURN) \ FUNCTION(SUPER) \ FUNCTION(THIS) \ FUNCTION(VAR) \ FUNCTION(WHILE) \ FUNCTION(TRUE) \ FUNCTION(FALSE) \ FUNCTION(EOFF) #define GENERATE_ENUM(ENUM) ENUM, #define GENERATE_STRING(STRING) #STRING, #define N_KEYWORDS 16 typedef enum { FOREACH_TOKEN(GENERATE_ENUM) } token_type_e; static const char *TOKEN_STRING[] = { FOREACH_TOKEN(GENERATE_STRING) }; static const char *KEYWORD_STRING_MAPPING[N_KEYWORDS] = { "and", "class", "else", "false", "for", "fun", "if", "nil", "or", "print", "return", "super", "this", "true", "var", "while" }; static const token_type_e KEYWORD_TYPE_MAPPING[N_KEYWORDS] = { AND, CLASS, ELSE, FALSE, FOR, FUN, IF, NIL, OR, PRINT, RETURN, SUPER, THIS, TRUE, VAR, WHILE }; class Token { private: token_type_e m_type; char *m_lexeme; // literal unsigned int m_line; char *m_string; public: void init(token_type_e type, char *lexeme, unsigned int line); char *to_string(void); void clean(void); }; #endif