HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
parse_location.hpp
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "concepts.hpp"
8#include <format>
9#include <memory>
10#include <iostream>
11#include <string_view>
12#include <filesystem>
13
14namespace hi::inline v1 {
15
23
27 int _line;
28
32 int _column;
33
34public:
37 parse_location() noexcept : _file({}), _line(0), _column(0) {}
38
42 parse_location(std::shared_ptr<std::filesystem::path> const& file) noexcept : _file(file), _line(0), _column(0) {}
43
48 _file(std::make_shared<std::filesystem::path>(hi_forward(file))), _line(0), _column(0)
49 {
50 }
51
57 parse_location(forward_of<std::shared_ptr<std::filesystem::path>> auto&& file, int line, int column) noexcept :
58 _file(hi_forward(file)), _line(line - 1), _column(column - 1)
59 {
60 }
61
66 parse_location(int line, int column) noexcept : _file(), _line(line - 1), _column(column - 1) {}
67
68 [[nodiscard]] bool has_file() const noexcept
69 {
70 return to_bool(_file);
71 }
72
73 [[nodiscard]] std::filesystem::path file() const noexcept
74 {
75 if (_file) {
76 return *_file;
77 } else {
78 return {};
79 }
80 }
81
82 [[nodiscard]] int line() const noexcept
83 {
84 return _line + 1;
85 }
86
87 [[nodiscard]] int column() const noexcept
88 {
89 return _column + 1;
90 }
91
92 [[nodiscard]] std::pair<int, int> line_and_column() const noexcept
93 {
94 return {_line + 1, _column + 1};
95 }
96
97 void set_file(forward_of<std::shared_ptr<std::filesystem::path>> auto && file) noexcept
98 {
99 _file = hi_forward(file);
100 }
101
102 void set_file(forward_of<std::filesystem::path> auto &&file) noexcept
103 {
104 _file = std::make_shared<std::filesystem::path>(hi_forward(file));
105 }
106
107 void set_line(int line) noexcept
108 {
109 _line = line - 1;
110 }
111
112 void set_column(int column) noexcept
113 {
114 _column = column - 1;
115 }
116
117 void set_line_and_column(std::pair<int, int> line_and_column) noexcept
118 {
119 _line = line_and_column.first - 1;
120 _column = line_and_column.second - 1;
121 }
122
123 void increment_column() noexcept
124 {
125 ++_column;
126 }
127
128 void tab_column() noexcept
129 {
130 _column /= 8;
131 _column += 1;
132 _column *= 8;
133 }
134
135 void increment_line() noexcept
136 {
137 _column = 0;
138 ++_line;
139 }
140
141 parse_location& operator+=(char c) noexcept
142 {
143 switch (c) {
144 case '\t':
145 _column = ((_column / 8) + 1) * 8;
146 break;
147 case '\f':
148 [[fallthrough]];
149 case '\n':
150 ++_line;
151 [[fallthrough]];
152 case '\r':
153 _column = 0;
154 break;
155 default:
156 ++_column;
157 }
158 return *this;
159 }
160
161 parse_location& operator+=(std::string const& s) noexcept
162 {
163 for (hilet c : s) {
164 *this += c;
165 }
166 return *this;
167 }
168
169 parse_location& operator+=(char const *s) noexcept
170 {
172 while (hilet c = *s++) {
173 *this += c;
174 }
175 return *this;
176 }
177
178 parse_location& operator+=(parse_location const& location) noexcept
179 {
180 if (location._line == 0) {
181 _column += location._column;
182 } else {
183 _line += location._line;
184 _column = location._column;
185 }
186 return *this;
187 }
188
189 friend std::string to_string(parse_location const& l) noexcept
190 {
191 return std::format("{}:{}:{}", l.file().generic_string(), l.line(), l.column());
192 }
193
194 friend std::ostream& operator<<(std::ostream& os, parse_location const& l)
195 {
196 os << to_string(l);
197 return os;
198 }
199};
200
201} // namespace hi::inline v1
202
203template<typename CharT>
204struct std::formatter<hi::parse_location, CharT> : std::formatter<string_view, CharT> {
205 auto format(hi::parse_location t, auto& fc)
206 {
207 return std::formatter<string_view, CharT>::format(to_string(t), fc);
208 }
209};
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:118
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:15
geometry/margins.hpp
Definition assert.hpp:18
Definition parse_location.hpp:18
parse_location() noexcept
Construct an empty location object.
Definition parse_location.hpp:37
parse_location(int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:66
parse_location(std::shared_ptr< std::filesystem::path > const &file) noexcept
Construct a location.
Definition parse_location.hpp:42
parse_location(forward_of< std::shared_ptr< std::filesystem::path > > auto &&file, int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:57
parse_location(forward_of< std::filesystem::path > auto &&file) noexcept
Construct a location.
Definition parse_location.hpp:47
True if T is a forwarded type of Forward.
Definition concepts.hpp:130
T to_string(T... args)