HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
token.hpp
1// Copyright Take Vos 2023.
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 "../color/module.hpp"
9#include <format>
10#include <filesystem>
11#include <iterator>
12
13namespace hi { inline namespace v1 {
14
15hi_export struct token {
16 enum class kind_type : uint8_t {
17 none,
18 error_unexepected_character,
19 error_invalid_digit,
20 error_incomplete_exponent,
21 error_incomplete_string,
22 error_incomplete_comment,
23 error_after_lt_bang,
24 integer,
25 real,
26 sstr,
27 dstr,
28 bstr,
29 istr,
30 color,
31 lcomment,
32 bcomment,
33 ws,
34 id,
35 other
36 };
37
38 // clang-format off
39 constexpr static auto kind_type_metadata = enum_metadata{
40 kind_type::none, "none",
41 kind_type::error_unexepected_character, "error:unexpected character",
42 kind_type::error_invalid_digit, "error:invalid digit",
43 kind_type::error_incomplete_exponent, "error:incomplete exponent",
44 kind_type::error_incomplete_string, "error:incomplete string",
45 kind_type::error_incomplete_comment, "error:incomplete comment",
46 kind_type::error_after_lt_bang, "error:after_lt_bang",
47 kind_type::integer, "integer",
48 kind_type::real, "read",
49 kind_type::sstr, "single-quote string",
50 kind_type::dstr, "double-quote string",
51 kind_type::bstr, "back-quote string",
52 kind_type::istr, "ini string",
53 kind_type::color, "color",
54 kind_type::lcomment, "line comment",
55 kind_type::bcomment, "block comment",
56 kind_type::ws, "ws",
57 kind_type::id, "id",
58 kind_type::other, "other"
59 };
60 // clang-format on
61
62 constexpr static auto none = kind_type::none;
63 constexpr static auto error_unexepected_character = kind_type::error_unexepected_character;
64 constexpr static auto error_invalid_digit = kind_type::error_invalid_digit;
65 constexpr static auto error_incomplete_exponent = kind_type::error_incomplete_exponent;
66 constexpr static auto error_incomplete_string = kind_type::error_incomplete_string;
67 constexpr static auto error_incomplete_comment = kind_type::error_incomplete_comment;
68 constexpr static auto error_after_lt_bang = kind_type::error_after_lt_bang;
69 constexpr static auto integer = kind_type::integer;
70 constexpr static auto real = kind_type::real;
71 constexpr static auto sstr = kind_type::sstr;
72 constexpr static auto dstr = kind_type::dstr;
73 constexpr static auto bstr = kind_type::bstr;
74 constexpr static auto istr = kind_type::istr;
75 constexpr static auto color = kind_type::color;
76 constexpr static auto lcomment = kind_type::lcomment;
77 constexpr static auto bcomment = kind_type::bcomment;
78 constexpr static auto ws = kind_type::ws;
79 constexpr static auto id = kind_type::id;
80 constexpr static auto other = kind_type::other;
81
82 std::vector<char> capture = {};
83 size_t line_nr = 0;
84 size_t column_nr = 0;
85 kind_type kind = kind_type::none;
86
87 constexpr token() noexcept = default;
88 constexpr token(token const&) noexcept = default;
89 constexpr token(token&&) noexcept = default;
90 constexpr token& operator=(token const&) noexcept = default;
91 constexpr token& operator=(token&&) noexcept = default;
92
93 constexpr token(kind_type kind, std::string_view capture, size_t column_nr) noexcept :
94 kind(kind), capture(), line_nr(0), column_nr(column_nr)
95 {
96 std::copy(capture.begin(), capture.end(), std::back_inserter(this->capture));
97 }
98
99 constexpr token(kind_type kind, std::string_view capture, size_t line_nr, size_t column_nr) noexcept :
100 kind(kind), capture(), line_nr(line_nr), column_nr(column_nr)
101 {
102 std::copy(capture.begin(), capture.end(), std::back_inserter(this->capture));
103 }
104
105 [[nodiscard]] constexpr friend bool operator==(token const&, token const&) noexcept = default;
106
107 [[nodiscard]] constexpr bool operator==(kind_type rhs) const noexcept
108 {
109 return kind == rhs;
110 }
111
112 [[nodiscard]] constexpr bool operator==(std::string_view rhs) const noexcept
113 {
114 return static_cast<std::string_view>(*this) == rhs;
115 }
116
117 [[nodiscard]] constexpr bool operator==(char rhs) const noexcept
118 {
119 return kind == kind_type::other and capture.size() == 1 and capture.front() == rhs;
120 }
121
122 constexpr operator std::string() const noexcept
123 {
124 return std::string{capture.data(), capture.size()};
125 }
126
127 template<std::integral T>
128 constexpr operator T() const
129 {
130 return from_string<T>(static_cast<std::string_view>(*this));
131 }
132
133 template<std::floating_point T>
134 operator T() const
135 {
136 return from_string<T>(static_cast<std::string_view>(*this));
137 }
138
139 operator color() const
140 {
141 hi_axiom(kind == kind_type::color);
142 return color_from_sRGB(static_cast<std::string_view>(*this));
143 }
144
145private:
146 constexpr operator std::string_view() const noexcept
147 {
148 return std::string_view{capture.data(), capture.size()};
149 }
150};
151
159hi_export template<std::input_iterator It, std::sentinel_for<It> ItEnd>
160[[nodiscard]] constexpr std::string token_location(It& it, ItEnd last, std::string_view path) noexcept
161{
162 if (it == last) {
163 return std::format("{}:eof", path);
164 } else {
165 return std::format("{}:{}:{}", path, it->line_nr + 1, it->column_nr + 1);
166 }
167}
168
169hi_export template<std::input_iterator It>
170[[nodiscard]] constexpr std::string token_location(It& it, std::string_view path) noexcept
171{
172 return token_location(it, std::default_sentinel, path);
173}
174
175}} // namespace hi::v1
176
177hi_export template<typename CharT>
178struct std::formatter<hi::token, CharT> : std::formatter<std::string, CharT> {
179 auto format(hi::token const& t, auto& fc) const
180 {
181 return std::formatter<std::string, CharT>::format(
182 std::format(
183 "{} \"{}\" {}:{}", hi::token::kind_type_metadata[t.kind], static_cast<std::string>(t), t.line_nr, t.column_nr),
184 fc);
185 }
186};
187
188namespace hi { inline namespace v1 {
189
190hi_export inline std::ostream& operator<<(std::ostream& lhs, token const& rhs)
191{
192 return lhs << std::format("{}", rhs);
193}
194
195}} // namespace hi::v1
color color_from_sRGB(float r, float g, float b, float a) noexcept
Convert gama corrected sRGB color to the linear color.
Definition sRGB.hpp:142
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
hi_export constexpr std::string token_location(It &it, ItEnd last, std::string_view path) noexcept
Create a location string for error messages.
Definition token.hpp:160
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
This is a RGBA floating point color.
Definition color.hpp:45
Definition token.hpp:15
A object that holds enum-values and strings.
Definition enum_metadata.hpp:35
T back_inserter(T... args)
T begin(T... args)
T copy(T... args)
T data(T... args)
T end(T... args)
T front(T... args)
T size(T... args)