return of NixCode

This commit is contained in:
Ben Burdette 2020-04-08 09:48:21 -06:00
parent 47ed067d45
commit 54f91923c8
3 changed files with 85 additions and 113 deletions

View file

@ -31,36 +31,43 @@ int main()
.name = "name",
.description = "error description",
.hint = std::optional(
hintfmt("there was a %1%", "warning"))
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;
auto problem_symbol = testTable.create("problem");
SymbolTable testTable;
auto problem_file = testTable.create("myfile.nix");
printErrorInfo(
ErrorInfo::NixLangWarning(
"warning name",
"warning description",
Pos(problem_symbol, 40, 13),
std::nullopt,
"this is the problem line of code",
std::nullopt,
hintfmt("this hint has %1% templated %2%!!", "yellow", "values")));
ErrorInfo{
.level = elWarning,
.name = "warning name",
.description = "warning description",
.hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
.nixCode = NixCode {
.errPos = Pos(problem_file, 40, 13),
.prevLineOfCode = std::nullopt,
.errLineOfCode = "this is the problem line of code",
.nextLineOfCode = std::nullopt
}});
// NixLangError is just the same as NixLangWarning, except for the Error
// flag.
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")));
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"),
}});
*/ return 0;
return 0;
}

View file

@ -17,51 +17,45 @@ string showErrPos(const ErrPos &errPos)
};
}
void printCodeLines(const string &prefix, const ErrorInfo &einfo)
void printCodeLines(const string &prefix, const NixCode &nixCode)
{
if (einfo.errPos.has_value()) {
// previous line of code.
if (einfo.prevLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%")
% prefix
% (einfo.errPos->lineNumber - 1)
% *einfo.prevLineOfCode
<< std::endl;
}
// line of code containing the error.%2$+5d%
// previous line of code.
if (nixCode.prevLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%")
% prefix
% (einfo.errPos->lineNumber)
% einfo.errLineOfCode
% prefix
% (nixCode.errPos.lineNumber - 1)
% *nixCode.prevLineOfCode
<< std::endl;
// error arrows for the column range.
if (einfo.errPos->column > 0) {
int start = einfo.errPos->column;
std::string spaces;
for (int i = 0; i < start; ++i) {
spaces.append(" ");
}
std::string arrows("^");
std::cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << std::endl;
}
// next line of code.
if (einfo.nextLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%")
% prefix
% (einfo.errPos->lineNumber + 1)
% *einfo.nextLineOfCode
<< std::endl;
}
}
// line of code containing the error.%2$+5d%
std::cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errPos.lineNumber)
% nixCode.errLineOfCode
<< std::endl;
// error arrows for the column range.
if (nixCode.errPos.column > 0) {
int start = nixCode.errPos.column;
std::string spaces;
for (int i = 0; i < start; ++i) {
spaces.append(" ");
}
std::string arrows("^");
std::cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << std::endl;
}
// next line of code.
if (nixCode.nextLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%")
% prefix
% (nixCode.errPos.lineNumber + 1)
% *nixCode.nextLineOfCode
<< std::endl;
}
}
void printErrorInfo(const ErrorInfo &einfo)
@ -107,14 +101,14 @@ void printErrorInfo(const ErrorInfo &einfo)
<< std::endl;
// filename.
if (einfo.errPos.has_value()) {
if (einfo.errPos->nixFile != "") {
string eline = einfo.errLineOfCode != ""
? string(" ") + showErrPos(*einfo.errPos)
if (einfo.nixCode.has_value()) {
if (einfo.nixCode->errPos.nixFile != "") {
string eline = einfo.nixCode->errLineOfCode != ""
? string(" ") + showErrPos(einfo.nixCode->errPos)
: "";
std::cout << format("%1%in file: " ANSI_BLUE "%2%%3%" ANSI_NORMAL)
% prefix % einfo.errPos->nixFile % eline << std::endl;
% prefix % einfo.nixCode->errPos.nixFile % eline << std::endl;
std::cout << prefix << std::endl;
} else {
std::cout << format("%1%from command line argument") % prefix << std::endl;
@ -127,8 +121,8 @@ void printErrorInfo(const ErrorInfo &einfo)
std::cout << prefix << std::endl;
// lines of code.
if (einfo.errLineOfCode != "") {
printCodeLines(prefix, einfo);
if (einfo.nixCode->errLineOfCode != "") {
printCodeLines(prefix, *einfo.nixCode);
std::cout << prefix << std::endl;
}

View file

@ -29,9 +29,24 @@ public:
{
lineNumber = pos.line;
column = pos.column;
nixFile = pos.file.str();
nixFile = pos.file;
return *this;
}
template <class P>
ErrPos(const P &p)
{
*this = p;
}
};
class NixCode
{
public:
ErrPos errPos;
std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
};
// ----------------------------------------------------------------
@ -99,55 +114,11 @@ public:
string name;
string description;
std::optional<hintformat> hint;
std::optional<string> prevLineOfCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
std::optional<ErrPos> errPos;
std::optional<NixCode> nixCode;
static std::optional<string> programName;
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);
// constructor is protected, so only the builder classes can create an ErrorInfo.
};
// --------------------------------------------------------