-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.cpp
381 lines (329 loc) · 8.87 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <vector>
#include "token.h"
#include "InputStream.h"
#include "Lexer.h"
using namespace std;
char Lexer::advance() {
return istream->getNext();
}
Token Lexer::getCurrentToken() {
return *tokenList->at(currentIntToken);
}
Token Lexer::peekNextToken() {
return *tokenList->at(currentIntToken + 1);
}
void Lexer::startState() {
state = START;
currentChar = ' ';
while (currentChar != EOF) {
switch (state) {
case START:
// Deal with last character state things
charSoFar = "";
currentChar = advance();
curLineNum = istream->getLineNumber();
state = parseChar(currentChar);
break;
case PUNCUTATION:
// token has been added -> go back to Start State
state = START;
break;
case COLON:
// get next character and see if it's a colon dash - else add the colon
state = checkDash();
break;
case ID:
// Add first char to string and get rest of ID string
charSoFar += currentChar;
state = getID();
break;
case ERROR:
break;
case STRING:
// Add first char to string and get rest of the string
charSoFar += currentChar;
state = getString();
break;
case END_OF_STRING:
state = checkEndOfString();
break;
case START_OF_COMMENT:
state = getComment();
break;
case SINGLELINE_COMMENT:
break;
case MULTILINE_COMMENT:
state = getMultiComment();
break;
case VERTICAL_BAR:
state = getVerticalBar();
break;
default:
createAndAddToken("UNDEFINED", charSoFar, curLineNum);
state = START;
break;
}
}
createAndAddToken("EOF", "");
}
State Lexer::getVerticalBar() {
if (currentChar == '#') {
charSoFar += currentChar;
createAndAddToken("COMMENT", charSoFar, curLineNum);
//advance();
return START;
}
else {
charSoFar += currentChar;
//char i = istream->peek();
if (currentChar == '|') {
currentChar = advance();
return VERTICAL_BAR;
}
else {
return MULTILINE_COMMENT;
}
}
}
State Lexer::getMultiComment() {
while (istream->peek() != EOF && istream->peek() != '|') {
charSoFar += advance();
}
if (istream->peek() == '|') {
// Add |
currentChar = advance();
charSoFar += currentChar;
// Give next function the currentChar of what's after the bar
currentChar = advance();
return VERTICAL_BAR;
}
return UNDEFINED;
}
State Lexer::getComment() {
charSoFar += currentChar;
if (istream->peek() == '|') {
currentChar = advance();
charSoFar += currentChar;
return MULTILINE_COMMENT;
}
else {
while (istream->peek() != EOF && istream->peek() != '\n') {
charSoFar += advance();
}
if (istream->peek() == '\n') {
//createAndAddToken("COMMENT", charSoFar);
return START;
}
if (istream->peek() == EOF) {
// just get back into while loop so it can kick itself out and end;
return START;
}
return UNDEFINED;
}
}
State Lexer::checkEndOfString() {
if (currentChar == '\'' && istream->peek() != '\'') {
// end of string
createAndAddToken("STRING", charSoFar, curLineNum);
}
else {
currentChar = advance();
// false alarm - string contained a quote
return STRING;
}
return START;
}
State Lexer::getString() {
while (istream->peek() != EOF && istream->peek() != '\'') {
charSoFar += advance();
}
if (istream->peek() == '\'') {
// Could be end of string
charSoFar += advance();
return END_OF_STRING;
}
else {
// returned EOF
return UNDEFINED;
}
}
State Lexer::getID() {
while (istream->peek() != '\n' && istream->peek() != EOF && !isspace(istream->peek())) {
if (isalnum((unsigned) istream->peek()) || istream->peek() == '_') {
currentChar = advance();
charSoFar += currentChar;
}
else {
break;
// undefined character
/* createAndAddToken("ID", charSoFar);
return START;*/
}
}
// ID string has ended (because of space, newline or EOF)
// If special token add it
if (!isSpecialID()) {
// if not a "FACTS", "SCHEMES" etc. add an ID token
createAndAddToken("ID", charSoFar);
}
return START;
}
bool Lexer::isSpecialID() {
if (charSoFar == "Facts") {
createAndAddToken("FACTS", charSoFar);
}
else if (charSoFar == "Queries") {
createAndAddToken("QUERIES", charSoFar);
}
else if (charSoFar == "Schemes") {
createAndAddToken("SCHEMES", charSoFar);
}
else if (charSoFar == "Rules") {
createAndAddToken("RULES", charSoFar);
}
else {
// ID string not special
return false;
}
return true;
}
State Lexer::checkDash() {
if (istream->peek() == '-') {
createAndAddToken("COLON_DASH", charSoFar + advance());
}
else {
createAndAddToken("COLON", charSoFar);
}
return START;
}
State Lexer::parseChar(char currentChar) {
if (isPunct(currentChar)) {
return PUNCUTATION;
}
else if (isGrammarPunct(currentChar)) {
return PUNCUTATION;
}
else if (isWhitespace(currentChar)) {
return START;
}
else if (isColon(currentChar)) {
return COLON;
}
else if (isalpha((unsigned) currentChar)) {
return ID;
}
else if (currentChar == '\'') {
return STRING;
}
else if (currentChar == '#') {
return START_OF_COMMENT;
}
else {
charSoFar += currentChar;
return UNDEFINED;
}
}
Lexer::Lexer(string filename) {
currentIntToken = 0;
istream = new InputStream(filename);
tokenList = new vector<Token*>;
startState();
exitAndReturn();
}
void Lexer::exitAndReturn() {
/*for (signed int i = 0; i < (signed) tokenList->size(); i++) {
cout << tokenList->at(i)->toString() << endl;
}
cout << "Total Tokens = " + to_string(tokenList->size()) << endl;*/
}
bool Lexer::isColon(char currentChar) {
if (currentChar == ':') {
charSoFar += currentChar;
return true;
}
return false;
}
string Lexer::parenType(char currentChar) {
string type;
if (currentChar == '(') {
type = "LEFT_PAREN";
}
if (currentChar == ')') {
type = "RIGHT_PAREN";
}
else if (currentChar == '*') {
type = "MULTIPLY";
}
else if (currentChar == '+') {
type = "ADD";
}
return type;
}
bool Lexer::isGrammarPunct(char currentChar) {
string type;
if (currentChar == '.') {
type = "PERIOD";
}
else if (currentChar == ',') {
type = "COMMA";
}
else if (currentChar == '?') {
type = "Q_MARK";
}
// Put value into string to match function type
string output = "";
output += currentChar;
if (type != "") {
createAndAddToken(type, output);
}
return type != "";
}
bool Lexer::isPunct(char currentChar) {
string type;
if (currentChar == '(' || currentChar == ')' || currentChar == '*' || currentChar == '+') {
type = parenType(currentChar);
}
// Put value into string to match function type
string output = "";
output += currentChar;
if (type != "") {
createAndAddToken(type, output);
}
return type != "";
}
bool Lexer::isWhitespace(char currentChar) {
if (currentChar == '\n' || currentChar == ' ' || currentChar =='\r' || currentChar == '\t' || isspace(currentChar)) {
return true;
}
return false;
}
void Lexer::createAndAddToken(string type, string value, int lineNum) {
value = "\"" + value + "\"";
if (lineNum == 0) {
lineNum = istream->getLineNumber();
}
Token* addToken = new Token(type, value, lineNum);
tokenList->push_back(addToken);
}
/*
3 Start states
1. If you get the current character and in the start state (switch state) and it is a one char you create the token, advcance in the string
add the char token to the list of tokens (8 single character tokens) and then go back to the start state
(created a function with the character and token type)
2. Start and you see a colon you go to the state -> initialize a new token with line number etc. and then go to state Colon or Colon Dash
If you see a dash add the dash to the token character and set the token type to colon dash and create that token and add it to the list
Go back to start state;
Can use if statement > "a" < "Z' etc.
Get a character and it is a string and it isn't punction or -1 then create the String token until you see a single quote. Then go to a state "End of String?"
Advance therer - if it is a single quote again then add it to the string and keep going. If there was only one create the String token
If you see a letter - add it to the charSoFar and then get the line number - token type = ID -> keep appending info if it is alphabet or digit
When you run out of letters or digits. If charSoFar == "Facts" or Queries etc. change the TokenType. Then go back to the Start state after adding it
If you see a # set it as a comment, add line number, advance.
Single line comment as long as there is no end of line \n or EOF then keep adding it - Mlutliline you see a vertical bar you keep adding until EOF or verticalBar and Comment
Try block and if there is an undefined throw an error and quit
Start state see an undefined char go to that
*/