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

@ -11,77 +11,56 @@ int main()
// In each program where errors occur, this has to be set. // In each program where errors occur, this has to be set.
ErrorInfo::programName = std::optional("error-demo"); ErrorInfo::programName = std::optional("error-demo");
// There are currently four error types: // Error in a program; no hint and no nix code.
// printErrorInfo(
// ProgramError, ProgramWarning, NixLangError, NixLangWarning. ErrorInfo { .level = elError,
// .name = "name",
// Each error type is created with a specific sequence of builder functions. .description = "error description",
// Unlike with a constructor, each parameter is clearly named. });
// If the sequence of function calls isn't followed, then there's a type
// error. This should make for a consistent look in the code when errors are
// created.
// ProgramError takes name, description, and an optional hint. // Warning with name, description, and hint.
printErrorInfo( ProgramError() // The hintfmt function makes all the substituted text yellow.
.name("name") printErrorInfo(
.description("error description") ErrorInfo { .level = elWarning,
.nohint() .name = "name",
); .description = "error description",
.hint = std::optional(
hintfmt("there was a %1%", "warning")),
});
// ProgramWarning takes name, description, and an optional hint.
// The hint is in the form of a hintfmt class, which wraps boost::format(),
// and makes all the substituted text yellow.
printErrorInfo( ProgramWarning()
.name("warning name")
.description("warning description")
// the templated value, 'warning', is automatically colored yellow.
.hint(hintfmt("there was a %1%", "warning"))
);
/* // Warning with nix file, line number, column, and the lines of
// some invalid errors:
// type error: no hint function.
ProgramError()
.name("name")
.description("error description");
// type error: description before name.
ProgramError()
.description("error description")
.name("name")
.nohint();
// type error: hint function with regular boost format, not special
hintfmt. ProgramError() .description("error description") .name("name")
.hint(format("there was a %1%") % "warning");
*/
// NixLangWarning adds nix file, line number, column range, and the lines of
// code where a warning occurred. // code where a warning occurred.
SymbolTable testTable; SymbolTable testTable;
auto problem_symbol = testTable.create("problem"); auto problem_file = testTable.create("myfile.nix");
printErrorInfo(NixLangWarning() printErrorInfo(
.name("warning name") ErrorInfo{
.description("warning description") .level = elWarning,
.pos(Pos(problem_symbol, 40, 13)) .name = "warning name",
.linesOfCode(std::nullopt, .description = "warning description",
"this is the problem line of code", .hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
std::nullopt) .nixCode = NixCode {
.hint(hintfmt("this hint has %1% templated %2%!!", "yellow" , "values"))); .errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = std::nullopt,
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::nullopt
}});
// Error with previous and next lines of code.
printErrorInfo(
ErrorInfo{
.level = elError,
.name = "error name",
.description = "error description",
.hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = std::optional("previous line of code"),
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::optional("next line of code"),
}});
// 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,44 +8,41 @@ namespace nix
std::optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
string showErrLine(ErrLine &errLine) string showErrPos(const ErrPos &errPos)
{ {
if (errLine.column > 0) { if (errPos.column > 0) {
return fmt("(%1%:%2%)", errLine.lineNumber, errLine.column); return fmt("(%1%:%2%)", errPos.lineNumber, errPos.column);
} else { } 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. // previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) { if (nixCode.prevLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%", std::cout << fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errLine->lineNumber - 1), (nixCode.errPos.lineNumber - 1),
*nixCode.errLine->prevLineOfCode) *nixCode.prevLineOfCode)
<< std::endl; << std::endl;
} }
// line of code containing the error.%2$+5d% // line of code containing the error.%2$+5d%
std::cout << fmt("%1% %|2$5d|| %3%", std::cout << fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errLine->lineNumber), (nixCode.errPos.lineNumber),
nixCode.errLine->errLineOfCode) nixCode.errLineOfCode)
<< std::endl; << std::endl;
// error arrows for the column range. // error arrows for the column range.
if (nixCode.errLine->column > 0) { if (nixCode.errPos.column > 0) {
int start = nixCode.errLine->column; int start = nixCode.errPos.column;
std::string spaces; std::string spaces;
for (int i = 0; i < start; ++i) { for (int i = 0; i < start; ++i) {
spaces.append(" "); spaces.append(" ");
} }
// for now, length of 1.
std::string arrows("^"); std::string arrows("^");
std::cout << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL, std::cout << fmt("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL,
@ -54,22 +51,17 @@ void printCodeLines(string &prefix, NixCode &nixCode)
arrows) << std::endl; arrows) << std::endl;
} }
// next line of code. // next line of code.
if (nixCode.errLine->nextLineOfCode.has_value()) { if (nixCode.nextLineOfCode.has_value()) {
std::cout << fmt("%1% %|2$5d|| %3%", std::cout << fmt("%1% %|2$5d|| %3%",
prefix, prefix,
(nixCode.errLine->lineNumber + 1), (nixCode.errPos.lineNumber + 1),
*nixCode.errLine->nextLineOfCode) *nixCode.nextLineOfCode)
<< std::endl; << std::endl;
} }
}
} }
void printErrorInfo(ErrorInfo &einfo) void printErrorInfo(const ErrorInfo &einfo)
{ {
int errwidth = 80; int errwidth = 80;
string prefix = " "; string prefix = " ";
@ -102,24 +94,26 @@ void printErrorInfo(ErrorInfo &einfo)
dashes.append("-"); dashes.append("-");
// divider. // divider.
std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL std::cout << fmt("%1%%2%" ANSI_BLUE " %3% %4% %5% %6%" ANSI_NORMAL,
, prefix prefix,
, levelString levelString,
, "---" "---",
, einfo.name einfo.name,
, dashes dashes,
, einfo.programName.value_or("")) einfo.programName.value_or(""))
<< std::endl; << std::endl;
// filename. // filename.
if (einfo.nixCode.has_value()) { if (einfo.nixCode.has_value()) {
if (einfo.nixCode->nixFile.has_value()) { if (einfo.nixCode->errPos.nixFile != "") {
string eline = einfo.nixCode->errLine.has_value() string eline = einfo.nixCode->errLineOfCode != ""
? string(" ") + showErrLine(*einfo.nixCode->errLine) ? string(" ") + showErrPos(einfo.nixCode->errPos)
: ""; : "";
std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL std::cout << fmt("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL,
, prefix, *einfo.nixCode->nixFile, eline) << std::endl; prefix,
einfo.nixCode->errPos.nixFile,
eline) << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} else { } else {
std::cout << fmt("%1%from command line argument", prefix) << std::endl; std::cout << fmt("%1%from command line argument", prefix) << std::endl;
@ -132,7 +126,7 @@ void printErrorInfo(ErrorInfo &einfo)
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
// lines of code. // lines of code.
if (einfo.nixCode.has_value()) { if (einfo.nixCode->errLineOfCode != "") {
printCodeLines(prefix, *einfo.nixCode); printCodeLines(prefix, *einfo.nixCode);
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }

View file

@ -17,172 +17,40 @@ typedef enum {
elError elError
} ErrLevel; } ErrLevel;
class ErrorInfo; class ErrPos
class ErrLine
{ {
public: public:
int lineNumber; int lineNumber;
int column; int column;
std::optional<string> prevLineOfCode; string nixFile;
string errLineOfCode;
std::optional<string> nextLineOfCode; 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 class NixCode
{ {
public: public:
std::optional<string> nixFile; ErrPos errPos;
std::optional<ErrLine> errLine; std::optional<string> prevLineOfCode;
string errLineOfCode;
ErrLine& ensureErrLine() std::optional<string> nextLineOfCode;
{
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().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. // are always in yellow.
template <class T> template <class T>
@ -222,9 +90,13 @@ public:
friend class AddHint; friend class AddHint;
private: private:
format fmt; format fmt;
}; };
std::ostream& operator<<(std::ostream &os, const hintformat &hf)
{
return os << hf.str();
}
template<typename... Args> template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... 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; return f;
} }
// the template layer for adding a hint. // -------------------------------------------------
template <class T> // ErrorInfo.
class AddHint : private T class ErrorInfo
{ {
public: public:
T& hint(const hintformat &hf) ErrLevel level;
{ string name;
GetEI().hint = std::optional(hf.str()); string description;
return *this; std::optional<hintformat> hint;
} std::optional<NixCode> nixCode;
T& nohint()
{ static std::optional<string> programName;
GetEI().hint = std::nullopt;
return *this; private:
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
}; };
// --------------------------------------------------------
// 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 // error printing
// just to cout for now. // just to cout for now.
void printErrorInfo(ErrorInfo &einfo); void printErrorInfo(const ErrorInfo &einfo);
} }