initializer style

This commit is contained in:
Ben Burdette 2020-04-08 09:07:58 -06:00
parent 00c507cc52
commit 47ed067d45
3 changed files with 91 additions and 160 deletions

View file

@ -18,22 +18,26 @@ int main()
// ProgramError takes name, description, and an optional hint. // ProgramError takes name, description, and an optional hint.
printErrorInfo( printErrorInfo(
ErrorInfo::ProgramError("name", ErrorInfo { .level = elError,
"error description", .name = "name",
std::nullopt)); .description = "error description",
});
// 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( printErrorInfo(
ErrorInfo::ProgramWarning("name", ErrorInfo { .level = elWarning,
"warning description", .name = "name",
std::optional( .description = "error description",
hintfmt("there was a %1%", "warning")))); .hint = std::optional(
hintfmt("there was a %1%", "warning"))
});
// NixLangWarning adds nix file, line number, column range, and the lines of // 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_symbol = testTable.create("problem");
printErrorInfo( printErrorInfo(
@ -58,5 +62,5 @@ int main()
std::optional("next line of code"), std::optional("next line of code"),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values"))); hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
return 0; */ return 0;
} }

View file

@ -8,74 +8,37 @@ namespace nix
std::optional<string> ErrorInfo::programName = std::nullopt; std::optional<string> ErrorInfo::programName = std::nullopt;
ErrorInfo ErrorInfo::ProgramError(const string &name, string showErrPos(const ErrPos &errPos)
const string &description,
const std::optional<hintformat> &hf)
{ {
return ProgramEI(elError, name, description, hf); if (errPos.column > 0) {
} return (format("(%1%:%2%)") % errPos.lineNumber % errPos.column).str();
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.column > 0) {
return (format("(%1%:%2%)") % errLine.lineNumber % errLine.column).str();
} else { } else {
return (format("(%1%)") % errLine.lineNumber).str(); return (format("(%1%)") % errPos.lineNumber).str();
}; };
} }
void printCodeLines(const string &prefix, const NixCode &nixCode) void printCodeLines(const string &prefix, const ErrorInfo &einfo)
{ {
if (einfo.errPos.has_value()) {
if (nixCode.errLine.has_value()) {
// previous line of code. // previous line of code.
if (nixCode.errLine->prevLineOfCode.has_value()) { if (einfo.prevLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (nixCode.errLine->lineNumber - 1) % (einfo.errPos->lineNumber - 1)
% *nixCode.errLine->prevLineOfCode % *einfo.prevLineOfCode
<< std::endl; << std::endl;
} }
// line of code containing the error.%2$+5d% // line of code containing the error.%2$+5d%
std::cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (nixCode.errLine->lineNumber) % (einfo.errPos->lineNumber)
% nixCode.errLine->errLineOfCode % einfo.errLineOfCode
<< std::endl; << std::endl;
// error arrows for the column range. // error arrows for the column range.
if (nixCode.errLine->column > 0) { if (einfo.errPos->column > 0) {
int start = nixCode.errLine->column; int start = einfo.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(" ");
@ -89,11 +52,11 @@ void printCodeLines(const string &prefix, const NixCode &nixCode)
// next line of code. // next line of code.
if (nixCode.errLine->nextLineOfCode.has_value()) { if (einfo.nextLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (nixCode.errLine->lineNumber + 1) % (einfo.errPos->lineNumber + 1)
% *nixCode.errLine->nextLineOfCode % *einfo.nextLineOfCode
<< std::endl; << std::endl;
} }
@ -144,14 +107,14 @@ void printErrorInfo(const ErrorInfo &einfo)
<< std::endl; << std::endl;
// filename. // filename.
if (einfo.nixCode.has_value()) { if (einfo.errPos.has_value()) {
if (einfo.nixCode->nixFile.has_value()) { if (einfo.errPos->nixFile != "") {
string eline = einfo.nixCode->errLine.has_value() string eline = einfo.errLineOfCode != ""
? string(" ") + showErrLine(*einfo.nixCode->errLine) ? string(" ") + showErrPos(*einfo.errPos)
: ""; : "";
std::cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL) std::cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL)
% prefix % *einfo.nixCode->nixFile % eline << std::endl; % prefix % einfo.errPos->nixFile % eline << std::endl;
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} else { } else {
std::cout << format("%1%from command line argument") % prefix << std::endl; std::cout << format("%1%from command line argument") % prefix << std::endl;
@ -164,8 +127,8 @@ void printErrorInfo(const 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.errLineOfCode != "") {
printCodeLines(prefix, *einfo.nixCode); printCodeLines(prefix, einfo);
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }

View file

@ -17,23 +17,21 @@ 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;
};
class NixCode template <class P>
{ ErrPos& operator=(const P &pos)
public: {
std::optional<string> nixFile; lineNumber = pos.line;
std::optional<ErrLine> errLine; column = pos.column;
nixFile = pos.file.str();
return *this;
}
}; };
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -79,6 +77,11 @@ 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)
{ {
@ -95,95 +98,56 @@ public:
ErrLevel level; ErrLevel level;
string name; string name;
string description; string description;
std::optional<NixCode> nixCode; std::optional<hintformat> hint;
std::optional<string> hint; std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
std::optional<ErrPos> errPos;
static std::optional<string> programName; static std::optional<string> programName;
ErrorInfo& set_name(const string &name) { this->name = name; return *this; }
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: private:
template <class P> // template <class P>
static ErrorInfo NixLangEI(ErrLevel level, // static ErrorInfo NixLangEI(ErrLevel level,
const string &name, // const string &name,
const string &description, // const string &description,
const P &pos, // const P &pos,
std::optional<string> prevloc, // std::optional<string> prevloc,
string loc, // string loc,
std::optional<string> nextloc, // std::optional<string> nextloc,
const std::optional<hintformat> &hf) // const std::optional<hintformat> &hf)
{ // {
ErrorInfo ei(level); // ErrorInfo ei(level);
ei.name = name; // ei.name = name;
ei.description = description; // ei.description = description;
if (hf.has_value()) // if (hf.has_value())
ei.hint = std::optional<string>(hf->str()); // ei.hint = std::optional<string>(hf->str());
else // else
ei.hint = std::nullopt; // ei.hint = std::nullopt;
ErrLine errline; // ErrLine errline;
errline.lineNumber = pos.line; // errline.lineNumber = pos.line;
errline.column = pos.column; // errline.column = pos.column;
errline.prevLineOfCode = prevloc; // errline.prevLineOfCode = prevloc;
errline.errLineOfCode = loc; // errline.errLineOfCode = loc;
errline.nextLineOfCode = nextloc; // errline.nextLineOfCode = nextloc;
NixCode nixcode; // NixCode nixcode;
nixcode.nixFile = pos.file; // nixcode.nixFile = pos.file;
nixcode.errLine = std::optional(errline); // nixcode.errLine = std::optional(errline);
ei.nixCode = std::optional(nixcode); // ei.nixCode = std::optional(nixcode);
return ei; // return ei;
} // }
static ErrorInfo ProgramEI(ErrLevel level, // static ErrorInfo ProgramEI(ErrLevel level,
const string &name, // const string &name,
const string &description, // const string &description,
const std::optional<hintformat> &hf); // const std::optional<hintformat> &hf);
// constructor is protected, so only the builder classes can create an ErrorInfo. // constructor is protected, so only the builder classes can create an ErrorInfo.
ErrorInfo(ErrLevel level)
{
this->level = level;
}
}; };
// -------------------------------------------------------- // --------------------------------------------------------