38 return lhs._index < rhs._index;
86 return lhs._row == rhs._row
87 && lhs._column == rhs._column
88 && lhs._index == rhs._index;
100 return !(lhs == rhs);
108 [[nodiscard]]
auto column()
const {
return _column; }
115 [[nodiscard]]
auto index()
const {
return _index; }
122 [[nodiscard]]
auto row()
const {
return _row; }
125 size_t _row, _column;
172 [[nodiscard]]
virtual std::string
line(
const Location& location)
const = 0;
181 [[nodiscard]]
virtual std::string
line(
size_t line_number)
const = 0;
195 [[nodiscard]]
virtual std::string
contents()
const = 0;
202 [[nodiscard]]
virtual std::string
path()
const = 0;
209 [[nodiscard]]
virtual size_t size()
const = 0;
267 [[nodiscard]] std::string
line(
const Location& location)
const override;
276 [[nodiscard]] std::string
line(
size_t line_number)
const override;
290 [[nodiscard]] std::string
contents()
const override;
297 [[nodiscard]] std::string
path()
const override;
304 [[nodiscard]]
size_t size()
const override;
315 return lhs._path == rhs._path;
327 return !(lhs == rhs);
331 std::optional<std::filesystem::path> _working_path;
332 std::filesystem::path _path;
A Source implementation that reads from a file on disk.
Definition: source.hpp:215
std::string contents() const override
Returns the entire file contents.
FileSource(std::filesystem::path path)
Creates a file source from a filesystem path.
size_t size() const override
Returns the total size of the file in characters.
std::string path() const override
Returns the file system path to the file (possibly working path relative)
std::string line(const Location &location) const override
Returns the contents of the line containing the given location.
Location from_coords(size_t row, size_t column) const override
Maps (row, column) to a Location within the file.
Location from_index(size_t index) const override
Maps an absolute index to a Location within the file.
std::string line(size_t line_number) const override
Returns the contents of the specified line number.
size_t line_count() const override
Returns the number of lines in the file.
void set_working_path(const std::filesystem::path &path)
Sets a working directory used to relativize the displayed path.
friend bool operator==(const FileSource &lhs, const FileSource &rhs)
Equality compares path.
Definition: source.hpp:314
friend bool operator!=(const FileSource &lhs, const FileSource &rhs)
Inequality based on operator==
Definition: source.hpp:326
std::string substr(const Location &start, const Location &end) const override
Extracts a substring delimited by two locations.
A position inside a Source, expressed as (row, column, index)
Definition: source.hpp:13
auto column() const
Returns the 0-based column number.
Definition: source.hpp:108
auto index() const
Returns the 0-based absolute character index.
Definition: source.hpp:115
Location(size_t row, size_t column, size_t index)
Constructs a location with the given coordinates and absolute index.
friend bool operator>(const Location &lhs, const Location &rhs)
Greater-than comparison derived from <
Definition: source.hpp:61
friend bool operator<(const Location &lhs, const Location &rhs)
Orders locations by their absolute index.
Definition: source.hpp:37
auto row() const
Returns the 0-based row (line) number.
Definition: source.hpp:122
friend bool operator!=(const Location &lhs, const Location &rhs)
Inequality based on operator==
Definition: source.hpp:99
friend bool operator<=(const Location &lhs, const Location &rhs)
Less-than-or-equal comparison derived from >=
Definition: source.hpp:49
Location()
Constructs a default-initialized location (all -1).
friend bool operator>=(const Location &lhs, const Location &rhs)
Greater-than-or-equal comparison derived from <
Definition: source.hpp:73
friend bool operator==(const Location &lhs, const Location &rhs)
Equality compares row, column and index.
Definition: source.hpp:85
Abstract interface for reading and mapping source text.
Definition: source.hpp:132
virtual std::string substr(const Location &start, const Location &end) const =0
Returns the substring between two locations.
virtual Location from_coords(size_t row, size_t column) const =0
Returns a location corresponding to the given row and column.
virtual size_t line_count() const =0
Returns the total number of lines in the source.
virtual ~Source()=default
virtual Location from_index(size_t index) const =0
Returns a location for the given absolute character index.
virtual size_t size() const =0
Returns the total size (in characters) of the source.
virtual std::string contents() const =0
Returns the entire contents of the source.
virtual std::string line(size_t line_number) const =0
Returns the contents of the specified line number.
virtual std::string line(const Location &location) const =0
Returns the full line at the given location.
virtual std::string path() const =0
Returns a displayable path or identifier of the source.
std::ostream & operator<<(std::ostream &os, const pretty_diagnostics::Location &location)
Streams a readable description of a Location