formatted with astyle

This commit is contained in:
Ben Burdette 2020-04-02 16:02:40 -06:00
parent 1c329ca433
commit c6b3fcddb0
3 changed files with 308 additions and 282 deletions

View file

@ -3,109 +3,99 @@
#include <iostream> #include <iostream>
#include <optional> #include <optional>
namespace nix { namespace nix
{
std::optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
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. // previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) { if (nixCode.errLine->prevLineOfCode.has_value()) {
std::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
<< std::endl; << std::endl;
} }
// line of code containing the error.%2$+5d% // line of code containing the error.%2$+5d%
std::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
<< std::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()) {
{
int start = nixCode.errLine->columnRange->start; int start = nixCode.errLine->columnRange->start;
std::string spaces; std::string spaces;
for (int i = 0; i < start; ++i) for (int i = 0; i < start; ++i) {
{
spaces.append(" "); spaces.append(" ");
} }
int len = nixCode.errLine->columnRange->len; int len = nixCode.errLine->columnRange->len;
std::string arrows; std::string arrows;
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i) {
{
arrows.append("^"); arrows.append("^");
} }
std::cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << std::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()) {
std::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
<< std::endl; << std::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: {
case ErrLevel::elError: levelString = ANSI_RED;
{ levelString += "error:";
levelString = ANSI_RED; levelString += ANSI_NORMAL;
levelString += "error:"; break;
levelString += ANSI_NORMAL; }
break; case ErrLevel::elWarning: {
} levelString = ANSI_YELLOW;
case ErrLevel::elWarning: levelString += "warning:";
{ levelString += ANSI_NORMAL;
levelString = ANSI_YELLOW; break;
levelString += "warning:"; }
levelString += ANSI_NORMAL; default: {
break; levelString = (format("invalid error level: %1%") % einfo.level).str();
} 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 ndl = prefix.length() + levelString.length() + 3 + einfo.name.length() + einfo.programName.value_or("").length();
int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl; int dashwidth = ndl > (errwidth - 3) ? 3 : errwidth - ndl;
string dashes; string dashes;
for (int i = 0; i < dashwidth; ++i) for (int i = 0; i < dashwidth; ++i)
@ -113,29 +103,25 @@ void printErrorInfo(ErrorInfo &einfo)
// divider. // divider.
std::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("")
<< std::endl; << std::endl;
// filename. // filename.
if (einfo.nixCode.has_value()) if (einfo.nixCode.has_value()) {
{ if (einfo.nixCode->nixFile.has_value()) {
if (einfo.nixCode->nixFile.has_value())
{
string eline = einfo.nixCode->errLine.has_value() string eline = einfo.nixCode->errLine.has_value()
? string(" ") + showErrLine(*einfo.nixCode->errLine) ? string(" ") + showErrLine(*einfo.nixCode->errLine)
: ""; : "";
std::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 << std::endl; % prefix % *einfo.nixCode->nixFile % eline << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} } else {
else
{
std::cout << format("%1%from command line argument") % prefix << std::endl; std::cout << format("%1%from command line argument") % prefix << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }
@ -146,15 +132,13 @@ void printErrorInfo(ErrorInfo &einfo)
std::cout << prefix << std::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);
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }
// hint // hint
if (einfo.hint.has_value()) if (einfo.hint.has_value()) {
{
std::cout << prefix << *einfo.hint << std::endl; std::cout << prefix << *einfo.hint << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }

View file

@ -9,41 +9,45 @@
#include <boost/format.hpp> #include <boost/format.hpp>
namespace nix { namespace nix
{
typedef enum { typedef enum {
elWarning, elWarning,
elError elError
} ErrLevel; } ErrLevel;
class ColumnRange { class ColumnRange
public: {
unsigned int start; public:
unsigned int len; unsigned int start;
unsigned int len;
}; };
class ErrorInfo; class ErrorInfo;
class ErrLine { class ErrLine
public: {
int lineNumber; public:
std::optional<ColumnRange> columnRange; int lineNumber;
std::optional<string> prevLineOfCode; std::optional<ColumnRange> columnRange;
string errLineOfCode; std::optional<string> prevLineOfCode;
std::optional<string> nextLineOfCode; string errLineOfCode;
std::optional<string> nextLineOfCode;
}; };
class NixCode { class NixCode
public: {
std::optional<string> nixFile; public:
std::optional<ErrLine> errLine; std::optional<string> nixFile;
std::optional<ErrLine> errLine;
ErrLine& ensureErrLine() ErrLine& ensureErrLine()
{ {
if (!this->errLine.has_value()) if (!this->errLine.has_value())
this->errLine = std::optional(ErrLine()); this->errLine = std::optional(ErrLine());
return *this->errLine; return *this->errLine;
} }
}; };
// ------------------------------------------------- // -------------------------------------------------
@ -65,149 +69,180 @@ class AddNixFile;
template <class T> template <class T>
class AddErrLine; class AddErrLine;
template <class T> template <class T>
class AddLineNumber; class AddLineNumber;
template <class T> template <class T>
class AddColumnRange; class AddColumnRange;
template <class T> template <class T>
class AddLOC; class AddLOC;
// The error info class itself. // The error info class itself.
class ErrorInfo { class ErrorInfo
public: {
ErrLevel level; public:
string name; ErrLevel level;
string description; string name;
std::optional<NixCode> nixCode; string description;
std::optional<string> hint; std::optional<NixCode> nixCode;
ErrorInfo& GetEI() { return *this; } std::optional<string> hint;
ErrorInfo& GetEI()
{
return *this;
}
static std::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).
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 = std::optional(NixCode()); this->nixCode = std::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; {
return *this; GetEI().name = name;
} return *this;
protected: }
ErrorInfo& GetEI() { return T::GetEI(); } protected:
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; {
return *this; GetEI().description = description;
} return *this;
protected: }
ErrorInfo& GetEI() { return T::GetEI(); } protected:
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; {
return *this; GetEI().ensureNixCode().nixFile = filename;
} return *this;
protected: }
ErrorInfo& GetEI() { return T::GetEI(); } protected:
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; {
return *this; GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
} return *this;
protected: }
ErrorInfo& GetEI() { return T::GetEI(); } protected:
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 }; {
return *this; GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
} return *this;
protected: }
ErrorInfo& GetEI() { return T::GetEI(); } protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
}; };
template <class T> template <class T>
class AddLOC : private T class AddLOC : private T
{ {
public: public:
T& linesOfCode(std::optional<string> prevloc, string loc, std::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().prevLineOfCode = prevloc;
GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc; GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc;
return *this; GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc;
} return *this;
protected: }
ErrorInfo& GetEI() { return T::GetEI(); } protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
}; };
// ---------------------------------------------------------------- // ----------------------------------------------------------------
// format for hints. same as boost format, except templated values // format for hints. same as boost format, except templated values
// are always in yellow. // are always in yellow.
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>
@ -216,69 +251,79 @@ 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;
} }
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); {
} 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; } template<class T>
hintfmt& operator%(const T &value)
{
fmt % yellowify(value);
return *this;
}
template <typename U> template <typename U>
friend class AddHint; friend class AddHint;
private: private:
format fmt; format fmt;
}; };
// the template layer for adding a hint. // 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 = std::optional(hfmt.fmt.str()); {
return *this; GetEI().hint = std::optional(hfmt.fmt.str());
} return *this;
T& nohint() { }
GetEI().hint = std::nullopt; T& nohint()
return *this; {
} GetEI().hint = std::nullopt;
protected: return *this;
ErrorInfo& GetEI() { return T::GetEI(); } }
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
}; };
// -------------------------------------------------------- // --------------------------------------------------------
// error types // error types
typedef AddName< typedef AddName<
AddDescription< AddDescription<
AddHint< AddHint<
EIError>>> ProgramError; EIError>>> ProgramError;
typedef AddName< typedef AddName<
AddDescription< AddDescription<
AddHint< AddHint<
EIWarning>>> ProgramWarning; EIWarning>>> ProgramWarning;
typedef AddName< typedef AddName<
AddDescription< AddDescription<
AddNixFile< AddNixFile<
AddLineNumber< AddLineNumber<
AddColumnRange< AddColumnRange<
AddLOC< AddLOC<
AddHint< AddHint<
EIError>>>>>>> NixLangError; EIError>>>>>>> NixLangError;
typedef AddName< typedef AddName<
AddDescription< AddDescription<
AddNixFile< AddNixFile<
AddLineNumber< AddLineNumber<
AddColumnRange< AddColumnRange<
AddLOC< AddLOC<
AddHint< AddHint<
EIWarning>>>>>>> NixLangWarning; EIWarning>>>>>>> NixLangWarning;
// -------------------------------------------------------- // --------------------------------------------------------

View file

@ -1,45 +1,45 @@
#include "../../src/libutil/error.hh" #include "../../src/libutil/error.hh"
#include <optional>
#include <iostream> #include <iostream>
#include <optional>
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 = std::optional("error-test"); ErrorInfo::programName = std::optional("error-test");
// There are currently four error types: // There are currently four error types:
// //
// ProgramError, ProgramWarning, NixLangError, NixLangWarning. // ProgramError, ProgramWarning, NixLangError, NixLangWarning.
// //
// Each error type is created with a specific sequence of builder functions. // Each error type is created with a specific sequence of builder functions.
// Unlike with a constructor, each parameter is clearly named. // Unlike with a constructor, each parameter is clearly named.
// If the sequence of function calls isn't followed, then there's a type error. // If the sequence of function calls isn't followed, then there's a type
// This should make for a consistent look in the code when errors are created. // error. This should make for a consistent look in the code when errors are
// created.
// ProgramError takes name, description, and an optional hint. // ProgramError takes name, description, and an optional hint.
printErrorInfo( printErrorInfo( ProgramError()
ProgramError() .name("name")
.name("name") .description("error description")
.description("error description") .nohint()
.nohint() );
);
// ProgramWarning takes name, description, and an optional hint. // ProgramWarning takes name, description, and an optional hint.
// The hint is in the form of a hintfmt class, which wraps boost::format(), and // The hint is in the form of a hintfmt class, which wraps boost::format(),
// makes all the substituted text yellow. // and makes all the substituted text yellow.
printErrorInfo( printErrorInfo( ProgramWarning()
ProgramWarning() .name("warning name")
.name("warning name") .description("warning description")
.description("warning description") .hint(hintfmt("there was a %1%") %
.hint(hintfmt("there was a %1%") % "warning") // 'warning' will be yellow. "warning") // 'warning' will be yellow.
); );
/* /*
// some invalid errors: // some invalid errors:
// type error: no hint function. // type error: no hint function.
ProgramError() ProgramError()
.name("name") .name("name")
@ -51,41 +51,38 @@ int main()
.name("name") .name("name")
.nohint(); .nohint();
// type error: hint function with regular boost format, not special hintfmt. // type error: hint function with regular boost format, not special
ProgramError() hintfmt. ProgramError() .description("error description") .name("name")
.description("error description")
.name("name")
.hint(format("there was a %1%") % "warning"); .hint(format("there was a %1%") % "warning");
*/ */
// NixLangWarning adds nix file, line number, column range, and the lines of code // NixLangWarning adds nix file, line number, column range, and the lines of
// where a warning occurred. // code where a warning occurred.
printErrorInfo( printErrorInfo(NixLangWarning()
NixLangWarning() .name("warning name")
.name("warning name") .description("warning description")
.description("warning description") .nixFile("myfile.nix")
.nixFile("myfile.nix") .lineNumber(40)
.lineNumber(40) .columnRange(13, 7)
.columnRange(13,7) .linesOfCode(std::nullopt,
.linesOfCode(std::nullopt "this is the problem line of code",
,"this is the problem line of code" std::nullopt)
,std::nullopt) .hint(hintfmt("this hint has %1% templated %2%!!") %
.hint(hintfmt("this hint has %1% templated %2%!!") % "yellow" % "values") "yellow" % "values"));
);
// NixLangError is just the same as NixLangWarning, except for the Error flag. // NixLangError is just the same as NixLangWarning, except for the Error
printErrorInfo( // flag.
NixLangError() printErrorInfo(NixLangError()
.name("error name") .name("error name")
.description("error description") .description("error description")
.nixFile("myfile.nix") .nixFile("myfile.nix")
.lineNumber(40) .lineNumber(40)
.columnRange(13,7) .columnRange(13, 7)
.linesOfCode(std::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",
,std::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"));
return 0; return 0;
} }