constructor style basically working

This commit is contained in:
Ben Burdette 2020-04-06 19:43:22 -06:00
parent 2248cc6716
commit ec449c8450
3 changed files with 222 additions and 180 deletions

View file

@ -22,21 +22,25 @@ int main()
// created. // created.
// ProgramError takes name, description, and an optional hint. // ProgramError takes name, description, and an optional hint.
printErrorInfo( ProgramError() printErrorInfo(
.name("name") ErrorInfo::ProgramError("name",
.description("error description") "error description",
.nohint() std::nullopt));
);
// ProgramWarning takes name, description, and an optional hint. // ProgramWarning takes name, description, and an optional hint.
// The hint is in the form of a hintfmt class, which wraps boost::format(), // The hint is in the form of a hintfmt class, which wraps boost::format(),
// and makes all the substituted text yellow. // and makes all the substituted text yellow.
printErrorInfo( ProgramWarning() printErrorInfo(
.name("warning name") ErrorInfo::ProgramWarning("name",
.description("warning description") "warning description",
// the templated value, 'warning', is automatically colored yellow. std::optional(hintfmt("there was a %1%", "warning"))));
.hint(hintfmt("there was a %1%", "warning"))
); // printErrorInfo( ProgramWarning()
// .name("warning name")
// .description("warning description")
// // the templated value, 'warning', is automatically colored yellow.
// .hint(hintfmt("there was a %1%", "warning"))
// );
/* /*
// some invalid errors: // some invalid errors:
@ -63,25 +67,34 @@ int main()
SymbolTable testTable; SymbolTable testTable;
auto problem_symbol = testTable.create("problem"); auto problem_symbol = testTable.create("problem");
printErrorInfo(NixLangWarning() printErrorInfo(ErrorInfo::NixLangWarning(
.name("warning name") "warning name",
.description("warning description") "warning description",
.pos(Pos(problem_symbol, 40, 13)) Pos(problem_symbol, 40, 13),
.linesOfCode(std::nullopt, std::nullopt,
"this is the problem line of code", "this is the problem line of code",
std::nullopt) std::nullopt,
.hint(hintfmt("this hint has %1% templated %2%!!", "yellow" , "values"))); hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
// // NixLangError is just the same as NixLangWarning, except for the Error
// // flag.
// printErrorInfo(NixLangError()
// .name("error name")
// .description("error description")
// .pos(Pos(problem_symbol, 40, 13))
// .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")));
printErrorInfo(ErrorInfo::NixLangError(
"error name",
"error description",
Pos(problem_symbol, 40, 13),
std::optional("previous line of code"),
"this is the problem line of code",
std::optional("next line of code"),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
// NixLangError is just the same as NixLangWarning, except for the Error
// flag.
printErrorInfo(NixLangError()
.name("error name")
.description("error description")
.pos(Pos(problem_symbol, 40, 13))
.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; return 0;
} }

View file

@ -8,7 +8,43 @@ namespace nix
std::optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
string showErrLine(ErrLine &errLine) ErrorInfo ErrorInfo::ProgramError(const string &name,
const string &description,
const std::optional<hintformat> &hf)
{
return ProgramEI(elError, name, description, hf);
}
ErrorInfo ErrorInfo::ProgramWarning(const string &name,
const string &description,
const std::optional<hintformat> &hf)
{
return ProgramEI(elWarning, name, description, hf);
}
ErrorInfo ErrorInfo::ProgramEI(ErrLevel level,
const string &name,
const string &description,
const std::optional<hintformat> &hf)
{
ErrorInfo ei(elError);
ei.name = name;
ei.description = description;
if (hf.has_value())
ei.hint = std::optional<string>(hf->str());
else
ei.hint = std::nullopt;
return ei;
}
string showErrLine(const ErrLine &errLine)
{ {
if (errLine.columnRange.has_value()) { if (errLine.columnRange.has_value()) {
return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str(); return (format("(%1%:%2%)") % errLine.lineNumber % errLine.columnRange->start).str();
@ -17,7 +53,7 @@ string showErrLine(ErrLine &errLine)
}; };
} }
void printCodeLines(string &prefix, NixCode &nixCode) void printCodeLines(const string &prefix, const NixCode &nixCode)
{ {
if (nixCode.errLine.has_value()) { if (nixCode.errLine.has_value()) {
@ -69,7 +105,7 @@ void printCodeLines(string &prefix, NixCode &nixCode)
} }
void printErrorInfo(ErrorInfo &einfo) void printErrorInfo(const ErrorInfo &einfo)
{ {
int errwidth = 80; int errwidth = 80;
string prefix = " "; string prefix = " ";

View file

@ -41,153 +41,8 @@ class NixCode
public: public:
std::optional<string> nixFile; std::optional<string> nixFile;
std::optional<ErrLine> errLine; std::optional<ErrLine> errLine;
ErrLine& ensureErrLine()
{
if (!this->errLine.has_value())
this->errLine = std::optional(ErrLine());
return *this->errLine;
}
}; };
// -------------------------------------------------
// 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().columnRange = { .start = aPos.column, .len = 1 };
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 for hints. same as fmt, except templated values
// are always in yellow. // are always in yellow.
@ -229,7 +84,6 @@ public:
friend class AddHint; friend class AddHint;
private: private:
format fmt; format fmt;
}; };
template<typename... Args> template<typename... Args>
@ -240,6 +94,143 @@ inline hintformat hintfmt(const std::string & fs, const Args & ... args)
return f; return f;
} }
// -------------------------------------------------
// ErrorInfo.
class ErrorInfo
{
public:
ErrLevel level;
string name;
string description;
std::optional<NixCode> nixCode;
std::optional<string> hint;
static std::optional<string> programName;
static ErrorInfo ProgramError(const string &name,
const string &description,
const std::optional<hintformat> &hf);
static ErrorInfo ProgramWarning(const string &name,
const string &description,
const std::optional<hintformat> &hf);
template <class P>
static ErrorInfo NixLangError(const string &name,
const string &description,
const P &pos,
std::optional<string> prevloc,
string loc,
std::optional<string> nextloc,
const std::optional<hintformat> &hf)
{
return NixLangEI(elError, name, description, pos, prevloc, loc, nextloc, hf);
}
template <class P>
static ErrorInfo NixLangWarning(const string &name,
const string &description,
const P &pos,
std::optional<string> prevloc,
string loc,
std::optional<string> nextloc,
const std::optional<hintformat> &hf)
{
return NixLangEI(elWarning, name, description, pos, prevloc, loc, nextloc, hf);
}
private:
template <class P>
static ErrorInfo NixLangEI(ErrLevel level,
const string &name,
const string &description,
const P &pos,
std::optional<string> prevloc,
string loc,
std::optional<string> nextloc,
const std::optional<hintformat> &hf)
{
ErrorInfo ei(level);
ei.name = name;
ei.description = description;
if (hf.has_value())
ei.hint = std::optional<string>(hf->str());
else
ei.hint = std::nullopt;
ErrLine errline;
errline.lineNumber = pos.line;
errline.columnRange = { .start = pos.column, .len = 1 };
errline.prevLineOfCode = prevloc;
errline.errLineOfCode = loc;
errline.nextLineOfCode = nextloc;
NixCode nixcode;
nixcode.nixFile = pos.file;
nixcode.errLine = std::optional(errline);
ei.nixCode = std::optional(nixcode);
return ei;
}
static ErrorInfo ProgramEI(ErrLevel level,
const string &name,
const string &description,
const std::optional<hintformat> &hf);
// constructor is protected, so only the builder classes can create an ErrorInfo.
ErrorInfo(ErrLevel level)
{
this->level = level;
}
};
/*
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().columnRange = { .start = aPos.column, .len = 1 };
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();
}
};
*/
/*
// the template layer for adding a hint. // the template layer for adding a hint.
template <class T> template <class T>
class AddHint : private T class AddHint : private T
@ -261,11 +252,12 @@ protected:
return T::GetEI(); return T::GetEI();
} }
}; };
*/
// -------------------------------------------------------- // --------------------------------------------------------
// error types // error types
typedef AddName< /*typedef AddName<
AddDescription< AddDescription<
AddHint< AddHint<
EIError>>> ProgramError; EIError>>> ProgramError;
@ -290,11 +282,12 @@ typedef AddName<
EIWarning>>>>> NixLangWarning; EIWarning>>>>> NixLangWarning;
*/
// -------------------------------------------------------- // --------------------------------------------------------
// error printing // error printing
// just to cout for now. // just to cout for now.
void printErrorInfo(ErrorInfo &einfo); void printErrorInfo(const ErrorInfo &einfo);
} }