mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Introduce AbstractPos
This makes the position object used in exceptions abstract, with a method getSource() to get the source code of the file in which the error originated. This is needed for lazy trees because source files don't necessarily exist in the filesystem, and we don't want to make libutil depend on the InputAccessor type in libfetcher.
This commit is contained in:
parent
edb54c62e6
commit
b3fdab28a2
16 changed files with 231 additions and 279 deletions
|
@ -8,6 +8,65 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
struct SourcePathAdapter : AbstractPos
|
||||
{
|
||||
Path path;
|
||||
|
||||
SourcePathAdapter(Path path)
|
||||
: path(std::move(path))
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<std::string> getSource() const override
|
||||
{
|
||||
try {
|
||||
return readFile(path);
|
||||
} catch (Error &) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
void print(std::ostream & out) const override
|
||||
{
|
||||
out << path;
|
||||
}
|
||||
};
|
||||
|
||||
struct StringPosAdapter : AbstractPos
|
||||
{
|
||||
void print(std::ostream & out) const override
|
||||
{
|
||||
out << "«string»";
|
||||
}
|
||||
};
|
||||
|
||||
struct StdinPosAdapter : AbstractPos
|
||||
{
|
||||
void print(std::ostream & out) const override
|
||||
{
|
||||
out << "«stdin»";
|
||||
}
|
||||
};
|
||||
|
||||
Pos::operator std::shared_ptr<AbstractPos>() const
|
||||
{
|
||||
std::shared_ptr<AbstractPos> pos;
|
||||
|
||||
if (auto path = std::get_if<Path>(&origin))
|
||||
pos = std::make_shared<SourcePathAdapter>(*path);
|
||||
else if (std::get_if<stdin_tag>(&origin))
|
||||
pos = std::make_shared<StdinPosAdapter>();
|
||||
else if (std::get_if<string_tag>(&origin))
|
||||
pos = std::make_shared<StringPosAdapter>();
|
||||
|
||||
if (pos) {
|
||||
pos->line = line;
|
||||
pos->column = column;
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/* Displaying abstract syntax trees. */
|
||||
|
||||
static void showString(std::ostream & str, std::string_view s)
|
||||
|
@ -248,24 +307,10 @@ void ExprPos::show(const SymbolTable & symbols, std::ostream & str) const
|
|||
|
||||
std::ostream & operator << (std::ostream & str, const Pos & pos)
|
||||
{
|
||||
if (!pos)
|
||||
if (auto pos2 = (std::shared_ptr<AbstractPos>) pos) {
|
||||
str << *pos2;
|
||||
} else
|
||||
str << "undefined position";
|
||||
else
|
||||
{
|
||||
auto f = format(ANSI_BOLD "%1%" ANSI_NORMAL ":%2%:%3%");
|
||||
switch (pos.origin) {
|
||||
case foFile:
|
||||
f % (const std::string &) pos.file;
|
||||
break;
|
||||
case foStdin:
|
||||
case foString:
|
||||
f % "(string)";
|
||||
break;
|
||||
default:
|
||||
throw Error("unhandled Pos origin!");
|
||||
}
|
||||
str << (f % pos.line % pos.column).str();
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue