2020-04-01 21:51:14 +00:00
|
|
|
#ifndef error_hh
|
|
|
|
#define error_hh
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-03-27 16:55:09 +00:00
|
|
|
#include "ansicolor.hh"
|
2020-03-22 18:25:47 +00:00
|
|
|
#include <string>
|
|
|
|
#include <optional>
|
2020-03-24 17:21:35 +00:00
|
|
|
#include <iostream>
|
2020-03-27 16:03:02 +00:00
|
|
|
#include <iomanip>
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-04-03 14:48:20 +00:00
|
|
|
#include "types.hh"
|
2020-03-27 16:55:09 +00:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
namespace nix
|
|
|
|
{
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
typedef enum {
|
2020-04-02 20:25:43 +00:00
|
|
|
elWarning,
|
|
|
|
elError
|
|
|
|
} ErrLevel;
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
class ColumnRange
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int len;
|
2020-03-22 18:25:47 +00:00
|
|
|
};
|
|
|
|
|
2020-03-24 17:21:35 +00:00
|
|
|
class ErrorInfo;
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
class ErrLine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int lineNumber;
|
|
|
|
std::optional<ColumnRange> columnRange;
|
|
|
|
std::optional<string> prevLineOfCode;
|
|
|
|
string errLineOfCode;
|
|
|
|
std::optional<string> nextLineOfCode;
|
2020-03-24 17:21:35 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-03-24 17:21:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------
|
|
|
|
// ErrorInfo.
|
|
|
|
|
2020-04-01 22:20:20 +00:00
|
|
|
// Forward friend class declarations. "builder classes"
|
2020-03-24 17:21:35 +00:00
|
|
|
template <class T>
|
|
|
|
class AddName;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class AddDescription;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class AddNixCode;
|
2020-03-22 18:25:47 +00:00
|
|
|
|
2020-03-24 20:24:57 +00:00
|
|
|
template <class T>
|
|
|
|
class AddNixFile;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class AddErrLine;
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-24 20:24:57 +00:00
|
|
|
class AddLineNumber;
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-24 20:24:57 +00:00
|
|
|
class AddColumnRange;
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-24 20:24:57 +00:00
|
|
|
class AddLOC;
|
|
|
|
|
|
|
|
// The error info class itself.
|
2020-04-02 22:02:40 +00:00
|
|
|
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 AddNixCode<ErrorInfo>;
|
|
|
|
friend AddNixFile<ErrorInfo>;
|
|
|
|
friend AddErrLine<ErrorInfo>;
|
|
|
|
friend AddLineNumber<ErrorInfo>;
|
|
|
|
friend AddColumnRange<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;
|
|
|
|
}
|
2020-03-24 17:21:35 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 16:52:03 +00:00
|
|
|
// Init as error
|
2020-03-24 17:21:35 +00:00
|
|
|
class EIError : public ErrorInfo
|
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
protected:
|
|
|
|
EIError() : ErrorInfo(elError) {}
|
2020-03-24 17:21:35 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 16:52:03 +00:00
|
|
|
// Init as warning
|
2020-03-24 17:21:35 +00:00
|
|
|
class EIWarning : public ErrorInfo
|
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
protected:
|
|
|
|
EIWarning() : ErrorInfo(elWarning) {}
|
2020-03-22 18:25:47 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 16:52:03 +00:00
|
|
|
// Builder class definitions.
|
2020-03-24 15:18:23 +00:00
|
|
|
template <class T>
|
|
|
|
class AddName : private T
|
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
|
|
|
T& name(const std::string &name)
|
|
|
|
{
|
|
|
|
GetEI().name = name;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
ErrorInfo& GetEI()
|
|
|
|
{
|
|
|
|
return T::GetEI();
|
|
|
|
}
|
2020-03-24 15:18:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2020-04-02 22:02:40 +00:00
|
|
|
class AddDescription : private T
|
2020-03-24 15:18:23 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
|
|
|
T& description(const std::string &description)
|
|
|
|
{
|
|
|
|
GetEI().description = description;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
ErrorInfo& GetEI()
|
|
|
|
{
|
|
|
|
return T::GetEI();
|
|
|
|
}
|
2020-03-24 15:18:23 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-27 16:03:02 +00:00
|
|
|
class AddNixFile : private T
|
2020-03-24 20:24:57 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
|
|
|
T& nixFile(string filename)
|
|
|
|
{
|
|
|
|
GetEI().ensureNixCode().nixFile = filename;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
ErrorInfo& GetEI()
|
|
|
|
{
|
|
|
|
return T::GetEI();
|
|
|
|
}
|
2020-03-24 20:24:57 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-27 16:03:02 +00:00
|
|
|
class AddLineNumber : private T
|
2020-03-24 17:21:35 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
|
|
|
T& lineNumber(int lineNumber)
|
|
|
|
{
|
|
|
|
GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
ErrorInfo& GetEI()
|
|
|
|
{
|
|
|
|
return T::GetEI();
|
|
|
|
}
|
2020-03-24 17:21:35 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-27 16:03:02 +00:00
|
|
|
class AddColumnRange : private T
|
2020-03-24 20:24:57 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
|
|
|
T& columnRange(unsigned int start, unsigned int len)
|
|
|
|
{
|
|
|
|
GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
ErrorInfo& GetEI()
|
|
|
|
{
|
|
|
|
return T::GetEI();
|
|
|
|
}
|
2020-03-24 20:24:57 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-27 16:03:02 +00:00
|
|
|
class AddLOC : private T
|
2020-03-24 20:24:57 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-03-24 20:24:57 +00:00
|
|
|
};
|
|
|
|
|
2020-04-02 03:30:19 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------
|
2020-04-03 14:48:20 +00:00
|
|
|
// format for hints. same as fmt, except templated values
|
2020-04-02 03:30:19 +00:00
|
|
|
// are always in yellow.
|
|
|
|
|
2020-03-27 16:03:02 +00:00
|
|
|
template <class T>
|
|
|
|
class yellowify
|
|
|
|
{
|
|
|
|
public:
|
2020-04-02 22:02:40 +00:00
|
|
|
yellowify(T &s) : value(s) {}
|
|
|
|
T &value;
|
2020-03-27 16:03:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
std::ostream& operator<<(std::ostream &out, const yellowify<T> &y)
|
|
|
|
{
|
2020-04-01 22:20:20 +00:00
|
|
|
return out << ANSI_YELLOW << y.value << ANSI_NORMAL;
|
2020-03-27 16:03:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 14:48:20 +00:00
|
|
|
class hintformat
|
2020-03-27 16:03:02 +00:00
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
2020-04-03 14:48:20 +00:00
|
|
|
hintformat(string format) :fmt(format)
|
2020-04-02 22:02:40 +00:00
|
|
|
{
|
|
|
|
fmt.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
|
|
|
|
}
|
|
|
|
template<class T>
|
2020-04-03 14:48:20 +00:00
|
|
|
hintformat& operator%(const T &value)
|
2020-04-02 22:02:40 +00:00
|
|
|
{
|
|
|
|
fmt % yellowify(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:48:20 +00:00
|
|
|
std::string str() const
|
|
|
|
{
|
|
|
|
return fmt.str();
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:02:40 +00:00
|
|
|
template <typename U>
|
|
|
|
friend class AddHint;
|
|
|
|
private:
|
|
|
|
format fmt;
|
2020-04-01 22:20:20 +00:00
|
|
|
|
2020-03-27 16:03:02 +00:00
|
|
|
};
|
|
|
|
|
2020-04-03 14:48:20 +00:00
|
|
|
template<typename... Args>
|
|
|
|
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
|
|
|
|
{
|
|
|
|
hintformat f(fs);
|
|
|
|
formatHelper(f, args...);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2020-04-02 03:30:19 +00:00
|
|
|
// the template layer for adding a hint.
|
2020-04-02 22:02:40 +00:00
|
|
|
template <class T>
|
2020-03-27 16:03:02 +00:00
|
|
|
class AddHint : private T
|
|
|
|
{
|
2020-04-02 22:02:40 +00:00
|
|
|
public:
|
2020-04-03 14:48:20 +00:00
|
|
|
T& hint(const hintformat &hf)
|
2020-04-02 22:02:40 +00:00
|
|
|
{
|
2020-04-03 14:48:20 +00:00
|
|
|
GetEI().hint = std::optional(hf.str());
|
2020-04-02 22:02:40 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
T& nohint()
|
|
|
|
{
|
|
|
|
GetEI().hint = std::nullopt;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
ErrorInfo& GetEI()
|
|
|
|
{
|
|
|
|
return T::GetEI();
|
|
|
|
}
|
2020-03-27 16:03:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// error types
|
2020-03-24 20:24:57 +00:00
|
|
|
|
2020-03-27 16:55:09 +00:00
|
|
|
typedef AddName<
|
2020-04-02 22:02:40 +00:00
|
|
|
AddDescription<
|
|
|
|
AddHint<
|
|
|
|
EIError>>> ProgramError;
|
2020-03-31 18:42:41 +00:00
|
|
|
|
2020-03-27 16:55:09 +00:00
|
|
|
typedef AddName<
|
2020-04-02 22:02:40 +00:00
|
|
|
AddDescription<
|
|
|
|
AddHint<
|
|
|
|
EIWarning>>> ProgramWarning;
|
2020-03-24 17:21:35 +00:00
|
|
|
|
2020-03-24 20:24:57 +00:00
|
|
|
typedef AddName<
|
2020-04-02 22:02:40 +00:00
|
|
|
AddDescription<
|
|
|
|
AddNixFile<
|
|
|
|
AddLineNumber<
|
|
|
|
AddColumnRange<
|
|
|
|
AddLOC<
|
|
|
|
AddHint<
|
|
|
|
EIError>>>>>>> NixLangError;
|
2020-03-31 18:42:41 +00:00
|
|
|
|
2020-03-24 20:24:57 +00:00
|
|
|
typedef AddName<
|
2020-04-02 22:02:40 +00:00
|
|
|
AddDescription<
|
|
|
|
AddNixFile<
|
|
|
|
AddLineNumber<
|
|
|
|
AddColumnRange<
|
|
|
|
AddLOC<
|
|
|
|
AddHint<
|
|
|
|
EIWarning>>>>>>> NixLangWarning;
|
2020-03-24 15:18:23 +00:00
|
|
|
|
2020-03-27 16:03:02 +00:00
|
|
|
|
|
|
|
// --------------------------------------------------------
|
|
|
|
// error printing
|
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
// just to cout for now.
|
2020-03-27 16:03:02 +00:00
|
|
|
void printErrorInfo(ErrorInfo &einfo);
|
|
|
|
|
2020-03-22 18:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 21:51:14 +00:00
|
|
|
#endif
|