using std:: everywhere; fix a formatting error; add exception flags

This commit is contained in:
Ben Burdette 2020-04-01 21:30:19 -06:00
parent dd7b8183a5
commit e697884f65
3 changed files with 44 additions and 40 deletions

View file

@ -5,9 +5,8 @@
namespace nix { namespace nix {
optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
// return basic_format?
string showErrLine(ErrLine &errLine) string showErrLine(ErrLine &errLine)
{ {
if (errLine.columnRange.has_value()) if (errLine.columnRange.has_value())
@ -27,19 +26,19 @@ void printCodeLines(string &prefix, NixCode &nixCode)
{ {
// previous line of code. // previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) { if (nixCode.errLine->prevLineOfCode.has_value()) {
cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (nixCode.errLine->lineNumber - 1) % (nixCode.errLine->lineNumber - 1)
% *nixCode.errLine->prevLineOfCode % *nixCode.errLine->prevLineOfCode
<< endl; << std::endl;
} }
// line of code containing the error.%2$+5d% // line of code containing the error.%2$+5d%
cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (nixCode.errLine->lineNumber) % (nixCode.errLine->lineNumber)
% nixCode.errLine->errLineOfCode % nixCode.errLine->errLineOfCode
<< endl; << std::endl;
// error arrows for the column range. // error arrows for the column range.
if (nixCode.errLine->columnRange.has_value()) if (nixCode.errLine->columnRange.has_value())
@ -58,18 +57,18 @@ void printCodeLines(string &prefix, NixCode &nixCode)
arrows.append("^"); arrows.append("^");
} }
cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << endl; std::cout << format("%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()) {
cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (nixCode.errLine->lineNumber + 1) % (nixCode.errLine->lineNumber + 1)
% *nixCode.errLine->nextLineOfCode % *nixCode.errLine->nextLineOfCode
<< endl; << std::endl;
} }
} }
@ -113,14 +112,14 @@ void printErrorInfo(ErrorInfo &einfo)
dashes.append("-"); dashes.append("-");
// divider. // divider.
cout << format("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL) std::cout << format("%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("")
<< endl; << std::endl;
// filename. // filename.
if (einfo.nixCode.has_value()) if (einfo.nixCode.has_value())
@ -131,33 +130,33 @@ void printErrorInfo(ErrorInfo &einfo)
? string(" ") + showErrLine(*einfo.nixCode->errLine) ? string(" ") + showErrLine(*einfo.nixCode->errLine)
: ""; : "";
cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL) std::cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL)
% prefix % *einfo.nixCode->nixFile % eline << endl; % prefix % *einfo.nixCode->nixFile % eline << std::endl;
cout << prefix << endl; std::cout << prefix << std::endl;
} }
else else
{ {
cout << format("%1%from command line argument") % prefix << endl; std::cout << format("%1%from command line argument") % prefix << std::endl;
cout << prefix << endl; std::cout << prefix << std::endl;
} }
} }
// description // description
cout << prefix << einfo.description << endl; std::cout << prefix << einfo.description << std::endl;
cout << prefix << endl; std::cout << prefix << std::endl;
// lines of code. // lines of code.
if (einfo.nixCode.has_value()) if (einfo.nixCode.has_value())
{ {
printCodeLines(prefix, *einfo.nixCode); printCodeLines(prefix, *einfo.nixCode);
cout << prefix << endl; std::cout << prefix << std::endl;
} }
// hint // hint
if (einfo.hint.has_value()) if (einfo.hint.has_value())
{ {
cout << prefix << *einfo.hint << endl; std::cout << prefix << *einfo.hint << std::endl;
cout << prefix << endl; std::cout << prefix << std::endl;
} }
} }

View file

@ -27,21 +27,21 @@ class ErrorInfo;
class ErrLine { class ErrLine {
public: public:
int lineNumber; int lineNumber;
optional<ColumnRange> columnRange; std::optional<ColumnRange> columnRange;
optional<string> prevLineOfCode; std::optional<string> prevLineOfCode;
string errLineOfCode; string errLineOfCode;
optional<string> nextLineOfCode; std::optional<string> nextLineOfCode;
}; };
class NixCode { class NixCode {
public: public:
optional<string> nixFile; std::optional<string> nixFile;
optional<ErrLine> errLine; std::optional<ErrLine> errLine;
ErrLine& ensureErrLine() ErrLine& ensureErrLine()
{ {
if (!this->errLine.has_value()) if (!this->errLine.has_value())
this->errLine = optional(ErrLine()); this->errLine = std::optional(ErrLine());
return *this->errLine; return *this->errLine;
} }
}; };
@ -80,11 +80,11 @@ class ErrorInfo {
ErrLevel level; ErrLevel level;
string name; string name;
string description; string description;
optional<NixCode> nixCode; std::optional<NixCode> nixCode;
optional<string> hint; std::optional<string> hint;
ErrorInfo& GetEI() { return *this; } ErrorInfo& GetEI() { return *this; }
static optional<string> programName; static std::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).
@ -100,7 +100,7 @@ class ErrorInfo {
NixCode& ensureNixCode() NixCode& ensureNixCode()
{ {
if (!this->nixCode.has_value()) if (!this->nixCode.has_value())
this->nixCode = optional(NixCode()); this->nixCode = std::optional(NixCode());
return *this->nixCode; return *this->nixCode;
} }
protected: protected:
@ -187,7 +187,7 @@ 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(std::optional<string> prevloc, string loc, std::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;
@ -197,6 +197,11 @@ class AddLOC : private T
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
// ----------------------------------------------------------------
// format for hints. same as boost format, except templated values
// are always in yellow.
template <class T> template <class T>
class yellowify class yellowify
{ {
@ -211,11 +216,12 @@ 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.
class hintfmt class hintfmt
{ {
public: public:
hintfmt(string format) :fmt(format) {} hintfmt(string format) :fmt(format) {
fmt.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
}
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; }
@ -226,12 +232,13 @@ class hintfmt
}; };
// the template layer for adding a hint.
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 = std::optional(hfmt.fmt.str());
return *this; return *this;
} }
T& nohint() { T& nohint() {

View file

@ -3,14 +3,12 @@
#include <optional> #include <optional>
#include <iostream> #include <iostream>
int main() int main()
{ {
using namespace nix; using namespace nix;
// In each program where errors occur, this has to be set. // In each program where errors occur, this has to be set.
ErrorInfo::programName = optional("error-test"); ErrorInfo::programName = std::optional("error-test");
// There are currently four error types: // There are currently four error types:
// //
@ -83,9 +81,9 @@ int main()
.nixFile("myfile.nix") .nixFile("myfile.nix")
.lineNumber(40) .lineNumber(40)
.columnRange(13,7) .columnRange(13,7)
.linesOfCode(optional("previous line of code") .linesOfCode(std::optional("previous line of code")
,"this is the problem line of code" ,"this is the problem line of code"
,optional("next line of code")) ,std::optional("next line of code"))
.hint(hintfmt("this hint has %1% templated %2%!!") % "yellow" % "values") .hint(hintfmt("this hint has %1% templated %2%!!") % "yellow" % "values")
); );