80 return lhs._row == rhs._row && lhs._column == rhs._column && lhs._index == rhs._index;
98 [[nodiscard]]
auto column()
const {
return _column; }
105 [[nodiscard]]
auto index()
const {
return _index; }
112 [[nodiscard]]
auto row()
const {
return _row; }
115 size_t _row, _column;
162 [[nodiscard]]
virtual std::string
line(
const Location& location)
const = 0;
171 [[nodiscard]]
virtual std::string
line(
size_t line_number)
const = 0;
185 [[nodiscard]]
virtual const std::string&
contents()
const = 0;
192 [[nodiscard]]
virtual std::string
path()
const = 0;
199 [[nodiscard]]
virtual size_t size()
const = 0;
251 [[nodiscard]] std::string
line(
const Location& location)
const override;
260 [[nodiscard]] std::string
line(
size_t line_number)
const override;
274 [[nodiscard]]
const std::string&
contents()
const override;
281 [[nodiscard]] std::string
path()
const override;
288 [[nodiscard]]
size_t size()
const override;
291 std::vector<size_t> _line_starts;
292 std::string _display_path;
293 std::string _contents;
307 explicit FileSource(
const std::filesystem::path&
path,
const std::filesystem::path& working_path = std::filesystem::current_path());
330 [[nodiscard]]
static std::string _read_contents(
const std::filesystem::path&
path);
A Source implementation that reads from a file on disk.
Definition: source.hpp:299
friend bool operator==(const FileSource &lhs, const FileSource &rhs)
Equality compares path.
Definition: source.hpp:317
friend bool operator!=(const FileSource &lhs, const FileSource &rhs)
Inequality based on operator==
Definition: source.hpp:327
FileSource(const std::filesystem::path &path, const std::filesystem::path &working_path=std::filesystem::current_path())
Creates a file source from a filesystem path.
A position inside a Source, expressed as (row, column, index)
Definition: source.hpp:15
auto column() const
Returns the 0-based column number.
Definition: source.hpp:98
auto index() const
Returns the 0-based absolute character index.
Definition: source.hpp:105
Location()=delete
Constructs a default-initialized location (all -1).
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:59
friend bool operator<(const Location &lhs, const Location &rhs)
Orders locations by their absolute index.
Definition: source.hpp:39
auto row() const
Returns the 0-based row (line) number.
Definition: source.hpp:112
friend bool operator!=(const Location &lhs, const Location &rhs)
Inequality based on operator==
Definition: source.hpp:91
friend bool operator<=(const Location &lhs, const Location &rhs)
Less-than-or-equal comparison derived from >=
Definition: source.hpp:49
friend bool operator>=(const Location &lhs, const Location &rhs)
Greater-than-or-equal comparison derived from <
Definition: source.hpp:69
friend bool operator==(const Location &lhs, const Location &rhs)
Equality compares row, column and index.
Definition: source.hpp:79
Abstract interface for reading and mapping source text.
Definition: source.hpp:122
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 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 const std::string & contents() const =0
Returns the entire contents of the source.
virtual std::string path() const =0
Returns a displayable path or identifier of the source.
A Source implementation that reads from an in-memory string.
Definition: source.hpp:205
Location from_coords(size_t row, size_t column) const override
Maps (row, column) to a Location within the string.
std::string path() const override
Returns a displayable path or identifier of the source.
std::string line(const Location &location) const override
Returns the contents of the line containing the given location.
Location from_index(size_t index) const override
Maps an absolute index to a Location within the string.
std::string substr(const Location &start, const Location &end) const override
Extracts a substring delimited by two locations.
std::string line(size_t line_number) const override
Returns the contents of the specified line number.
size_t size() const override
Returns the total size (in characters) of the source.
const std::string & contents() const override
Returns the entire contents of the source.
StringSource(std::string contents, std::string display_path="<memory>")
Creates a string source with an optional display path.
size_t line_count() const override
Returns the total number of lines in the string.
std::ostream & operator<<(std::ostream &os, const pretty_diagnostics::Location &location)
Streams a readable description of a Location