-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.cpp
73 lines (63 loc) · 1.99 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
#include "headerFiles/lexer.h"
Lexer Lexer::instance;
Lexer &Lexer::getinstance()
{
return instance;
}
pair<vector<vector<string>>, vector<vector<string>>> Lexer::lexingMethod(string filename)
{
// Initializing
vector<vector<string>> mainVec, whileVec;
vector<string> words;
string lineText, word;
char ch;
// Read from the text file
ifstream MyReadFile(filename);
// Use a while loop together with the getline() function to read the file line by line
while (getline(MyReadFile, lineText))
{
int lineSize = lineText.size();
for (int i = 0; i < lineSize; i++)
{
ch = lineText[i];
// this indicates the end of the while loop
if (ch != '}')
{
if (ch != ' ')
{
word += ch;
}
// if a space was found, push the compleate word into the array.
if (ch == ' ')
{
words.push_back({word});
word = "";
}
// if i got to the end of the line, push the entire vector of words into the main vec.
if (i == lineSize - 1)
{
words.push_back({word});
word = "";
// capturing the while loop body
if (words[0][0] == '\t')
{
// removing the '\t' from the lines
words[0].erase(0, 1);
whileVec.push_back(words);
words.clear();
}
else
{
mainVec.push_back(words);
words.clear();
}
}
}
}
}
// Close the file
MyReadFile.close();
// combine bouth of the vectors together
ToolBox tInstance;
return tInstance.combine(whileVec, mainVec);
};