handle Pos instead of individual file/line/columnrange args

This commit is contained in:
Ben Burdette 2020-04-03 13:15:59 -06:00
parent 7b7801d3f0
commit 9bb528d392
2 changed files with 31 additions and 80 deletions

View file

@ -8,7 +8,6 @@
#include <iomanip>
#include "types.hh"
#include <boost/format.hpp>
namespace nix
{
@ -62,19 +61,7 @@ template <class T>
class AddDescription;
template <class T>
class AddNixCode;
template <class T>
class AddNixFile;
template <class T>
class AddErrLine;
template <class T>
class AddLineNumber;
template <class T>
class AddColumnRange;
class AddPos;
template <class T>
class AddLOC;
@ -99,11 +86,7 @@ public:
// when they are direct descendants (children but not grandchildren).
friend AddName<ErrorInfo>;
friend AddDescription<ErrorInfo>;
friend AddNixCode<ErrorInfo>;
friend AddNixFile<ErrorInfo>;
friend AddErrLine<ErrorInfo>;
friend AddLineNumber<ErrorInfo>;
friend AddColumnRange<ErrorInfo>;
friend AddPos<ErrorInfo>;
friend AddLOC<ErrorInfo>;
NixCode& ensureNixCode()
@ -168,44 +151,15 @@ protected:
};
template <class T>
class AddNixFile : private T
class AddPos : private T
{
public:
T& nixFile(string filename)
template <class P>
T& pos(const P &aPos)
{
GetEI().ensureNixCode().nixFile = filename;
return *this;
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
};
template <class T>
class AddLineNumber : private T
{
public:
T& lineNumber(int lineNumber)
{
GetEI().ensureNixCode().ensureErrLine().lineNumber = lineNumber;
return *this;
}
protected:
ErrorInfo& GetEI()
{
return T::GetEI();
}
};
template <class T>
class AddColumnRange : private T
{
public:
T& columnRange(unsigned int start, unsigned int len)
{
GetEI().ensureNixCode().ensureErrLine().columnRange = { start, len };
GetEI().ensureNixCode().nixFile = aPos.file;
GetEI().ensureNixCode().ensureErrLine().lineNumber = aPos.line;
GetEI().ensureNixCode().ensureErrLine().columnRange = { .start = aPos.column, .len = 1 };
return *this;
}
protected:
@ -312,32 +266,28 @@ protected:
// error types
typedef AddName<
AddDescription<
AddHint<
EIError>>> ProgramError;
AddDescription<
AddHint<
EIError>>> ProgramError;
typedef AddName<
AddDescription<
AddHint<
EIWarning>>> ProgramWarning;
AddDescription<
AddHint<
EIWarning>>> ProgramWarning;
typedef AddName<
AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
AddLOC<
AddHint<
EIError>>>>>>> NixLangError;
AddDescription<
AddPos<
AddLOC<
AddHint<
EIError>>>>> NixLangError;
typedef AddName<
AddDescription<
AddNixFile<
AddLineNumber<
AddColumnRange<
AddLOC<
AddHint<
EIWarning>>>>>>> NixLangWarning;
AddDescription<
AddPos<
AddLOC<
AddHint<
EIWarning>>>>> NixLangWarning;
// --------------------------------------------------------

View file

@ -1,4 +1,5 @@
#include "../../src/libutil/error.hh"
#include "../../src/libexpr/nixexpr.hh"
#include <iostream>
#include <optional>
@ -58,12 +59,14 @@ int main()
// NixLangWarning adds nix file, line number, column range, and the lines of
// code where a warning occurred.
SymbolTable testTable;
auto problem_symbol = testTable.create("problem");
printErrorInfo(NixLangWarning()
.name("warning name")
.description("warning description")
.nixFile("myfile.nix")
.lineNumber(40)
.columnRange(13, 7)
.pos(Pos(problem_symbol, 40, 13))
.linesOfCode(std::nullopt,
"this is the problem line of code",
std::nullopt)
@ -74,9 +77,7 @@ int main()
printErrorInfo(NixLangError()
.name("error name")
.description("error description")
.nixFile("myfile.nix")
.lineNumber(40)
.columnRange(13, 7)
.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"))