-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (86 loc) · 2.77 KB
/
main.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
#include <iostream>
#include <string>
#include <iterator>
#include "LLParserLibrary/LLParser.h"
#include "LLParserLibrary/LLTableBuilderLibrary/TokenLibrary/TokenInformation/TokenInformation.h"
int const REQUIRED_ARGC = 1;
std::string const NO_ARGUMENT_ERROR = "Error: file with rules is not specified";
void Print(std::vector<TokenInformation> const & tokenInformations, size_t from, size_t to, std::string const & color)
{
std::cout << "\033[" + color + "m";
StreamPosition outputStreamPosition;
if (from > 0)
{
StreamString previousStreamString = tokenInformations.at(from - 1).GetTokenStreamString();
outputStreamPosition = previousStreamString.position;
outputStreamPosition.IncreaseColumn(previousStreamString.string.length());
}
for (size_t i = from; i < to && i < tokenInformations.size(); ++i)
{
TokenInformation const & tokenInformation = tokenInformations.at(i);
StreamString const & streamString = tokenInformation.GetTokenStreamString();
std::string const & streamStringString = streamString.string;
StreamPosition const & streamPosition = streamString.position;
long const & line = streamPosition.GetLine();
long const & column = streamPosition.GetColumn();
while (line > outputStreamPosition.GetLine())
{
std::cout << "\n";
outputStreamPosition.IncreaseLine();
outputStreamPosition.SetColumn(1);
}
char const indentCharacter = outputStreamPosition.GetColumn() == 1 ? '\t' : ' ';
while (column > outputStreamPosition.GetColumn())
{
std::cout << indentCharacter;
outputStreamPosition.IncreaseColumn();
}
for (char ch : streamStringString)
{
std::cout << ch;
if (ch == '\n' || ch == '\r')
{
outputStreamPosition.IncreaseLine();
outputStreamPosition.SetColumn(1);
}
else
{
outputStreamPosition.IncreaseColumn();
}
}
}
std::cout << "\033[m";
}
int main(int argc, char * argv[])
{
if (argc - 1 < REQUIRED_ARGC)
{
std::cerr << NO_ARGUMENT_ERROR << "\n";
return EXIT_FAILURE;
}
LLParser llParser(argv[1]);
if (argc - 1 > REQUIRED_ARGC)
{
std::vector<TokenInformation> tokenInformations;
size_t failIndex = 0;
std::unordered_set<Token> expectedTokens;
if (!llParser.IsValid(argv[2], tokenInformations, failIndex, expectedTokens))
{
long shift = tokenInformations.at(failIndex).GetTokenStreamString().position.GetColumn() - 1;
Print(tokenInformations, 0, failIndex, "32");
Print(tokenInformations, failIndex, failIndex + 1, "31");
Print(tokenInformations, failIndex + 1, tokenInformations.size(), "");
std::cout << "\n";
for (Token word : expectedTokens)
{
std::cout << "\033[m";
std::cout << std::string(shift, ' ');
std::cout << "\033[33m";
std::cout << TokenExtensions::ToString(word);
std::cout << "\033[m";
std::cout << "\n";
}
}
}
return EXIT_SUCCESS;
}