Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
source.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <optional>
5
6namespace pretty_diagnostics {
13class Location {
14public:
22 Location(size_t row, size_t column, size_t index);
23
28
37 friend bool operator<(const Location& lhs, const Location& rhs) {
38 return lhs._index < rhs._index;
39 }
40
49 friend bool operator<=(const Location& lhs, const Location& rhs) {
50 return rhs >= lhs;
51 }
52
61 friend bool operator>(const Location& lhs, const Location& rhs) {
62 return rhs < lhs;
63 }
64
73 friend bool operator>=(const Location& lhs, const Location& rhs) {
74 return !(lhs < rhs);
75 }
76
85 friend bool operator==(const Location& lhs, const Location& rhs) {
86 return lhs._row == rhs._row
87 && lhs._column == rhs._column
88 && lhs._index == rhs._index;
89 }
90
99 friend bool operator!=(const Location& lhs, const Location& rhs) {
100 return !(lhs == rhs);
101 }
102
108 [[nodiscard]] auto column() const { return _column; }
109
115 [[nodiscard]] auto index() const { return _index; }
116
122 [[nodiscard]] auto row() const { return _row; }
123
124private:
125 size_t _row, _column;
126 size_t _index;
127};
128
132class Source {
133public:
134 virtual ~Source() = default;
135
144 [[nodiscard]] virtual Location from_coords(size_t row, size_t column) const = 0;
145
153 [[nodiscard]] virtual Location from_index(size_t index) const = 0;
154
163 [[nodiscard]] virtual std::string substr(const Location& start, const Location& end) const = 0;
164
172 [[nodiscard]] virtual std::string line(const Location& location) const = 0;
173
181 [[nodiscard]] virtual std::string line(size_t line_number) const = 0;
182
188 [[nodiscard]] virtual size_t line_count() const = 0;
189
195 [[nodiscard]] virtual std::string contents() const = 0;
196
202 [[nodiscard]] virtual std::string path() const = 0;
203
209 [[nodiscard]] virtual size_t size() const = 0;
210};
211
215class FileSource final : public Source {
216public:
222 explicit FileSource(std::filesystem::path path);
223
229 void set_working_path(const std::filesystem::path& path);
230
239 [[nodiscard]] Location from_coords(size_t row, size_t column) const override;
240
248 [[nodiscard]] Location from_index(size_t index) const override;
249
258 [[nodiscard]] std::string substr(const Location& start, const Location& end) const override;
259
267 [[nodiscard]] std::string line(const Location& location) const override;
268
276 [[nodiscard]] std::string line(size_t line_number) const override;
277
283 [[nodiscard]] size_t line_count() const override;
284
290 [[nodiscard]] std::string contents() const override;
291
297 [[nodiscard]] std::string path() const override;
298
304 [[nodiscard]] size_t size() const override;
305
314 friend bool operator==(const FileSource& lhs, const FileSource& rhs) {
315 return lhs._path == rhs._path;
316 }
317
326 friend bool operator!=(const FileSource& lhs, const FileSource& rhs) {
327 return !(lhs == rhs);
328 }
329
330private:
331 std::optional<std::filesystem::path> _working_path;
332 std::filesystem::path _path;
333};
334} // namespace pretty_diagnostics
335
344std::ostream& operator<<(std::ostream& os, const pretty_diagnostics::Location& location);
345
354std::ostream& operator<<(std::ostream& os, const pretty_diagnostics::FileSource& source);
355
364std::ostream& operator<<(std::ostream& os, const pretty_diagnostics::Source& source);
365
366// BSD 3-Clause License
367//
368// Copyright (c) 2025, Timo Behrend
369//
370// Redistribution and use in source and binary forms, with or without
371// modification, are permitted provided that the following conditions are met:
372//
373// 1. Redistributions of source code must retain the above copyright notice, this
374// list of conditions and the following disclaimer.
375//
376// 2. Redistributions in binary form must reproduce the above copyright notice,
377// this list of conditions and the following disclaimer in the documentation
378// and/or other materials provided with the distribution.
379//
380// 3. Neither the name of the copyright holder nor the names of its
381// contributors may be used to endorse or promote products derived from
382// this software without specific prior written permission.
383//
384// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
385// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
386// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
387// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
388// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
389// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
390// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
391// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
392// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
393// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.
Definition: label.hpp:5
std::ostream & operator<<(std::ostream &os, const pretty_diagnostics::Location &location)
Streams a readable description of a Location