-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.hpp
180 lines (153 loc) · 2.75 KB
/
token.hpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef TOKEN_HEADER
#define TOKEN_HEADER
#include <string>
enum class TokenType {
// used for invalid/empty tokens
t_null = 0,
// generic, unassigned token type. Temporarily assigned to contextful tokens
t_generic,
// end of file
t_eof,
// formatting and blocks
t_semicolon = 100,
t_curly_left,
t_curly_right,
t_paren_left,
t_paren_right,
t_coma,
//reserved keywords
t_if = 200,
t_else,
t_while,
t_for,
t_break,
t_continue,
t_return,
// type names
t_bool = 300,
t_char,
t_int,
t_float,
t_pointer,
// identifiers
t_ident,
// literals
t_literal_bool = 400,
t_literal_char,
t_literal_string,
t_literal_int,
t_literal_float,
// partial literals. needed for first pass, but will never appear in output
t_literal_char_start, // excludes closing '
t_literal_string_start, // excludes closing "
// variable declaration and assignment
t_array, // []
t_assign,
t_bracket_left,
t_bracket_right,
// binary operators
t_plus = 500,
t_minus,
t_multiply,
t_divide,
t_modulo,
t_equal,
t_not_equal,
t_greater_than,
t_less_than,
t_gt_or_eq,
t_lt_or_eq,
t_and,
t_or,
t_bitwise_and,
t_bitwise_or,
// unary operators
t_not = 600,
t_bitwise_not,
t_reference,
t_dereference,
t_increment,
t_decrement,
t_cast,
// nonterminals
epsilon = 1000,
PROGRAM,
FUNCTION,
VAR_DECL,
F_DECL_ARGS,
ARG_DECLS,
BLOCK,
STATEMENTS,
STATEMENT,
STATEMENT_BODY,
EXPR, // 1010
E0,
E1,
E2,
E3,
E4,
E5,
E6,
E7,
E8,
E9, // 1020
E10,
E11,
E12,
U1,
S1,
B2,
B3,
B5,
B6,
O12, // 1030
CONDITION,
ELSE_BLOCKS,
FUNCTION_CALL,
CALL_ARGS,
CALL_ARGS_S,
LOOP,
FOR_LOOP,
FOR_STATEMENT,
FOR_CONDITION,
WHILE_LOOP, // 1040
TYPE_NAME,
L_VALUE,
R_VALUE,
LITERAL
};
bool is_terminal(TokenType n) {
return (int) n < 1000;
}
class Token {
private:
TokenType type_;
std::string lexeme_;
public:
Token() : type_(TokenType::t_null), lexeme_("") {}
Token(TokenType t) : type_(t), lexeme_("") {}
Token (TokenType t, std::string l) : type_(t), lexeme_(l) {}
~Token () {}
TokenType get_type() const {
return type_;
}
std::string get_lexeme() const {
return lexeme_;
}
bool is_type() const { // if this token names a type
return type_ == TokenType::t_char ||
type_ == TokenType::t_float ||
type_ == TokenType::t_int ||
type_ == TokenType::t_pointer;
}
bool is_numeric_literal() const {
return type_ == TokenType::t_literal_int ||
type_ == TokenType::t_literal_bool ||
type_ == TokenType::t_literal_char ||
type_ == TokenType::t_literal_float;
}
bool is_terminal() const {
return (int) type_ < 1000;
}
};
#endif