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,
33 size_t start_row, size_t start_column,
34 size_t end_row, size_t end_column);
35
43 Span(const std::shared_ptr<Source>& source,
44 size_t start_index,
45 size_t end_index);
46
55 friend bool operator<(const Span& lhs, const Span& rhs) {
56 return lhs._start < rhs._start;
57 }
58
67 friend bool operator<=(const Span& lhs, const Span& rhs) {
68 return rhs >= lhs;
69 }
70
79 friend bool operator>(const Span& lhs, const Span& rhs) {
80 return rhs < lhs;
81 }
82
91 friend bool operator>=(const Span& lhs, const Span& rhs) {
92 return !(lhs < rhs);
93 }
94
103 friend bool operator==(const Span& lhs, const Span& rhs) {
104 return lhs._source == rhs._source
105 && lhs._start == rhs._start
106 && lhs._end == rhs._end;
107 }
108
117 friend bool operator!=(const Span& lhs, const Span& rhs) {
118 return !(lhs == rhs);
119 }
120
131 [[nodiscard]] Span join(const Span& other) const;
132
140 [[nodiscard]] bool intersects(const Span& other) const;
141
147 [[nodiscard]] std::string substr() const;
148
154 [[nodiscard]] size_t width() const;
155
161 [[nodiscard]] size_t line() const;
162
168 [[nodiscard]] const std::shared_ptr<Source>& source() const { return _source; }
169
175 [[nodiscard]] Location start() const { return _start; }
176
182 [[nodiscard]] Location end() const { return _end; }
183
184private:
185 std::shared_ptr<Source> _source;
186 Location _start, _end;
187};
188} // namespace pretty_diagnostics
189
198std::ostream& operator<<(std::ostream& os, const pretty_diagnostics::Span& span);
199
200// BSD 3-Clause License
201//
202// Copyright (c) 2025, Timo Behrend
203//
204// Redistribution and use in source and binary forms, with or without
205// modification, are permitted provided that the following conditions are met:
206//
207// 1. Redistributions of source code must retain the above copyright notice, this
208// list of conditions and the following disclaimer.
209//
210// 2. Redistributions in binary form must reproduce the above copyright notice,
211// this list of conditions and the following disclaimer in the documentation
212// and/or other materials provided with the distribution.
213//
214// 3. Neither the name of the copyright holder nor the names of its
215// contributors may be used to endorse or promote products derived from
216// this software without specific prior written permission.
217//
218// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
219// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
220// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
221// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
222// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
224// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
225// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
226// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
227// 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:13
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:117
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:103
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:67
Location end() const
Returns the end location of the span.
Definition: span.hpp:182
size_t line() const
Returns the 1-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:79
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:55
const std::shared_ptr< Source > & source() const
Returns the backing source of this span.
Definition: span.hpp:168
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:91
Location start() const
Returns the starting location of the span.
Definition: span.hpp:175
Definition: label.hpp:5
std::ostream & operator<<(std::ostream &os, const pretty_diagnostics::Span &span)
Streams a human-readable representation of a span for debugging.