1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "lix/libutil/position.hh"

namespace nix {

std::optional<std::string> Pos::Origin::getSource() const
{
    using OptStr = std::optional<std::string>;

    // clang-format off
    return std::visit(overloaded {
        [](std::monostate const &) -> OptStr {
            return std::nullopt;
        },
        [](Pos::Stdin const & s) -> OptStr {
            return *s.source;
        },
        [](Pos::String const & s) -> OptStr {
            return *s.source;
        },
        [](CheckedSourcePath const & path) -> OptStr {
            try {
                return path.readFile();
            } catch (Error const &) {
                return std::nullopt;
            }
        },
        [](Hidden const &) -> OptStr {
            return std::nullopt;
        },
    }, *this);
    // clang-format on
}

Pos::Pos(const Pos * other)
{
    if (!other) {
        return;
    }
    line = other->line;
    column = other->column;
    origin = std::move(other->origin);
}

Pos::operator std::shared_ptr<Pos>() const
{
    return std::make_shared<Pos>(&*this);
}

std::optional<LinesOfCode> Pos::getCodeLines() const
{
    if (line == 0)
        return std::nullopt;

    if (auto source = getSource()) {
        LinesIterator lines(*source), end;
        LinesOfCode loc;

        if (line > 1)
            std::advance(lines, line - 2);
        if (lines != end && line > 1)
            loc.prevLineOfCode = *lines++;
        if (lines != end)
            loc.errLineOfCode = *lines++;
        if (lines != end)
            loc.nextLineOfCode = *lines++;

        return loc;
    }

    return std::nullopt;
}


std::optional<std::string> Pos::getSource() const
{
    return this->origin.getSource();
}

void Pos::print(std::ostream & out, bool showOrigin) const
{
    if (showOrigin) {
        std::visit(overloaded {
            [&](const std::monostate &) { out << "«none»"; },
            [&](const Pos::Stdin &) { out << "«stdin»"; },
            [&](const Pos::String & s) { out << "«string»"; },
            [&](const SourcePath & path) { out << path; },
            [&](Hidden) { out << "«internal»"; },
        }, origin);
        out << ":";
    }
    out << line;
    if (column > 0)
        out << ":" << column;
}

std::ostream & operator<<(std::ostream & str, const Pos & pos)
{
    pos.print(str, true);
    return str;
}

void Pos::LinesIterator::bump(bool atFirst)
{
    if (!atFirst) {
        pastEnd = input.empty();
        if (!input.empty() && input[0] == '\r')
            input.remove_prefix(1);
        if (!input.empty() && input[0] == '\n')
            input.remove_prefix(1);
    }

    // nix line endings are not only \n as eg std::getline assumes, but also
    // \r\n **and \r alone**. not treating them all the same causes error
    // reports to not match with line numbers as the parser expects them.
    auto eol = input.find_first_of("\r\n");

    if (eol > input.size())
        eol = input.size();

    curLine = input.substr(0, eol);
    input.remove_prefix(eol);
}

}