remove columnrange; switch to fmt in error.cc

This commit is contained in:
Ben Burdette 2020-04-07 10:14:15 -06:00
parent 2248cc6716
commit 20c0984a46
2 changed files with 36 additions and 43 deletions

View file

@ -10,10 +10,10 @@ std::optional<string> ErrorInfo::programName = std::nullopt;
string showErrLine(ErrLine &errLine) string showErrLine(ErrLine &errLine)
{ {
if (errLine.columnRange.has_value()) { if (errLine.column > 0) {
return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str(); return fmt("(%1%:%2%)", errLine.lineNumber, errLine.column);
} else { } else {
return (format("(%1%)") % errLine.lineNumber).str(); return fmt("(%1%)", errLine.lineNumber);
}; };
} }
@ -23,45 +23,45 @@ void printCodeLines(string &prefix, NixCode &nixCode)
if (nixCode.errLine.has_value()) { if (nixCode.errLine.has_value()) {
// previous line of code. // previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) { if (nixCode.errLine->prevLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%") std::cout << fmt("%1% %|2$5d|| %3%",
% prefix prefix,
% (nixCode.errLine->lineNumber - 1) (nixCode.errLine->lineNumber - 1),
% *nixCode.errLine->prevLineOfCode *nixCode.errLine->prevLineOfCode)
<< std::endl; << std::endl;
} }
// line of code containing the error.%2$+5d% // line of code containing the error.%2$+5d%
std::cout << format("%1% %|2$5d|| %3%") std::cout << fmt("%1% %|2$5d|| %3%",
% prefix prefix,
% (nixCode.errLine->lineNumber) (nixCode.errLine->lineNumber),
% nixCode.errLine->errLineOfCode nixCode.errLine->errLineOfCode)
<< std::endl; << std::endl;
// error arrows for the column range. // error arrows for the column range.
if (nixCode.errLine->columnRange.has_value()) { if (nixCode.errLine->column > 0) {
int start = nixCode.errLine->columnRange->start; int start = nixCode.errLine->column;
std::string spaces; std::string spaces;
for (int i = 0; i < start; ++i) { for (int i = 0; i < start; ++i) {
spaces.append(" "); spaces.append(" ");
} }
int len = nixCode.errLine->columnRange->len; // for now, length of 1.
std::string arrows; std::string arrows("^");
for (int i = 0; i < len; ++i) {
arrows.append("^");
}
std::cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << std::endl; std::cout << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
prefix,
spaces,
arrows) << std::endl;
} }
// next line of code. // next line of code.
if (nixCode.errLine->nextLineOfCode.has_value()) { if (nixCode.errLine->nextLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%") std::cout << fmt("%1% %|2$5d|| %3%",
% prefix prefix,
% (nixCode.errLine->lineNumber + 1) (nixCode.errLine->lineNumber + 1),
% *nixCode.errLine->nextLineOfCode *nixCode.errLine->nextLineOfCode)
<< std::endl; << std::endl;
} }
@ -89,7 +89,7 @@ void printErrorInfo(ErrorInfo &einfo)
break; break;
} }
default: { default: {
levelString = (format("invalid error level: %1%") % einfo.level).str(); levelString = fmt("invalid error level: %1%", einfo.level);
break; break;
} }
} }
@ -102,13 +102,13 @@ void printErrorInfo(ErrorInfo &einfo)
dashes.append("-"); dashes.append("-");
// divider. // divider.
std::cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL) std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL
% prefix , prefix
% levelString , levelString
% "---" , "---"
% einfo.name , einfo.name
% dashes , dashes
% einfo.programName.value_or("") , einfo.programName.value_or(""))
<< std::endl; << std::endl;
// filename. // filename.
@ -118,11 +118,11 @@ void printErrorInfo(ErrorInfo &einfo)
? string(" ") + showErrLine(*einfo.nixCode->errLine) ? string(" ") + showErrLine(*einfo.nixCode->errLine)
: ""; : "";
std::cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL) std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL
% prefix % *einfo.nixCode->nixFile % eline << std::endl; , prefix, *einfo.nixCode->nixFile, eline) << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} else { } else {
std::cout << format("%1%from command line argument") % prefix << std::endl; std::cout << fmt("%1%from command line argument", prefix) << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }
} }

View file

@ -17,20 +17,13 @@ typedef enum {
elError elError
} ErrLevel; } ErrLevel;
class ColumnRange
{
public:
unsigned int start;
unsigned int len;
};
class ErrorInfo; class ErrorInfo;
class ErrLine class ErrLine
{ {
public: public:
int lineNumber; int lineNumber;
std::optional<ColumnRange> columnRange; int column;
std::optional<string> prevLineOfCode; std::optional<string> prevLineOfCode;
string errLineOfCode; string errLineOfCode;
std::optional<string> nextLineOfCode; std::optional<string> nextLineOfCode;
@ -159,7 +152,7 @@ public:
{ {
GetEI().ensureNixCode().nixFile = aPos.file; GetEI().ensureNixCode().nixFile = aPos.file;
GetEI().ensureNixCode().ensureErrLine().lineNumber = aPos.line; GetEI().ensureNixCode().ensureErrLine().lineNumber = aPos.line;
GetEI().ensureNixCode().ensureErrLine().columnRange = { .start = aPos.column, .len = 1 }; GetEI().ensureNixCode().ensureErrLine().column = aPos.column;
return *this; return *this;
} }
protected: protected: