forked from lix-project/lix
formatted with astyle
This commit is contained in:
parent
1c329ca433
commit
c6b3fcddb0
|
@ -3,18 +3,16 @@
|
|||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
namespace nix {
|
||||
namespace nix
|
||||
{
|
||||
|
||||
std::optional<string> ErrorInfo::programName = std::nullopt;
|
||||
|
||||
string showErrLine(ErrLine &errLine)
|
||||
{
|
||||
if (errLine.columnRange.has_value())
|
||||
{
|
||||
if (errLine.columnRange.has_value()) {
|
||||
return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str();
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return (format("(%1%)") % errLine.lineNumber).str();
|
||||
};
|
||||
}
|
||||
|
@ -22,8 +20,7 @@ string showErrLine(ErrLine &errLine)
|
|||
void printCodeLines(string &prefix, NixCode &nixCode)
|
||||
{
|
||||
|
||||
if (nixCode.errLine.has_value())
|
||||
{
|
||||
if (nixCode.errLine.has_value()) {
|
||||
// previous line of code.
|
||||
if (nixCode.errLine->prevLineOfCode.has_value()) {
|
||||
std::cout << format("%1% %|2$5d|| %3%")
|
||||
|
@ -41,19 +38,16 @@ void printCodeLines(string &prefix, NixCode &nixCode)
|
|||
<< std::endl;
|
||||
|
||||
// error arrows for the column range.
|
||||
if (nixCode.errLine->columnRange.has_value())
|
||||
{
|
||||
if (nixCode.errLine->columnRange.has_value()) {
|
||||
int start = nixCode.errLine->columnRange->start;
|
||||
std::string spaces;
|
||||
for (int i = 0; i < start; ++i)
|
||||
{
|
||||
for (int i = 0; i < start; ++i) {
|
||||
spaces.append(" ");
|
||||
}
|
||||
|
||||
int len = nixCode.errLine->columnRange->len;
|
||||
std::string arrows;
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
for (int i = 0; i < len; ++i) {
|
||||
arrows.append("^");
|
||||
}
|
||||
|
||||
|
@ -81,24 +75,20 @@ void printErrorInfo(ErrorInfo &einfo)
|
|||
string prefix = " ";
|
||||
|
||||
string levelString;
|
||||
switch (einfo.level)
|
||||
{
|
||||
case ErrLevel::elError:
|
||||
{
|
||||
switch (einfo.level) {
|
||||
case ErrLevel::elError: {
|
||||
levelString = ANSI_RED;
|
||||
levelString += "error:";
|
||||
levelString += ANSI_NORMAL;
|
||||
break;
|
||||
}
|
||||
case ErrLevel::elWarning:
|
||||
{
|
||||
case ErrLevel::elWarning: {
|
||||
levelString = ANSI_YELLOW;
|
||||
levelString += "warning:";
|
||||
levelString += ANSI_NORMAL;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
default: {
|
||||
levelString = (format("invalid error level: %1%") % einfo.level).str();
|
||||
break;
|
||||
}
|
||||
|
@ -122,10 +112,8 @@ void printErrorInfo(ErrorInfo &einfo)
|
|||
<< std::endl;
|
||||
|
||||
// filename.
|
||||
if (einfo.nixCode.has_value())
|
||||
{
|
||||
if (einfo.nixCode->nixFile.has_value())
|
||||
{
|
||||
if (einfo.nixCode.has_value()) {
|
||||
if (einfo.nixCode->nixFile.has_value()) {
|
||||
string eline = einfo.nixCode->errLine.has_value()
|
||||
? string(" ") + showErrLine(*einfo.nixCode->errLine)
|
||||
: "";
|
||||
|
@ -133,9 +121,7 @@ void printErrorInfo(ErrorInfo &einfo)
|
|||
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
|
||||
{
|
||||
} else {
|
||||
std::cout << format("%1%from command line argument") % prefix << std::endl;
|
||||
std::cout << prefix << std::endl;
|
||||
}
|
||||
|
@ -146,15 +132,13 @@ void printErrorInfo(ErrorInfo &einfo)
|
|||
std::cout << prefix << std::endl;
|
||||
|
||||
// lines of code.
|
||||
if (einfo.nixCode.has_value())
|
||||
{
|
||||
if (einfo.nixCode.has_value()) {
|
||||
printCodeLines(prefix, *einfo.nixCode);
|
||||
std::cout << prefix << std::endl;
|
||||
}
|
||||
|
||||
// hint
|
||||
if (einfo.hint.has_value())
|
||||
{
|
||||
if (einfo.hint.has_value()) {
|
||||
std::cout << prefix << *einfo.hint << std::endl;
|
||||
std::cout << prefix << std::endl;
|
||||
}
|
||||
|
|
|
@ -9,14 +9,16 @@
|
|||
|
||||
#include <boost/format.hpp>
|
||||
|
||||
namespace nix {
|
||||
namespace nix
|
||||
{
|
||||
|
||||
typedef enum {
|
||||
elWarning,
|
||||
elError
|
||||
} ErrLevel;
|
||||
|
||||
class ColumnRange {
|
||||
class ColumnRange
|
||||
{
|
||||
public:
|
||||
unsigned int start;
|
||||
unsigned int len;
|
||||
|
@ -24,7 +26,8 @@ class ColumnRange {
|
|||
|
||||
class ErrorInfo;
|
||||
|
||||
class ErrLine {
|
||||
class ErrLine
|
||||
{
|
||||
public:
|
||||
int lineNumber;
|
||||
std::optional<ColumnRange> columnRange;
|
||||
|
@ -33,7 +36,8 @@ class ErrLine {
|
|||
std::optional<string> nextLineOfCode;
|
||||
};
|
||||
|
||||
class NixCode {
|
||||
class NixCode
|
||||
{
|
||||
public:
|
||||
std::optional<string> nixFile;
|
||||
std::optional<ErrLine> errLine;
|
||||
|
@ -75,14 +79,18 @@ template <class T>
|
|||
class AddLOC;
|
||||
|
||||
// The error info class itself.
|
||||
class ErrorInfo {
|
||||
class ErrorInfo
|
||||
{
|
||||
public:
|
||||
ErrLevel level;
|
||||
string name;
|
||||
string description;
|
||||
std::optional<NixCode> nixCode;
|
||||
std::optional<string> hint;
|
||||
ErrorInfo& GetEI() { return *this; }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
static std::optional<string> programName;
|
||||
|
||||
|
@ -105,7 +113,10 @@ class ErrorInfo {
|
|||
}
|
||||
protected:
|
||||
// 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
|
||||
|
@ -127,74 +138,98 @@ template <class T>
|
|||
class AddName : private T
|
||||
{
|
||||
public:
|
||||
T& name(const std::string &name){
|
||||
T& name(const std::string &name)
|
||||
{
|
||||
GetEI().name = name;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class AddDescription : private T
|
||||
{
|
||||
public:
|
||||
T& description(const std::string &description){
|
||||
T& description(const std::string &description)
|
||||
{
|
||||
GetEI().description = description;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class AddNixFile : private T
|
||||
{
|
||||
public:
|
||||
T& nixFile(string filename) {
|
||||
T& nixFile(string filename)
|
||||
{
|
||||
GetEI().ensureNixCode().nixFile = filename;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class AddLineNumber : private T
|
||||
{
|
||||
public:
|
||||
T& lineNumber(int lineNumber) {
|
||||
T& lineNumber(int lineNumber)
|
||||
{
|
||||
GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class AddColumnRange : private T
|
||||
{
|
||||
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;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class AddLOC : private T
|
||||
{
|
||||
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().nextLineOfCode = nextloc;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -219,11 +254,16 @@ std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
|
|||
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; }
|
||||
hintfmt& operator%(const T &value)
|
||||
{
|
||||
fmt % yellowify(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
friend class AddHint;
|
||||
|
@ -237,16 +277,21 @@ template <class T>
|
|||
class AddHint : private T
|
||||
{
|
||||
public:
|
||||
T& hint(hintfmt &hfmt) {
|
||||
T& hint(hintfmt &hfmt)
|
||||
{
|
||||
GetEI().hint = std::optional(hfmt.fmt.str());
|
||||
return *this;
|
||||
}
|
||||
T& nohint() {
|
||||
T& nohint()
|
||||
{
|
||||
GetEI().hint = std::nullopt;
|
||||
return *this;
|
||||
}
|
||||
protected:
|
||||
ErrorInfo& GetEI() { return T::GetEI(); }
|
||||
ErrorInfo& GetEI()
|
||||
{
|
||||
return T::GetEI();
|
||||
}
|
||||
};
|
||||
|
||||
// --------------------------------------------------------
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "../../src/libutil/error.hh"
|
||||
|
||||
#include <optional>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
@ -16,25 +16,25 @@ int main()
|
|||
//
|
||||
// Each error type is created with a specific sequence of builder functions.
|
||||
// Unlike with a constructor, each parameter is clearly named.
|
||||
// If the sequence of function calls isn't followed, then there's a type error.
|
||||
// This should make for a consistent look in the code when errors are created.
|
||||
// If the sequence of function calls isn't followed, then there's a type
|
||||
// error. This should make for a consistent look in the code when errors are
|
||||
// created.
|
||||
|
||||
// ProgramError takes name, description, and an optional hint.
|
||||
printErrorInfo(
|
||||
ProgramError()
|
||||
printErrorInfo( ProgramError()
|
||||
.name("name")
|
||||
.description("error description")
|
||||
.nohint()
|
||||
);
|
||||
|
||||
// ProgramWarning takes name, description, and an optional hint.
|
||||
// The hint is in the form of a hintfmt class, which wraps boost::format(), and
|
||||
// makes all the substituted text yellow.
|
||||
printErrorInfo(
|
||||
ProgramWarning()
|
||||
// The hint is in the form of a hintfmt class, which wraps boost::format(),
|
||||
// and makes all the substituted text yellow.
|
||||
printErrorInfo( ProgramWarning()
|
||||
.name("warning name")
|
||||
.description("warning description")
|
||||
.hint(hintfmt("there was a %1%") % "warning") // 'warning' will be yellow.
|
||||
.hint(hintfmt("there was a %1%") %
|
||||
"warning") // 'warning' will be yellow.
|
||||
);
|
||||
|
||||
/*
|
||||
|
@ -51,41 +51,38 @@ int main()
|
|||
.name("name")
|
||||
.nohint();
|
||||
|
||||
// type error: hint function with regular boost format, not special hintfmt.
|
||||
ProgramError()
|
||||
.description("error description")
|
||||
.name("name")
|
||||
// type error: hint function with regular boost format, not special
|
||||
hintfmt. ProgramError() .description("error description") .name("name")
|
||||
.hint(format("there was a %1%") % "warning");
|
||||
*/
|
||||
|
||||
// NixLangWarning adds nix file, line number, column range, and the lines of code
|
||||
// where a warning occurred.
|
||||
printErrorInfo(
|
||||
NixLangWarning()
|
||||
// NixLangWarning adds nix file, line number, column range, and the lines of
|
||||
// code where a warning occurred.
|
||||
printErrorInfo(NixLangWarning()
|
||||
.name("warning name")
|
||||
.description("warning description")
|
||||
.nixFile("myfile.nix")
|
||||
.lineNumber(40)
|
||||
.columnRange(13, 7)
|
||||
.linesOfCode(std::nullopt
|
||||
,"this is the problem line of code"
|
||||
,std::nullopt)
|
||||
.hint(hintfmt("this hint has %1% templated %2%!!") % "yellow" % "values")
|
||||
);
|
||||
.linesOfCode(std::nullopt,
|
||||
"this is the problem line of code",
|
||||
std::nullopt)
|
||||
.hint(hintfmt("this hint has %1% templated %2%!!") %
|
||||
"yellow" % "values"));
|
||||
|
||||
// NixLangError is just the same as NixLangWarning, except for the Error flag.
|
||||
printErrorInfo(
|
||||
NixLangError()
|
||||
// NixLangError is just the same as NixLangWarning, except for the Error
|
||||
// flag.
|
||||
printErrorInfo(NixLangError()
|
||||
.name("error name")
|
||||
.description("error description")
|
||||
.nixFile("myfile.nix")
|
||||
.lineNumber(40)
|
||||
.columnRange(13, 7)
|
||||
.linesOfCode(std::optional("previous line of code")
|
||||
,"this is the problem line of code"
|
||||
,std::optional("next line of code"))
|
||||
.hint(hintfmt("this hint has %1% templated %2%!!") % "yellow" % "values")
|
||||
);
|
||||
.linesOfCode(std::optional("previous line of code"),
|
||||
"this is the problem line of code",
|
||||
std::optional("next line of code"))
|
||||
.hint(hintfmt("this hint has %1% templated %2%!!") %
|
||||
"yellow" % "values"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue