-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.cpp
More file actions
128 lines (122 loc) · 4.43 KB
/
scan.cpp
File metadata and controls
128 lines (122 loc) · 4.43 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
#include "./scan.hh"
token getToken(string input, int& index, int limit) {
string output = ""; token currentToken;
index--;
state currentState, nextState;
int nextIndex;
nextState = currentState = START;
while (currentState != DONE) {
currentState = nextState;
nextIndex = index + 1;
//cout <<"looking at: "<<input[nextIndex] << " State: " << currentState << endl;
// todo in each state
switch (currentState) {
case START:
if (isalpha(input[nextIndex]) || isdigit(input[nextIndex]) || isSymbol(input[nextIndex]) || input[nextIndex] == ':') {
output += input[nextIndex];
//cout << "start: " <<output<< endl;
index++;
//if(isSymbol(input[index])) currentToken.tType = SPECIAL;
}
else if (input[nextIndex] == ' ' || input[nextIndex] == '\t' || input[nextIndex] == '\n') index++;
else if (input[nextIndex] == '{') index++;
else index++; // error
if (isSymbol(input[index])) currentToken.tType = SPECIAL;
break;
case INCOMMENT:
index++;
break;
case INASSIGN:
if (input[nextIndex] == '=') {
output += input[nextIndex];
currentToken.tType = SPECIAL;
//cout << "inid: " << output<< endl;
index++;
}
break;
case INID:
if (isalpha(input[nextIndex]) || isdigit(input[nextIndex])) {
output += input[nextIndex];
//currentToken.tType = ID;
//cout << "inid: " << output<< endl;
index++;
}
currentToken.tType = ID;
break;
case INNUM:
if (isdigit(input[nextIndex])) {
output += input[nextIndex];
//cout << "innum: " << output<< endl;
index++;
}
currentToken.tType = NUM;
break;
case DONE:
//cout << "Done: " << output<< endl;
index++;
if (isReservedWord(output))currentToken.tType = RESERVED;
currentToken.tVal = output;
return currentToken;
break;
case ERROR:
//cout << "ERROR: " << output<< endl;
if (index == limit && output == "") {
currentToken.tVal = "";
currentToken.tType = eof;
return currentToken;
}
isError = true;
currentToken.tType = ERR;
currentToken.tVal = output;
return currentToken;
default: // error
cout << "output error" << endl;
break;
}
// transition from each state
switch (currentState) {
case START:
if (input[nextIndex] == '{') nextState = INCOMMENT;
else if (input[nextIndex] == ':') nextState = INASSIGN;
else if (isalpha(input[nextIndex])) nextState = INID;
else if (isdigit(input[nextIndex])) nextState = INNUM;
else if (isSymbol(input[nextIndex])) nextState = DONE;
else if (input[nextIndex] == ' ' || input[nextIndex] == '\t' || input[nextIndex] == '\n') nextState = START;
else nextState = ERROR;
break;
case INCOMMENT:
if (input[nextIndex] == '}') nextState = START;
else nextState = INCOMMENT;
break;
case INASSIGN:
if (input[nextIndex] == '=') nextState = DONE;
else nextState = ERROR;
break;
case INID:
if (isalpha(input[nextIndex]) || isdigit(input[nextIndex])) nextState = INID;
else nextState = DONE;
break;
case INNUM:
if (isdigit(input[nextIndex])) nextState = INNUM;
else nextState = DONE;
case DONE:
nextState = DONE; // not sure
break;
case ERROR:
nextState = ERROR; // no way out
break;
default: // error
cout << "No Token" << endl;
break;
}
if (index < limit && input[index] == '\n') {
if (lineNum <= lines.size())
{
cout << lineNum << ": " << lines[lineNum - 1];
lineNum++;
}
if (textBuffer != "") cout << textBuffer;
textBuffer = "";
}
}
}