print LOC for stdin, string args

This commit is contained in:
Ben Burdette 2020-05-20 22:18:26 -06:00
parent 85ce455b85
commit 6a420d672c
6 changed files with 97 additions and 36 deletions

View file

@ -121,7 +121,7 @@ Pos findDerivationFilename(EvalState & state, Value & v, std::string what)
Symbol file = state.symbols.create(filename); Symbol file = state.symbols.create(filename);
return { file, lineno, 0 }; return { foFile, file, lineno, 0 };
} }

View file

@ -249,7 +249,7 @@ private:
friend struct ExprAttrs; friend struct ExprAttrs;
friend struct ExprLet; friend struct ExprLet;
Expr * parse(const char * text, const Path & path, Expr * parse(const char * text, FileOrigin origin, const Path & path,
const Path & basePath, StaticEnv & staticEnv); const Path & basePath, StaticEnv & staticEnv);
public: public:

View file

@ -24,11 +24,12 @@ MakeError(RestrictedPathError, Error);
struct Pos struct Pos
{ {
FileOrigin origin;
Symbol file; Symbol file;
unsigned int line, column; unsigned int line, column;
Pos() : line(0), column(0) { }; Pos() : origin(foString), line(0), column(0) { };
Pos(const Symbol & file, unsigned int line, unsigned int column) Pos(FileOrigin origin, const Symbol & file, unsigned int line, unsigned int column)
: file(file), line(line), column(column) { }; : origin(origin), file(file), line(line), column(column) { };
operator bool() const operator bool() const
{ {
return line != 0; return line != 0;

View file

@ -30,7 +30,8 @@ namespace nix {
SymbolTable & symbols; SymbolTable & symbols;
Expr * result; Expr * result;
Path basePath; Path basePath;
Symbol path; Symbol file;
FileOrigin origin;
ErrorInfo error; ErrorInfo error;
Symbol sLetBody; Symbol sLetBody;
ParseData(EvalState & state) ParseData(EvalState & state)
@ -250,7 +251,7 @@ static Expr * stripIndentation(const Pos & pos, SymbolTable & symbols, vector<Ex
static inline Pos makeCurPos(const YYLTYPE & loc, ParseData * data) static inline Pos makeCurPos(const YYLTYPE & loc, ParseData * data)
{ {
return Pos(data->path, loc.first_line, loc.first_column); return Pos(data->origin, data->file, loc.first_line, loc.first_column);
} }
#define CUR_POS makeCurPos(*yylocp, data) #define CUR_POS makeCurPos(*yylocp, data)
@ -576,13 +577,24 @@ formal
namespace nix { namespace nix {
Expr * EvalState::parse(const char * text, Expr * EvalState::parse(const char * text, FileOrigin origin,
const Path & path, const Path & basePath, StaticEnv & staticEnv) const Path & path, const Path & basePath, StaticEnv & staticEnv)
{ {
yyscan_t scanner; yyscan_t scanner;
ParseData data(*this); ParseData data(*this);
data.origin = origin;
switch (origin) {
case foFile:
data.file = data.symbols.create(path);
break;
case foStdin:
case foString:
data.file = data.symbols.create(text);
break;
default:
throw Error("invalid FileOrigin in parse");
}
data.basePath = basePath; data.basePath = basePath;
data.path = data.symbols.create(path);
yylex_init(&scanner); yylex_init(&scanner);
yy_scan_string(text, scanner); yy_scan_string(text, scanner);
@ -632,13 +644,13 @@ Expr * EvalState::parseExprFromFile(const Path & path)
Expr * EvalState::parseExprFromFile(const Path & path, StaticEnv & staticEnv) Expr * EvalState::parseExprFromFile(const Path & path, StaticEnv & staticEnv)
{ {
return parse(readFile(path).c_str(), path, dirOf(path), staticEnv); return parse(readFile(path).c_str(), foFile, path, dirOf(path), staticEnv);
} }
Expr * EvalState::parseExprFromString(std::string_view s, const Path & basePath, StaticEnv & staticEnv) Expr * EvalState::parseExprFromString(std::string_view s, const Path & basePath, StaticEnv & staticEnv)
{ {
return parse(s.data(), "(string)", basePath, staticEnv); return parse(s.data(), foString, "", basePath, staticEnv);
} }
@ -651,7 +663,8 @@ Expr * EvalState::parseExprFromString(std::string_view s, const Path & basePath)
Expr * EvalState::parseStdin() Expr * EvalState::parseStdin()
{ {
//Activity act(*logger, lvlTalkative, format("parsing standard input")); //Activity act(*logger, lvlTalkative, format("parsing standard input"));
return parseExprFromString(drainFD(0), absPath(".")); // return parseExprFromString(foStdin, drainFD(0), absPath("."));
return parse(drainFD(0).data(), foStdin, "", absPath("."), staticBaseEnv);
} }

View file

@ -59,22 +59,51 @@ void getCodeLines(NixCode &nixCode)
return; return;
// check this magic value! // check this magic value!
if (nixCode.errPos.file == "(string)") if (nixCode.errPos.origin == foFile) {
return; try {
AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC);
try { if (!fd)
AutoCloseFD fd = open(nixCode.errPos.file.c_str(), O_RDONLY | O_CLOEXEC); throw SysError("opening file '%1%'", nixCode.errPos.file);
if (!fd)
throw SysError("opening file '%1%'", nixCode.errPos.file);
// count the newlines.
int count = 0;
string line;
int pl = nixCode.errPos.line - 1;
do
{
line = readLine(fd.get());
++count;
if (count < pl)
{
;
}
else if (count == pl) {
nixCode.prevLineOfCode = line;
} else if (count == pl + 1) {
nixCode.errLineOfCode = line;
} else if (count == pl + 2) {
nixCode.nextLineOfCode = line;
break;
}
} while (true);
}
catch (EndOfFile &eof) {
;
}
catch (std::exception &e) {
printError("error reading nix file: %s\n%s", nixCode.errPos.file, e.what());
}
} else {
std::istringstream iss(nixCode.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 = nixCode.errPos.line - 1;
do do
{ {
line = readLine(fd.get()); std::getline(iss, line);
// std::cout << "getline result: " << std::getline(iss, line) << std::endl;
++count; ++count;
if (count < pl) if (count < pl)
{ {
@ -88,14 +117,11 @@ void getCodeLines(NixCode &nixCode)
nixCode.nextLineOfCode = line; nixCode.nextLineOfCode = line;
break; break;
} }
if (!iss.good())
break;
} while (true); } while (true);
} }
catch (EndOfFile &eof) {
;
}
catch (std::exception &e) {
printError("error reading nix file: %s\n%s", nixCode.errPos.file, e.what());
}
} }
@ -225,15 +251,27 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
// filename, line, column. // filename, line, column.
if (einfo.nixCode.has_value()) { if (einfo.nixCode.has_value()) {
if (einfo.nixCode->errPos.file != "") { switch (einfo.nixCode->errPos.origin) {
out << fmt("%1%in file: " ANSI_BLUE "%2% %3%" ANSI_NORMAL, case foFile: {
prefix, out << fmt("%1%in file: " ANSI_BLUE "%2% %3%" ANSI_NORMAL,
einfo.nixCode->errPos.file, prefix,
showErrPos(einfo.nixCode->errPos)) << std::endl; einfo.nixCode->errPos.file,
out << prefix << std::endl; showErrPos(einfo.nixCode->errPos)) << std::endl;
} else { out << prefix << std::endl;
out << fmt("%1%from command line argument", prefix) << std::endl; break;
out << prefix << std::endl; }
case foString: {
out << fmt("%1%from command line argument", prefix) << std::endl;
out << prefix << std::endl;
break;
}
case foStdin: {
out << fmt("%1%from stdin", prefix) << std::endl;
out << prefix << std::endl;
break;
}
default:
throw Error("invalid FileOrigin in errPos");
} }
} }

View file

@ -32,10 +32,18 @@ typedef enum {
lvlVomit lvlVomit
} Verbosity; } Verbosity;
typedef enum {
foFile,
foStdin,
foString
} FileOrigin;
struct ErrPos { struct ErrPos {
int line = 0; int line = 0;
int column = 0; int column = 0;
string file; string file;
FileOrigin origin;
operator bool() const operator bool() const
{ {
@ -45,6 +53,7 @@ struct ErrPos {
template <class P> template <class P>
ErrPos& operator=(const P &pos) ErrPos& operator=(const P &pos)
{ {
origin = pos.origin;
line = pos.line; line = pos.line;
column = pos.column; column = pos.column;
file = pos.file; file = pos.file;