1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-09-20 23:28:26 -04:00
nix/src/libutil/error.hh

286 lines
5.6 KiB
C++
Raw Normal View History

#ifndef error_hh
#define error_hh
2020-03-27 12:55:09 -04:00
#include "ansicolor.hh"
#include <string>
#include <optional>
2020-03-24 13:21:35 -04:00
#include <iostream>
2020-03-27 12:03:02 -04:00
#include <iomanip>
2020-03-27 12:55:09 -04:00
#include <boost/format.hpp>
namespace nix {
2020-03-31 11:36:20 -04:00
typedef enum
{ elWarning
, elError
2020-03-31 11:36:20 -04:00
} ErrLevel;
class ColumnRange {
public:
2020-03-24 13:21:35 -04:00
unsigned int start;
unsigned int len;
};
2020-03-24 13:21:35 -04:00
class ErrorInfo;
class ErrLine {
public:
2020-03-24 13:21:35 -04:00
int lineNumber;
optional<ColumnRange> columnRange;
optional<string> prevLineOfCode;
string errLineOfCode;
optional<string> nextLineOfCode;
};
class NixCode {
public:
2020-03-24 13:21:35 -04:00
optional<string> nixFile;
optional<ErrLine> errLine;
ErrLine& ensureErrLine()
{
if (!this->errLine.has_value())
this->errLine = optional(ErrLine());
return *this->errLine;
2020-03-24 13:21:35 -04:00
}
};
// -------------------------------------------------
// ErrorInfo.
// Forward friend class declarations. "builder classes"
2020-03-24 13:21:35 -04:00
template <class T>
class AddName;
template <class T>
class AddDescription;
template <class T>
class AddNixCode;
template <class T>
class AddNixFile;
template <class T>
class AddErrLine;
template <class T>
class AddLineNumber;
template <class T>
class AddColumnRange;
template <class T>
class AddLOC;
// The error info class itself.
class ErrorInfo {
public:
2020-03-24 13:21:35 -04:00
ErrLevel level;
string name;
string description;
optional<NixCode> nixCode;
2020-03-27 12:03:02 -04:00
optional<string> hint;
2020-03-24 13:21:35 -04:00
ErrorInfo& GetEI() { return *this; }
2020-03-25 12:52:03 -04:00
static optional<string> programName;
2020-03-24 13:21:35 -04:00
// give these access to the private constructor,
2020-03-27 12:03:02 -04:00
// when they are direct descendants (children but not grandchildren).
2020-03-24 13:21:35 -04:00
friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>;
friend AddNixCode<ErrorInfo>;
friend AddNixFile<ErrorInfo>;
friend AddErrLine<ErrorInfo>;
friend AddLineNumber<ErrorInfo>;
friend AddColumnRange<ErrorInfo>;
friend AddLOC<ErrorInfo>;
2020-03-24 13:21:35 -04:00
NixCode& ensureNixCode()
{
if (!this->nixCode.has_value())
this->nixCode = optional(NixCode());
return *this->nixCode;
}
2020-03-24 13:21:35 -04:00
protected:
// constructor is protected, so only the builder classes can create an ErrorInfo.
2020-03-24 13:21:35 -04:00
ErrorInfo(ErrLevel level) { this->level = level; }
};
2020-03-25 12:52:03 -04:00
// Init as error
2020-03-24 13:21:35 -04:00
class EIError : public ErrorInfo
{
protected:
EIError() : ErrorInfo(elError) {}
};
2020-03-25 12:52:03 -04:00
// Init as warning
2020-03-24 13:21:35 -04:00
class EIWarning : public ErrorInfo
{
protected:
EIWarning() : ErrorInfo(elWarning) {}
};
2020-03-25 12:52:03 -04:00
// Builder class definitions.
2020-03-24 11:18:23 -04:00
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>
2020-03-27 12:03:02 -04:00
class AddNixFile : private T
{
public:
T& nixFile(string filename) {
GetEI().ensureNixCode().nixFile = filename;
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
template <class T>
2020-03-27 12:03:02 -04:00
class AddLineNumber : private T
2020-03-24 13:21:35 -04:00
{
public:
T& lineNumber(int lineNumber) {
GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
2020-03-24 13:21:35 -04:00
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
template <class T>
2020-03-27 12:03:02 -04:00
class AddColumnRange : private T
{
public:
T& columnRange(unsigned int start, unsigned int len) {
GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
template <class T>
2020-03-27 12:03:02 -04:00
class AddLOC : private T
{
public:
T& linesOfCode(optional<string> prevloc, string loc, 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(); }
};
2020-03-27 12:03:02 -04:00
template <class T>
class yellowify
{
public:
yellowify(T &s) : value(s) {}
T &value;
};
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
2020-03-27 12:55:09 -04:00
typedef AddName<
AddDescription<
AddHint<
2020-03-31 13:56:37 -04:00
EIError>>> ProgramError;
2020-03-31 14:42:41 -04:00
2020-03-27 12:55:09 -04:00
typedef AddName<
AddDescription<
AddHint<
2020-03-31 13:56:37 -04:00
EIWarning>>> ProgramWarning;
2020-03-24 13:21:35 -04:00
typedef AddName<
AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
2020-03-27 12:03:02 -04:00
AddLOC<
2020-03-27 12:55:09 -04:00
AddHint<
2020-03-31 13:56:37 -04:00
EIError>>>>>>> NixLangError;
2020-03-31 14:42:41 -04:00
typedef AddName<
AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
2020-03-27 12:55:09 -04:00
AddLOC<
AddHint<
2020-03-31 13:56:37 -04:00
EIWarning>>>>>>> NixLangWarning;
2020-03-24 11:18:23 -04:00
2020-03-27 12:03:02 -04:00
// --------------------------------------------------------
// error printing
// just to cout for now.
2020-03-27 12:03:02 -04:00
void printErrorInfo(ErrorInfo &einfo);
}
#endif