Clingo
Loading...
Searching...
No Matches
location.hh
1#pragma once
2
3#include <clingo/core/symbol.hh>
4
5#include <cstddef>
6#include <optional>
7#include <variant>
8
9namespace CppClingo {
10
13
15class Position {
16 public:
18 Position(String file, size_t line, size_t column) : file_{file}, line_{line}, column_{column} {}
20 [[nodiscard]] auto file() const -> String const & { return *file_; }
22 [[nodiscard]] auto line() const -> size_t { return line_; }
24 [[nodiscard]] auto column() const -> size_t { return column_; }
25
27 friend auto operator==(Position const &a, Position const &b) -> bool = default;
29 friend auto operator<=>(Position const &a, Position const &b) = default;
30
32 template <class T> friend auto operator<<(T &out, Position const &pos) -> T & {
33 out << pos.file() << ":" << pos.line() << ":" << pos.column();
34 return out;
35 }
36
37 private:
38 SharedString file_;
39 size_t line_;
40 size_t column_;
41};
42
44class Location {
45 public:
47 Location(Position begin, Position end) : begin_{std::move(begin)}, end_{std::move(end)} {}
48
50 [[nodiscard]] auto begin() const -> Position const & { return begin_; }
52 [[nodiscard]] auto end() const -> Position const & { return end_; }
53
55 friend auto operator+(Location const &a, Location const &b) -> Location { return {a.begin_, b.end_}; }
57 friend auto operator+(Location const &a, Position b) -> Location { return {a.begin_, std::move(b)}; }
59 friend auto operator+(Location const &a, std::optional<Position> b) -> Location {
60 if (b.has_value()) {
61 return {a.begin_, b.value()};
62 }
63 return a;
64 }
66 friend auto operator+(Position a, Location const &b) -> Location { return {std::move(a), b.end_}; }
68 friend auto operator+(std::optional<Position> a, Location const &b) -> Location {
69 if (a) {
70 return {*a, b.end_};
71 }
72 return b;
73 }
75 friend auto operator+=(Location &a, Location const &b) -> Location & {
76 a.end_ = b.end_;
77 return a;
78 }
80 friend auto operator+=(Location &a, Position b) -> Location & {
81 a.end_ = std::move(b);
82 return a;
83 }
85 friend auto operator+=(Location &a, std::optional<Position> b) -> Location & {
86 if (b) {
87 a.end_ = *std::move(b);
88 }
89 return a;
90 }
91
93 friend auto operator==(Location const &a, Location const &b) -> bool = default;
95 friend auto operator<=>(Location const &a, Location const &b) = default;
96
98 template <class T> friend auto operator<<(T &out, Location const &loc) -> T & {
99 out << loc.begin() << "-";
100 if (loc.end().file() != loc.begin().file()) {
101 out << loc.end();
102 } else if (loc.end().line() != loc.begin().line()) {
103 out << loc.end().line() << ":" << loc.end().column();
104 } else {
105 out << loc.end().column();
106 }
107 return out;
108 }
109
110 private:
111 Position begin_;
112 Position end_;
113};
114
116inline auto operator+(Position a, Position b) -> Location {
117 return {std::move(a), std::move(b)};
118}
119
121template <class T>
122 requires requires(T const &x) { x.loc(); }
123auto location(T const &x) -> Location const & {
124 return x.loc();
125}
126
128template <class... T>
129 requires requires(T const &...x) { (location(x), ...); }
130auto location(std::variant<T...> const &x) -> Location const & {
131 return std::visit([](auto const &y) -> Location const & { return location(y); }, x);
132}
133
135
136} // namespace CppClingo
The Location of an expression in an input source.
Definition location.hh:44
auto end() const -> Position const &
The position where the expression ends.
Definition location.hh:52
Location(Position begin, Position end)
Construct a location.
Definition location.hh:47
friend auto operator+(Location const &a, std::optional< Position > b) -> Location
Create a new location from the given one optionally adjusting its end position.
Definition location.hh:59
friend auto operator+(Location const &a, Location const &b) -> Location
Create a new location from the beginning and end of the given two locations.
Definition location.hh:55
friend auto operator<=>(Location const &a, Location const &b)=default
Compare two positions.
friend auto operator<<(T &out, Location const &loc) -> T &
Output the location to the given stream.
Definition location.hh:98
friend auto operator+(Location const &a, Position b) -> Location
Create a new location from the beginning of the location and the position.
Definition location.hh:57
friend auto operator+(std::optional< Position > a, Location const &b) -> Location
Create a new location from the given one optionally adjusting its start position.
Definition location.hh:68
friend auto operator==(Location const &a, Location const &b) -> bool=default
Compare two positions.
friend auto operator+=(Location &a, Location const &b) -> Location &
See the corresponding + operator.
Definition location.hh:75
friend auto operator+=(Location &a, Position b) -> Location &
See the corresponding + operator.
Definition location.hh:80
auto begin() const -> Position const &
The position where the expression starts.
Definition location.hh:50
friend auto operator+(Position a, Location const &b) -> Location
Create a new location from the position of the end of the location.
Definition location.hh:66
friend auto operator+=(Location &a, std::optional< Position > b) -> Location &
See the corresponding + operator.
Definition location.hh:85
A point in an input source.
Definition location.hh:15
auto column() const -> size_t
The column number.
Definition location.hh:24
friend auto operator<=>(Position const &a, Position const &b)=default
Compare two positions.
auto file() const -> String const &
The name of a file/stream/string.
Definition location.hh:20
friend auto operator==(Position const &a, Position const &b) -> bool=default
Compare two positions.
auto line() const -> size_t
The line number.
Definition location.hh:22
friend auto operator<<(T &out, Position const &pos) -> T &
Output the position to the given stream.
Definition location.hh:32
Position(String file, size_t line, size_t column)
Construct a position.
Definition location.hh:18
Class managing the lifetime of a String.
Definition symbol.hh:93
Reference to a string stored in a symbol store.
Definition symbol.hh:18
auto location(T const &x) -> Location const &
Get the location of an expression.
Definition location.hh:123
auto operator+(Sign a, Sign b) -> Sign
Combine two signs.