Nix 2.93.3
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
position.hh
Go to the documentation of this file.
1#pragma once
7
8#include <cstdint>
9#include <string>
10
12
13namespace nix {
14
18struct Pos
19{
20 uint32_t line = 0;
21 uint32_t column = 0;
22
23 struct Stdin {
24 ref<std::string> source;
25 bool operator==(const Stdin & rhs) const
26 { return *source == *rhs.source; }
27 bool operator!=(const Stdin & rhs) const
28 { return *source != *rhs.source; }
29 bool operator<(const Stdin & rhs) const
30 { return *source < *rhs.source; }
31 };
32 struct String {
33 ref<std::string> source;
34 bool operator==(const String & rhs) const
35 { return *source == *rhs.source; }
36 bool operator!=(const String & rhs) const
37 { return *source != *rhs.source; }
38 bool operator<(const String & rhs) const
39 { return *source < *rhs.source; }
40 };
41 struct Hidden {
42 auto operator<=>(const Hidden &) const = default;
43 };
44
45 typedef std::variant<std::monostate, Stdin, String, CheckedSourcePath, Hidden> Origin;
46
47 Origin origin = std::monostate();
48
49 Pos() { }
50 Pos(uint32_t line, uint32_t column, Origin origin)
51 : line(line), column(column), origin(origin) { }
52 Pos(Pos & other) = default;
53 Pos(const Pos & other) = default;
54 Pos(Pos && other) = default;
55 Pos(const Pos * other);
56
57 explicit operator bool() const { return line > 0; }
58
59 operator std::shared_ptr<Pos>() const;
60
64 std::optional<std::string> getSource() const;
65
66 void print(std::ostream & out, bool showOrigin) const;
67
68 std::optional<LinesOfCode> getCodeLines() const;
69
70 bool operator==(const Pos & rhs) const = default;
71 bool operator!=(const Pos & rhs) const = default;
72 bool operator<(const Pos & rhs) const;
73
74 struct LinesIterator {
75 using difference_type = size_t;
76 using value_type = std::string_view;
77 using reference = const std::string_view &;
78 using pointer = const std::string_view *;
79 using iterator_category = std::input_iterator_tag;
80
81 LinesIterator(): pastEnd(true) {}
82 explicit LinesIterator(std::string_view input): input(input), pastEnd(input.empty()) {
83 if (!pastEnd)
84 bump(true);
85 }
86
87 LinesIterator & operator++() {
88 bump(false);
89 return *this;
90 }
91 LinesIterator operator++(int) {
92 auto result = *this;
93 ++*this;
94 return result;
95 }
96
97 reference operator*() const { return curLine; }
98 pointer operator->() const { return &curLine; }
99
100 bool operator!=(const LinesIterator & other) const {
101 return !(*this == other);
102 }
103 bool operator==(const LinesIterator & other) const {
104 return (pastEnd && other.pastEnd)
105 || (std::forward_as_tuple(input.size(), input.data())
106 == std::forward_as_tuple(other.input.size(), other.input.data()));
107 }
108
109 private:
110 std::string_view input, curLine;
111 bool pastEnd = false;
112
113 void bump(bool atFirst);
114 };
115};
116
117std::ostream & operator<<(std::ostream & str, const Pos & pos);
118
119}
Definition ref.hh:19
SourcePath.
Definition position.hh:41
Definition position.hh:23
Definition position.hh:32
Definition position.hh:19
std::optional< std::string > getSource() const
Definition position.cc:51