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

View file

@ -56,6 +56,13 @@ typedef enum {
foString foString
} FileOrigin; } 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. // ErrPos indicates the location of an error in a nix file.
struct ErrPos { struct ErrPos {
int line = 0; 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 { struct Trace {
std::optional<ErrPos> pos; std::optional<ErrPos> pos;
hintformat hint; hintformat hint;

View file

@ -17,10 +17,10 @@ nix-env -q --foo 2>&1 | grep "unknown flag"
# Eval Errors. # Eval Errors.
eval_arg_res=$(nix-instantiate --eval -E 'let a = {} // a; in a.foo' 2>&1 || true) 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" 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) 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" echo $eval_stdin_res | grep "infinite recursion encountered"