nixCode -> LinesOfCode

This commit is contained in:
Ben Burdette 2020-06-24 08:33:53 -06:00
parent 1d43a6e123
commit 00fe653ea5
3 changed files with 67 additions and 55 deletions

View file

@ -56,22 +56,25 @@ string showErrPos(const ErrPos &errPos)
}
}
void getCodeLines(NixCode &nixCode)
std::optional<LinesOfCode> getCodeLines(const ErrPos &errPos)
{
if (nixCode.errPos.line <= 0)
return;
if (errPos.line <= 0)
return std::nullopt;
if (nixCode.errPos.origin == foFile) {
if (errPos.origin == foFile) {
LinesOfCode loc;
try {
AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd)
logError(SysError("opening file '%1%'", nixCode.errPos.file).info());
AutoCloseFD fd = open(errPos.file.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd) {
logError(SysError("opening file '%1%'", errPos.file).info());
return std::nullopt;
}
else
{
// count the newlines.
int count = 0;
string line;
int pl = nixCode.errPos.line - 1;
int pl = errPos.line - 1;
do
{
line = readLine(fd.get());
@ -81,28 +84,33 @@ void getCodeLines(NixCode &nixCode)
;
}
else if (count == pl) {
nixCode.prevLineOfCode = line;
loc.prevLineOfCode = line;
} else if (count == pl + 1) {
nixCode.errLineOfCode = line;
loc.errLineOfCode = line;
} else if (count == pl + 2) {
nixCode.nextLineOfCode = line;
loc.nextLineOfCode = line;
break;
}
} while (true);
return loc;
}
}
catch (EndOfFile &eof) {
;
// TODO: return maybe partial loc?
return std::nullopt;
}
catch (std::exception &e) {
printError("error reading nix file: %s\n%s", nixCode.errPos.file, e.what());
printError("error reading nix file: %s\n%s", errPos.file, e.what());
return std::nullopt;
}
} else {
std::istringstream iss(nixCode.errPos.file);
std::istringstream iss(errPos.file);
// count the newlines.
int count = 0;
string line;
int pl = nixCode.errPos.line - 1;
int pl = errPos.line - 1;
LinesOfCode loc;
do
{
@ -113,42 +121,47 @@ void getCodeLines(NixCode &nixCode)
;
}
else if (count == pl) {
nixCode.prevLineOfCode = line;
loc.prevLineOfCode = line;
} else if (count == pl + 1) {
nixCode.errLineOfCode = line;
loc.errLineOfCode = line;
} else if (count == pl + 2) {
nixCode.nextLineOfCode = line;
loc.nextLineOfCode = line;
break;
}
if (!iss.good())
break;
} while (true);
return loc;
}
}
// if nixCode contains lines of code, print them to the ostream, indicating the error column.
void printCodeLines(std::ostream &out, const string &prefix, const NixCode &nixCode)
void printCodeLines(std::ostream &out,
const string &prefix,
const ErrPos &errPos,
const LinesOfCode &loc)
{
// previous line of code.
if (nixCode.prevLineOfCode.has_value()) {
if (loc.prevLineOfCode.has_value()) {
out << std::endl
<< fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errPos.line - 1),
*nixCode.prevLineOfCode);
(errPos.line - 1),
*loc.prevLineOfCode);
}
if (nixCode.errLineOfCode.has_value()) {
if (loc.errLineOfCode.has_value()) {
// line of code containing the error.
out << std::endl
<< fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errPos.line),
*nixCode.errLineOfCode);
(errPos.line),
*loc.errLineOfCode);
// error arrows for the column range.
if (nixCode.errPos.column > 0) {
int start = nixCode.errPos.column;
if (errPos.column > 0) {
int start = errPos.column;
std::string spaces;
for (int i = 0; i < start; ++i) {
spaces.append(" ");
@ -165,12 +178,12 @@ void printCodeLines(std::ostream &out, const string &prefix, const NixCode &nixC
}
// next line of code.
if (nixCode.nextLineOfCode.has_value()) {
if (loc.nextLineOfCode.has_value()) {
out << std::endl
<< fmt("%1% %|2$5d|| %3%",
prefix,
(nixCode.errPos.line + 1),
*nixCode.nextLineOfCode);
(errPos.line + 1),
*loc.nextLineOfCode);
}
}
@ -286,14 +299,13 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
}
if (einfo.errPos.has_value()) {
NixCode nixcode { .errPos = *einfo.errPos };
getCodeLines(nixcode);
auto loc = getCodeLines(*einfo.errPos);
// lines of code.
if (nixcode.errLineOfCode.has_value()) {
if (loc.has_value()) {
if (nl)
out << std::endl << prefix;
printCodeLines(out, prefix, nixcode);
printCodeLines(out, prefix, *einfo.errPos, *loc);
nl = true;
}
}
@ -318,9 +330,9 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
out << ANSI_BLUE << "at: " << ANSI_YELLOW << showErrPos(pos) <<
ANSI_BLUE << " in file: " << ANSI_NORMAL << pos.file << std::endl;
nl = true;
NixCode nc { .errPos = pos };
getCodeLines(nc);
printCodeLines(out, prefix, nc);
auto loc = getCodeLines(pos);
if (loc.has_value())
printCodeLines(out, prefix, pos, *loc);
} catch(const std::bad_optional_access& e) {
out << iter->hint.str() << std::endl;
}

View file

@ -56,6 +56,13 @@ typedef enum {
foString
} FileOrigin;
// the lines of code surrounding an error.
struct LinesOfCode {
std::optional<string> prevLineOfCode;
std::optional<string> errLineOfCode;
std::optional<string> nextLineOfCode;
};
// ErrPos indicates the location of an error in a nix file.
struct ErrPos {
int line = 0;
@ -86,13 +93,6 @@ struct ErrPos {
}
};
struct NixCode {
ErrPos errPos;
std::optional<string> prevLineOfCode;
std::optional<string> errLineOfCode;
std::optional<string> nextLineOfCode;
};
struct Trace {
std::optional<ErrPos> pos;
hintformat hint;

View file

@ -17,10 +17,10 @@ nix-env -q --foo 2>&1 | grep "unknown flag"
# Eval Errors.
eval_arg_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true)
echo $eval_arg_res | grep "command line argument (1:15)"
echo $eval_arg_res | grep "at: (1:15) from command line argument"
echo $eval_arg_res | grep "infinite recursion encountered"
eval_stdin_res=$(echo 'let a = {} // a; in a.foo' | nix-instantiate --eval -E - 2>&1 || true)
echo $eval_stdin_res | grep "stdin (1:15)"
echo $eval_stdin_res | grep "at: (1:15) from stdin"
echo $eval_stdin_res | grep "infinite recursion encountered"