Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
span.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "source.hpp"
4
5namespace pretty_diagnostics {
12class Span {
13public:
21 Span(const std::shared_ptr<Source>& source, const Location& start, const Location& end);
22
32 Span(const std::shared_ptr<Source>& source, size_t start_row, size_t start_column, size_t end_row, size_t end_column);
33
41 Span(const std::shared_ptr<Source>& source, size_t start_index, size_t end_index);
42
51 friend bool operator<(const Span& lhs, const Span& rhs) { return lhs._start < rhs._start; }
52
61 friend bool operator<=(const Span& lhs, const Span& rhs) { return rhs >= lhs; }
62
71 friend bool operator>(const Span& lhs, const Span& rhs) { return rhs < lhs; }
72
81 friend bool operator>=(const Span& lhs, const Span& rhs) { return !(lhs < rhs); }
82
91 friend bool operator==(const Span& lhs, const Span& rhs) {
92 return lhs._source == rhs._source && lhs._start == rhs._start && lhs._end == rhs._end;
93 }
94
103 friend bool operator!=(const Span& lhs, const Span& rhs) { return !(lhs == rhs); }
104
115 [[nodiscard]] Span join(const Span& other) const;
116
124 [[nodiscard]] bool intersects(const Span& other) const;
125
131 [[nodiscard]] std::string substr() const;
132
138 [[nodiscard]] size_t width() const;
139
145 [[nodiscard]] size_t line() const;
146
152 [[nodiscard]] const std::shared_ptr<Source>& source() const { return _source; }
153
159 [[nodiscard]] Location start() const { return _start; }
160
166 [[nodiscard]] Location end() const { return _end; }
167
168private:
169 std::shared_ptr<Source> _source;
170 Location _start, _end;
171};
172} // namespace pretty_diagnostics
173
182std::ostream& operator<<(std::ostream& os, const pretty_diagnostics::Span& span);
183
184// BSD 3-Clause License
185//
186// Copyright (c) 2025, Timo Behrend
187//
188// Redistribution and use in source and binary forms, with or without
189// modification, are permitted provided that the following conditions are met:
190//
191// 1. Redistributions of source code must retain the above copyright notice, this
192// list of conditions and the following disclaimer.
193//
194// 2. Redistributions in binary form must reproduce the above copyright notice,
195// this list of conditions and the following disclaimer in the documentation
196// and/or other materials provided with the distribution.
197//
198// 3. Neither the name of the copyright holder nor the names of its
199// contributors may be used to endorse or promote products derived from
200// this software without specific prior written permission.
201//
202// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
203// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
205// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
206// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
208// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
209// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
210// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
211// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
A position inside a Source, expressed as (row, column, index)
Definition: source.hpp:15
Represents a contiguous region within a Source
Definition: span.hpp:12
friend bool operator!=(const Span &lhs, const Span &rhs)
Inequality based on ==
Definition: span.hpp:103
Span join(const Span &other) const
Combines this span with another span into a single continuous span.
friend bool operator==(const Span &lhs, const Span &rhs)
Equality compares source, start and end.
Definition: span.hpp:91
std::string substr() const
Extracts the substring of the source covered by this span.
friend bool operator<=(const Span &lhs, const Span &rhs)
Less-than-or-equal comparison derived from >=
Definition: span.hpp:61
Location end() const
Returns the end location of the span.
Definition: span.hpp:166
size_t line() const
Returns the 0-based line number of the span's start.
size_t width() const
Returns the number of characters covered by this span.
friend bool operator>(const Span &lhs, const Span &rhs)
Greater-than comparison derived from <
Definition: span.hpp:71
bool intersects(const Span &other) const
Returns true if this span intersects the other span.
Span(const std::shared_ptr< Source > &source, size_t start_index, size_t end_index)
Constructs a span from 0-based character indices into the source content.
Span(const std::shared_ptr< Source > &source, size_t start_row, size_t start_column, size_t end_row, size_t end_column)
Constructs a span from row/column coordinates.
friend bool operator<(const Span &lhs, const Span &rhs)
Orders spans by their start location.
Definition: span.hpp:51
const std::shared_ptr< Source > & source() const
Returns the backing source of this span.
Definition: span.hpp:152
Span(const std::shared_ptr< Source > &source, const Location &start, const Location &end)
Constructs a span from explicit start and end locations within the same source.
friend bool operator>=(const Span &lhs, const Span &rhs)
Greater-than-or-equal comparison derived from <
Definition: span.hpp:81
Location start() const
Returns the starting location of the span.
Definition: span.hpp:159
Definition: color.hpp:5
std::ostream & operator<<(std::ostream &os, const pretty_diagnostics::Span &span)
Streams a human-readable representation of a span for debugging.