Nix 2.36.0
Nix, the purely functional 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#include <variant>
11
13
14namespace nix {
15
19struct Pos
20{
21 uint32_t line = 0;
22 uint32_t column = 0;
23
24 struct Stdin
25 {
26 ref<std::string> source;
27
28 bool operator==(const Stdin & rhs) const noexcept
29 {
30 return *source == *rhs.source;
31 }
32
33 std::strong_ordering operator<=>(const Stdin & rhs) const noexcept
34 {
35 return *source <=> *rhs.source;
36 }
37 };
38
39 struct String
40 {
41 ref<std::string> source;
42
43 bool operator==(const String & rhs) const noexcept
44 {
45 return *source == *rhs.source;
46 }
47
48 std::strong_ordering operator<=>(const String & rhs) const noexcept
49 {
50 return *source <=> *rhs.source;
51 }
52 };
53
54 typedef std::variant<std::monostate, Stdin, String, SourcePath> Origin;
55
56 Origin origin = std::monostate();
57
58 Pos() {}
59
60 Pos(uint32_t line, uint32_t column, Origin origin)
61 : line(line)
62 , column(column)
63 , origin(origin)
64 {
65 }
66
67 explicit operator bool() const
68 {
69 return line > 0;
70 }
71
72 operator std::shared_ptr<const Pos>() const;
73
77 std::optional<std::string> getSource() const;
78
79 void print(std::ostream & out, bool showOrigin) const;
80
81 std::optional<LinesOfCode> getCodeLines() const;
82
83 bool operator==(const Pos & rhs) const = default;
84 auto operator<=>(const Pos & rhs) const = default;
85
86 std::optional<std::string> getSnippetUpTo(const Pos & end) const;
87
91 std::optional<SourcePath> getSourcePath() const;
92
93 struct LinesIterator
94 {
95 using difference_type = size_t;
96 using value_type = std::string_view;
97 using reference = const std::string_view &;
98 using pointer = const std::string_view *;
99 using iterator_category = std::input_iterator_tag;
100
101 LinesIterator()
102 : pastEnd(true)
103 {
104 }
105
106 explicit LinesIterator(std::string_view input)
107 : input(input)
108 , pastEnd(input.empty())
109 {
110 if (!pastEnd)
111 bump(true);
112 }
113
114 LinesIterator & operator++()
115 {
116 bump(false);
117 return *this;
118 }
119
120 LinesIterator operator++(int)
121 {
122 auto result = *this;
123 ++*this;
124 return result;
125 }
126
127 reference operator*() const
128 {
129 return curLine;
130 }
131
132 pointer operator->() const
133 {
134 return &curLine;
135 }
136
137 bool operator!=(const LinesIterator & other) const
138 {
139 return !(*this == other);
140 }
141
142 bool operator==(const LinesIterator & other) const
143 {
144 return (pastEnd && other.pastEnd)
145 || (std::forward_as_tuple(input.size(), input.data())
146 == std::forward_as_tuple(other.input.size(), other.input.data()));
147 }
148
149 private:
150 std::string_view input, curLine;
151 bool pastEnd = false;
152
153 void bump(bool atFirst);
154 };
155};
156
157std::ostream & operator<<(std::ostream & str, const Pos & pos);
158
159} // namespace nix
Definition ref.hh:58
SourcePath.
Definition position.hh:25
Definition position.hh:40
Definition position.hh:20
std::optional< std::string > getSource() const
Definition position.cc:34
std::optional< SourcePath > getSourcePath() const
Definition position.cc:57