camelcase; optional hint

This commit is contained in:
Ben Burdette 2020-03-27 10:03:02 -06:00
parent d44c9c5581
commit a3ef00be6c
3 changed files with 119 additions and 57 deletions

View file

@ -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;
if (einfo.hint.has_value())
{
cout << prefix << endl;
cout << prefix << *einfo.hint << endl;
cout << prefix << endl;
}
}

View file

@ -1,12 +1,16 @@
#pragma once
#include "types.hh"
#include "util.hh"
#include <string>
#include <optional>
#include <iostream>
#include <iomanip>
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> nixCode;
string hint;
optional<string> hint;
ErrorInfo& GetEI() { return *this; }
static optional<string> programName;
// give these access to the private constructor,
// when they are direct descendants.
// when they are direct descendants (children but not grandchildren).
friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>;
friend AddNixCode<ErrorInfo>;
@ -150,7 +154,7 @@ class AddDescription : private T
};
template <class T>
class AddNixFile : public T
class AddNixFile : private T
{
public:
T& nixFile(string filename) {
@ -162,7 +166,7 @@ class AddNixFile : public T
};
template <class T>
class AddLineNumber : public T
class AddLineNumber : private T
{
public:
T& lineNumber(int lineNumber) {
@ -174,7 +178,7 @@ class AddLineNumber : public T
};
template <class T>
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 T>
class AddLOC : public T
class AddLOC : private T
{
public:
T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) {
@ -199,17 +203,64 @@ class AddLOC : public T
ErrorInfo& GetEI() { return T::GetEI(); }
};
typedef AddNixFile<AddErrLine<NixCode>> MkNixCode;
template <class T>
class yellowify
{
public:
yellowify(T &s) : value(s) {}
T &value;
};
typedef AddName<AddDescription<EIError>> StandardError;
typedef AddName<AddDescription<EIWarning>> StandardWarning;
template <class T>
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) {}
template<class T>
hintfmt& operator%(const T &value) { fmt % yellowify(value); return *this; }
template <typename U>
friend class AddHint;
private:
format fmt;
};
template <class T>
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<AddDescription<AddHint<EIError>>> StandardError;
typedef AddName<AddDescription<AddHint<EIWarning>>> StandardWarning;
typedef AddName<
AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
AddLOC<EIError>>>>>> MkNixError;
AddLOC<
AddHint<EIError>>>>>>> MkNixError;
typedef AddName<
AddDescription<
AddNixFile<
@ -217,10 +268,15 @@ typedef AddName<
AddColumnRange<
AddLOC<EIWarning>>>>>> 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);
}

View file

@ -8,8 +8,8 @@ using std::nullopt;
using std::cout;
using std::endl;
int main() {
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;
}