This repository was archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
77 lines (71 loc) · 1.63 KB
/
Copy pathtoken.cpp
File metadata and controls
77 lines (71 loc) · 1.63 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
// token.cpp
// Copyright (c) 2018 Ni Kesu. All rights reserved.
#include <iostream>
#include <cctype>
#include <cstring>
#include "token.h"
using std::string;
using std::vector;
string removeBlank(const string & s)
{
string r;
for (auto c : s) {
if (!isblank(c)) {
r += c;
}
}
return r;
}
Token getToken(const std::string & s, size_t i)
{
Token r;
if (isdigit(s[i])) {
r.type = T_LITERAL;
while (isdigit(s[i]) || s[i] == '.') {
r.str += s[i];
i++;
}
return r;
}
else if (isalpha(s[i]) || s[i] == '_') {
r.type = T_IDENTIFIER;
while (isalpha(s[i]) || isdigit(s[i]) || s[i] == '_') {
r.str += s[i];
i++;
}
return r;
}
else {
size_t num = sizeof(operatorList) / sizeof(operatorList[0]);
for (size_t j = 0; j < num; j++) {
if (strlen(operatorList[j]) == 2) {
if (s[i] == operatorList[j][0] && s[i+1] == operatorList[j][1]) {
r.type = static_cast<TokenType>(j);
r.str = operatorList[j];
return r;
}
}
}
for (size_t j = 0; j < num; j++) {
if (strlen(operatorList[j]) == 1) {
if (s[i] == operatorList[j][0]) {
r.type = static_cast<TokenType>(j);
r.str = operatorList[j];
return r;
}
}
}
std::cerr << "getToken Failed!";
exit(-1);
}
}
vector<Token> getTokenStream(const string & s)
{
vector<Token> r;
string t = removeBlank(s);
for (size_t i = 0; i < t.size();) {
r.push_back(getToken(t, i));
i += r.back().str.size();
}
return r;
}