-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToken.cpp
More file actions
365 lines (310 loc) · 11.7 KB
/
Token.cpp
File metadata and controls
365 lines (310 loc) · 11.7 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <sstream>
#include "Token.h"
#include <initializer_list>
#include <exception>
class Character {
int ch;
public:
int get() const { return ch; }
bool isEOF() const { return ch == std::istream::traits_type::eof(); }
bool isDigit() const { return ch >= '0' && ch <= '9'; }
bool isAlpha() const { return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'; }
bool is(std::initializer_list<int> targetList) const { for (int i : targetList) { if (ch == i) return true; } return false; }
Character(int ch) :ch(ch) {}
};
//该函数将在自动机解析保留字或标识符结束时调用
Token *parseReservedOrIdentifier(const std::string & str) {
using Reserved = TokenReserved::Reserved;
switch (str.length()) { //这里首先根据字符串长短分类,减少搜索次数
case 2:
if (str == "if") return new TokenReserved(Reserved::If);
if (str == "do") return new TokenReserved(Reserved::Do);
break;
case 3:
if (str == "var") return new TokenReserved(Reserved::Var);
if (str == "end") return new TokenReserved(Reserved::End);
if (str == "odd") return new TokenReserved(Reserved::Odd);
break;
case 4:
if (str == "then") return new TokenReserved(Reserved::Then);
if (str == "call") return new TokenReserved(Reserved::Call);
if (str == "read") return new TokenReserved(Reserved::Read);
break;
case 5:
if (str == "const") return new TokenReserved(Reserved::Const);
if (str == "begin") return new TokenReserved(Reserved::Begin);
if (str == "while") return new TokenReserved(Reserved::While);
if (str == "write") return new TokenReserved(Reserved::Write);
break;
case 9:
if (str == "procedure") return new TokenReserved(Reserved::Procedure);
break;
}
return new TokenIdentifier(str);
}
Token *parseOperator(const std::string &str) {
using Operator = TokenOperator::Operator;
switch (str.length()) {
case 1:
if (str == "+") return new TokenOperator(Operator::Plus);
if (str == "-") return new TokenOperator(Operator::Minus);
if (str == "*") return new TokenOperator(Operator::Multiplication);
if (str == "/") return new TokenOperator(Operator::Division);
if (str == "<") return new TokenOperator(Operator::Less);
if (str == ">") return new TokenOperator(Operator::Greater);
if (str == "#") return new TokenOperator(Operator::Pound);
if (str == "=") return new TokenOperator(Operator::Equal);
break;
case 2:
if (str == "<=") return new TokenOperator(Operator::LessEqual);
if (str == ">=") return new TokenOperator(Operator::GreaterEqual);
if (str == ":=") return new TokenOperator(Operator::Walrus);
break;
}
return new TokenError(str, "Unknown Operator");
}
Token *parseDelimiter(const std::string &str) {
using Delimiter = TokenDelimiter::Delimiter;
if (str == "(") return new TokenDelimiter(Delimiter::LeftBracket);
if (str == ")") return new TokenDelimiter(Delimiter::RightBracket);
if (str == ",") return new TokenDelimiter(Delimiter::Comma);
if (str == ";") return new TokenDelimiter(Delimiter::Semicolons);
if (str == ".") return new TokenDelimiter(Delimiter::Period);
return new TokenError(str, "Unknown Delimiter");
}
Token * Token::fromStream(std::istream &input){
std::ostringstream buffer;
int state = 0;
while (true) {
const Character nextChar = input.peek();
switch (state) {
case 0: { //起始状态
if (nextChar.isAlpha()) //是字母,转移到识别标识符和保留字的分支
state = 1;
else if (nextChar.isDigit()) //是数字,转移到识别数字的分支
state = 2;
else if (nextChar.is({ '+','-','*','/','<','>','#','=',':' }))
state = 3;
else if (nextChar.is({ ' ', '\t' }))
state = 5;
else if (nextChar.is({ '\r' }))
state = 6;
else if (nextChar.is({ '\n' }))
state = 7;
else if (nextChar.is({ '(', ')', ',', ';', '.' }))
state = 8;
else if (nextChar.isEOF())
return new TokenEnd();
else //无法接受的字符,将其吞掉后再返回错误
state = 9;
} break;
case 1: { //接收标识符和保留字状态
if (nextChar.isAlpha() || nextChar.isDigit())//如果下一个字符仍然可和当前输入组成标识符或保留字,优先继续输入
break;
return parseReservedOrIdentifier(buffer.str()); //至此,我们已经不能继续追加下一个字符,考虑将当前输入解析成保留字或者标识符
} break;
case 2: { // 接收无符号数字状态
if (nextChar.isDigit())
break;
return new TokenUnsigned(buffer.str());
}break;
case 3: {
if (nextChar.is({ '=' })) { // 如果下一个字符是双字符运算符的第二个字符,就转到状态4继续接收
state = 4;
break;
}
return parseOperator(buffer.str());
} break;
case 4: return parseOperator(buffer.str()); //运算符最多两个字符,到这里直接接收就行
case 5: {
if (nextChar.is({ ' ', '\t' }))
break;
return new TokenWhitespace(buffer.str());
} break;
case 6: {
if (nextChar.is({ '\n' })) {
state = 7;
break;
}
return new TokenError(buffer.str(), "Incomplete line breaks");
} break;
case 7: return new TokenLineWrap(buffer.str());
case 8: return parseDelimiter(buffer.str());
case 9: return new TokenError(buffer.str(), "Unexcepted Character");
} // switch
// 如果能运行到这里,说明我们的自动机接受输入流的下一个字符
buffer.put(input.get()); //从输入流接收一个字符
}
}
bool Token::operator==(Token &other) const
{
return other.type == this->type && other.raw == this->raw;
}
const std::string TokenReserved::getStringByReserved(Reserved reserved)
{
switch (reserved) {
case Reserved::Const: return "const";
case Reserved::Var: return "var";
case Reserved::Procedure: return "procedure";
case Reserved::Begin: return "begin";
case Reserved::End: return "end";
case Reserved::Odd: return "odd";
case Reserved::If: return "if";
case Reserved::Then: return "then";
case Reserved::Call: return "call";
case Reserved::While: return "while";
case Reserved::Do: return "do";
case Reserved::Read: return "read";
case Reserved::Write: return "write";
default: throw std::exception("Unknown Reserved");
}
}
std::ostream &TokenReserved::printToStream(std::ostream &out) const
{
return out << R"({"type":"RESERVED","reserved":")" << getStringByReserved(reserved) << R"("})";
}
bool TokenReserved::match(const Token &other) const
{
auto o = reinterpret_cast<const TokenReserved *>(&other);
return o != nullptr && this->reserved == o->reserved;
}
std::ostream &TokenReserved::simplePrintToStream(std::ostream &out) const
{
return out << "RESERVED:'" << getStringByReserved(reserved) << "'";
}
const std::string TokenOperator::getStringByOperator(Operator op)
{
switch (op) {
case Operator::Plus: return "+";
case Operator::Minus: return "-";
case Operator::Multiplication: return "*";
case Operator::Division: return "/";
case Operator::Less: return "<";
case Operator::LessEqual: return "<=";
case Operator::Greater: return ">";
case Operator::GreaterEqual: return ">=";
case Operator::Pound: return "#";
case Operator::Equal: return "=";
case Operator::Walrus: return ":=";
default: throw std::exception("Unknown Operator");
}
}
std::ostream &TokenOperator::printToStream(std::ostream &out) const
{
return out << R"({"type":"OPERATOR","operator":")" << getStringByOperator(op) << R"("})";
}
bool TokenOperator::match(const Token &other) const
{
auto o = dynamic_cast<const TokenOperator *>(&other);
return o != nullptr && this->op == o->op;
}
std::ostream &TokenOperator::simplePrintToStream(std::ostream &out) const
{
return out << "OPERATOR:'" << getStringByOperator(op)<<"'";
}
const std::string TokenDelimiter::getStringByDelimiter(Delimiter delimiter)
{
switch (delimiter) {
case Delimiter::LeftBracket: return "(";
case Delimiter::RightBracket: return ")";
case Delimiter::Comma: return ",";
case Delimiter::Semicolons: return ";";
case Delimiter::Period: return ".";
default: throw std::exception("Unknown Delimiter");
}
}
std::ostream &TokenDelimiter::printToStream(std::ostream &out) const
{
return out << R"({"type":"DELIMITER","delimiter":")" << getStringByDelimiter(delimiter) << R"("})";
}
bool TokenDelimiter::match(const Token &other) const
{
auto o = dynamic_cast<const TokenDelimiter *>(&other);
return o != nullptr && this->delimiter == o->delimiter;
}
std::ostream &TokenDelimiter::simplePrintToStream(std::ostream &out) const
{
return out << "DELIMITER:'" << getStringByDelimiter(delimiter) << "'";
}
std::ostream &operator<<(std::ostream &out, const Token &token)
{
return token.simplePrintToStream(out);
}
std::ostream &TokenIdentifier::printToStream(std::ostream &out) const
{
return out << R"({"type":"IDENTIFIER","identifier":")" << getRaw() << R"("})";
}
bool TokenIdentifier::match(const Token &other) const
{
auto o = dynamic_cast<const TokenIdentifier *>(&other);
return o != nullptr;
}
std::ostream &TokenIdentifier::simplePrintToStream(std::ostream &out) const
{
return out << "ID:'" << getRaw() << "'";
}
std::ostream &TokenUnsigned::printToStream(std::ostream &out) const
{
return out << R"({"type":"UNSIGNED","value":)" << value << R"(})";
}
bool TokenUnsigned::match(const Token &other) const
{
auto o = dynamic_cast<const TokenUnsigned *>(&other);
return o != nullptr;
}
std::ostream &TokenUnsigned::simplePrintToStream(std::ostream &out) const
{
return out << "UNSIGNED:" << getRaw();
}
std::ostream &TokenWhitespace::printToStream(std::ostream &out) const
{
return out << R"({"type":"WHITESPACE"})";
}
bool TokenWhitespace::match(const Token &other) const
{
auto o = dynamic_cast<const TokenWhitespace *>(&other);
return o != nullptr;
}
std::ostream &TokenWhitespace::simplePrintToStream(std::ostream &out) const
{
return out << "WHITESPACE";
}
std::ostream &TokenLineWrap::printToStream(std::ostream &out) const
{
return out << R"({"type":"LINEWRAP"})";
}
bool TokenLineWrap::match(const Token &other) const
{
auto o = dynamic_cast<const TokenLineWrap *>(&other);
return o != nullptr;
}
std::ostream &TokenLineWrap::simplePrintToStream(std::ostream &out) const
{
return out << "LINEWRAP";
}
std::ostream &TokenError::printToStream(std::ostream &out) const
{
return out << R"({"type":"ERROR","error":")" << error << R"(","raw":")" << getRaw() << R"("})";
}
bool TokenError::match(const Token &other) const
{
auto o = dynamic_cast<const TokenError *>(&other);
return o != nullptr;
}
std::ostream &TokenError::simplePrintToStream(std::ostream &out) const
{
return out << "ERROR:'" << error << "'";
}
std::ostream &TokenEnd::printToStream(std::ostream &out) const
{
return out << R"({"type":"END"})";
}
bool TokenEnd::match(const Token &other) const
{
auto o = dynamic_cast<const TokenEnd *>(&other);
return o != nullptr;
}
std::ostream &TokenEnd::simplePrintToStream(std::ostream &out) const
{
return out << "END";
}