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