aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/error-demo/error-demo.cc22
-rw-r--r--src/libutil/error.cc85
-rw-r--r--src/libutil/error.hh150
3 files changed, 94 insertions, 163 deletions
diff --git a/src/error-demo/error-demo.cc b/src/error-demo/error-demo.cc
index ef8b56308..7eef0b162 100644
--- a/src/error-demo/error-demo.cc
+++ b/src/error-demo/error-demo.cc
@@ -18,22 +18,26 @@ int main()
// ProgramError takes name, description, and an optional hint.
printErrorInfo(
- ErrorInfo::ProgramError("name",
- "error description",
- std::nullopt));
+ ErrorInfo { .level = elError,
+ .name = "name",
+ .description = "error description",
+ });
// 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(
- ErrorInfo::ProgramWarning("name",
- "warning description",
- std::optional(
- hintfmt("there was a %1%", "warning"))));
+ ErrorInfo { .level = elWarning,
+ .name = "name",
+ .description = "error description",
+ .hint = std::optional(
+ hintfmt("there was a %1%", "warning"))
+ });
+
// NixLangWarning adds nix file, line number, column range, and the lines of
// code where a warning occurred.
- SymbolTable testTable;
+/* SymbolTable testTable;
auto problem_symbol = testTable.create("problem");
printErrorInfo(
@@ -58,5 +62,5 @@ int main()
std::optional("next line of code"),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
- return 0;
+*/ return 0;
}
diff --git a/src/libutil/error.cc b/src/libutil/error.cc
index db8821a5c..24ed4df2e 100644
--- a/src/libutil/error.cc
+++ b/src/libutil/error.cc
@@ -8,74 +8,37 @@ namespace nix
std::optional<string> ErrorInfo::programName = std::nullopt;
-ErrorInfo ErrorInfo::ProgramError(const string &name,
- const string &description,
- const std::optional<hintformat> &hf)
+string showErrPos(const ErrPos &errPos)
{
- 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.column > 0) {
- return (format("(%1%:%2%)") % errLine.lineNumber % errLine.column).str();
+ if (errPos.column > 0) {
+ return (format("(%1%:%2%)") % errPos.lineNumber % errPos.column).str();
} 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 (nixCode.errLine.has_value()) {
+ if (einfo.errPos.has_value()) {
// previous line of code.
- if (nixCode.errLine->prevLineOfCode.has_value()) {
+ if (einfo.prevLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%")
- % prefix
- % (nixCode.errLine->lineNumber - 1)
- % *nixCode.errLine->prevLineOfCode
+ % prefix
+ % (einfo.errPos->lineNumber - 1)
+ % *einfo.prevLineOfCode
<< std::endl;
}
// line of code containing the error.%2$+5d%
std::cout << format("%1% %|2$5d|| %3%")
% prefix
- % (nixCode.errLine->lineNumber)
- % nixCode.errLine->errLineOfCode
+ % (einfo.errPos->lineNumber)
+ % einfo.errLineOfCode
<< std::endl;
// error arrows for the column range.
- if (nixCode.errLine->column > 0) {
- int start = nixCode.errLine->column;
+ if (einfo.errPos->column > 0) {
+ int start = einfo.errPos->column;
std::string spaces;
for (int i = 0; i < start; ++i) {
spaces.append(" ");
@@ -89,11 +52,11 @@ void printCodeLines(const string &prefix, const NixCode &nixCode)
// next line of code.
- if (nixCode.errLine->nextLineOfCode.has_value()) {
+ if (einfo.nextLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%")
% prefix
- % (nixCode.errLine->lineNumber + 1)
- % *nixCode.errLine->nextLineOfCode
+ % (einfo.errPos->lineNumber + 1)
+ % *einfo.nextLineOfCode
<< std::endl;
}
@@ -144,14 +107,14 @@ void printErrorInfo(const ErrorInfo &einfo)
<< std::endl;
// filename.
- if (einfo.nixCode.has_value()) {
- if (einfo.nixCode->nixFile.has_value()) {
- string eline = einfo.nixCode->errLine.has_value()
- ? string(" ") + showErrLine(*einfo.nixCode->errLine)
+ if (einfo.errPos.has_value()) {
+ if (einfo.errPos->nixFile != "") {
+ string eline = einfo.errLineOfCode != ""
+ ? string(" ") + showErrPos(*einfo.errPos)
: "";
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;
} else {
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;
// lines of code.
- if (einfo.nixCode.has_value()) {
- printCodeLines(prefix, *einfo.nixCode);
+ if (einfo.errLineOfCode != "") {
+ printCodeLines(prefix, einfo);
std::cout << prefix << std::endl;
}
diff --git a/src/libutil/error.hh b/src/libutil/error.hh
index e3bb2c1dd..b687bde81 100644
--- a/src/libutil/error.hh
+++ b/src/libutil/error.hh
@@ -17,23 +17,21 @@ typedef enum {
elError
} ErrLevel;
-class ErrorInfo;
-
-class ErrLine
+class ErrPos
{
public:
int lineNumber;
int column;
- std::optional<string> prevLineOfCode;
- string errLineOfCode;
- std::optional<string> nextLineOfCode;
-};
+ string nixFile;
-class NixCode
-{
-public:
- std::optional<string> nixFile;
- std::optional<ErrLine> errLine;
+ template <class P>
+ ErrPos& operator=(const P &pos)
+ {
+ lineNumber = pos.line;
+ column = pos.column;
+ nixFile = pos.file.str();
+ return *this;
+ }
};
// ----------------------------------------------------------------
@@ -79,6 +77,11 @@ private:
format fmt;
};
+std::ostream& operator<<(std::ostream &os, const hintformat &hf)
+{
+ return os << hf.str();
+}
+
template<typename... Args>
inline hintformat hintfmt(const std::string & fs, const Args & ... args)
{
@@ -95,95 +98,56 @@ public:
ErrLevel level;
string name;
string description;
- std::optional<NixCode> nixCode;
- std::optional<string> hint;
+ std::optional<hintformat> hint;
+ std::optional<string> prevLineOfCode;
+ string errLineOfCode;
+ std::optional<string> nextLineOfCode;
+ std::optional<ErrPos> errPos;
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:
- 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.column = pos.column;
- 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);
+ // 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.column = pos.column;
+ // 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;
- }
+
};
// --------------------------------------------------------