-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.cpp
193 lines (179 loc) · 5.45 KB
/
Token.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
#include "lib/Token.hpp"
// Judge whether the id is reserved word.
//
// @param id The ID string need to be judged as reserved word or not
// @return true if the id is a reserved word
bool is_resd_word(const std::string &id) {
if (resd_words.find(id) == resd_words.end())
return false;
return true;
}
// Judge whether the type is const value.
//
// @param tt The token type need to be judged as const value type or not
// @return true if the type is a const value's type
bool is_cval_type(const TokenType &tt) {
if (tt == CINT || tt == CBOOL || tt == CCHAR || tt == CFLOAT)
return true;
return false;
}
// Judge whether the token type is control token type.
//
// @param tt The token type need to be judged as control type or not
// @return true if the type is a control token type
bool is_ctrl_type(const TokenType &tt) {
if (tt == UNKNOWN || tt == ERROR || tt == NONE || tt == ENDFILE)
return true;
return false;
}
// Get the specifical type name by token type.
//
// @param tt The token's specific type, and use this to get the token type's string
// @return the token type's string
std::string get_type_name(const TokenType &tt) {
for (auto item : type_name)
if (item.first == tt)
return item.second;
return "";
}
Token &Token::operator=(const Token &t) {
copyUnion(t);
name = t.name;
type = t.type;
return *this;
}
Token &Token::operator=(int i) {
ival = i;
type = CINT;
return *this;
}
Token &Token::operator=(bool b) {
bval = b;
type = CBOOL;
return *this;
}
Token &Token::operator=(char c) {
cval = c;
type = CCHAR;
return *this;
}
Token &Token::operator=(double d) {
dval = d;
type = CFLOAT;
return *this;
}
void Token::copyUnion(const Token &t) {
switch (t.type) {
case CINT : ival = t.ival; break;
case CBOOL : bval = t.bval; break;
case CCHAR : cval = t.cval; break;
case CFLOAT : dval = t.dval; break;
case ID : sptr = t.sptr; break;
default: break;
}
}
// Get the specific Token object using token type (tt) and the string object which
// contains the token's information.
std::shared_ptr<Token> make_token(TokenType tt, std::string str) {
if (tt == ID) {
auto it = resd_words.find(str);
// If the token is regard as a ID, need to judge whether it is a CBOOL,
// such as "true" or "false". Besides, if need to reserve other key words,
// such as "nullptr", "NULL", also could judge at here.
if (it == resd_words.end()) {
if (str == "true") {
auto tk = std::make_shared<Token>(CBOOL, str);
*tk = true;
return tk;
} else if (str == "false") {
auto tk = std::make_shared<Token>(CBOOL, str);
*tk = false;
return tk;
}
return std::make_shared<Token>(tt, str);
} else { // If is a keyword token
tt = resd_words[str];
return std::make_shared<Token>(tt, str);
}
} else if (tt == CNUM) {
bool is_float = false;
for (auto x : str) {
if (x == '.') {
is_float = true;
break;
}
}
if (is_float) {
auto tk = std::make_shared<Token>(CFLOAT, str);
*tk = std::stod(str);
return tk;
} else {
auto tk = std::make_shared<Token>(CINT, str);
*tk = std::stoi(str);
return tk;
}
} else if (tt == CCHAR) {
auto tk = std::make_shared<Token>(CCHAR, str);
if (str[1] != '\\')
*tk = str[1];
else {
switch (str[2]) {
case 'a':
*tk = '\a';
break;
case 'b':
*tk = '\b';
break;
case 'f':
*tk = '\f';
break;
case 'n':
*tk = '\n';
break;
case 'r':
*tk = '\r';
break;
case 't':
*tk = '\t';
break;
case 'v':
*tk = '\v';
break;
case '\\':
*tk = '\\';
break;
case '\?':
*tk = '\?';
break;
case '\"':
*tk = '\"';
break;
case '0':
*tk = '\0';
break;
default:
break;
}
}
return tk;
} else {
return std::make_shared<Token>(tt, str);
}
}
// Overload the operator '<<', use this operator to print Token information formatly
std::ostream &operator<<(std::ostream &os, const Token &tk) {
if (tk.type == ID) {
os << std::left << std::setw(10) << tk.name << std::right << std::setw(10)
<< get_type_name(tk.type) << std::setw(22) << tk.sptr << std::endl;
return os;
}
os << std::left << std::setw(10) << tk.name << std::right << std::setw(10)
<< get_type_name(tk.type) << std::setw(10) << tk.type << std::endl;
return os;
}
std::string token2string(const Token &tt) {
for (auto var : TokenGrammaSymVec)
if (var.first == tt.type)
return var.second;
return "";
}