aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/nixexpr.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-12-13 00:48:04 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-12-13 00:50:43 +0100
commitb3fdab28a216683365f7f04bfa9bbc5cd122d753 (patch)
tree54bd67dc94f2596be2d77a13b0b84a0afde8b067 /src/libexpr/nixexpr.cc
parentedb54c62e6c66f3d26642c024d92bd20b30abf85 (diff)
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.
Diffstat (limited to 'src/libexpr/nixexpr.cc')
-rw-r--r--src/libexpr/nixexpr.cc79
1 files changed, 62 insertions, 17 deletions
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index 2be560d76..bdfb6457a 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -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;
}