Nix 2.26.3
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
12#include "source-path.hh"
13
14namespace nix {
15
19struct Pos
20{
21 uint32_t line = 0;
22 uint32_t column = 0;
23
24 struct Stdin {
25 ref<std::string> source;
26 bool operator==(const Stdin & rhs) const noexcept
27 { return *source == *rhs.source; }
28 std::strong_ordering operator<=>(const Stdin & rhs) const noexcept
29 { return *source <=> *rhs.source; }
30 };
31 struct String {
32 ref<std::string> source;
33 bool operator==(const String & rhs) const noexcept
34 { return *source == *rhs.source; }
35 std::strong_ordering operator<=>(const String & rhs) const noexcept
36 { return *source <=> *rhs.source; }
37 };
38
39 typedef std::variant<std::monostate, Stdin, String, SourcePath> Origin;
40
41 Origin origin = std::monostate();
42
43 Pos() { }
44 Pos(uint32_t line, uint32_t column, Origin origin)
45 : line(line), column(column), origin(origin) { }
46 Pos(Pos & other) = default;
47 Pos(const Pos & other) = default;
48 Pos(Pos && other) = default;
49 Pos(const Pos * other);
50
51 explicit operator bool() const { return line > 0; }
52
53 operator std::shared_ptr<Pos>() const;
54
58 std::optional<std::string> getSource() const;
59
60 void print(std::ostream & out, bool showOrigin) const;
61
62 std::optional<LinesOfCode> getCodeLines() const;
63
64 bool operator==(const Pos & rhs) const = default;
65 auto operator<=>(const Pos & rhs) const = default;
66
67 std::optional<std::string> getSnippetUpTo(const Pos & end) const;
68
72 std::optional<SourcePath> getSourcePath() const {
73 return *std::get_if<SourcePath>(&origin);
74 }
75
76 struct LinesIterator {
77 using difference_type = size_t;
78 using value_type = std::string_view;
79 using reference = const std::string_view &;
80 using pointer = const std::string_view *;
81 using iterator_category = std::input_iterator_tag;
82
83 LinesIterator(): pastEnd(true) {}
84 explicit LinesIterator(std::string_view input): input(input), pastEnd(input.empty()) {
85 if (!pastEnd)
86 bump(true);
87 }
88
89 LinesIterator & operator++() {
90 bump(false);
91 return *this;
92 }
93 LinesIterator operator++(int) {
94 auto result = *this;
95 ++*this;
96 return result;
97 }
98
99 reference operator*() const { return curLine; }
100 pointer operator->() const { return &curLine; }
101
102 bool operator!=(const LinesIterator & other) const {
103 return !(*this == other);
104 }
105 bool operator==(const LinesIterator & other) const {
106 return (pastEnd && other.pastEnd)
107 || (std::forward_as_tuple(input.size(), input.data())
108 == std::forward_as_tuple(other.input.size(), other.input.data()));
109 }
110
111 private:
112 std::string_view input, curLine;
113 bool pastEnd = false;
114
115 void bump(bool atFirst);
116 };
117};
118
119std::ostream & operator<<(std::ostream & str, const Pos & pos);
120
121}
Definition lexer.l:6769
Definition ref.hh:15
PosIdx end
Definition lexer.l:5814
uint32_t size_t
Definition lexer.l:6336
uint32_t line
Definition lexer.l:6526
std::ostream & str
Definition lexer.l:1728
SourcePath.
Definition position.hh:24
Definition position.hh:31
Definition position.hh:20
std::optional< std::string > getSource() const
Definition position.cc:45
std::optional< SourcePath > getSourcePath() const
Definition position.hh:72