MkNixCode, MkErrLine approach

This commit is contained in:
Ben Burdette 2020-03-24 11:21:35 -06:00
parent 4171ab4bbd
commit 0166e7ab6d
3 changed files with 211 additions and 20 deletions

View file

@ -94,7 +94,11 @@ void print_error(ErrorInfo &einfo)
level_string = "warning:"; // TODO make yellow. level_string = "warning:"; // TODO make yellow.
break; break;
} }
} default:
{
level_string = "wat:";
break;
}}
int ndl = level_string.length() + 3 + einfo.name.length() + einfo.program.length(); int ndl = level_string.length() + 3 + einfo.name.length() + einfo.program.length();
int dashwidth = errwidth - 3 ? 3 : 80 - ndl; int dashwidth = errwidth - 3 ? 3 : 80 - ndl;

View file

@ -3,6 +3,7 @@
#include "types.hh" #include "types.hh"
#include <string> #include <string>
#include <optional> #include <optional>
#include <iostream>
using std::string; using std::string;
using std::optional; using std::optional;
@ -16,35 +17,171 @@ enum ErrLevel
class ColumnRange { class ColumnRange {
public: public:
unsigned int start; unsigned int start;
unsigned int len; unsigned int len;
}; };
class ErrorInfo;
// -------------------------------------------------
// forward declarations before ErrLine.
template <class T>
class AddLineNumber;
template <class T>
class AddColumnRange;
template <class T>
class AddLOC;
class ErrLine { class ErrLine {
public: public:
int lineNumber; int lineNumber;
optional<ColumnRange> columnRange; optional<ColumnRange> columnRange;
optional<string> prevLineOfCode; optional<string> prevLineOfCode;
string errLineOfCode; string errLineOfCode;
optional<string> nextLineOfCode; optional<string> nextLineOfCode;
friend AddLineNumber<ErrLine>;
friend AddColumnRange<ErrLine>;
friend AddLOC<ErrLine>;
ErrLine& GetEL() { return *this; }
private:
ErrLine() {}
}; };
template <class T>
class AddLineNumber : public T
{
public:
T& lineNumber(int lineNumber) {
GetEL().lineNumber = lineNumber;
return *this;
}
protected:
ErrLine& GetEL() { return T::GetEL(); }
};
template <class T>
class AddColumnRange : public T
{
public:
T& columnRange(unsigned int start, unsigned int len) {
GetEL().columnRange = { start, len };
return *this;
}
protected:
ErrLine& GetEL() { return T::GetEL(); }
};
template <class T>
class AddLOC : public T
{
public:
T& linesOfCode(optional<string> prevloc, string loc, optional<string> nextloc) {
GetEL().prevLineOfCode = prevloc;
GetEL().errLineOfCode = loc;
GetEL().nextLineOfCode = nextloc;
return *this;
}
protected:
ErrLine& GetEL() { return T::GetEL(); }
};
typedef AddLineNumber<AddColumnRange<AddLOC<ErrLine>>> MkErrLine;
MkErrLine mkErrLine;
// -------------------------------------------------
// NixCode.
template <class T>
class AddNixFile;
template <class T>
class AddErrLine;
class NixCode { class NixCode {
public: public:
optional<string> nixFile; optional<string> nixFile;
optional<ErrLine> errLine; optional<ErrLine> errLine;
friend AddNixFile<NixCode>;
friend AddErrLine<NixCode>;
friend ErrorInfo;
NixCode& GetNC() { return *this; }
private:
NixCode() {}
}; };
template <class T>
class AddNixFile : public T
{
public:
T& nixFile(string filename) {
GetNC().nixFile = filename;
return *this;
}
protected:
NixCode& GetNC() { return T::GetNC(); }
};
template <class T>
class AddErrLine : public T
{
public:
T& errLine(ErrLine errline) {
GetNC().errLine = errline;
return *this;
}
protected:
NixCode& GetNC() { return T::GetNC(); }
};
typedef AddNixFile<AddErrLine<NixCode>> MkNixCode;
// -------------------------------------------------
// ErrorInfo.
template <class T>
class AddName;
template <class T>
class AddDescription;
template <class T>
class AddNixCode;
class ErrorInfo { class ErrorInfo {
public: public:
ErrLevel level; ErrLevel level;
string name; string name;
string description; string description;
string program; string program;
optional<NixCode> nixCode; optional<NixCode> nixCode;
string hint; string hint;
ErrorInfo& GetEI() { return *this; } ErrorInfo& GetEI() { return *this; }
// give these access to the private constructor,
// when they are direct descendants.
friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>;
friend AddNixCode<ErrorInfo>;
protected:
ErrorInfo(ErrLevel level) { this->level = level; }
};
class EIError : public ErrorInfo
{
protected:
EIError() : ErrorInfo(elError) {}
};
class EIWarning : public ErrorInfo
{
protected:
EIWarning() : ErrorInfo(elWarning) {}
}; };
template <class T> template <class T>
@ -71,7 +208,23 @@ class AddDescription : private T
ErrorInfo& GetEI() { return T::GetEI(); } ErrorInfo& GetEI() { return T::GetEI(); }
}; };
typedef AddName<AddDescription<ErrorInfo>> StandardError; template <class T>
class AddNixCode : private T
{
public:
T& nixcode(const NixCode &nixcode){
GetEI().nixCode = nixcode;
return *this;
}
protected:
ErrorInfo& GetEI() { return T::GetEI(); }
};
typedef AddName<AddDescription<EIError>> StandardError;
typedef AddName<AddDescription<EIWarning>> StandardWarning;
typedef AddName<AddDescription<AddNixCode<EIError>>> MkNixError;
typedef AddName<AddDescription<AddNixCode<EIWarning>>> MkNixWarning;
string showErrLine(ErrLine &errLine); string showErrLine(ErrLine &errLine);

View file

@ -4,11 +4,13 @@
#include <iostream> #include <iostream>
using std::optional; using std::optional;
using std::nullopt;
int main() { int main() {
using namespace nix; using namespace nix;
/*
ColumnRange columnRange; ColumnRange columnRange;
columnRange.start = 24; columnRange.start = 24;
columnRange.len = 14; columnRange.len = 14;
@ -30,11 +32,43 @@ using namespace nix;
generic.nixCode = nixcode; generic.nixCode = nixcode;
print_error(generic); print_error(generic);
*/
StandardError standardError; StandardError standardError;
print_error(standardError.name("blah").description("blah")); print_error(standardError
.name("name")
.description("description"));
StandardWarning standardWarning;
print_error(standardWarning
.name("warning name")
.description("warning description"));
print_error(MkNixError()
.name("name")
.description("description")
.nixcode(
MkNixCode()
.nixFile("myfile.nix")
.errLine(MkErrLine().lineNumber(40)
.columnRange(50,10)
.linesOfCode(nullopt
,"this is the problem line of code"
,nullopt))));
print_error(MkNixWarning()
.name("name")
.description("description")
.nixcode(
MkNixCode()
.nixFile("myfile.nix")
.errLine(MkErrLine().lineNumber(40)
.columnRange(50,10)
.linesOfCode(nullopt
,"this is the problem line of code"
,nullopt))));
return 0; return 0;
} }