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