8#include "small_vector.hpp"
11#include "exception.hpp"
12#include "parse_location.hpp"
13#include "charconv.hpp"
21namespace hi::inline
v1 {
23enum class tokenizer_name_t : uint8_t {
25 ErrorInvalidCharacter,
26 ErrorEOTInBlockComment,
40constexpr char const *to_const_string(tokenizer_name_t name)
noexcept
43 case tokenizer_name_t::NotAssigned:
45 case tokenizer_name_t::ErrorInvalidCharacter:
46 return "ErrorInvalidCharacter";
47 case tokenizer_name_t::ErrorEOTInBlockComment:
48 return "ErrorEOTInBlockComment";
49 case tokenizer_name_t::ErrorEOTInString:
50 return "ErrorEOTInString";
51 case tokenizer_name_t::ErrorLFInString:
52 return "ErrorLFInString";
53 case tokenizer_name_t::Name:
55 case tokenizer_name_t::StringLiteral:
56 return "StringLiteral";
57 case tokenizer_name_t::IntegerLiteral:
58 return "IntegerLiteral";
59 case tokenizer_name_t::DateLiteral:
61 case tokenizer_name_t::TimeLiteral:
63 case tokenizer_name_t::FloatLiteral:
64 return "FloatLiteral";
65 case tokenizer_name_t::Operator:
67 case tokenizer_name_t::End:
76 return lhs << to_const_string(rhs);
82template<
typename CharT>
83struct std::formatter<
hi::tokenizer_name_t, CharT> : std::formatter<char const *, CharT> {
84 auto format(hi::tokenizer_name_t
const& t,
auto& fc)
const
86 return std::formatter<char const *, CharT>{}.format(hi::to_const_string(t), fc);
90template<
typename CharT>
91struct std::formatter<
hi::token_t, CharT> : std::formatter<std::string_view, CharT> {
92 auto format(hi::token_t
const& t,
auto& fc)
const ->
decltype(std::formatter<char const *, CharT>{}.format(
"", fc));
95namespace hi::inline
v1 {
98 tokenizer_name_t name = tokenizer_name_t::NotAssigned;
101 bool is_binary =
false;
105 constexpr token_t()
noexcept =
default;
108 constexpr token_t& operator=(
token_t const& other)
noexcept =
default;
112 name(name), value(
std::move(value)), location(), is_binary(
false), precedence(0)
116 operator bool()
const noexcept
118 return name != tokenizer_name_t::NotAssigned;
121 explicit operator long double()
const
127 throw parse_error(std::format(
"Could not convert token {} to long double", *
this));
131 explicit operator double()
const
137 throw parse_error(std::format(
"Could not convert token {} to double", *
this));
141 explicit operator float()
const
147 throw parse_error(std::format(
"Could not convert token {} to float", *
this));
151 template<std::
integral T>
152 explicit operator T()
const
155 return hi::from_string<T>(value);
158 throw parse_error(std::format(
"Could not convert token {} to {}", *
this,
typeid(T).name()));
167 explicit operator decimal()
const
172 explicit operator std::chrono::year_month_day()
const
174 hilet parts = split(value,
'-');
175 if (parts.size() != 3) {
176 throw parse_error(
"Expect date to be in the format YYYY-MM-DD");
179 hilet year = std::chrono::year{stoi(parts[0])};
180 hilet month = std::chrono::month{narrow_cast<unsigned int>(stoi(parts[1]))};
181 hilet day = std::chrono::day{narrow_cast<unsigned int>(stoi(parts[2]))};
182 return {year, month, day};
188 if (value.
size() > 0) {
198 return lhs << rhs.repr();
201 [[nodiscard]]
friend bool operator==(
token_t const& lhs,
token_t const& rhs)
noexcept
203 return (lhs.name == rhs.name) && (lhs.value == rhs.value);
206 [[nodiscard]]
friend bool operator==(
token_t const& lhs, tokenizer_name_t
const& rhs)
noexcept
208 return lhs.name == rhs;
211 [[nodiscard]]
friend bool operator==(
token_t const& lhs,
const char *rhs)
noexcept
213 return lhs.value == rhs;
218using token_iterator =
typename token_vector::iterator;
224 token_iterator next_token;
226 parse_result() noexcept : found(
false), value(), next_token() {}
228 parse_result(T
const& value, token_iterator next_token) : found(
true), value(value), next_token(next_token) {}
230 operator bool()
const noexcept
235 T
const& operator*()
const noexcept
261parseTokens(std::string_view::const_iterator first, std::string_view::const_iterator last)
noexcept;
265template<
typename CharT>
266auto std::formatter<hi::token_t, CharT>::format(hi::token_t
const& t,
auto& fc)
const
267 ->
decltype(std::formatter<char const *, CharT>{}.format(
"", fc))
269 return std::formatter<std::string_view, CharT>{}.format(t.repr(), fc);
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
std::vector< token_t > parseTokens(std::string_view text) noexcept
The HikoGUI namespace.
Definition ascii.hpp:19
Definition decimal.hpp:20
A variant of text.
Definition label.hpp:36
Definition parse_location.hpp:18
Definition tokenizer.hpp:97
Definition tokenizer.hpp:221