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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <format>
10#include <memory>
11#include <iostream>
12#include <string_view>
13
14namespace hi::inline v1 {
15
22 std::string _file = {};
23
27 int _line = 0;
28
32 int _column = 0;
33
34public:
37 constexpr parse_location() noexcept = default;
38
42 constexpr parse_location(std::string file, int line = 1, int column = 1) noexcept : _file(std::move(file)), _line(line - 1), _column(column - 1) {}
43
48 constexpr parse_location(int line, int column) noexcept : parse_location(std::string{}, line, column) {}
49
50 [[nodiscard]] constexpr bool has_file() const noexcept
51 {
52 return not _file.empty();
53 }
54
55 [[nodiscard]] constexpr std::string file() const noexcept
56 {
57 return _file;
58 }
59
60 [[nodiscard]] constexpr int line() const noexcept
61 {
62 return _line + 1;
63 }
64
65 [[nodiscard]] constexpr int column() const noexcept
66 {
67 return _column + 1;
68 }
69
70 [[nodiscard]] constexpr std::pair<int, int> line_and_column() const noexcept
71 {
72 return {_line + 1, _column + 1};
73 }
74
75 constexpr void set_file(std::string file) noexcept
76 {
77 _file = std::move(file);
78 }
79
80 constexpr void set_line(int line) noexcept
81 {
82 _line = line - 1;
83 }
84
85 constexpr void set_column(int column) noexcept
86 {
87 _column = column - 1;
88 }
89
90 constexpr void set_line_and_column(std::pair<int, int> line_and_column) noexcept
91 {
92 _line = line_and_column.first - 1;
93 _column = line_and_column.second - 1;
94 }
95
96 constexpr void increment_column() noexcept
97 {
98 ++_column;
99 }
100
101 constexpr void tab_column() noexcept
102 {
103 _column /= 8;
104 _column += 1;
105 _column *= 8;
106 }
107
108 constexpr void increment_line() noexcept
109 {
110 _column = 0;
111 ++_line;
112 }
113
114 constexpr parse_location& operator+=(char c) noexcept
115 {
116 switch (c) {
117 case '\t':
118 _column = ((_column / 8) + 1) * 8;
119 break;
120 case '\f':
121 [[fallthrough]];
122 case '\n':
123 ++_line;
124 [[fallthrough]];
125 case '\r':
126 _column = 0;
127 break;
128 default:
129 ++_column;
130 }
131 return *this;
132 }
133
134 constexpr parse_location& operator+=(std::string const& s) noexcept
135 {
136 for (hilet c : s) {
137 *this += c;
138 }
139 return *this;
140 }
141
142 constexpr parse_location& operator+=(char const *s) noexcept
143 {
144 hi_assert_not_null(s);
145 while (hilet c = *s++) {
146 *this += c;
147 }
148 return *this;
149 }
150
151 constexpr parse_location& operator+=(parse_location const& location) noexcept
152 {
153 if (location._line == 0) {
154 _column += location._column;
155 } else {
156 _line += location._line;
157 _column = location._column;
158 }
159 return *this;
160 }
161
162 [[nodiscard]] friend std::string to_string(parse_location const& l) noexcept
163 {
164 return std::format("{}:{}:{}", l.file(), l.line(), l.column());
165 }
166
167 friend std::ostream& operator<<(std::ostream& os, parse_location const& l)
168 {
169 os << to_string(l);
170 return os;
171 }
172};
173
174} // namespace hi::inline v1
175
176template<typename CharT>
177struct std::formatter<hi::parse_location, CharT> : std::formatter<string_view, CharT> {
178 auto format(hi::parse_location t, auto& fc) const
179 {
180 return std::formatter<string_view, CharT>::format(to_string(t), fc);
181 }
182};
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Definition parse_location.hpp:18
constexpr parse_location() noexcept=default
Construct an empty location object.
constexpr parse_location(int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:48
T empty(T... args)
T move(T... args)
T to_string(T... args)