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 <fmt/format.h>
9#include <memory>
10#include <iostream>
11
12namespace tt {
13
21
25 int _line;
26
30 int _column;
31
32public:
35 parse_location() noexcept : _file({}), _line(0), _column(0) {}
36
40 parse_location(std::shared_ptr<URL> const &file) noexcept : _file(file), _line(0), _column(0) {}
41
45 parse_location(URL const &file) noexcept : _file(std::make_shared<URL>(std::move(file))), _line(0), _column(0) {}
46
52 parse_location(std::shared_ptr<URL> const &file, int line, int column) noexcept : _file(file), _line(line - 1), _column(column - 1) {}
53
58 parse_location(int line, int column) noexcept : _file(), _line(line - 1), _column(column - 1) {}
59
60 [[nodiscard]] bool has_file() const noexcept {
61 return static_cast<bool>(_file);
62 }
63
64 [[nodiscard]] URL const &file() const noexcept {
65 return *_file;
66 }
67
68 [[nodiscard]] int line() const noexcept {
69 return _line + 1;
70 }
71
72 [[nodiscard]] int column() const noexcept {
73 return _column + 1;
74 }
75
76 [[nodiscard]] std::pair<int,int> line_and_column() const noexcept {
77 return {_line + 1, _column + 1};
78 }
79
80 void set_file(std::shared_ptr<URL> file) {
81 _file = std::move(file);
82 }
83
84 void set_file(URL file) {
85 _file = std::make_shared<URL>(std::move(file));
86 }
87
88 void set_line(int line) noexcept {
89 _line = line - 1;
90 }
91
92 void set_column(int column) noexcept {
93 _column = column - 1;
94 }
95
96 void set_line_and_column(std::pair<int,int> line_and_column) noexcept {
97 _line = line_and_column.first - 1;
98 _column = line_and_column.second - 1;
99 }
100
101 void increment_column() noexcept {
102 ++_column;
103 }
104
105 void tab_column() noexcept {
106 _column /= 8;
107 _column += 1;
108 _column *= 8;
109 }
110
111 void increment_line() noexcept {
112 _column = 0;
113 ++_line;
114 }
115
116
117 parse_location &operator+=(char c) noexcept {
118 switch (c) {
119 case '\t':
120 _column = ((_column / 8) + 1) * 8;
121 break;
122 case '\f':
123 [[fallthrough]];
124 case '\n':
125 ++_line;
126 [[fallthrough]];
127 case '\r':
128 _column = 0;
129 break;
130 default:
131 ++_column;
132 }
133 return *this;
134 }
135
136 parse_location &operator+=(std::string const &s) noexcept {
137 for (ttlet c: s) {
138 *this += c;
139 }
140 return *this;
141 }
142
143 parse_location &operator+=(char const *s) noexcept {
144 while (ttlet c = *s++) {
145 *this += c;
146 }
147 return *this;
148 }
149
150 parse_location &operator+=(parse_location const &location) noexcept {
151 if (location._line == 0) {
152 _column += location._column;
153 } else {
154 _line += location._line;
155 _column = location._column;
156 }
157 return *this;
158 }
159
160 friend std::string to_string(parse_location const &l) noexcept {
161 return fmt::format("{0}:{1}:{2}", l.file(), l.line(), l.column());
162 }
163
164 friend std::ostream& operator<<(std::ostream &os, parse_location const &l) {
165 os << to_string(l);
166 return os;
167 }
168};
169
170
171}
172
173
A File object.
Definition file.hpp:62
Definition parse_location.hpp:16
parse_location(URL const &file) noexcept
Construct a location.
Definition parse_location.hpp:45
parse_location() noexcept
Construct an empty location object.
Definition parse_location.hpp:35
parse_location(std::shared_ptr< URL > const &file) noexcept
Construct a location.
Definition parse_location.hpp:40
parse_location(std::shared_ptr< URL > const &file, int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:52
parse_location(int line, int column) noexcept
Construct a location.
Definition parse_location.hpp:58
Definition URL.hpp:46
T move(T... args)