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

View file

@ -27,21 +27,21 @@ class ErrorInfo;
class ErrLine {
public:
int lineNumber;
optional<ColumnRange> columnRange;
optional<string> prevLineOfCode;
std::optional<ColumnRange> columnRange;
std::optional<string> prevLineOfCode;
string errLineOfCode;
optional<string> nextLineOfCode;
std::optional<string> nextLineOfCode;
};
class NixCode {
public:
optional<string> nixFile;
optional<ErrLine> errLine;
std::optional<string> nixFile;
std::optional<ErrLine> errLine;
ErrLine& ensureErrLine()
{
if (!this->errLine.has_value())
this->errLine = optional(ErrLine());
this->errLine = std::optional(ErrLine());
return *this->errLine;
}
};
@ -80,11 +80,11 @@ class ErrorInfo {
ErrLevel level;
string name;
string description;
optional<NixCode> nixCode;
optional<string> hint;
std::optional<NixCode> nixCode;
std::optional<string> hint;
ErrorInfo& GetEI() { return *this; }
static optional<string> programName;
static std::optional<string> programName;
// give these access to the private constructor,
// when they are direct descendants (children but not grandchildren).
@ -100,7 +100,7 @@ class ErrorInfo {
NixCode& ensureNixCode()
{
if (!this->nixCode.has_value())
this->nixCode = optional(NixCode());
this->nixCode = std::optional(NixCode());
return *this->nixCode;
}
protected:
@ -187,7 +187,7 @@ template <class T>
class AddLOC : private T
{
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().errLineOfCode = loc;
GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc;
@ -197,6 +197,11 @@ class AddLOC : private T
ErrorInfo& GetEI() { return T::GetEI(); }
};
// ----------------------------------------------------------------
// format for hints. same as boost format, except templated values
// are always in yellow.
template <class T>
class yellowify
{
@ -211,11 +216,12 @@ std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
}
// hint format shows templated values in yellow.
class hintfmt
{
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>
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>
class AddHint : private T
{
public:
T& hint(hintfmt &hfmt) {
GetEI().hint = optional(hfmt.fmt.str());
GetEI().hint = std::optional(hfmt.fmt.str());
return *this;
}
T& nohint() {

View file

@ -3,14 +3,12 @@
#include <optional>
#include <iostream>
int main()
{
using namespace nix;
// 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:
//
@ -83,9 +81,9 @@ int main()
.nixFile("myfile.nix")
.lineNumber(40)
.columnRange(13,7)
.linesOfCode(optional("previous line of code")
.linesOfCode(std::optional("previous 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")
);