Repository navigation
Expand file tree
/
Copy pathTokenCursor.cpp
More file actions
123 lines (95 loc) · 3.28 KB
/
Copy pathTokenCursor.cpp
File metadata and controls
123 lines (95 loc) · 3.28 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
#include "TokenCursor.h"
#include "CharacterType.h"
bool TokenCursor::isKeyword(string &word) const {
return this->keywords.count(word) > 0;
}
Token* TokenCursor::handleWhitespace(const string &line, int &start, int *current, State &state, Token *token) {
if (token->getType() == START) {
++start;
*current = start;
} else {
state = F;
}
return scanCharacter(line, start, current, state, token);
}
Token *TokenCursor::handleTerminal(const string &line, int &start, int *current, State &state, Token *token) {
if (token->getType() != START) {
return scanCharacter(line, start, current, F, token);
}
char ch = line[*current];
auto it = terminals.find(ch);
if (it == terminals.end()) {
return nullptr;
}
TokenType terminal = it->second;
++(*current);
switch (terminal) {
case PLUS:
case MINUS:
token = new Token(terminal, "1");
break;
case MULTIPLY:
case DIVIDE:
token = new Token(terminal, "2");
break;
default:
token = new Token(terminal);
}
state = F;
return scanCharacter(line, start, current, state, token);
}
Token *TokenCursor::handleNumberOrIdentifier(const string &line, int &start, int *current, State &state, Token *token) {
char ch = line[*current];
CharacterType characterType(ch);
if (characterType.type() == NUMBER) {
state = transitionTable[state][INTEGER - 7];
++(*current);
token = new Token(INTEGER, line.substr(start, *current - start));
return scanCharacter(line, start, current, state, token);
}
if (characterType.type() == STRING) {
state = transitionTable[state][IDENTIFIER - 7];
++(*current);
token = new Token(IDENTIFIER, line.substr(start, *current - start));
return scanCharacter(line, start, current, state, token);
}
return scanCharacter(line, start, current, F, token);
}
Token *TokenCursor::finalizeToken(const string &line, int start, int *current, State state, Token *token) {
if (state != F) return token;
if (token->getType() == INTEGER) {
string number = line.substr(start, *current - start);
return new Token(INTEGER, number);
}
if (token->getType() == IDENTIFIER) {
string identifier = line.substr(start, *current - start);
if (isKeyword(identifier)) {
return new Token(keywords.at(identifier), identifier);
}
return new Token(IDENTIFIER, identifier);
}
return token;
}
Token *TokenCursor::scanCharacter(const string &line, int start, int *current, State state, Token *token) {
if (state == F || *current >= static_cast<int>(line.length())) {
return finalizeToken(line, start, current, state, token);
}
if (line[*current] == ' ') {
return handleWhitespace(line, start, current, state, token);
}
if (terminals.count(line[*current]) > 0) {
return handleTerminal(line, start, current, state, token);
}
if (auto res = handleNumberOrIdentifier(line, start, current, state, token)) {
return res;
}
Error::printWarning(line, *current, "Unexpected character: ");
++(*current);
return scanCharacter(line, state, current, state, token);
}
Token* TokenCursor::scanToken(const string &line, int *offset) {
State state = S;
int start = *offset;
Token *start_token = new Token(START);
return this->scanCharacter(line, start, offset, state, start_token);
}