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/module.hpp"
8#include "../color/module.hpp"
9#include <format>
10#include <filesystem>
11#include <iterator>
12
13namespace hi { inline namespace v1 {
14
15struct 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
145 inline friend std::ostream& operator<<(std::ostream& lhs, token const& rhs)
146 {
147 return lhs << std::format("{}", rhs);
148 }
149
150private:
151 constexpr operator std::string_view() const noexcept
152 {
153 return std::string_view{capture.data(), capture.size()};
154 }
155};
156
164template<typename It, std::sentinel_for<It> ItEnd>
165[[nodiscard]] constexpr std::string token_location(It& it, ItEnd last, std::filesystem::path const& path) noexcept
166{
167 if (it == last) {
168 return std::format("{}:eof", path.filename().generic_string());
169 } else {
170 return std::format("{}:{}:{}", path.filename().generic_string(), it->line_nr + 1, it->column_nr + 1);
171 }
172}
173
174}} // namespace hi::v1
175
176template<typename CharT>
177struct std::formatter<hi::token, CharT> : std::formatter<std::string, CharT> {
178 auto format(hi::token const& t, auto& fc)
179 {
180 return std::formatter<std::string, CharT>::format(
181 std::format(
182 "{} \"{}\" {}:{}",
183 hi::token::kind_type_metadata[t.kind],
184 static_cast<std::string>(t),
185 t.line_nr,
186 t.column_nr),
187 fc);
188 }
189};
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
color color_from_sRGB(std::floating_point auto r, std::floating_point auto g, std::floating_point auto b, std::floating_point auto a) noexcept
Convert gama corrected sRGB color to the linear color.
Definition sRGB.hpp:139
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
constexpr std::string token_location(It &it, ItEnd last, std::filesystem::path const &path) noexcept
Create a location string for error messages.
Definition token.hpp:165
This is a RGBA floating point color.
Definition color.hpp:44
Definition token.hpp:15
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)