Complete parser class

This commit is contained in:
Hazem Krimi
2024-03-23 00:08:15 +01:00
parent e488aad6da
commit 4c77ab9817
2 changed files with 51 additions and 3 deletions
+8
View File
@@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <regex>
#include <parser.h> #include <parser.h>
using namespace std; using namespace std;
@@ -6,8 +7,15 @@ using namespace std;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
string path = argv[1]; string path = argv[1];
if (!regex_match(path, regex("^.+\\.vm"))) {
cout << "Wrong file extension!" << endl;
return 1;
}
Parser parser(path); Parser parser(path);
parser.printFile(); parser.printFile();
return 0; return 0;
} }
+43 -3
View File
@@ -4,6 +4,7 @@
#include <regex> #include <regex>
#include <fstream> #include <fstream>
#include <cctype> #include <cctype>
#include <vector>
using namespace std; using namespace std;
@@ -33,7 +34,8 @@ private:
{ {
if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text)) if (regex_search(text, matched, regex("^(.*)?(\\/\\/.*)")) || isEmptyLine(text))
{ {
if (!isEmptyLine(matched[1])) { if (!isEmptyLine(matched[1]))
{
string command = matched[1]; string command = matched[1];
vmCode.append(command + '\n'); vmCode.append(command + '\n');
} }
@@ -54,14 +56,52 @@ public:
removeCommentsAndWhitespace(); removeCommentsAndWhitespace();
} }
void printFile() vector<vector<string>> getTokens()
{ {
stringstream vmCodeStream(vmCode); stringstream vmCodeStream(vmCode);
string text; string text;
smatch matched;
vector<vector<string>> tokens;
while (getline(vmCodeStream, text, '\n')) while (getline(vmCodeStream, text, '\n'))
{ {
cout << text << endl; vector<string> matchedVector;
if (regex_search(text, matched, regex("^(.*) (.*) (.*)")))
{
matchedVector.push_back(matched[1]);
matchedVector.push_back(matched[2]);
matchedVector.push_back(matched[3]);
}
else
{
matchedVector.push_back(text);
}
tokens.push_back(matchedVector);
}
return tokens;
}
void printFile()
{
vector<vector<string>> tokens = getTokens();
for (const auto &vec : tokens)
{
if (vec.size() > 1)
{
cout << vec[0] << "-";
if (vec.size() > 1)
cout << vec[1] << "-";
if (vec.size() > 1)
cout << vec[2];
cout << endl;
}
else
{
cout << vec[0] << endl;
}
} }
} }
}; };