From a3ef00be6c668bd59a879a4acdb1599225d86b44 Mon Sep 17 00:00:00 2001 From: Ben Burdette Date: Fri, 27 Mar 2020 10:03:02 -0600 Subject: camelcase; optional hint --- src/libutil/error.cc | 60 ++++++++++++++++++++------------------ src/libutil/error.hh | 82 +++++++++++++++++++++++++++++++++++++++++++--------- tests/errors/main.cc | 34 ++++++++++++---------- 3 files changed, 119 insertions(+), 57 deletions(-) diff --git a/src/libutil/error.cc b/src/libutil/error.cc index 9fbd234e0..d6595070f 100644 --- a/src/libutil/error.cc +++ b/src/libutil/error.cc @@ -24,55 +24,55 @@ string showErrLine(ErrLine &errLine) }; } -void print_code_lines(string &prefix, NixCode &nix_code) +void printCodeLines(string &prefix, NixCode &nixCode) { - if (nix_code.errLine.has_value()) + if (nixCode.errLine.has_value()) { // previous line of code. - if (nix_code.errLine->prevLineOfCode.has_value()) { + if (nixCode.errLine->prevLineOfCode.has_value()) { cout << format("%1% %|2$5d|| %3%") % prefix - % (nix_code.errLine->lineNumber - 1) - % *nix_code.errLine->prevLineOfCode + % (nixCode.errLine->lineNumber - 1) + % *nixCode.errLine->prevLineOfCode << endl; } // line of code containing the error.%2$+5d% cout << format("%1% %|2$5d|| %3%") % prefix - % (nix_code.errLine->lineNumber) - % nix_code.errLine->errLineOfCode + % (nixCode.errLine->lineNumber) + % nixCode.errLine->errLineOfCode << endl; // error arrows for the column range. - if (nix_code.errLine->columnRange.has_value()) + if (nixCode.errLine->columnRange.has_value()) { - int start = nix_code.errLine->columnRange->start; + int start = nixCode.errLine->columnRange->start; std::string spaces; for (int i = 0; i < start; ++i) { spaces.append(" "); } - int len = nix_code.errLine->columnRange->len; + 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; + cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << endl; } // next line of code. - if (nix_code.errLine->nextLineOfCode.has_value()) { + if (nixCode.errLine->nextLineOfCode.has_value()) { cout << format("%1% %|2$5d|| %3%") % prefix - % (nix_code.errLine->lineNumber + 1) - % *nix_code.errLine->nextLineOfCode + % (nixCode.errLine->lineNumber + 1) + % *nixCode.errLine->nextLineOfCode << endl; } @@ -80,36 +80,36 @@ void print_code_lines(string &prefix, NixCode &nix_code) } -void print_error(ErrorInfo &einfo) +void printErrorInfo(ErrorInfo &einfo) { int errwidth = 80; string prefix = " "; - string level_string; + string levelString; switch (einfo.level) { case ErrLevel::elError: { - level_string = ANSI_RED; - level_string += "error:"; - level_string += ANSI_NORMAL; + levelString = ANSI_RED; + levelString += "error:"; + levelString += ANSI_NORMAL; break; } case ErrLevel::elWarning: { - level_string = ANSI_YELLOW; - level_string += "warning:"; - level_string += ANSI_NORMAL; + levelString = ANSI_YELLOW; + levelString += "warning:"; + levelString += ANSI_NORMAL; break; } default: { - level_string = "wat:"; + levelString = "wat:"; break; } } - int ndl = prefix.length() + level_string.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length(); + int ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length(); int dashwidth = ndl > (errwidth - 3) ? 3 : 80 - ndl; string dashes; @@ -119,7 +119,7 @@ void print_error(ErrorInfo &einfo) // divider. cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL) % prefix - % level_string + % levelString % "---" % einfo.name % dashes @@ -152,11 +152,15 @@ void print_error(ErrorInfo &einfo) // lines of code. if (einfo.nixCode.has_value()) - print_code_lines(prefix, *einfo.nixCode); + printCodeLines(prefix, *einfo.nixCode); // hint - cout << prefix << einfo.hint << endl; - cout << prefix << endl; + if (einfo.hint.has_value()) + { + cout << prefix << endl; + cout << prefix << *einfo.hint << endl; + cout << prefix << endl; + } } diff --git a/src/libutil/error.hh b/src/libutil/error.hh index 7260dfd5b..f02d7288a 100644 --- a/src/libutil/error.hh +++ b/src/libutil/error.hh @@ -1,12 +1,16 @@ #pragma once -#include "types.hh" +#include "util.hh" #include #include #include +#include using std::string; using std::optional; +using boost::format; +using std::cout; +using std::endl; namespace nix { @@ -81,13 +85,13 @@ class ErrorInfo { string name; string description; optional nixCode; - string hint; + optional hint; ErrorInfo& GetEI() { return *this; } static optional programName; // give these access to the private constructor, - // when they are direct descendants. + // when they are direct descendants (children but not grandchildren). friend AddName; friend AddDescription; friend AddNixCode; @@ -150,7 +154,7 @@ class AddDescription : private T }; template -class AddNixFile : public T +class AddNixFile : private T { public: T& nixFile(string filename) { @@ -162,7 +166,7 @@ class AddNixFile : public T }; template -class AddLineNumber : public T +class AddLineNumber : private T { public: T& lineNumber(int lineNumber) { @@ -174,7 +178,7 @@ class AddLineNumber : public T }; template -class AddColumnRange : public T +class AddColumnRange : private T { public: T& columnRange(unsigned int start, unsigned int len) { @@ -186,7 +190,7 @@ class AddColumnRange : public T }; template -class AddLOC : public T +class AddLOC : private T { public: T& linesOfCode(optional prevloc, string loc, optional nextloc) { @@ -199,17 +203,64 @@ class AddLOC : public T ErrorInfo& GetEI() { return T::GetEI(); } }; -typedef AddNixFile> MkNixCode; +template +class yellowify +{ +public: + yellowify(T &s) : value(s) {} + T &value; +}; + +template +std::ostream& operator<<(std::ostream &out, const yellowify &y) +{ + return out << ANSI_YELLOW << y.value << ANSI_NORMAL; +} + +// hint format shows templated values in yellow. +class hintfmt +{ + public: + hintfmt(string format) :fmt(format) {} + template + hintfmt& operator%(const T &value) { fmt % yellowify(value); return *this; } + + template + friend class AddHint; + private: + format fmt; + +}; + +template +class AddHint : private T +{ + public: + T& hint(hintfmt &hfmt) { + GetEI().hint = optional(hfmt.fmt.str()); + return *this; + } + T& nohint() { + GetEI().hint = std::nullopt; + return *this; + } + protected: + ErrorInfo& GetEI() { return T::GetEI(); } +}; + +// -------------------------------------------------------- +// error types -typedef AddName> StandardError; -typedef AddName> StandardWarning; +typedef AddName>> StandardError; +typedef AddName>> StandardWarning; typedef AddName< AddDescription< AddNixFile< AddLineNumber< AddColumnRange< - AddLOC>>>>> MkNixError; + AddLOC< + AddHint>>>>>> MkNixError; typedef AddName< AddDescription< AddNixFile< @@ -217,10 +268,15 @@ typedef AddName< AddColumnRange< AddLOC>>>>> MkNixWarning; + +// -------------------------------------------------------- +// error printing + +void printErrorInfo(ErrorInfo &einfo); + string showErrLine(ErrLine &errLine); -void print_code_lines(string &prefix, NixCode &nix_code); +void printCodeLines(string &prefix, NixCode &nixCode); -void print_error(ErrorInfo &einfo); } diff --git a/tests/errors/main.cc b/tests/errors/main.cc index f53101629..35d2a1cdf 100644 --- a/tests/errors/main.cc +++ b/tests/errors/main.cc @@ -8,9 +8,9 @@ using std::nullopt; using std::cout; using std::endl; -int main() { - -using namespace nix; +int main() +{ + using namespace nix; ErrorInfo::programName = optional("errorTest"); @@ -35,19 +35,23 @@ using namespace nix; generic.program = "nixtool.exe"; generic.nixCode = nixcode; - print_error(generic); + printErrorInfo(generic); */ - print_error(StandardError() + printErrorInfo(StandardError() .name("name") - .description("description")); + .description("description") + .nohint() + ); - print_error(StandardWarning() + printErrorInfo(StandardWarning() .name("warning name") - .description("warning description")); + .description("warning description") + .nohint() + ); - print_error(MkNixWarning() + printErrorInfo(MkNixWarning() .name("warning name") .description("warning description") .nixFile("myfile.nix") @@ -57,19 +61,17 @@ using namespace nix; ,"this is the problem line of code" ,nullopt)); - print_error(MkNixError() + printErrorInfo(MkNixError() .name("error name") .description("error description") .nixFile("myfile.nix") .lineNumber(40) .columnRange(13,7) - .linesOfCode(nullopt + .linesOfCode(optional("previous line of code") ,"this is the problem line of code" - ,nullopt)); + ,optional("next line of code")) + .hint(hintfmt("this hint has %1% templated %2%!!") % "yellow" % "values") + ); return 0; } - - - - -- cgit v1.2.3