Skip to content

Commit 2bf5d00

Browse files
committed
Frontend part updates: added not-full lex-errors support
1 parent 507c898 commit 2bf5d00

File tree

7 files changed

+119
-17
lines changed

7 files changed

+119
-17
lines changed

DiffFrontend/DiffFrontend.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ SOURCES += \
1919
statjsonreader.cpp
2020

2121
HEADERS += \
22-
ast-nodes.h \
2322
diffviewer.h \
2423
diffviewertext.h \
2524
diffviewertextbuilder.h \

DiffFrontend/diffviewertext.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ QString DiffViewerText::getText()
1616
return text;
1717
}
1818

19-
void DiffViewerText::setTextDescriptionFromJson(QJsonValue jsonVal,QJsonObject comments, bool isTopLevel)
19+
void DiffViewerText::generateHTMLTextFromLexemsArrayJson(QJsonArray lexems, QJsonObject comments)
20+
{
21+
DiffViewerTextBuilder builder;
22+
text = builder.generateTextFromLexemsArray(lexems,comments);
23+
}
24+
25+
void DiffViewerText::generateHTMLTextFromJson(QJsonValue jsonVal,QJsonObject comments, bool isTopLevel)
2026
{
2127
DiffViewerTextBuilder builder;
2228
text = builder.generateText(jsonVal,comments, isTopLevel);

DiffFrontend/diffviewertext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class DiffViewerText : public QObject
1010
public:
1111
explicit DiffViewerText(QObject *parent = nullptr);
1212
QString getText();
13-
void setTextDescriptionFromJson(QJsonValue obj, QJsonObject comments, bool isTopLevel = true);
13+
void generateHTMLTextFromJson(QJsonValue obj, QJsonObject comments, bool isTopLevel = true);
14+
void generateHTMLTextFromLexemsArrayJson(QJsonArray lexems, QJsonObject comments);
1415
private:
1516

1617
QJsonDocument loadedJson;

DiffFrontend/diffviewertextbuilder.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ QString DiffViewerTextBuilder::generateText(QJsonValue jsonVal, QJsonObject comm
2222
return text;
2323
}
2424

25+
QString DiffViewerTextBuilder::generateTextFromLexemsArray(QJsonArray lexems, QJsonObject comments)
26+
{
27+
this->comments = comments;
28+
text.append("<pre>");
29+
for(int i = 0; i < lexems.size(); i++){
30+
QJsonObject lex = lexems[i].toObject();
31+
if(getTrueLine(lex["line"].toInt()) == cur_line){
32+
pasteSpaces(lex["column"].toInt() - cur_column);
33+
} else {
34+
pasteNewLinesAndComments(getTrueLine(lex["line"].toInt()));
35+
pasteSpaces(lex["column"].toInt() - cur_column);
36+
cur_line = getTrueLine(lex["line"].toInt());
37+
}
38+
pasteLexem(lex);
39+
if(lex["type"] == "string"){
40+
int newlines = lex["string"].toString().count('\n');
41+
cur_line += newlines;
42+
}
43+
cur_column = lex["column"].toInt() + lex["string"].toString().size();
44+
}
45+
text.append("</pre>");
46+
return text;
47+
}
48+
2549
int DiffViewerTextBuilder::getTrueLine(int cur_line)
2650
{
2751
return cur_line - diff_line;

DiffFrontend/diffviewertextbuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class DiffViewerTextBuilder
1313
DiffViewerTextBuilder();
1414

1515
QString generateText(QJsonValue, QJsonObject, bool);
16+
QString generateTextFromLexemsArray(QJsonArray, QJsonObject);
1617

1718
private:
1819
QString text;

DiffFrontend/mainwindow.cpp

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,55 @@ void MainWindow::on_actionloadFiles_triggered()
3030
QProcess* backend_process = new QProcess(this);
3131
QString file = "diff-backend.exe";
3232
qDebug() << file;
33+
cleanOldJsonFiles();
3334
backend_process->start(file,lispFiles);
3435
if (!backend_process->waitForFinished()) {
3536
qDebug() << "something is wrong";
3637
}
38+
qDebug() << "Backend is ok!";
39+
if (QFile::exists("lexer-errors-msgs1.json")) {
40+
viewerMode = ViewerMode::ErrorsMode;
41+
file1ErrorsMode = ErrorsModeTypes::LexicalErrors;
42+
errorsMsgs1 = convertJsonArrayToErrorsMsgsMap(getJsonDocument("lexer-errors-msgs1.json").array());
43+
}
44+
if (QFile::exists("lexer-errors-msgs2.json")) {
45+
viewerMode = ViewerMode::ErrorsMode;
46+
file2ErrorsMode = ErrorsModeTypes::LexicalErrors;
47+
errorsMsgs2 = convertJsonArrayToErrorsMsgsMap(getJsonDocument("lexer-errors-msgs2.json").array());
48+
}
3749

38-
stats = Stat("stats.json");
39-
fillStatsTree();
40-
synTreeJson1 = getJsonDocument("res1.json");
41-
synTreeJson2 = getJsonDocument("res2.json");
4250
if (QFile::exists("comments1.json")) {
4351
commentsJsonObj1 = getJsonDocument("comments1.json").object();
4452
}
4553
if (QFile::exists("comments2.json")) {
4654
commentsJsonObj2 = getJsonDocument("comments2.json").object();
4755
}
48-
analyzeSynTree(synTreeJson1,1);
49-
analyzeSynTree(synTreeJson2,2);
5056

51-
doc1.setTextDescriptionFromJson(QJsonValue(synTreeJson1.array()),commentsJsonObj1);
52-
doc2.setTextDescriptionFromJson(QJsonValue(synTreeJson2.array()),commentsJsonObj2);
53-
ui->plainTextEdit->appendHtml(doc1.getText());
54-
ui->plainTextEdit_2->appendHtml(doc2.getText());
57+
if(viewerMode == ViewerMode::NormalMode){
58+
stats = Stat("stats.json");
59+
fillStatsTree();
60+
synTreeJson1 = getJsonDocument("res1.json");
61+
synTreeJson2 = getJsonDocument("res2.json");
62+
analyzeSynTree(synTreeJson1,1);
63+
analyzeSynTree(synTreeJson2,2);
64+
65+
doc1.generateHTMLTextFromJson(QJsonValue(synTreeJson1.array()),commentsJsonObj1);
66+
doc2.generateHTMLTextFromJson(QJsonValue(synTreeJson2.array()),commentsJsonObj2);
67+
ui->plainTextEdit->appendHtml(doc1.getText());
68+
ui->plainTextEdit_2->appendHtml(doc2.getText());
69+
} else if (viewerMode == ViewerMode::ErrorsMode){
70+
if(QFile::exists("lexems1.json")){
71+
qDebug("I am here");
72+
lexemsArrayJson1 = getJsonDocument("lexems1.json").array();
73+
doc1.generateHTMLTextFromLexemsArrayJson(lexemsArrayJson1,commentsJsonObj1);
74+
ui->plainTextEdit->appendHtml(doc1.getText());
75+
}
76+
if(QFile::exists("lexems2.json")){
77+
lexemsArrayJson2 = getJsonDocument("lexems2.json").array();
78+
doc1.generateHTMLTextFromLexemsArrayJson(lexemsArrayJson2,commentsJsonObj2);
79+
ui->plainTextEdit_2->appendHtml(doc2.getText());
80+
}
81+
}
5582
}
5683

5784
void MainWindow::fillStatsTree()
@@ -119,6 +146,30 @@ void MainWindow::analyzeSynTree(QJsonDocument &doc, int num)
119146
}
120147
}
121148

149+
void MainWindow::cleanOldJsonFiles()
150+
{
151+
QFile::remove("lexems1.json");
152+
QFile::remove("lexems2.json");
153+
QFile::remove("comments1.json");
154+
QFile::remove("comments2.json");
155+
QFile::remove("lexer-errors-msgs1.json");
156+
QFile::remove("lexer-errors-msgs2.json");
157+
QFile::remove("res1.json");
158+
QFile::remove("res2.json");
159+
QFile::remove("stats.json");
160+
}
161+
162+
ErrorsMsgsMap MainWindow::convertJsonArrayToErrorsMsgsMap(QJsonArray array)
163+
{
164+
ErrorsMsgsMap errorsMsgsMap;
165+
for(int i = 0; i < array.size(); i++){
166+
int errorLexId = array[i].toObject()["errorLexId"].toInt();
167+
errorsMsgsMap[errorLexId] = array[i].toObject();
168+
errorsMsgsMap[errorLexId].insert("isSelected",QJsonValue(false));
169+
}
170+
return errorsMsgsMap;
171+
}
172+
122173
QJsonDocument MainWindow::getJsonDocument(QString pathname)
123174
{
124175
QFile loadFile(pathname);
@@ -137,20 +188,20 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
137188
qDebug() << item->text(0);
138189
QString text = item->text(0);
139190
if(text == "all"){
140-
doc1.setTextDescriptionFromJson(QJsonValue(synTreeJson1.array()), commentsJsonObj1);
141-
doc2.setTextDescriptionFromJson(QJsonValue(synTreeJson2.array()), commentsJsonObj2);
191+
doc1.generateHTMLTextFromJson(QJsonValue(synTreeJson1.array()), commentsJsonObj1);
192+
doc2.generateHTMLTextFromJson(QJsonValue(synTreeJson2.array()), commentsJsonObj2);
142193
ui->plainTextEdit->clear();
143194
ui->plainTextEdit_2->clear();
144195
ui->plainTextEdit->appendHtml(doc1.getText());
145196
ui->plainTextEdit_2->appendHtml(doc2.getText());
146197
} else {
147198
if(nameToObj1.contains(text)){
148-
doc1.setTextDescriptionFromJson(QJsonValue(nameToObj1[text]), commentsJsonObj1,false);
199+
doc1.generateHTMLTextFromJson(QJsonValue(nameToObj1[text]), commentsJsonObj1,false);
149200
ui->plainTextEdit->clear();
150201
ui->plainTextEdit->appendHtml(doc1.getText());
151202
}
152203
if(nameToObj2.contains(text)){
153-
doc2.setTextDescriptionFromJson(QJsonValue(nameToObj2[text]), commentsJsonObj2,false);
204+
doc2.generateHTMLTextFromJson(QJsonValue(nameToObj2[text]), commentsJsonObj2,false);
154205
ui->plainTextEdit_2->clear();
155206
ui->plainTextEdit_2->appendHtml(doc2.getText());
156207
}

DiffFrontend/mainwindow.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33

44
#include "diffviewertext.h"
55

6+
#include <QJsonArray>
7+
#include <QJsonObject>
68
#include <QMainWindow>
9+
#include <QMap>
710
#include <QTreeWidgetItem>
811
#include "stat.h"
912

1013
QT_BEGIN_NAMESPACE
1114
namespace Ui { class MainWindow; }
1215
QT_END_NAMESPACE
1316

17+
enum class ViewerMode {NormalMode, ErrorsMode};
18+
enum class ErrorsModeTypes {LexicalErrors, SyntaxErrors};
19+
20+
using ErrorsMsgsMap = QMap<int,QJsonObject>;
21+
1422
class MainWindow : public QMainWindow
1523
{
1624
Q_OBJECT
@@ -35,11 +43,23 @@ private slots:
3543
Ui::MainWindow *ui;
3644
void fillStatsTree();
3745
void analyzeSynTree(QJsonDocument& doc, int num);
46+
void cleanOldJsonFiles();
47+
ErrorsMsgsMap convertJsonArrayToErrorsMsgsMap(QJsonArray array);
3848
QJsonDocument getJsonDocument(QString pathname);
49+
3950
QJsonDocument synTreeJson1;
4051
QJsonDocument synTreeJson2;
4152
QJsonObject commentsJsonObj1;
4253
QJsonObject commentsJsonObj2;
54+
//for errors
55+
QJsonArray lexemsArrayJson1;
56+
QJsonArray lexemsArrayJson2;
57+
ErrorsMsgsMap errorsMsgs1;
58+
ErrorsMsgsMap errorsMsgs2;
59+
60+
ViewerMode viewerMode = ViewerMode::NormalMode;
61+
ErrorsModeTypes file1ErrorsMode;
62+
ErrorsModeTypes file2ErrorsMode;
4363

4464
};
4565
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)