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", .name = "name",
.description = "error description", .description = "error description",
.hint = std::optional( .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 // 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( printErrorInfo(
ErrorInfo::NixLangWarning( ErrorInfo{
"warning name", .level = elWarning,
"warning description", .name = "warning name",
Pos(problem_symbol, 40, 13), .description = "warning description",
std::nullopt, .hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
"this is the problem line of code", .nixCode = NixCode {
std::nullopt, .errPos = Pos(problem_file, 40, 13),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values"))); .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 // NixLangError is just the same as NixLangWarning, except for the Error
// flag. // flag.
printErrorInfo( printErrorInfo(
ErrorInfo::NixLangError( ErrorInfo{
"error name", .level = elError,
"error description", .name = "error name",
Pos(problem_symbol, 40, 13), .description = "error description",
std::optional("previous line of code"), .hint = hintfmt("this hint has %1% templated %2%!!", "yellow", "values"),
"this is the problem line of code", .nixCode = NixCode {
std::optional("next line of code"), .errPos = Pos(problem_file, 40, 13),
hintfmt("this hint has %1% templated %2%!!", "yellow", "values"))); .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,28 +17,27 @@ 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. // previous line of code.
if (einfo.prevLineOfCode.has_value()) { if (nixCode.prevLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (einfo.errPos->lineNumber - 1) % (nixCode.errPos.lineNumber - 1)
% *einfo.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 << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (einfo.errPos->lineNumber) % (nixCode.errPos.lineNumber)
% einfo.errLineOfCode % nixCode.errLineOfCode
<< std::endl; << std::endl;
// error arrows for the column range. // error arrows for the column range.
if (einfo.errPos->column > 0) { if (nixCode.errPos.column > 0) {
int start = einfo.errPos->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(" ");
@ -49,19 +48,14 @@ void printCodeLines(const string &prefix, const ErrorInfo &einfo)
std::cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << std::endl; std::cout << format("%1% |%2%" ANSI_RED "%3%" ANSI_NORMAL) % prefix % spaces % arrows << std::endl;
} }
// next line of code. // next line of code.
if (einfo.nextLineOfCode.has_value()) { if (nixCode.nextLineOfCode.has_value()) {
std::cout << format("%1% %|2$5d|| %3%") std::cout << format("%1% %|2$5d|| %3%")
% prefix % prefix
% (einfo.errPos->lineNumber + 1) % (nixCode.errPos.lineNumber + 1)
% *einfo.nextLineOfCode % *nixCode.nextLineOfCode
<< std::endl; << std::endl;
} }
}
} }
void printErrorInfo(const ErrorInfo &einfo) void printErrorInfo(const ErrorInfo &einfo)
@ -107,14 +101,14 @@ void printErrorInfo(const ErrorInfo &einfo)
<< std::endl; << std::endl;
// filename. // filename.
if (einfo.errPos.has_value()) { if (einfo.nixCode.has_value()) {
if (einfo.errPos->nixFile != "") { if (einfo.nixCode->errPos.nixFile != "") {
string eline = einfo.errLineOfCode != "" string eline = einfo.nixCode->errLineOfCode != ""
? string(" ") + showErrPos(*einfo.errPos) ? string(" ") + showErrPos(einfo.nixCode->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.errPos->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 << format("%1%from command line argument") % prefix << std::endl; 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; std::cout << prefix << std::endl;
// lines of code. // lines of code.
if (einfo.errLineOfCode != "") { if (einfo.nixCode->errLineOfCode != "") {
printCodeLines(prefix, einfo); printCodeLines(prefix, *einfo.nixCode);
std::cout << prefix << std::endl; std::cout << prefix << std::endl;
} }

View file

@ -29,9 +29,24 @@ public:
{ {
lineNumber = pos.line; lineNumber = pos.line;
column = pos.column; column = pos.column;
nixFile = pos.file.str(); nixFile = pos.file;
return *this; 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 name;
string description; string description;
std::optional<hintformat> hint; std::optional<hintformat> hint;
std::optional<string> prevLineOfCode; std::optional<NixCode> nixCode;
string errLineOfCode;
std::optional<string> nextLineOfCode;
std::optional<ErrPos> errPos;
static std::optional<string> programName; static std::optional<string> programName;
private: 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.
}; };
// -------------------------------------------------------- // --------------------------------------------------------