HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
parse_location.hpp
1// Copyright Take Vos 2019-2020.
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 "URL.hpp"
8#include <format>
9#include <memory>
10#include <iostream>
11#include <string_view>
12
13namespace tt {
14
22
26 int _line;
27
31 int _column;
32
33public:
36 parse_location() noexcept : _file({}), _line(0), _column(0) {}
37
41 parse_location(std::shared_ptr<URL> const &file) noexcept : _file(file), _line(0), _column(0) {}
42
46 parse_location(URL const &file) noexcept : _file(std::make_shared<URL>(std::move(file))), _line(0), _column(0) {}
47
53 parse_location(std::shared_ptr<URL> const &file, int line, int column) noexcept :
54 _file(file), _line(line - 1), _column(column - 1)
55 {
56 }
57
62 parse_location(int line, int column) noexcept : _file(), _line(line - 1), _column(column - 1) {}
63
64 [[nodiscard]] bool has_file() const noexcept
65 {
66 return static_cast<bool>(_file);
67 }
68
69 [[nodiscard]] URL const &file() const noexcept
70 {
71 return *_file;
72 }
73
74 [[nodiscard]] int line() const noexcept
75 {
76 return _line + 1;
77 }
78
79 [[nodiscard]] int column() const noexcept
80 {
81 return _column + 1;
82 }
83
84 [[nodiscard]] std::pair<int, int> line_and_column() const noexcept
85 {
86 return {_line + 1, _column + 1};
87 }
88
89 void set_file(std::shared_ptr<URL> file)
90 {
91 _file = std::move(file);
92 }
93
94 void set_file(URL file)
95 {
96 _file = std::make_shared<URL>(std::move(file));
97 }
98
99 void set_line(int line) noexcept
100 {
101 _line = line - 1;
102 }
103
104 void set_column(int column) noexcept
105 {
106 _column = column - 1;
107 }
108
109 void set_line_and_column(std::pair<int, int> line_and_column) noexcept
110 {
111 _line = line_and_column.first - 1;
112 _column = line_and_column.second - 1;
113 }
114
115 void increment_column() noexcept
116 {
117 ++_column;
118 }
119
120 void tab_column() noexcept
121 {
122 _column /= 8;
123 _column += 1;
124 _column *= 8;
125 }
126
127 void increment_line() noexcept
128 {
129 _column = 0;
130 ++_line;
131 }
132
133 parse_location &operator+=(char c) noexcept
134 {
135 switch (c) {
136 case '\t': _column = ((_column / 8) + 1) * 8; break;
137 case '\f': [[fallthrough]];
138 case '\n': ++_line; [[fallthrough]];
139 case '\r': _column = 0; break;
140 default: ++_column;
141 }
142 return *this;
143 }
144
145 parse_location &operator+=(std::string const &s) noexcept
146 {
147 for (ttlet c : s) {
148 *this += c;
149 }
150 return *this;
151 }
152
153 parse_location &operator+=(char const *s) noexcept
154 {
155 while (ttlet c = *s++) {
156 *this += c;
157 }
158 return *this;
159 }
160
161 parse_location &operator+=(parse_location const &location) noexcept
162 {
163 if (location._line == 0) {
164 _column += location._column;
165 } else {
166 _line += location._line;
167 _column = location._column;
168 }
169 return *this;
170 }
171
172 friend std::string to_string(parse_location const &l) noexcept
173 {
174 return std::format("{0}:{1}:{2}", l.file(), l.line(), l.column());
175 }
176
177 friend std::ostream &operator<<(std::ostream &os, parse_location const &l)
178 {
179 os << to_string(l);
180 return os;
181 }
182};
183
184} // namespace tt
185
186namespace std {
187
188template<typename CharT>
189struct formatter<tt::parse_location, CharT> : formatter<string_view, CharT> {
190 auto format(tt::parse_location t, auto &fc)
191 {
192 return formatter<string_view, CharT>::format(to_string(t), fc);
193 }
194};
195
196} // namespace std
STL namespace.
A File object.
Definition file.hpp:62
Definition parse_location.hpp:17
parse_location(URL const &file) noexcept
Construct a location.
Definition parse_location.hpp:46
parse_location() noexcept
Construct an empty location object.
Definition parse_location.hpp:36
parse_location(std::shared_ptr< URL > const &file) noexcept
Construct a location.
Definition parse_location.hpp:41
parse_location(std::shared_ptr< URL > const &file, int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:53
parse_location(int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:62
Definition URL.hpp:47
T move(T... args)
T to_string(T... args)