Adds some colours to the REPL, show errors live!
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2021-01-02 19:08:58 +01:00
parent ee53e7abd8
commit df9c4933af
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 87 additions and 20 deletions

View File

@ -1,10 +1,44 @@
#include <iostream>
#include <ncurses.h>
#include <sstream>
#include "../src/Parser/Lexer/Lexer.hpp"
#include "../src/Parser/Parser.hpp"
void ParseAndUpdate(const std::vector<std::u8string> lines, WINDOW* diagnosticsWindow, WINDOW* parsedWindow) {
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;
waddnstr(inputWindow, (char*)script.c_str(), start);
wattron(inputWindow, COLOR_PAIR(1));
waddnstr(inputWindow, (char*)script.c_str() + start, end - start);
wattroff(inputWindow, COLOR_PAIR(1));
waddnstr(inputWindow, (char*)script.c_str() + end, script.size() - end);
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) {
std::u8string script;
for (const auto& line : lines) {
script += line;
@ -14,27 +48,27 @@ void ParseAndUpdate(const std::vector<std::u8string> lines, WINDOW* diagnosticsW
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);
wclear(diagnosticsWindow);
waddstr(diagnosticsWindow, (char*)script.c_str());
for (const auto& diag : logger.GetMessages()) {
const MalachScript::Diagnostics::Diagnostic* diag = nullptr;
wclear(diagnosticsWindow);
if (logger.GetMessages().size() > 0) {
diag = &logger.GetMessages()[0];
wattron(diagnosticsWindow, COLOR_PAIR(1));
waddch(diagnosticsWindow, '[');
waddstr(diagnosticsWindow, "Error");
waddch(diagnosticsWindow, ']');
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());
waddch(diagnosticsWindow, ' ');
waddstr(diagnosticsWindow,
MalachScript::Diagnostics::DiagnosticTypeHelper::ToEnglishString(diag.GetType()).c_str());
waddstr(diagnosticsWindow, " - ");
auto start = diag.GetSpan().GetStart() - 3;
if (start < 0)
start = 0;
if (start > script.size())
start = script.size() - 1;
auto end = diag.GetSpan().GetEnd() + 3;
if (end >= script.size())
end = script.size() - 1;
waddstr(diagnosticsWindow, (char*)script.substr(start, end - start).c_str());
waddstr(diagnosticsWindow,
MalachScript::Diagnostics::DiagnosticTypeHelper::ToEnglishString(diag->GetType()).c_str());
waddch(diagnosticsWindow, '\n');
}
wrefresh(diagnosticsWindow);
@ -47,6 +81,8 @@ void ParseAndUpdate(const std::vector<std::u8string> lines, WINDOW* diagnosticsW
waddstr(parsedWindow, ss.str().c_str());
}
wrefresh(parsedWindow);
UpdateScriptWithParseInfo(inputWindow, script, diag, firstToken);
}
int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
@ -56,6 +92,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
keypad(stdscr, true);
set_tabsize(4);
start_color();
init_pair(1, COLOR_BLACK, COLOR_RED);
clear();
int maxX;
int maxY;
getmaxyx(stdscr, maxY, maxX);
@ -83,6 +123,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
int row = 0;
int col = 0;
// Special handler so we can keep the column
int rightPos = 0;
std::vector<std::u8string> lines = {u8""};
int c;
@ -95,6 +138,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
case KEY_LEFT: {
if (col > 0) {
wmove(inputWindow, row, --col);
rightPos = col;
}
break;
}
@ -102,11 +146,16 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
auto& line = lines[row];
if (col < (int)line.size()) {
wmove(inputWindow, row, ++col);
rightPos = col;
}
break;
}
case KEY_UP:
if (row > 0) {
col = rightPos;
if (col > (int)lines[row - 1].size()) {
col = (int)lines[row - 1].size();
}
wmove(inputWindow, --row, col);
}
@ -114,6 +163,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
break;
case KEY_DOWN:
if (row < (int)lines.size() - 1) {
col = rightPos;
if (col > (int)lines[row + 1].size()) {
col = (int)lines[row + 1].size();
}
wmove(inputWindow, ++row, col);
}
break;
@ -123,7 +176,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
mvwdelch(inputWindow, row, --col);
auto& line = lines[row];
line = line.erase(col, 1);
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow);
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
}
break;
case 10:
@ -153,8 +206,21 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
}
waddstr(inputWindow, (char*)line.c_str());
col += 4;
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;
wmove(inputWindow, row, col);
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow);
break;
}
default: {
@ -167,8 +233,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
}
waddstr(inputWindow, (char*)line.c_str());
col++;
rightPos = col;
wmove(inputWindow, row, col);
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow);
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
break;
}
}