-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
202 lines (185 loc) · 4.25 KB
/
lexer.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
193
194
195
196
197
198
199
200
201
202
#include "lexer.hpp"
#include "token.hpp"
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
// implement the lexer function here
TokenList tokenize(istream & ins)
{
TokenList mylist;
bool isAssign = false;
bool isOpen = false;
size_t delimcount = 0;
ins.seekg(0, ins.end);
size_t size = ins.tellg();
ins.seekg(0, ins.beg);
size_t linecount = 0;
size_t sizecount = 0;
string str;
char c;
while (ins.get(c)) {
sizecount++;
if (linecount == 0) {
linecount++;
}
if (delimcount == 1) {
if (c == 0x0a) {
mylist.emplace_back(STRING, linecount, str);
str.clear();
mylist.emplace_back(ERROR, linecount, "Error: String delim span multi lines.");
break;
}
if (c == 0x22) {
isAssign = true;
mylist.emplace_back(STRING, linecount, str);
mylist.emplace_back(STRING_DELIM, linecount);
//mylist.emplace_back(EOL, linecount);
str.clear();
delimcount = 0;
}
else {
isAssign = true;
str = str + c;
}
if (sizecount == size) {
mylist.emplace_back(EOL, linecount);
}
}
else if (c == 0x2c || c == 0x0a || c == 0x22 || c == 0x0d || c == 0x28 || c == 0x29 || c == 0x20 || c == 0x09 || c == 0x0b || c == 0x0c || c == 0x3d || c==0x23) {
if (delimcount != 0) {
str = str + c;
}
else {
if (str.length() >= 1) {
mylist.emplace_back(STRING, linecount, str);
str.clear();
if (c == 0x0a) {
mylist.emplace_back(EOL, linecount);
isAssign = false;
if (isOpen) {
mylist.emplace_back(ERROR, linecount, "Error: Unmatched paren");
}
linecount++;
//str.clear();
}
else if (c == 0x23) {
char c1;
ins.get(c1);
while (c1 != 0x0a) {
ins.get(c1);
}
if(isAssign)
mylist.emplace_back(EOL, linecount);
linecount++;
isAssign = false;
}
else if (c == 0x2c) {
mylist.emplace_back(SEP, linecount);
//str.clear();
}
else if (c == 0x28) {
if (isOpen) {
mylist.emplace_back(ERROR, linecount, "Error: Unmatched paren");
break;
}
else {
isOpen = true;
mylist.emplace_back(OPEN_PAREN, linecount);
}
}
else if (c == 0x29) {
if (!isOpen) {
mylist.emplace_back(ERROR, linecount, "Error: Unmatched paren");
break;
}
else {
isOpen = false;
mylist.emplace_back(CLOSE_PAREN, linecount);
}
}
else if (c == 0x22) {
delimcount++;
mylist.emplace_back(STRING_DELIM, linecount);
}
else if (c == 0x3d) {
mylist.emplace_back(EQUAL, linecount);
}
//isAssign = false;
}
else {
if (c == 0x0a) {
if (isAssign) {
mylist.emplace_back(EOL, linecount);
}
/*if (str.length() >= 1) {
mylist.emplace_back(EOL, linecount);
linecount++;
}
else*/
linecount++;
isAssign = false;
}
else if (c == 0x23) {
char c1;
ins.get(c1);
while (c1 != 0x0a) {
ins.get(c1);
}
if (isAssign) {
mylist.emplace_back(EOL, linecount);
}
isAssign = false;
linecount++;
}
else if (c == 0x2c) {
mylist.emplace_back(SEP, linecount);
//str.clear();
}
else if (c == 0x28) {
if (isOpen) {
mylist.emplace_back(ERROR, linecount, "Error: Unmatched paren");
break;
}
else {
isOpen = true;
mylist.emplace_back(OPEN_PAREN, linecount);
}
}
else if (c == 0x29) {
if (!isOpen) {
mylist.emplace_back(ERROR, linecount, "Error: Unmatched paren");
break;
}
else {
isOpen = false;
mylist.emplace_back(CLOSE_PAREN, linecount);
}
}
else if (c == 0x22) {
mylist.emplace_back(STRING_DELIM, linecount);
delimcount++;
}
else if (c == 0x3d) {
mylist.emplace_back(EQUAL, linecount);
}
}
}
}
else {
// if(c!=0x0)
str = str + c;
isAssign = true;
if (sizecount == size && str.length() >= 1) {
mylist.emplace_back(STRING, linecount, str);
mylist.emplace_back(EOL, linecount);
}
}
}
//TokenList::iterator it = mylist.end();
//if (it->type() != EOL) {}
//if (mylist.begin()->type()!=EOL) {}
/*if (isOpen) {
}*/
return mylist;
}