2021-01-02 16:41:53 +00:00
|
|
|
#include <ncurses.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include "../src/Parser/Lexer/Lexer.hpp"
|
|
|
|
#include "../src/Parser/Parser.hpp"
|
2021-01-03 14:10:43 +00:00
|
|
|
#include "../src/Parser/Statements/ParsedStatementStringifier.hpp"
|
2021-01-02 16:41:53 +00:00
|
|
|
|
2021-01-02 18:08:58 +00:00
|
|
|
void UpdateScriptWithParseInfo(WINDOW* inputWindow, const std::u8string& script,
|
|
|
|
const MalachScript::Diagnostics::Diagnostic* diag,
|
|
|
|
[[maybe_unused]] const MalachScript::Parser::LexToken* firstToken) {
|
|
|
|
std::stringstream ss((char*)script.c_str());
|
|
|
|
|
|
|
|
int col;
|
|
|
|
int row;
|
|
|
|
getyx(inputWindow, row, col);
|
|
|
|
wclear(inputWindow);
|
|
|
|
|
|
|
|
if (diag != nullptr) {
|
|
|
|
auto start = diag->GetSpan().GetStart();
|
|
|
|
auto end = diag->GetSpan().GetEnd() + 1;
|
2021-01-03 13:45:53 +00:00
|
|
|
if (end > script.size()) {
|
|
|
|
end = script.size();
|
|
|
|
if (start == end && start > 0) {
|
|
|
|
start--;
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 18:08:58 +00:00
|
|
|
|
|
|
|
waddnstr(inputWindow, (char*)script.c_str(), start);
|
|
|
|
wattron(inputWindow, COLOR_PAIR(1));
|
2021-01-03 13:45:53 +00:00
|
|
|
waddnstr(inputWindow, (char*)script.substr(start).data(), end - start);
|
2021-01-02 18:08:58 +00:00
|
|
|
wattroff(inputWindow, COLOR_PAIR(1));
|
2021-01-03 13:45:53 +00:00
|
|
|
waddnstr(inputWindow, (char*)script.substr(end).data(), script.size() - end);
|
2021-01-02 18:08:58 +00:00
|
|
|
|
|
|
|
if (start >= script.size() - 1) {
|
|
|
|
wattron(inputWindow, COLOR_PAIR(1));
|
|
|
|
waddch(inputWindow, ' ');
|
|
|
|
wattroff(inputWindow, COLOR_PAIR(1));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
waddstr(inputWindow, (char*)script.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
wmove(inputWindow, row, col);
|
|
|
|
|
|
|
|
wrefresh(inputWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParseAndUpdate(const std::vector<std::u8string> lines, WINDOW* diagnosticsWindow, WINDOW* parsedWindow,
|
|
|
|
WINDOW* inputWindow) {
|
2021-01-02 16:41:53 +00:00
|
|
|
std::u8string script;
|
|
|
|
for (const auto& line : lines) {
|
|
|
|
script += line;
|
|
|
|
script += '\n';
|
|
|
|
}
|
|
|
|
auto logger = MalachScript::Diagnostics::Logger();
|
|
|
|
auto lexer = MalachScript::Parser::Lexer(u8"diag", script, &logger);
|
|
|
|
const auto* firstToken = lexer.Lex();
|
|
|
|
[[maybe_unused]] const auto* parsedResult = MalachScript::Parser::Parser::Parse(firstToken, u8"diag", &logger);
|
2021-01-02 18:08:58 +00:00
|
|
|
|
|
|
|
const MalachScript::Diagnostics::Diagnostic* diag = nullptr;
|
2021-01-02 16:41:53 +00:00
|
|
|
wclear(diagnosticsWindow);
|
|
|
|
|
2021-01-03 13:42:26 +00:00
|
|
|
if (!logger.GetMessages().empty()) {
|
2021-01-02 18:08:58 +00:00
|
|
|
diag = &logger.GetMessages()[0];
|
|
|
|
|
|
|
|
wattron(diagnosticsWindow, COLOR_PAIR(1));
|
2021-01-02 16:41:53 +00:00
|
|
|
waddch(diagnosticsWindow, '[');
|
|
|
|
waddstr(diagnosticsWindow, "Error");
|
|
|
|
waddch(diagnosticsWindow, ']');
|
2021-01-02 18:08:58 +00:00
|
|
|
wattroff(diagnosticsWindow, COLOR_PAIR(1));
|
|
|
|
waddch(diagnosticsWindow, ' ');
|
|
|
|
waddstr(diagnosticsWindow, std::to_string(diag->GetSpan().GetStart() + 1).c_str());
|
|
|
|
waddch(diagnosticsWindow, '-');
|
|
|
|
waddstr(diagnosticsWindow, std::to_string(diag->GetSpan().GetEnd() + 1).c_str());
|
2021-01-02 16:41:53 +00:00
|
|
|
waddch(diagnosticsWindow, ' ');
|
2021-01-02 18:08:58 +00:00
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
waddstr(diagnosticsWindow,
|
2021-01-02 18:08:58 +00:00
|
|
|
MalachScript::Diagnostics::DiagnosticTypeHelper::ToEnglishString(diag->GetType()).c_str());
|
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
waddch(diagnosticsWindow, '\n');
|
|
|
|
}
|
|
|
|
wrefresh(diagnosticsWindow);
|
|
|
|
|
|
|
|
wclear(parsedWindow);
|
2021-01-03 13:42:26 +00:00
|
|
|
if (logger.GetMessages().empty()) {
|
2021-01-02 16:41:53 +00:00
|
|
|
std::stringstream ss;
|
2021-01-03 14:10:43 +00:00
|
|
|
MalachScript::Parser::ParsedStatementStringifier::Stringify(parsedResult, ss, "", true);
|
2021-01-02 16:41:53 +00:00
|
|
|
|
|
|
|
waddstr(parsedWindow, ss.str().c_str());
|
|
|
|
}
|
|
|
|
wrefresh(parsedWindow);
|
2021-01-02 18:08:58 +00:00
|
|
|
|
|
|
|
UpdateScriptWithParseInfo(inputWindow, script, diag, firstToken);
|
2021-01-04 12:33:52 +00:00
|
|
|
delete parsedResult;
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
2021-01-03 13:42:26 +00:00
|
|
|
setlocale(LC_ALL, "");
|
2021-01-02 16:41:53 +00:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
keypad(stdscr, true);
|
|
|
|
set_tabsize(4);
|
|
|
|
|
2021-01-02 18:08:58 +00:00
|
|
|
start_color();
|
|
|
|
init_pair(1, COLOR_BLACK, COLOR_RED);
|
|
|
|
clear();
|
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
int maxX;
|
|
|
|
int maxY;
|
|
|
|
getmaxyx(stdscr, maxY, maxX);
|
|
|
|
wresize(stdscr, maxY, maxX);
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
|
|
|
const int inputFieldSize = 30;
|
|
|
|
|
|
|
|
wborder(stdscr, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
wmove(stdscr, 31, 1);
|
|
|
|
whline(stdscr, ACS_HLINE, maxX - 2);
|
|
|
|
wmove(stdscr, 32, maxX / 2);
|
|
|
|
wvline(stdscr, ACS_VLINE, 25);
|
|
|
|
|
|
|
|
wrefresh(stdscr);
|
|
|
|
|
|
|
|
auto* inputWindow = newwin(inputFieldSize, maxX - 3, 1, 2);
|
|
|
|
keypad(inputWindow, true);
|
|
|
|
|
|
|
|
auto* diagnosticsWindow = newwin(25, (maxX - 4) / 2, 32, 2);
|
|
|
|
|
|
|
|
auto* parsedResultWindow = newwin(25, (maxX - 4) / 2 - 2, 32, (maxX - 4) / 2 + 4);
|
|
|
|
wrefresh(parsedResultWindow);
|
|
|
|
|
|
|
|
int row = 0;
|
|
|
|
int col = 0;
|
|
|
|
|
2021-01-02 18:08:58 +00:00
|
|
|
// Special handler so we can keep the column
|
|
|
|
int rightPos = 0;
|
|
|
|
|
2021-01-02 16:41:53 +00:00
|
|
|
std::vector<std::u8string> lines = {u8""};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
bool running = true;
|
|
|
|
while (running) {
|
|
|
|
c = wgetch(inputWindow);
|
|
|
|
switch (c) {
|
|
|
|
case 27: running = false; break;
|
|
|
|
|
|
|
|
case KEY_LEFT: {
|
|
|
|
if (col > 0) {
|
|
|
|
wmove(inputWindow, row, --col);
|
2021-01-02 18:08:58 +00:00
|
|
|
rightPos = col;
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KEY_RIGHT: {
|
|
|
|
auto& line = lines[row];
|
|
|
|
if (col < (int)line.size()) {
|
|
|
|
wmove(inputWindow, row, ++col);
|
2021-01-02 18:08:58 +00:00
|
|
|
rightPos = col;
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KEY_UP:
|
|
|
|
if (row > 0) {
|
2021-01-02 18:08:58 +00:00
|
|
|
col = rightPos;
|
|
|
|
if (col > (int)lines[row - 1].size()) {
|
|
|
|
col = (int)lines[row - 1].size();
|
|
|
|
}
|
2021-01-02 16:41:53 +00:00
|
|
|
wmove(inputWindow, --row, col);
|
|
|
|
}
|
|
|
|
|
|
|
|
wrefresh(stdscr);
|
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
if (row < (int)lines.size() - 1) {
|
2021-01-02 18:08:58 +00:00
|
|
|
col = rightPos;
|
|
|
|
if (col > (int)lines[row + 1].size()) {
|
|
|
|
col = (int)lines[row + 1].size();
|
|
|
|
}
|
2021-01-02 16:41:53 +00:00
|
|
|
wmove(inputWindow, ++row, col);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KEY_BACKSPACE:
|
|
|
|
case 127:
|
|
|
|
if (col > 0) {
|
|
|
|
mvwdelch(inputWindow, row, --col);
|
|
|
|
auto& line = lines[row];
|
|
|
|
line = line.erase(col, 1);
|
2021-01-02 18:08:58 +00:00
|
|
|
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
|
2021-01-02 16:41:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
case KEY_ENTER:
|
|
|
|
lines.insert(lines.begin() + row + 1, u8"");
|
|
|
|
row++;
|
|
|
|
col = 0;
|
|
|
|
for (size_t i = row - 1; i < lines.size(); i++) {
|
|
|
|
wmove(inputWindow, i, 0);
|
|
|
|
wclrtoeol(inputWindow);
|
|
|
|
waddstr(inputWindow, (char*)lines[i].c_str());
|
|
|
|
}
|
|
|
|
wmove(inputWindow, row, col);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
case KEY_STAB: {
|
|
|
|
wmove(inputWindow, row, 0);
|
|
|
|
wclrtoeol(inputWindow);
|
|
|
|
auto& line = lines[row];
|
|
|
|
if ((int)line.length() <= col) {
|
|
|
|
line += u8" ";
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < 4; i++) {
|
|
|
|
line.insert(line.begin() + col, u8' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
waddstr(inputWindow, (char*)line.c_str());
|
|
|
|
col += 4;
|
2021-01-02 18:08:58 +00:00
|
|
|
rightPos = col;
|
|
|
|
wmove(inputWindow, row, col);
|
|
|
|
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KEY_END: {
|
|
|
|
col = lines[row].size();
|
|
|
|
rightPos = col;
|
|
|
|
wmove(inputWindow, row, col);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KEY_HOME: {
|
|
|
|
col = 0;
|
|
|
|
rightPos = col;
|
2021-01-02 16:41:53 +00:00
|
|
|
wmove(inputWindow, row, col);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
wmove(inputWindow, row, 0);
|
|
|
|
auto& line = lines[row];
|
|
|
|
if ((int)line.length() <= col) {
|
|
|
|
line += (char8_t)c;
|
|
|
|
} else {
|
|
|
|
line.insert(line.begin() + col, c);
|
|
|
|
}
|
|
|
|
waddstr(inputWindow, (char*)line.c_str());
|
|
|
|
col++;
|
2021-01-02 18:08:58 +00:00
|
|
|
rightPos = col;
|
2021-01-02 16:41:53 +00:00
|
|
|
wmove(inputWindow, row, col);
|
2021-01-02 18:08:58 +00:00
|
|
|
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
|
2021-01-02 16:41:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wrefresh(inputWindow);
|
|
|
|
}
|
|
|
|
endwin();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|