1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 23:13:14 +02:00

Merge branch 'initializer-style' into error-format

This commit is contained in:
Ben Burdette 2020-04-15 10:06:20 -06:00
commit adf03b0b8e
3 changed files with 139 additions and 328 deletions

View file

@ -8,68 +8,60 @@ namespace nix
std::optional<string> ErrorInfo::programName = std::nullopt;
string showErrLine(ErrLine &errLine)
string showErrPos(const ErrPos &errPos)
{
if (errLine.column > 0) {
return fmt("(%1%:%2%)", errLine.lineNumber, errLine.column);
if (errPos.column > 0) {
return fmt("(%1%:%2%)", errPos.lineNumber, errPos.column);
} else {
return fmt("(%1%)", errLine.lineNumber);
return fmt("(%1%)", errPos.lineNumber);
};
}
void printCodeLines(string &prefix, NixCode &nixCode)
void printCodeLines(const string &prefix, const NixCode &nixCode)
{
if (nixCode.errLine.has_value()) {
// previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errLine->lineNumber - 1),
*nixCode.errLine->prevLineOfCode)
<< std::endl;
}
// line of code containing the error.%2$+5d%
// previous line of code.
if (nixCode.prevLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errLine->lineNumber),
nixCode.errLine->errLineOfCode)
(nixCode.errPos.lineNumber - 1),
*nixCode.prevLineOfCode)
<< std::endl;
// error arrows for the column range.
if (nixCode.errLine->column > 0) {
int start = nixCode.errLine->column;
std::string spaces;
for (int i = 0; i < start; ++i) {
spaces.append(" ");
}
// for now, length of 1.
std::string arrows("^");
std::cout << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
prefix,
spaces,
arrows) << std::endl;
}
// next line of code.
if (nixCode.errLine->nextLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errLine->lineNumber + 1),
*nixCode.errLine->nextLineOfCode)
<< std::endl;
}
}
// line of code containing the error.%2$+5d%
std::cout << fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errPos.lineNumber),
nixCode.errLineOfCode)
<< std::endl;
// error arrows for the column range.
if (nixCode.errPos.column > 0) {
int start = nixCode.errPos.column;
std::string spaces;
for (int i = 0; i < start; ++i) {
spaces.append(" ");
}
std::string arrows("^");
std::cout << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
prefix,
spaces,
arrows) << std::endl;
}
// next line of code.
if (nixCode.nextLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errPos.lineNumber + 1),
*nixCode.nextLineOfCode)
<< std::endl;
}
}
void printErrorInfo(ErrorInfo &einfo)
void printErrorInfo(const ErrorInfo &einfo)
{
int errwidth = 80;
string prefix = " ";
@ -102,24 +94,26 @@ void printErrorInfo(ErrorInfo &einfo)
dashes.append("-");
// divider.
std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL
, prefix
, levelString
, "---"
, einfo.name
, dashes
, einfo.programName.value_or(""))
std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL,
prefix,
levelString,
"---",
einfo.name,
dashes,
einfo.programName.value_or(""))
<< std::endl;
// filename.
if (einfo.nixCode.has_value()) {
if (einfo.nixCode->nixFile.has_value()) {
string eline = einfo.nixCode->errLine.has_value()
? string(" ") + showErrLine(*einfo.nixCode->errLine)
if (einfo.nixCode->errPos.nixFile != "") {
string eline = einfo.nixCode->errLineOfCode != ""
? string(" ") + showErrPos(einfo.nixCode->errPos)
: "";
std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL
, prefix, *einfo.nixCode->nixFile, eline) << std::endl;
std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL,
prefix,
einfo.nixCode->errPos.nixFile,
eline) << std::endl;
std::cout << prefix << std::endl;
} else {
std::cout << fmt("%1%from command line argument", prefix) << std::endl;
@ -132,7 +126,7 @@ void printErrorInfo(ErrorInfo &einfo)
std::cout << prefix << std::endl;
// lines of code.
if (einfo.nixCode.has_value()) {
if (einfo.nixCode->errLineOfCode != "") {
printCodeLines(prefix, *einfo.nixCode);
std::cout << prefix << std::endl;
}

View file

@ -17,172 +17,40 @@ typedef enum {
elError
} ErrLevel;
class ErrorInfo;
class ErrLine
class ErrPos
{
public:
int lineNumber;
int column;
std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
string nixFile;
template <class P>
ErrPos& operator=(const P &pos)
{
lineNumber = pos.line;
column = pos.column;
nixFile = pos.file;
return *this;
}
template <class P>
ErrPos(const P &p)
{
*this = p;
}
};
class NixCode
{
public:
std::optional<string> nixFile;
std::optional<ErrLine> errLine;
ErrLine& ensureErrLine()
{
if (!this->errLine.has_value())
this->errLine = std::optional(ErrLine());
return *this->errLine;
}
ErrPos errPos;
std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
};
// -------------------------------------------------
// ErrorInfo.
// Forward friend class declarations. "builder classes"
template <class T>
class AddName;
template <class T>
class AddDescription;
template <class T>
class AddPos;
template <class T>
class AddLOC;
// The error info class itself.
class ErrorInfo
{
public:
ErrLevel level;
string name;
string description;
std::optional<NixCode> nixCode;
std::optional<string> hint;
ErrorInfo& GetEI()
{
return *this;
}
static std::optional<string> programName;
// give these access to the private constructor,
// when they are direct descendants (children but not grandchildren).
friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>;
friend AddPos<ErrorInfo>;
friend AddLOC<ErrorInfo>;
NixCode& ensureNixCode()
{
if (!this->nixCode.has_value())
this->nixCode = std::optional(NixCode());
return *this->nixCode;
}
protected:
// constructor is protected, so only the builder classes can create an ErrorInfo.
ErrorInfo(ErrLevel level)
{
this->level = level;
}
};
// Init as error
class EIError : public ErrorInfo
{
protected:
EIError() : ErrorInfo(elError) {}
};
// Init as warning
class EIWarning : public ErrorInfo
{
protected:
EIWarning() : ErrorInfo(elWarning) {}
};
// Builder class definitions.
template <class T>
class AddName : private T
{
public:
T& name(const std::string &name)
{
GetEI().name = name;
return *this;
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
};
template <class T>
class AddDescription : private T
{
public:
T& description(const std::string &description)
{
GetEI().description = description;
return *this;
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
};
template <class T>
class AddPos : private T
{
public:
template <class P>
T& pos(const P &aPos)
{
GetEI().ensureNixCode().nixFile = aPos.file;
GetEI().ensureNixCode().ensureErrLine().lineNumber = aPos.line;
GetEI().ensureNixCode().ensureErrLine().column = aPos.column;
return *this;
}
protected:
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)
{
GetEI().ensureNixCode().ensureErrLine().prevLineOfCode = prevloc;
GetEI().ensureNixCode().ensureErrLine().errLineOfCode = loc;
GetEI().ensureNixCode().ensureErrLine().nextLineOfCode = nextloc;
return *this;
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
};
// ----------------------------------------------------------------
// format for hints. same as fmt, except templated values
// format function for hints. same as fmt, except templated values
// are always in yellow.
template <class T>
@ -222,9 +90,13 @@ public:
friend class AddHint;
private:
format fmt;
};
std::ostream& operator<<(std::ostream &os, const hintformat &hf)
{
return os << hf.str();
}
template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
{
@ -233,61 +105,27 @@ inline hintformat hintfmt(const std::string & fs, const Args & ... args)
return f;
}
// the template layer for adding a hint.
template <class T>
class AddHint : private T
// -------------------------------------------------
// ErrorInfo.
class ErrorInfo
{
public:
T& hint(const hintformat &hf)
{
GetEI().hint = std::optional(hf.str());
return *this;
}
T& nohint()
{
GetEI().hint = std::nullopt;
return *this;
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
ErrLevel level;
string name;
string description;
std::optional<hintformat> hint;
std::optional<NixCode> nixCode;
static std::optional<string> programName;
private:
};
// --------------------------------------------------------
// error types
typedef AddName<
AddDescription<
AddHint<
EIError>>> ProgramError;
typedef AddName<
AddDescription<
AddHint<
EIWarning>>> ProgramWarning;
typedef AddName<
AddDescription<
AddPos<
AddLOC<
AddHint<
EIError>>>>> NixLangError;
typedef AddName<
AddDescription<
AddPos<
AddLOC<
AddHint<
EIWarning>>>>> NixLangWarning;
// --------------------------------------------------------
// error printing
// just to cout for now.
void printErrorInfo(ErrorInfo &einfo);
void printErrorInfo(const ErrorInfo &einfo);
}