2022-08-19 19:52:57 +02:00
|
|
|
|
2023-05-23 16:07:58 +02:00
|
|
|
// FIXME writeExcerpt does not work well with the last line in a file
|
|
|
|
|
2022-08-21 20:56:58 +02:00
|
|
|
#include <sstream>
|
2022-08-24 12:36:43 +02:00
|
|
|
#include <cmath>
|
2022-08-21 20:56:58 +02:00
|
|
|
|
|
|
|
#include "zen/config.hpp"
|
|
|
|
|
2023-05-24 14:11:59 +02:00
|
|
|
#include "bolt/CST.hpp"
|
|
|
|
#include "bolt/Type.hpp"
|
2023-05-24 19:38:04 +02:00
|
|
|
#include "bolt/DiagnosticEngine.hpp"
|
2022-08-19 19:52:57 +02:00
|
|
|
#include "bolt/Diagnostics.hpp"
|
2022-08-21 20:56:58 +02:00
|
|
|
|
|
|
|
#define ANSI_RESET "\u001b[0m"
|
|
|
|
#define ANSI_BOLD "\u001b[1m"
|
2023-05-29 20:37:23 +02:00
|
|
|
#define ANSI_ITALIC "\u001b[3m"
|
2022-08-21 20:56:58 +02:00
|
|
|
#define ANSI_UNDERLINE "\u001b[4m"
|
|
|
|
#define ANSI_REVERSED "\u001b[7m"
|
|
|
|
|
|
|
|
#define ANSI_FG_BLACK "\u001b[30m"
|
|
|
|
#define ANSI_FG_RED "\u001b[31m"
|
|
|
|
#define ANSI_FG_GREEN "\u001b[32m"
|
|
|
|
#define ANSI_FG_YELLOW "\u001b[33m"
|
|
|
|
#define ANSI_FG_BLUE "\u001b[34m"
|
|
|
|
#define ANSI_FG_CYAN "\u001b[35m"
|
|
|
|
#define ANSI_FG_MAGENTA "\u001b[36m"
|
|
|
|
#define ANSI_FG_WHITE "\u001b[37m"
|
|
|
|
|
|
|
|
#define ANSI_BG_BLACK "\u001b[40m"
|
|
|
|
#define ANSI_BG_RED "\u001b[41m"
|
|
|
|
#define ANSI_BG_GREEN "\u001b[42m"
|
|
|
|
#define ANSI_BG_YELLOW "\u001b[43m"
|
|
|
|
#define ANSI_BG_BLUE "\u001b[44m"
|
|
|
|
#define ANSI_BG_CYAN "\u001b[45m"
|
|
|
|
#define ANSI_BG_MAGENTA "\u001b[46m"
|
|
|
|
#define ANSI_BG_WHITE "\u001b[47m"
|
2022-08-19 19:52:57 +02:00
|
|
|
|
|
|
|
namespace bolt {
|
|
|
|
|
2022-08-24 12:36:43 +02:00
|
|
|
template<typename T>
|
|
|
|
T countDigits(T number) {
|
|
|
|
if (number == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return std::ceil(std::log10(number+1));
|
|
|
|
}
|
|
|
|
|
2022-08-21 20:56:58 +02:00
|
|
|
Diagnostic::Diagnostic(DiagnosticKind Kind):
|
|
|
|
std::runtime_error("a compiler error occurred without being caught"), Kind(Kind) {}
|
|
|
|
|
2023-05-20 23:48:26 +02:00
|
|
|
static std::string describe(NodeKind Type) {
|
2022-08-21 20:56:58 +02:00
|
|
|
switch (Type) {
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::Identifier:
|
2023-05-21 14:50:28 +02:00
|
|
|
return "an identifier starting with a lowercase letter";
|
|
|
|
case NodeKind::IdentifierAlt:
|
|
|
|
return "an identifier starting with a capital letter";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::CustomOperator:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "an operator";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::IntegerLiteral:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "an integer literal";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::EndOfFile:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "end-of-file";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::BlockStart:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "the start of a new indented block";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::BlockEnd:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "the end of the current indented block";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::LineFoldEnd:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "the end of the current line-fold";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::LParen:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'('";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::RParen:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "')'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::LBrace:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'['";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::RBrace:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "']'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::LBracket:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'{'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::RBracket:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'}'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::Colon:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "':'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::Comma:
|
|
|
|
return "','";
|
|
|
|
case NodeKind::Equals:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'='";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::StringLiteral:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "a string literal";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::Dot:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'.'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::DotDot:
|
|
|
|
return "'..'";
|
|
|
|
case NodeKind::Tilde:
|
|
|
|
return "'~'";
|
|
|
|
case NodeKind::RArrow:
|
|
|
|
return "'->'";
|
|
|
|
case NodeKind::RArrowAlt:
|
|
|
|
return "'=>'";
|
|
|
|
case NodeKind::PubKeyword:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'pub'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::LetKeyword:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'let'";
|
2023-05-30 21:34:40 +02:00
|
|
|
case NodeKind::FnKeyword:
|
|
|
|
return "'fn'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::MutKeyword:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'mut'";
|
2023-05-22 11:55:31 +02:00
|
|
|
case NodeKind::MatchKeyword:
|
|
|
|
return "'match'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::ReturnKeyword:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'return'";
|
2023-05-20 23:48:26 +02:00
|
|
|
case NodeKind::TypeKeyword:
|
2022-08-21 20:56:58 +02:00
|
|
|
return "'type'";
|
2023-05-30 21:34:40 +02:00
|
|
|
case NodeKind::FunctionDeclaration:
|
|
|
|
return "a function declaration";
|
|
|
|
case NodeKind::VariableDeclaration:
|
|
|
|
return "a variable declaration";
|
2023-05-29 20:37:23 +02:00
|
|
|
case NodeKind::CallExpression:
|
|
|
|
return "a call-expression";
|
|
|
|
case NodeKind::InfixExpression:
|
|
|
|
return "an infix-expression";
|
|
|
|
case NodeKind::ReferenceExpression:
|
|
|
|
return "a function or variable reference";
|
|
|
|
case NodeKind::MatchExpression:
|
|
|
|
return "a match-expression";
|
2023-05-30 15:27:21 +02:00
|
|
|
case NodeKind::ConstantExpression:
|
|
|
|
return "a literal expression";
|
|
|
|
case NodeKind::IfStatement:
|
|
|
|
return "an if-statement";
|
|
|
|
case NodeKind::IfStatementPart:
|
|
|
|
return "a branch of an if-statement";
|
2022-08-21 20:56:58 +02:00
|
|
|
default:
|
|
|
|
ZEN_UNREACHABLE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 23:48:58 +02:00
|
|
|
static std::string describe(Token* T) {
|
|
|
|
switch (T->getKind()) {
|
|
|
|
case NodeKind::LineFoldEnd:
|
|
|
|
case NodeKind::BlockStart:
|
|
|
|
case NodeKind::BlockEnd:
|
|
|
|
case NodeKind::EndOfFile:
|
|
|
|
return describe(T->getKind());
|
|
|
|
default:
|
|
|
|
return "'" + T->getText() + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 19:04:25 +02:00
|
|
|
std::string describe(const Type* Ty) {
|
2022-08-21 20:56:58 +02:00
|
|
|
switch (Ty->getKind()) {
|
|
|
|
case TypeKind::Var:
|
2023-05-20 23:48:26 +02:00
|
|
|
{
|
|
|
|
auto TV = static_cast<const TVar*>(Ty);
|
|
|
|
if (TV->getVarKind() == VarKind::Rigid) {
|
|
|
|
return static_cast<const TVarRigid*>(TV)->Name;
|
|
|
|
}
|
|
|
|
return "a" + std::to_string(TV->Id);
|
|
|
|
}
|
2022-08-21 20:56:58 +02:00
|
|
|
case TypeKind::Arrow:
|
|
|
|
{
|
|
|
|
auto Y = static_cast<const TArrow*>(Ty);
|
|
|
|
std::ostringstream Out;
|
|
|
|
Out << "(";
|
|
|
|
bool First = true;
|
|
|
|
for (auto PT: Y->ParamTypes) {
|
|
|
|
if (First) First = false;
|
|
|
|
else Out << ", ";
|
|
|
|
Out << describe(PT);
|
|
|
|
}
|
|
|
|
Out << ") -> " << describe(Y->ReturnType);
|
|
|
|
return Out.str();
|
|
|
|
}
|
|
|
|
case TypeKind::Con:
|
|
|
|
{
|
|
|
|
auto Y = static_cast<const TCon*>(Ty);
|
2023-05-29 20:37:23 +02:00
|
|
|
return Y->DisplayName;
|
|
|
|
}
|
|
|
|
case TypeKind::App:
|
|
|
|
{
|
|
|
|
auto Y = static_cast<const TApp*>(Ty);
|
|
|
|
return describe(Y->Op) + " " + describe(Y->Arg);
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
2022-08-25 23:04:09 +02:00
|
|
|
case TypeKind::Tuple:
|
|
|
|
{
|
|
|
|
std::ostringstream Out;
|
|
|
|
auto Y = static_cast<const TTuple*>(Ty);
|
|
|
|
Out << "(";
|
|
|
|
if (Y->ElementTypes.size()) {
|
|
|
|
auto Iter = Y->ElementTypes.begin();
|
|
|
|
Out << describe(*Iter++);
|
|
|
|
while (Iter != Y->ElementTypes.end()) {
|
|
|
|
Out << ", " << describe(*Iter++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Out << ")";
|
|
|
|
return Out.str();
|
|
|
|
}
|
2023-05-22 17:06:31 +02:00
|
|
|
case TypeKind::TupleIndex:
|
|
|
|
{
|
|
|
|
auto Y = static_cast<const TTupleIndex*>(Ty);
|
|
|
|
return describe(Y->Ty) + "." + std::to_string(Y->I);
|
|
|
|
}
|
2023-05-29 20:37:23 +02:00
|
|
|
case TypeKind::Nil:
|
|
|
|
return "{}";
|
|
|
|
case TypeKind::Absent:
|
|
|
|
return "Abs";
|
|
|
|
case TypeKind::Present:
|
|
|
|
{
|
|
|
|
auto Y = static_cast<const TPresent*>(Ty);
|
|
|
|
return describe(Y->Ty);
|
|
|
|
}
|
|
|
|
case TypeKind::Field:
|
|
|
|
{
|
|
|
|
auto Y = static_cast<const TField*>(Ty);
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "{ " << Y->Name << ": " << describe(Y->Ty);
|
|
|
|
Ty = Y->RestTy;
|
|
|
|
while (Ty->getKind() == TypeKind::Field) {
|
|
|
|
auto Y = static_cast<const TField*>(Ty);
|
|
|
|
out << "; " + Y->Name + ": " + describe(Y->Ty);
|
|
|
|
Ty = Y->RestTy;
|
|
|
|
}
|
|
|
|
if (Ty->getKind() != TypeKind::Nil) {
|
|
|
|
out << "; " + describe(Ty);
|
|
|
|
}
|
|
|
|
out << " }";
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeForegroundANSI(Color C, std::ostream& Out) {
|
|
|
|
switch (C) {
|
|
|
|
case Color::None:
|
|
|
|
break;
|
|
|
|
case Color::Black:
|
|
|
|
Out << ANSI_FG_BLACK;
|
|
|
|
break;
|
|
|
|
case Color::White:
|
|
|
|
Out << ANSI_FG_WHITE;
|
|
|
|
break;
|
|
|
|
case Color::Red:
|
|
|
|
Out << ANSI_FG_RED;
|
|
|
|
break;
|
|
|
|
case Color::Yellow:
|
|
|
|
Out << ANSI_FG_YELLOW;
|
|
|
|
break;
|
|
|
|
case Color::Green:
|
|
|
|
Out << ANSI_FG_GREEN;
|
|
|
|
break;
|
|
|
|
case Color::Blue:
|
|
|
|
Out << ANSI_FG_BLUE;
|
|
|
|
break;
|
|
|
|
case Color::Cyan:
|
|
|
|
Out << ANSI_FG_CYAN;
|
|
|
|
break;
|
|
|
|
case Color::Magenta:
|
|
|
|
Out << ANSI_FG_MAGENTA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeBackgroundANSI(Color C, std::ostream& Out) {
|
|
|
|
switch (C) {
|
|
|
|
case Color::None:
|
|
|
|
break;
|
|
|
|
case Color::Black:
|
|
|
|
Out << ANSI_BG_BLACK;
|
|
|
|
break;
|
|
|
|
case Color::White:
|
|
|
|
Out << ANSI_BG_WHITE;
|
|
|
|
break;
|
|
|
|
case Color::Red:
|
|
|
|
Out << ANSI_BG_RED;
|
|
|
|
break;
|
|
|
|
case Color::Yellow:
|
|
|
|
Out << ANSI_BG_YELLOW;
|
|
|
|
break;
|
|
|
|
case Color::Green:
|
|
|
|
Out << ANSI_BG_GREEN;
|
|
|
|
break;
|
|
|
|
case Color::Blue:
|
|
|
|
Out << ANSI_BG_BLUE;
|
|
|
|
break;
|
|
|
|
case Color::Cyan:
|
|
|
|
Out << ANSI_BG_CYAN;
|
|
|
|
break;
|
|
|
|
case Color::Magenta:
|
|
|
|
Out << ANSI_BG_MAGENTA;
|
|
|
|
break;
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 19:38:04 +02:00
|
|
|
DiagnosticStore::~DiagnosticStore() {
|
|
|
|
for (auto D: Diagnostics) {
|
|
|
|
delete D;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-21 20:56:58 +02:00
|
|
|
ConsoleDiagnostics::ConsoleDiagnostics(std::ostream& Out):
|
|
|
|
Out(Out) {}
|
|
|
|
|
2022-08-24 12:36:43 +02:00
|
|
|
void ConsoleDiagnostics::setForegroundColor(Color C) {
|
2023-05-29 20:37:23 +02:00
|
|
|
ActiveStyle.setForegroundColor(C);
|
|
|
|
if (!EnableColors) {
|
|
|
|
return;
|
2022-08-24 12:36:43 +02:00
|
|
|
}
|
2023-05-29 20:37:23 +02:00
|
|
|
writeForegroundANSI(C, Out);
|
2022-08-24 12:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::setBackgroundColor(Color C) {
|
2023-05-29 20:37:23 +02:00
|
|
|
ActiveStyle.setBackgroundColor(C);
|
|
|
|
if (!EnableColors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (C == Color::None) {
|
|
|
|
Out << ANSI_RESET;
|
|
|
|
applyStyles();
|
|
|
|
}
|
|
|
|
writeBackgroundANSI(C, Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::applyStyles() {
|
|
|
|
if (ActiveStyle.isBold()) {
|
|
|
|
Out << ANSI_BOLD;
|
|
|
|
}
|
|
|
|
if (ActiveStyle.isUnderline()) {
|
|
|
|
Out << ANSI_UNDERLINE;
|
|
|
|
}
|
|
|
|
if (ActiveStyle.isItalic()) {
|
|
|
|
Out << ANSI_ITALIC;
|
|
|
|
}
|
|
|
|
if (ActiveStyle.hasBackgroundColor()) {
|
|
|
|
setBackgroundColor(ActiveStyle.getBackgroundColor());
|
|
|
|
}
|
|
|
|
if (ActiveStyle.hasForegroundColor()) {
|
|
|
|
setForegroundColor(ActiveStyle.getForegroundColor());
|
2022-08-24 12:36:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:57:26 +02:00
|
|
|
void ConsoleDiagnostics::setBold(bool Enable) {
|
2023-05-29 20:37:23 +02:00
|
|
|
ActiveStyle.setBold(Enable);
|
|
|
|
if (!EnableColors) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-24 20:57:26 +02:00
|
|
|
if (Enable) {
|
|
|
|
Out << ANSI_BOLD;
|
2023-05-29 20:37:23 +02:00
|
|
|
} else {
|
|
|
|
Out << ANSI_RESET;
|
|
|
|
applyStyles();
|
2022-08-24 20:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::setItalic(bool Enable) {
|
2023-05-29 20:37:23 +02:00
|
|
|
ActiveStyle.setItalic(Enable);
|
|
|
|
if (!EnableColors) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-24 20:57:26 +02:00
|
|
|
if (Enable) {
|
2023-05-29 20:37:23 +02:00
|
|
|
Out << ANSI_ITALIC;
|
|
|
|
} else {
|
|
|
|
Out << ANSI_RESET;
|
|
|
|
applyStyles();
|
2022-08-24 20:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::setUnderline(bool Enable) {
|
2023-05-29 20:37:23 +02:00
|
|
|
ActiveStyle.setItalic(Enable);
|
|
|
|
if (!EnableColors) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-24 20:57:26 +02:00
|
|
|
if (Enable) {
|
|
|
|
Out << ANSI_UNDERLINE;
|
2023-05-29 20:37:23 +02:00
|
|
|
} else {
|
|
|
|
Out << ANSI_RESET;
|
|
|
|
applyStyles();
|
2022-08-24 20:57:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:36:43 +02:00
|
|
|
void ConsoleDiagnostics::resetStyles() {
|
2023-05-29 20:37:23 +02:00
|
|
|
ActiveStyle.reset();
|
2022-08-24 12:36:43 +02:00
|
|
|
if (EnableColors) {
|
|
|
|
Out << ANSI_RESET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeGutter(
|
|
|
|
std::size_t GutterWidth,
|
|
|
|
std::size_t Line
|
|
|
|
) {
|
|
|
|
auto LineNumberDigitCount = countDigits(Line);
|
|
|
|
auto LeadingSpaces = GutterWidth - LineNumberDigitCount;
|
|
|
|
Out << " ";
|
|
|
|
setForegroundColor(Color::Black);
|
|
|
|
setBackgroundColor(Color::White);
|
|
|
|
for (std::size_t i = 0; i < LeadingSpaces; i++) {
|
|
|
|
Out << ' ';
|
|
|
|
}
|
|
|
|
Out << Line;
|
|
|
|
resetStyles();
|
|
|
|
Out << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeHighlight(
|
|
|
|
std::size_t GutterWidth,
|
|
|
|
TextRange Range,
|
|
|
|
Color HighlightColor,
|
|
|
|
std::size_t Line,
|
|
|
|
std::size_t LineLength
|
|
|
|
) {
|
|
|
|
if (Line < Range.Start.Line || Range.End.Line < Line) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out << " ";
|
|
|
|
setBackgroundColor(Color::White);
|
|
|
|
for (std::size_t i = 0; i < GutterWidth; i++) {
|
|
|
|
Out << ' ';
|
|
|
|
}
|
|
|
|
resetStyles();
|
|
|
|
Out << ' ';
|
|
|
|
std::size_t start_column = Range.Start.Line == Line ? Range.Start.Column : 1;
|
|
|
|
std::size_t end_column = Range.End.Line == Line ? Range.End.Column : LineLength+1;
|
|
|
|
for (std::size_t i = 1; i < start_column; i++) {
|
|
|
|
Out << ' ';
|
|
|
|
}
|
|
|
|
setForegroundColor(HighlightColor);
|
2023-05-27 18:48:44 +02:00
|
|
|
if (start_column == end_column) {
|
|
|
|
Out << "↖";
|
|
|
|
} else {
|
|
|
|
for (std::size_t i = start_column; i < end_column; i++) {
|
|
|
|
Out << '~';
|
|
|
|
}
|
2022-08-24 12:36:43 +02:00
|
|
|
}
|
|
|
|
resetStyles();
|
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeExcerpt(
|
2023-05-24 14:11:59 +02:00
|
|
|
const TextFile& File,
|
2022-08-24 12:36:43 +02:00
|
|
|
TextRange ToPrint,
|
|
|
|
TextRange ToHighlight,
|
|
|
|
Color HighlightColor
|
|
|
|
) {
|
|
|
|
|
|
|
|
auto Text = File.getText();
|
|
|
|
auto StartPos = ToPrint.Start;
|
|
|
|
auto EndPos = ToPrint.End;
|
|
|
|
auto StartLine = StartPos.Line-1 > ExcerptLinesPre ? StartPos.Line - ExcerptLinesPost : 1;
|
|
|
|
auto StartOffset = File.getStartOffset(StartLine);
|
|
|
|
auto EndLine = std::min(File.getLineCount(), EndPos.Line + ExcerptLinesPost);
|
|
|
|
auto EndOffset = File.getStartOffset(EndLine+1);
|
|
|
|
auto GutterWidth = std::max<std::size_t>(2, countDigits(EndLine+1));
|
|
|
|
auto HighlightStart = ToHighlight.Start;
|
|
|
|
auto HighlightEnd = ToHighlight.End;
|
|
|
|
auto HighlightRange = TextRange { HighlightStart, HighlightEnd };
|
|
|
|
|
|
|
|
std::size_t CurrColumn = 1;
|
|
|
|
std::size_t CurrLine = StartLine;
|
|
|
|
writeGutter(GutterWidth, CurrLine);
|
|
|
|
for (std::size_t i = StartOffset; i < EndOffset; i++) {
|
|
|
|
auto C = Text[i];
|
|
|
|
Out << C;
|
|
|
|
if (C == '\n') {
|
|
|
|
writeHighlight(GutterWidth, HighlightRange, HighlightColor, CurrLine, CurrColumn);
|
|
|
|
if (CurrLine == EndLine && C == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CurrLine++;
|
|
|
|
writeGutter(GutterWidth, CurrLine);
|
|
|
|
CurrColumn = 1;
|
|
|
|
} else {
|
|
|
|
CurrColumn++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:11:59 +02:00
|
|
|
void ConsoleDiagnostics::write(const std::string_view& S) {
|
|
|
|
Out << S;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::write(std::size_t I) {
|
|
|
|
Out << I;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeBinding(const ByteString& Name) {
|
|
|
|
write("'");
|
|
|
|
write(Name);
|
|
|
|
write("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeType(const Type* Ty) {
|
2023-05-29 20:37:23 +02:00
|
|
|
TypePath Path;
|
|
|
|
writeType(Ty, Path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeType(const Type* Ty, const TypePath& Underline) {
|
|
|
|
|
2023-05-24 14:11:59 +02:00
|
|
|
setForegroundColor(Color::Green);
|
2023-05-29 20:37:23 +02:00
|
|
|
|
|
|
|
class TypePrinter : public ConstTypeVisitor {
|
|
|
|
|
|
|
|
TypePath Path;
|
|
|
|
ConsoleDiagnostics& W;
|
|
|
|
const TypePath& Underline;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
TypePrinter(ConsoleDiagnostics& W, const TypePath& Underline):
|
|
|
|
W(W), Underline(Underline) {}
|
|
|
|
|
|
|
|
bool shouldUnderline() const {
|
|
|
|
return !Underline.empty() && Path == Underline;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enterType(const Type* Ty) override {
|
|
|
|
if (shouldUnderline()) {
|
|
|
|
W.setUnderline(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void exitType(const Type* Ty) override {
|
|
|
|
if (shouldUnderline()) {
|
|
|
|
W.setUnderline(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitAppType(const TApp *Ty) override {
|
|
|
|
auto Y = static_cast<const TApp*>(Ty);
|
|
|
|
Path.push_back(TypeIndex::forAppOpType());
|
|
|
|
visit(Y->Op);
|
|
|
|
Path.pop_back();
|
|
|
|
W.write(" ");
|
|
|
|
Path.push_back(TypeIndex::forAppArgType());
|
|
|
|
visit(Y->Arg);
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitVarType(const TVar* Ty) override {
|
|
|
|
if (Ty->getVarKind() == VarKind::Rigid) {
|
|
|
|
W.write(static_cast<const TVarRigid*>(Ty)->Name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
W.write("a");
|
|
|
|
W.write(Ty->Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitConType(const TCon *Ty) override {
|
|
|
|
W.write(Ty->DisplayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitArrowType(const TArrow* Ty) override {
|
|
|
|
W.write("(");
|
|
|
|
bool First = true;
|
|
|
|
std::size_t I = 0;
|
|
|
|
for (auto PT: Ty->ParamTypes) {
|
|
|
|
if (First) First = false;
|
|
|
|
else W.write(", ");
|
|
|
|
Path.push_back(TypeIndex::forArrowParamType(I++));
|
|
|
|
visit(PT);
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
W.write(") -> ");
|
|
|
|
Path.push_back(TypeIndex::forArrowReturnType());
|
|
|
|
visit(Ty->ReturnType);
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitTupleType(const TTuple *Ty) override {
|
|
|
|
W.write("(");
|
|
|
|
if (Ty->ElementTypes.size()) {
|
|
|
|
auto Iter = Ty->ElementTypes.begin();
|
|
|
|
Path.push_back(TypeIndex::forTupleElement(0));
|
|
|
|
visit(*Iter++);
|
|
|
|
Path.pop_back();
|
|
|
|
std::size_t I = 1;
|
|
|
|
while (Iter != Ty->ElementTypes.end()) {
|
|
|
|
W.write(", ");
|
|
|
|
Path.push_back(TypeIndex::forTupleElement(I++));
|
|
|
|
visit(*Iter++);
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
W.write(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitTupleIndexType(const TTupleIndex *Ty) override {
|
|
|
|
Path.push_back(TypeIndex::forTupleIndexType());
|
|
|
|
visit(Ty->Ty);
|
|
|
|
Path.pop_back();
|
|
|
|
W.write(".");
|
|
|
|
W.write(Ty->I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitNilType(const TNil *Ty) override {
|
|
|
|
W.write("{}");
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitAbsentType(const TAbsent *Ty) override {
|
|
|
|
W.write("Abs");
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitPresentType(const TPresent *Ty) override {
|
|
|
|
Path.push_back(TypeIndex::forPresentType());
|
|
|
|
visit(Ty->Ty);
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitFieldType(const TField* Ty) override {
|
|
|
|
W.write("{ ");
|
|
|
|
W.write(Ty->Name);
|
|
|
|
W.write(": ");
|
|
|
|
Path.push_back(TypeIndex::forFieldType());
|
|
|
|
visit(Ty->Ty);
|
|
|
|
Path.pop_back();
|
|
|
|
auto Ty2 = Ty->RestTy;
|
|
|
|
Path.push_back(TypeIndex::forFieldRest());
|
|
|
|
std::size_t I = 1;
|
|
|
|
while (Ty2->getKind() == TypeKind::Field) {
|
|
|
|
auto Y = static_cast<const TField*>(Ty2);
|
|
|
|
W.write("; ");
|
|
|
|
W.write(Y->Name);
|
|
|
|
W.write(": ");
|
|
|
|
Path.push_back(TypeIndex::forFieldType());
|
|
|
|
visit(Y->Ty);
|
|
|
|
Path.pop_back();
|
|
|
|
Ty2 = Y->RestTy;
|
|
|
|
Path.push_back(TypeIndex::forFieldRest());
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
if (Ty2->getKind() != TypeKind::Nil) {
|
|
|
|
W.write("; ");
|
|
|
|
visit(Ty);
|
|
|
|
}
|
|
|
|
W.write(" }");
|
|
|
|
for (auto K = 0; K < I; K++) {
|
|
|
|
Path.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
TypePrinter P { *this, Underline };
|
|
|
|
P.visit(Ty);
|
|
|
|
|
2023-05-24 14:11:59 +02:00
|
|
|
resetStyles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeType(std::size_t I) {
|
|
|
|
setForegroundColor(Color::Green);
|
|
|
|
write(I);
|
|
|
|
resetStyles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeNode(const Node* N) {
|
|
|
|
auto Range = N->getRange();
|
|
|
|
writeExcerpt(N->getSourceFile()->getTextFile(), Range, Range, Color::Red);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeLoc(const TextFile& File, const TextLoc& Loc) {
|
|
|
|
setForegroundColor(Color::Yellow);
|
|
|
|
write(File.getPath());
|
|
|
|
write(":");
|
|
|
|
write(Loc.Line);
|
|
|
|
write(":");
|
|
|
|
write(Loc.Column);
|
|
|
|
write(":");
|
|
|
|
resetStyles();
|
|
|
|
}
|
2022-08-21 20:56:58 +02:00
|
|
|
|
2023-05-24 14:11:59 +02:00
|
|
|
void ConsoleDiagnostics::writePrefix(const Diagnostic& D) {
|
|
|
|
setForegroundColor(Color::Red);
|
|
|
|
setBold(true);
|
|
|
|
write("error: ");
|
|
|
|
resetStyles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeTypeclassName(const ByteString& Name) {
|
|
|
|
setForegroundColor(Color::Magenta);
|
|
|
|
write(Name);
|
|
|
|
resetStyles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::writeTypeclassSignature(const TypeclassSignature& Sig) {
|
|
|
|
setForegroundColor(Color::Magenta);
|
|
|
|
write(Sig.Id);
|
|
|
|
for (auto TV: Sig.Params) {
|
|
|
|
write(" ");
|
|
|
|
write(describe(TV));
|
|
|
|
}
|
|
|
|
resetStyles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::addDiagnostic(Diagnostic* D) {
|
|
|
|
|
2023-05-24 19:38:04 +02:00
|
|
|
printDiagnostic(*D);
|
|
|
|
|
|
|
|
// Since this DiagnosticEngine is expected to own the diagnostic, we simply
|
|
|
|
// destroy the processed diagnostic so that there are no memory leaks.
|
|
|
|
delete D;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleDiagnostics::printDiagnostic(const Diagnostic& D) {
|
|
|
|
|
|
|
|
switch (D.getKind()) {
|
2022-08-21 20:56:58 +02:00
|
|
|
|
|
|
|
case DiagnosticKind::BindingNotFound:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const BindingNotFoundDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
2023-05-26 14:30:50 +02:00
|
|
|
write("binding ");
|
2023-05-24 14:11:59 +02:00
|
|
|
writeBinding(E.Name);
|
|
|
|
write(" was not found\n\n");
|
2022-08-25 19:04:25 +02:00
|
|
|
if (E.Initiator != nullptr) {
|
|
|
|
auto Range = E.Initiator->getRange();
|
|
|
|
//std::cerr << Range.Start.Line << ":" << Range.Start.Column << "-" << Range.End.Line << ":" << Range.End.Column << "\n";
|
|
|
|
writeExcerpt(E.Initiator->getSourceFile()->getTextFile(), Range, Range, Color::Red);
|
|
|
|
Out << "\n";
|
|
|
|
}
|
2023-05-24 14:11:59 +02:00
|
|
|
break;
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::UnexpectedToken:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const UnexpectedTokenDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
writeLoc(E.File, E.Actual->getStartLoc());
|
|
|
|
write(" expected ");
|
2022-08-21 20:56:58 +02:00
|
|
|
switch (E.Expected.size()) {
|
|
|
|
case 0:
|
2023-05-24 14:11:59 +02:00
|
|
|
write("nothing");
|
2022-08-21 20:56:58 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-05-24 14:11:59 +02:00
|
|
|
write(describe(E.Expected[0]));
|
2022-08-21 20:56:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
auto Iter = E.Expected.begin();
|
|
|
|
Out << describe(*Iter++);
|
2023-05-20 23:48:26 +02:00
|
|
|
NodeKind Prev = *Iter++;
|
2022-08-21 20:56:58 +02:00
|
|
|
while (Iter != E.Expected.end()) {
|
2023-05-24 14:11:59 +02:00
|
|
|
write(", ");
|
|
|
|
write(describe(Prev));
|
2022-08-21 20:56:58 +02:00
|
|
|
Prev = *Iter++;
|
|
|
|
}
|
2023-05-24 14:11:59 +02:00
|
|
|
write(" or ");
|
|
|
|
write(describe(Prev));
|
2022-08-21 20:56:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2023-05-26 23:48:58 +02:00
|
|
|
write(" but instead got ");
|
|
|
|
write(describe(E.Actual));
|
|
|
|
write("\n\n");
|
2022-08-24 20:57:26 +02:00
|
|
|
writeExcerpt(E.File, E.Actual->getRange(), E.Actual->getRange(), Color::Red);
|
2023-05-24 14:11:59 +02:00
|
|
|
write("\n");
|
|
|
|
break;
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::UnexpectedString:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const UnexpectedStringDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
writeLoc(E.File, E.Location);
|
|
|
|
write(" unexpected '");
|
2022-08-21 20:56:58 +02:00
|
|
|
for (auto Chr: E.Actual) {
|
|
|
|
switch (Chr) {
|
|
|
|
case '\\':
|
2023-05-24 14:11:59 +02:00
|
|
|
write("\\\\");
|
2022-08-21 20:56:58 +02:00
|
|
|
break;
|
|
|
|
case '\'':
|
2023-05-24 14:11:59 +02:00
|
|
|
write("\\'");
|
2022-08-21 20:56:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-24 14:11:59 +02:00
|
|
|
write(Chr);
|
2022-08-21 20:56:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-05-24 14:11:59 +02:00
|
|
|
write("'\n\n");
|
2022-08-24 20:57:26 +02:00
|
|
|
TextRange Range { E.Location, E.Location + E.Actual };
|
|
|
|
writeExcerpt(E.File, Range, Range, Color::Red);
|
2023-05-24 14:11:59 +02:00
|
|
|
write("\n");
|
|
|
|
break;
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::UnificationError:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const UnificationErrorDiagnostic&>(D);
|
2023-05-29 20:37:23 +02:00
|
|
|
auto Left = E.OrigLeft->resolve(E.LeftPath);
|
|
|
|
auto Right = E.OrigRight->resolve(E.RightPath);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
write("the types ");
|
|
|
|
writeType(Left);
|
|
|
|
write(" and ");
|
|
|
|
writeType(Right);
|
|
|
|
write(" failed to match\n\n");
|
2023-05-29 20:37:23 +02:00
|
|
|
setForegroundColor(Color::Yellow);
|
|
|
|
setBold(true);
|
|
|
|
write(" info: ");
|
|
|
|
resetStyles();
|
|
|
|
write("due to an equality constraint on ");
|
|
|
|
write(describe(E.Source->getKind()));
|
|
|
|
write(":\n\n");
|
|
|
|
write(" - left type ");
|
|
|
|
writeType(E.OrigLeft, E.LeftPath);
|
|
|
|
write("\n");
|
|
|
|
write(" - right type ");
|
|
|
|
writeType(E.OrigRight, E.RightPath);
|
|
|
|
write("\n\n");
|
|
|
|
writeNode(E.Source);
|
|
|
|
write("\n");
|
|
|
|
// if (E.Left != E.OrigLeft) {
|
|
|
|
// setForegroundColor(Color::Yellow);
|
|
|
|
// setBold(true);
|
|
|
|
// write(" info: ");
|
|
|
|
// resetStyles();
|
|
|
|
// write("the type ");
|
|
|
|
// writeType(E.Left);
|
|
|
|
// write(" occurs in the full type ");
|
|
|
|
// writeType(E.OrigLeft);
|
|
|
|
// write("\n\n");
|
|
|
|
// }
|
|
|
|
// if (E.Right != E.OrigRight) {
|
|
|
|
// setForegroundColor(Color::Yellow);
|
|
|
|
// setBold(true);
|
|
|
|
// write(" info: ");
|
|
|
|
// resetStyles();
|
|
|
|
// write("the type ");
|
|
|
|
// writeType(E.Right);
|
|
|
|
// write(" occurs in the full type ");
|
|
|
|
// writeType(E.OrigRight);
|
|
|
|
// write("\n\n");
|
|
|
|
// }
|
2023-05-24 14:11:59 +02:00
|
|
|
break;
|
2023-05-20 23:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::TypeclassMissing:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const TypeclassMissingDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
write("the type class ");
|
|
|
|
writeTypeclassSignature(E.Sig);
|
|
|
|
write(" is missing from the declaration's type signature\n\n");
|
|
|
|
writeNode(E.Decl);
|
|
|
|
write("\n\n");
|
|
|
|
break;
|
2023-05-20 23:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::InstanceNotFound:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const InstanceNotFoundDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
write("a type class instance ");
|
|
|
|
writeTypeclassName(E.TypeclassName);
|
2023-05-24 19:00:02 +02:00
|
|
|
write(" ");
|
2023-05-24 14:11:59 +02:00
|
|
|
writeType(E.Ty);
|
|
|
|
write(" was not found.\n\n");
|
|
|
|
writeNode(E.Source);
|
|
|
|
write("\n");
|
|
|
|
break;
|
2023-05-20 23:48:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::ClassNotFound:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const ClassNotFoundDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
write("the type class ");
|
|
|
|
writeTypeclassName(E.Name);
|
|
|
|
write(" was not found.\n\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::TupleIndexOutOfRange:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const TupleIndexOutOfRangeDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
write("the index ");
|
|
|
|
writeType(E.I);
|
|
|
|
write(" is out of range for tuple ");
|
|
|
|
writeType(E.Tuple);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DiagnosticKind::InvalidTypeToTypeclass:
|
|
|
|
{
|
2023-05-24 19:38:04 +02:00
|
|
|
auto E = static_cast<const InvalidTypeToTypeclassDiagnostic&>(D);
|
2023-05-24 14:11:59 +02:00
|
|
|
writePrefix(E);
|
|
|
|
write("the type ");
|
|
|
|
writeType(E.Actual);
|
|
|
|
write(" was applied to type class names ");
|
|
|
|
bool First = true;
|
|
|
|
for (auto Class: E.Classes) {
|
|
|
|
if (First) First = false;
|
|
|
|
else write(", ");
|
|
|
|
writeTypeclassName(Class);
|
|
|
|
}
|
|
|
|
write(" but this is invalid\n\n");
|
|
|
|
break;
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 20:37:23 +02:00
|
|
|
case DiagnosticKind::FieldNotFound:
|
|
|
|
{
|
|
|
|
auto E = static_cast<const FieldNotFoundDiagnostic&>(D);
|
|
|
|
writePrefix(E);
|
|
|
|
write("the field '");
|
|
|
|
write(E.Name);
|
|
|
|
write("' was required in one type but not found in another\n\n");
|
|
|
|
writeNode(E.Source);
|
|
|
|
write("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-21 20:56:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-08-19 19:52:57 +02:00
|
|
|
|
|
|
|
}
|