2020-03-22 20:25:47 +02:00
|
|
|
#include "error.hh"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
// return basic_format?
|
2020-03-23 23:29:49 +02:00
|
|
|
string showErrLine(ErrLine &errLine)
|
2020-03-22 20:25:47 +02:00
|
|
|
{
|
|
|
|
if (errLine.columnRange.has_value())
|
|
|
|
{
|
|
|
|
return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (format("(%1%)") % errLine.lineNumber).str();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_code_lines(string &prefix, NixCode &nix_code)
|
|
|
|
{
|
2020-03-23 23:29:49 +02:00
|
|
|
|
2020-03-22 20:25:47 +02:00
|
|
|
if (nix_code.errLine.has_value())
|
|
|
|
{
|
|
|
|
// previous line of code.
|
|
|
|
if (nix_code.errLine->prevLineOfCode.has_value()) {
|
2020-03-23 23:29:49 +02:00
|
|
|
cout << format("%1% %|2$5d|| %3%")
|
|
|
|
% prefix
|
|
|
|
% (nix_code.errLine->lineNumber - 1)
|
|
|
|
% *nix_code.errLine->prevLineOfCode
|
|
|
|
<< endl;
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-23 23:29:49 +02:00
|
|
|
// line of code containing the error.%2$+5d%
|
|
|
|
cout << format("%1% %|2$5d|| %3%")
|
|
|
|
% prefix
|
|
|
|
% (nix_code.errLine->lineNumber)
|
|
|
|
% nix_code.errLine->errLineOfCode
|
|
|
|
<< endl;
|
|
|
|
|
2020-03-22 20:25:47 +02:00
|
|
|
// error arrows for the column range.
|
|
|
|
if (nix_code.errLine->columnRange.has_value())
|
|
|
|
{
|
|
|
|
int start = nix_code.errLine->columnRange->start;
|
|
|
|
std::string spaces;
|
|
|
|
for (int i = 0; i < start; ++i)
|
|
|
|
{
|
|
|
|
spaces.append(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = nix_code.errLine->columnRange->len;
|
|
|
|
std::string arrows;
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
arrows.append("^");
|
|
|
|
}
|
|
|
|
|
2020-03-23 23:29:49 +02:00
|
|
|
cout << format("%1% |%2%%3%") % prefix % spaces % arrows;
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-23 23:29:49 +02:00
|
|
|
|
2020-03-22 20:25:47 +02:00
|
|
|
// next line of code.
|
|
|
|
if (nix_code.errLine->nextLineOfCode.has_value()) {
|
2020-03-23 23:29:49 +02:00
|
|
|
cout << format("%1% %|2$5d|| %3%")
|
|
|
|
% prefix
|
|
|
|
% (nix_code.errLine->lineNumber + 1)
|
|
|
|
% *nix_code.errLine->nextLineOfCode
|
|
|
|
<< endl;
|
2020-03-22 20:25:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_error(ErrorInfo &einfo)
|
|
|
|
{
|
|
|
|
int errwidth = 80;
|
|
|
|
string prefix = " ";
|
|
|
|
|
|
|
|
string level_string;
|
|
|
|
switch (einfo.level)
|
|
|
|
{
|
|
|
|
case ErrLevel::elError:
|
|
|
|
{
|
|
|
|
level_string = "error:"; // TODO make red.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ErrLevel::elWarning:
|
|
|
|
{
|
|
|
|
level_string = "warning:"; // TODO make yellow.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 17:18:23 +02:00
|
|
|
int ndl = level_string.length() + 3 + einfo.name.length() + einfo.program.length();
|
2020-03-22 20:25:47 +02:00
|
|
|
int dashwidth = errwidth - 3 ? 3 : 80 - ndl;
|
|
|
|
|
|
|
|
string dashes;
|
|
|
|
for (int i = 0; i < dashwidth; ++i)
|
|
|
|
dashes.append("-");
|
|
|
|
|
|
|
|
// divider.
|
|
|
|
cout << format("%1%%2% %3% %4% %5% %6%")
|
|
|
|
% prefix
|
|
|
|
% level_string
|
|
|
|
% "---"
|
2020-03-24 17:18:23 +02:00
|
|
|
% einfo.name
|
2020-03-22 20:25:47 +02:00
|
|
|
% dashes
|
2020-03-24 17:18:23 +02:00
|
|
|
% einfo.program
|
2020-03-23 23:29:49 +02:00
|
|
|
<< endl;
|
2020-03-22 20:25:47 +02:00
|
|
|
|
|
|
|
// filename.
|
|
|
|
if (einfo.nixCode.has_value())
|
|
|
|
{
|
|
|
|
if (einfo.nixCode->nixFile.has_value())
|
|
|
|
{
|
|
|
|
string eline = einfo.nixCode->errLine.has_value()
|
|
|
|
? string(" ") + showErrLine(*einfo.nixCode->errLine)
|
|
|
|
: "";
|
|
|
|
|
2020-03-23 23:29:49 +02:00
|
|
|
cout << format("%1%in file: %2%%3%") % prefix % *einfo.nixCode->nixFile % eline << endl;
|
2020-03-22 20:25:47 +02:00
|
|
|
cout << prefix << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cout << format("%1%from command line argument") % prefix << endl;
|
|
|
|
cout << prefix << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// description
|
|
|
|
cout << prefix << einfo.description << endl;
|
|
|
|
cout << prefix << endl;
|
|
|
|
|
|
|
|
// lines of code.
|
|
|
|
if (einfo.nixCode.has_value())
|
|
|
|
print_code_lines(prefix, *einfo.nixCode);
|
|
|
|
|
|
|
|
// hint
|
|
|
|
cout << prefix << einfo.hint << endl;
|
|
|
|
cout << prefix << endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|