Adds some colours to the REPL, show errors live!
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
ee53e7abd8
commit
df9c4933af
107
repl/main.cpp
107
repl/main.cpp
@ -1,10 +1,44 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "../src/Parser/Lexer/Lexer.hpp"
|
#include "../src/Parser/Lexer/Lexer.hpp"
|
||||||
#include "../src/Parser/Parser.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;
|
std::u8string script;
|
||||||
for (const auto& line : lines) {
|
for (const auto& line : lines) {
|
||||||
script += line;
|
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);
|
auto lexer = MalachScript::Parser::Lexer(u8"diag", script, &logger);
|
||||||
const auto* firstToken = lexer.Lex();
|
const auto* firstToken = lexer.Lex();
|
||||||
[[maybe_unused]] const auto* parsedResult = MalachScript::Parser::Parser::Parse(firstToken, u8"diag", &logger);
|
[[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, '[');
|
waddch(diagnosticsWindow, '[');
|
||||||
waddstr(diagnosticsWindow, "Error");
|
waddstr(diagnosticsWindow, "Error");
|
||||||
waddch(diagnosticsWindow, ']');
|
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, ' ');
|
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');
|
waddch(diagnosticsWindow, '\n');
|
||||||
}
|
}
|
||||||
wrefresh(diagnosticsWindow);
|
wrefresh(diagnosticsWindow);
|
||||||
@ -47,6 +81,8 @@ void ParseAndUpdate(const std::vector<std::u8string> lines, WINDOW* diagnosticsW
|
|||||||
waddstr(parsedWindow, ss.str().c_str());
|
waddstr(parsedWindow, ss.str().c_str());
|
||||||
}
|
}
|
||||||
wrefresh(parsedWindow);
|
wrefresh(parsedWindow);
|
||||||
|
|
||||||
|
UpdateScriptWithParseInfo(inputWindow, script, diag, firstToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
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);
|
keypad(stdscr, true);
|
||||||
set_tabsize(4);
|
set_tabsize(4);
|
||||||
|
|
||||||
|
start_color();
|
||||||
|
init_pair(1, COLOR_BLACK, COLOR_RED);
|
||||||
|
clear();
|
||||||
|
|
||||||
int maxX;
|
int maxX;
|
||||||
int maxY;
|
int maxY;
|
||||||
getmaxyx(stdscr, maxY, maxX);
|
getmaxyx(stdscr, maxY, maxX);
|
||||||
@ -83,6 +123,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
int row = 0;
|
int row = 0;
|
||||||
int col = 0;
|
int col = 0;
|
||||||
|
|
||||||
|
// Special handler so we can keep the column
|
||||||
|
int rightPos = 0;
|
||||||
|
|
||||||
std::vector<std::u8string> lines = {u8""};
|
std::vector<std::u8string> lines = {u8""};
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
@ -95,6 +138,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
case KEY_LEFT: {
|
case KEY_LEFT: {
|
||||||
if (col > 0) {
|
if (col > 0) {
|
||||||
wmove(inputWindow, row, --col);
|
wmove(inputWindow, row, --col);
|
||||||
|
rightPos = col;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -102,11 +146,16 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
auto& line = lines[row];
|
auto& line = lines[row];
|
||||||
if (col < (int)line.size()) {
|
if (col < (int)line.size()) {
|
||||||
wmove(inputWindow, row, ++col);
|
wmove(inputWindow, row, ++col);
|
||||||
|
rightPos = col;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
if (row > 0) {
|
if (row > 0) {
|
||||||
|
col = rightPos;
|
||||||
|
if (col > (int)lines[row - 1].size()) {
|
||||||
|
col = (int)lines[row - 1].size();
|
||||||
|
}
|
||||||
wmove(inputWindow, --row, col);
|
wmove(inputWindow, --row, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,6 +163,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
break;
|
break;
|
||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
if (row < (int)lines.size() - 1) {
|
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);
|
wmove(inputWindow, ++row, col);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -123,7 +176,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
mvwdelch(inputWindow, row, --col);
|
mvwdelch(inputWindow, row, --col);
|
||||||
auto& line = lines[row];
|
auto& line = lines[row];
|
||||||
line = line.erase(col, 1);
|
line = line.erase(col, 1);
|
||||||
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow);
|
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
@ -153,8 +206,21 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
}
|
}
|
||||||
waddstr(inputWindow, (char*)line.c_str());
|
waddstr(inputWindow, (char*)line.c_str());
|
||||||
col += 4;
|
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);
|
wmove(inputWindow, row, col);
|
||||||
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@ -167,8 +233,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
|
|||||||
}
|
}
|
||||||
waddstr(inputWindow, (char*)line.c_str());
|
waddstr(inputWindow, (char*)line.c_str());
|
||||||
col++;
|
col++;
|
||||||
|
rightPos = col;
|
||||||
wmove(inputWindow, row, col);
|
wmove(inputWindow, row, col);
|
||||||
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow);
|
ParseAndUpdate(lines, diagnosticsWindow, parsedResultWindow, inputWindow);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user