indenting

This commit is contained in:
Ben Burdette 2020-04-01 16:20:20 -06:00
parent 8713aeac5e
commit dd7b8183a5
2 changed files with 250 additions and 250 deletions

View file

@ -10,155 +10,155 @@ optional<string> ErrorInfo::programName = std::nullopt;
// return basic_format? // return basic_format?
string showErrLine(ErrLine &errLine) string showErrLine(ErrLine &errLine)
{ {
if (errLine.columnRange.has_value()) if (errLine.columnRange.has_value())
{ {
return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str(); return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str();
} }
else else
{ {
return (format("(%1%)") % errLine.lineNumber).str(); return (format("(%1%)") % errLine.lineNumber).str();
}; };
} }
void printCodeLines(string &prefix, NixCode &nixCode) void printCodeLines(string &prefix, NixCode &nixCode)
{ {
if (nixCode.errLine.has_value()) if (nixCode.errLine.has_value())
{
// previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) {
cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errLine->lineNumber - 1)
% *nixCode.errLine->prevLineOfCode
<< endl;
}
// line of code containing the error.%2$+5d%
cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errLine->lineNumber)
% nixCode.errLine->errLineOfCode
<< endl;
// error arrows for the column range.
if (nixCode.errLine->columnRange.has_value())
{ {
int start = nixCode.errLine->columnRange->start; // previous line of code.
std::string spaces; if (nixCode.errLine->prevLineOfCode.has_value()) {
for (int i = 0; i < start; ++i) cout << format("%1% %|2$5d|| %3%")
{ % prefix
spaces.append(" "); % (nixCode.errLine->lineNumber - 1)
} % *nixCode.errLine->prevLineOfCode
<< endl;
}
// line of code containing the error.%2$+5d%
cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errLine->lineNumber)
% nixCode.errLine->errLineOfCode
<< endl;
// error arrows for the column range.
if (nixCode.errLine->columnRange.has_value())
{
int start = nixCode.errLine->columnRange->start;
std::string spaces;
for (int i = 0; i < start; ++i)
{
spaces.append(" ");
}
int len = nixCode.errLine->columnRange->len;
std::string arrows;
for (int i = 0; i < len; ++i)
{
arrows.append("^");
}
cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << endl;
}
// next line of code.
if (nixCode.errLine->nextLineOfCode.has_value()) {
cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errLine->lineNumber + 1)
% *nixCode.errLine->nextLineOfCode
<< endl;
}
int len = nixCode.errLine->columnRange->len;
std::string arrows;
for (int i = 0; i < len; ++i)
{
arrows.append("^");
}
cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << endl;
} }
// next line of code.
if (nixCode.errLine->nextLineOfCode.has_value()) {
cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errLine->lineNumber + 1)
% *nixCode.errLine->nextLineOfCode
<< endl;
}
}
} }
void printErrorInfo(ErrorInfo &einfo) void printErrorInfo(ErrorInfo &einfo)
{ {
int errwidth = 80; int errwidth = 80;
string prefix = " "; string prefix = " ";
string levelString; string levelString;
switch (einfo.level) switch (einfo.level)
{
case ErrLevel::elError:
{
levelString = ANSI_RED;
levelString += "error:";
levelString += ANSI_NORMAL;
break;
}
case ErrLevel::elWarning:
{
levelString = ANSI_YELLOW;
levelString += "warning:";
levelString += ANSI_NORMAL;
break;
}
default:
{
levelString = (format("invalid error level: %1%") % einfo.level).str();
break;
}
}
int ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length();
int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl;
string dashes;
for (int i = 0; i < dashwidth; ++i)
dashes.append("-");
// divider.
cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL)
% prefix
% levelString
% "---"
% einfo.name
% dashes
% einfo.programName.value_or("")
<< endl;
// filename.
if (einfo.nixCode.has_value())
{
if (einfo.nixCode->nixFile.has_value())
{ {
string eline = einfo.nixCode->errLine.has_value() case ErrLevel::elError:
? string(" ") + showErrLine(*einfo.nixCode->errLine) {
: ""; levelString = ANSI_RED;
levelString += "error:";
cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL) levelString += ANSI_NORMAL;
% prefix % *einfo.nixCode->nixFile % eline << endl; break;
cout << prefix << endl; }
case ErrLevel::elWarning:
{
levelString = ANSI_YELLOW;
levelString += "warning:";
levelString += ANSI_NORMAL;
break;
}
default:
{
levelString = (format("invalid error level: %1%") % einfo.level).str();
break;
}
} }
else
int ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length();
int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl;
string dashes;
for (int i = 0; i < dashwidth; ++i)
dashes.append("-");
// divider.
cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL)
% prefix
% levelString
% "---"
% einfo.name
% dashes
% einfo.programName.value_or("")
<< endl;
// filename.
if (einfo.nixCode.has_value())
{ {
cout << format("%1%from command line argument") % prefix << endl; if (einfo.nixCode->nixFile.has_value())
cout << prefix << endl; {
string eline = einfo.nixCode->errLine.has_value()
? string(" ") + showErrLine(*einfo.nixCode->errLine)
: "";
cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL)
% prefix % *einfo.nixCode->nixFile % eline << endl;
cout << prefix << endl;
}
else
{
cout << format("%1%from command line argument") % prefix << endl;
cout << prefix << endl;
}
} }
}
// description // description
cout << prefix << einfo.description << endl; cout << prefix << einfo.description << endl;
cout << prefix << endl;
// lines of code.
if (einfo.nixCode.has_value())
{
printCodeLines(prefix, *einfo.nixCode);
cout << prefix << endl; cout << prefix << endl;
}
// hint // lines of code.
if (einfo.hint.has_value()) if (einfo.nixCode.has_value())
{ {
cout << prefix << *einfo.hint << endl; printCodeLines(prefix, *einfo.nixCode);
cout << prefix << endl; cout << prefix << endl;
} }
// hint
if (einfo.hint.has_value())
{
cout << prefix << *einfo.hint << endl;
cout << prefix << endl;
}
} }
} }

View file

@ -11,45 +11,45 @@
namespace nix { namespace nix {
typedef enum typedef enum {
{ elWarning elWarning
, elError , elError
} ErrLevel; } ErrLevel;
class ColumnRange { class ColumnRange {
public: public:
unsigned int start; unsigned int start;
unsigned int len; unsigned int len;
}; };
class ErrorInfo; class ErrorInfo;
class ErrLine { class ErrLine {
public: public:
int lineNumber; int lineNumber;
optional<ColumnRange> columnRange; optional<ColumnRange> columnRange;
optional<string> prevLineOfCode; optional<string> prevLineOfCode;
string errLineOfCode; string errLineOfCode;
optional<string> nextLineOfCode; optional<string> nextLineOfCode;
}; };
class NixCode { class NixCode {
public: public:
optional<string> nixFile; optional<string> nixFile;
optional<ErrLine> errLine; optional<ErrLine> errLine;
ErrLine& ensureErrLine() ErrLine& ensureErrLine()
{ {
if (!this->errLine.has_value()) if (!this->errLine.has_value())
this->errLine = optional(ErrLine()); this->errLine = optional(ErrLine());
return *this->errLine; return *this->errLine;
} }
}; };
// ------------------------------------------------- // -------------------------------------------------
// ErrorInfo. // ErrorInfo.
// Forward friend class declarations. "builder classes" // Forward friend class declarations. "builder classes"
template <class T> template <class T>
class AddName; class AddName;
@ -76,170 +76,170 @@ class AddLOC;
// The error info class itself. // The error info class itself.
class ErrorInfo { class ErrorInfo {
public: public:
ErrLevel level; ErrLevel level;
string name; string name;
string description; string description;
optional<NixCode> nixCode; optional<NixCode> nixCode;
optional<string> hint; optional<string> hint;
ErrorInfo& GetEI() { return *this; } ErrorInfo& GetEI() { return *this; }
static optional<string> programName; static optional<string> programName;
// give these access to the private constructor, // give these access to the private constructor,
// when they are direct descendants (children but not grandchildren). // when they are direct descendants (children but not grandchildren).
friend AddName<ErrorInfo>; friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>; friend AddDescription<ErrorInfo>;
friend AddNixCode<ErrorInfo>; friend AddNixCode<ErrorInfo>;
friend AddNixFile<ErrorInfo>; friend AddNixFile<ErrorInfo>;
friend AddErrLine<ErrorInfo>; friend AddErrLine<ErrorInfo>;
friend AddLineNumber<ErrorInfo>; friend AddLineNumber<ErrorInfo>;
friend AddColumnRange<ErrorInfo>; friend AddColumnRange<ErrorInfo>;
friend AddLOC<ErrorInfo>; friend AddLOC<ErrorInfo>;
NixCode& ensureNixCode() NixCode& ensureNixCode()
{ {
if (!this->nixCode.has_value()) if (!this->nixCode.has_value())
this->nixCode = optional(NixCode()); this->nixCode = optional(NixCode());
return *this->nixCode; return *this->nixCode;
} }
protected: protected:
// constructor is protected, so only the builder classes can create an ErrorInfo. // constructor is protected, so only the builder classes can create an ErrorInfo.
ErrorInfo(ErrLevel level) { this->level = level; } ErrorInfo(ErrLevel level) { this->level = level; }
}; };
// Init as error // Init as error
class EIError : public ErrorInfo class EIError : public ErrorInfo
{ {
protected: protected:
EIError() : ErrorInfo(elError) {} EIError() : ErrorInfo(elError) {}
}; };
// Init as warning // Init as warning
class EIWarning : public ErrorInfo class EIWarning : public ErrorInfo
{ {
protected: protected:
EIWarning() : ErrorInfo(elWarning) {} EIWarning() : ErrorInfo(elWarning) {}
}; };
// Builder class definitions. // Builder class definitions.
template <class T> template <class T>
class AddName : private T class AddName : private T
{ {
public: public:
T& name(const std::string &name){ T& name(const std::string &name){
GetEI().name = name; GetEI().name = name;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class AddDescription : private T class AddDescription : private T
{ {
public: public:
T& description(const std::string &description){ T& description(const std::string &description){
GetEI().description = description; GetEI().description = description;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class AddNixFile : private T class AddNixFile : private T
{ {
public: public:
T& nixFile(string filename) { T& nixFile(string filename) {
GetEI().ensureNixCode().nixFile = filename; GetEI().ensureNixCode().nixFile = filename;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class AddLineNumber : private T class AddLineNumber : private T
{ {
public: public:
T& lineNumber(int lineNumber) { T& lineNumber(int lineNumber) {
GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber; GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class AddColumnRange : private T class AddColumnRange : private T
{ {
public: public:
T& columnRange(unsigned int start, unsigned int len) { T& columnRange(unsigned int start, unsigned int len) {
GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len }; GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class AddLOC : private T class AddLOC : private T
{ {
public: public:
T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) { T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) {
GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc; GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc;
GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc; GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc;
GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc; GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
template <class T> template <class T>
class yellowify class yellowify
{ {
public: public:
yellowify(T &s) : value(s) {} yellowify(T &s) : value(s) {}
T &value; T &value;
}; };
template <class T> template <class T>
std::ostream& operator<<(std::ostream &out, const yellowify<T> &y) std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
{ {
return out << ANSI_YELLOW << y.value << ANSI_NORMAL; return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
} }
// hint format shows templated values in yellow. // hint format shows templated values in yellow.
class hintfmt class hintfmt
{ {
public: public:
hintfmt(string format) :fmt(format) {} hintfmt(string format) :fmt(format) {}
template<class T> template<class T>
hintfmt& operator%(const T &value) { fmt % yellowify(value); return *this; } hintfmt& operator%(const T &value) { fmt % yellowify(value); return *this; }
template <typename U>
friend class AddHint;
private:
format fmt;
template <typename U>
friend class AddHint;
private:
format fmt;
}; };
template <class T> template <class T>
class AddHint : private T class AddHint : private T
{ {
public: public:
T& hint(hintfmt &hfmt) { T& hint(hintfmt &hfmt) {
GetEI().hint = optional(hfmt.fmt.str()); GetEI().hint = optional(hfmt.fmt.str());
return *this; return *this;
} }
T& nohint() { T& nohint() {
GetEI().hint = std::nullopt; GetEI().hint = std::nullopt;
return *this; return *this;
} }
protected: protected:
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
// -------------------------------------------------------- // --------------------------------------------------------