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>(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 file() const noexcept
70 {
71 if (_file) {
72 return *_file;
73 } else {
74 return {};
75 }
76 }
77
78 [[nodiscard]] int line() const noexcept
79 {
80 return _line + 1;
81 }
82
83 [[nodiscard]] int column() const noexcept
84 {
85 return _column + 1;
86 }
87
88 [[nodiscard]] std::pair<int, int> line_and_column() const noexcept
89 {
90 return {_line + 1, _column + 1};
91 }
92
93 void set_file(std::shared_ptr<URL> file)
94 {
95 _file = std::move(file);
96 }
97
98 void set_file(URL file)
99 {
100 _file = std::make_shared<URL>(std::move(file));
101 }
102
103 void set_line(int line) noexcept
104 {
105 _line = line - 1;
106 }
107
108 void set_column(int column) noexcept
109 {
110 _column = column - 1;
111 }
112
113 void set_line_and_column(std::pair<int, int> line_and_column) noexcept
114 {
115 _line = line_and_column.first - 1;
116 _column = line_and_column.second - 1;
117 }
118
119 void increment_column() noexcept
120 {
121 ++_column;
122 }
123
124 void tab_column() noexcept
125 {
126 _column /= 8;
127 _column += 1;
128 _column *= 8;
129 }
130
131 void increment_line() noexcept
132 {
133 _column = 0;
134 ++_line;
135 }
136
137 parse_location &operator+=(char c) noexcept
138 {
139 switch (c) {
140 case '\t': _column = ((_column / 8) + 1) * 8; break;
141 case '\f': [[fallthrough]];
142 case '\n': ++_line; [[fallthrough]];
143 case '\r': _column = 0; break;
144 default: ++_column;
145 }
146 return *this;
147 }
148
149 parse_location &operator+=(std::string const &s) noexcept
150 {
151 for (ttlet c : s) {
152 *this += c;
153 }
154 return *this;
155 }
156
157 parse_location &operator+=(char const *s) noexcept
158 {
159 while (ttlet c = *s++) {
160 *this += c;
161 }
162 return *this;
163 }
164
165 parse_location &operator+=(parse_location const &location) noexcept
166 {
167 if (location._line == 0) {
168 _column += location._column;
169 } else {
170 _line += location._line;
171 _column = location._column;
172 }
173 return *this;
174 }
175
176 friend std::string to_string(parse_location const &l) noexcept
177 {
178 return std::format("{}:{}:{}", l.file(), l.line(), l.column());
179 }
180
181 friend std::ostream &operator<<(std::ostream &os, parse_location const &l)
182 {
183 os << to_string(l);
184 return os;
185 }
186};
187
188} // namespace tt
189
190namespace std {
191
192template<typename CharT>
193struct formatter<tt::parse_location, CharT> : formatter<string_view, CharT> {
194 auto format(tt::parse_location t, auto &fc)
195 {
196 return formatter<string_view, CharT>::format(to_string(t), fc);
197 }
198};
199
200} // 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)