-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.h
More file actions
91 lines (77 loc) · 1.92 KB
/
token.h
File metadata and controls
91 lines (77 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef TOKEN_H
#define TOKEN_H
#include <string>
#include <unordered_map>
namespace AR437 {
// Token types for the AR language
enum class TokenType {
// Literals
NUMBER,
STRING,
BOOLEAN,
IDENTIFIER,
// Keywords
LET, // variable declaration
FUNC, // function declaration
IF, // conditional
ELSE, // else clause
WHILE, // loop
FOR, // for loop
RETURN, // return statement
TRUE, // boolean true
FALSE, // boolean false
NULL_TOK, // null value
DISPLAY, // display statement
READ, // read statement
// Operators
PLUS, // +
MINUS, // -
MULTIPLY, // *
DIVIDE, // /
MODULO, // %
ASSIGN, // =
EQUAL, // ==
NOT_EQUAL, // !=
LESS, // <
LESS_EQUAL, // <=
GREATER, // >
GREATER_EQUAL, // >=
AND, // &&
OR, // ||
NOT, // !
// Delimiters
SEMICOLON, // ;
COMMA, // ,
LPAREN, // (
RPAREN, // )
LBRACE, // {
RBRACE, // }
LBRACKET, // [
RBRACKET, // ]
// Special
NEWLINE, // \n
EOF_TOK, // End of file
INVALID // Invalid token
};
struct Token {
TokenType type;
std::string value;
int line;
int column;
Token(TokenType t = TokenType::INVALID, const std::string& v = "", int l = 1, int c = 1)
: type(t), value(v), line(l), column(c) {}
std::string toString() const;
};
class TokenUtil {
public:
static std::string tokenTypeToString(TokenType type);
static TokenType getKeywordType(const std::string& word);
static bool isKeyword(const std::string& word);
static bool isOperator(char c);
static bool isDelimiter(char c);
private:
static std::unordered_map<std::string, TokenType> keywords;
static void initializeKeywords();
};
} // namespace AR437
#endif // TOKEN_H