8#include "small_vector.hpp"
9#include "utility/module.hpp"
11#include "parse_location.hpp"
19namespace hi::inline
v1 {
21enum class tokenizer_name_t : uint8_t {
23 ErrorInvalidCharacter,
24 ErrorEOTInBlockComment,
38constexpr char const *to_const_string(tokenizer_name_t name)
noexcept
41 case tokenizer_name_t::NotAssigned:
43 case tokenizer_name_t::ErrorInvalidCharacter:
44 return "ErrorInvalidCharacter";
45 case tokenizer_name_t::ErrorEOTInBlockComment:
46 return "ErrorEOTInBlockComment";
47 case tokenizer_name_t::ErrorEOTInString:
48 return "ErrorEOTInString";
49 case tokenizer_name_t::ErrorLFInString:
50 return "ErrorLFInString";
51 case tokenizer_name_t::Name:
53 case tokenizer_name_t::StringLiteral:
54 return "StringLiteral";
55 case tokenizer_name_t::IntegerLiteral:
56 return "IntegerLiteral";
57 case tokenizer_name_t::DateLiteral:
59 case tokenizer_name_t::TimeLiteral:
61 case tokenizer_name_t::FloatLiteral:
62 return "FloatLiteral";
63 case tokenizer_name_t::Operator:
65 case tokenizer_name_t::End:
74 return lhs << to_const_string(rhs);
80template<
typename CharT>
81struct std::formatter<
hi::tokenizer_name_t, CharT> : std::formatter<char const *, CharT> {
82 auto format(hi::tokenizer_name_t
const& t,
auto& fc)
const
84 return std::formatter<char const *, CharT>{}.format(hi::to_const_string(t), fc);
88template<
typename CharT>
89struct std::formatter<
hi::token_t, CharT> : std::formatter<std::string_view, CharT> {
90 auto format(hi::token_t
const& t,
auto& fc)
const ->
decltype(std::formatter<char const *, CharT>{}.format(
"", fc));
93namespace hi::inline
v1 {
96 tokenizer_name_t name = tokenizer_name_t::NotAssigned;
99 bool is_binary =
false;
103 constexpr token_t()
noexcept =
default;
106 constexpr token_t& operator=(
token_t const& other)
noexcept =
default;
110 name(name), value(
std::move(value)), location(), is_binary(
false), precedence(0)
114 operator bool()
const noexcept
116 return name != tokenizer_name_t::NotAssigned;
119 explicit operator long double()
const
125 throw parse_error(std::format(
"Could not convert token {} to long double", *
this));
129 explicit operator double()
const
135 throw parse_error(std::format(
"Could not convert token {} to double", *
this));
139 explicit operator float()
const
145 throw parse_error(std::format(
"Could not convert token {} to float", *
this));
149 template<std::
integral T>
150 explicit operator T()
const
153 return hi::from_string<T>(value);
156 throw parse_error(std::format(
"Could not convert token {} to {}", *
this,
typeid(T).name()));
165 explicit operator decimal()
const
170 explicit operator std::chrono::year_month_day()
const
172 hilet parts = split(value,
'-');
173 if (parts.size() != 3) {
174 throw parse_error(
"Expect date to be in the format YYYY-MM-DD");
177 hilet year = std::chrono::year{stoi(parts[0])};
178 hilet month = std::chrono::month{narrow_cast<unsigned int>(stoi(parts[1]))};
179 hilet day = std::chrono::day{narrow_cast<unsigned int>(stoi(parts[2]))};
180 return {year, month, day};
186 if (value.size() > 0) {
196 return lhs << rhs.repr();
199 [[nodiscard]]
friend bool operator==(
token_t const& lhs,
token_t const& rhs)
noexcept
201 return (lhs.name == rhs.name) && (lhs.value == rhs.value);
204 [[nodiscard]]
friend bool operator==(
token_t const& lhs, tokenizer_name_t
const& rhs)
noexcept
206 return lhs.name == rhs;
209 [[nodiscard]]
friend bool operator==(
token_t const& lhs,
const char *rhs)
noexcept
211 return lhs.value == rhs;
216using token_iterator =
typename token_vector::iterator;
222 token_iterator next_token;
224 parse_result() noexcept : found(
false), value(), next_token() {}
226 parse_result(T
const& value, token_iterator next_token) : found(
true), value(value), next_token(next_token) {}
228 operator bool()
const noexcept
233 T
const& operator*()
const noexcept
259parseTokens(std::string_view::const_iterator first, std::string_view::const_iterator last)
noexcept;
263template<
typename CharT>
264auto std::formatter<hi::token_t, CharT>::format(hi::token_t
const& t,
auto& fc)
const
265 ->
decltype(std::formatter<char const *, CharT>{}.format(
"", fc))
267 return std::formatter<std::string_view, CharT>{}.format(t.repr(), fc);
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
std::vector< token_t > parseTokens(std::string_view text) noexcept
geometry/margins.hpp
Definition cache.hpp:11
Definition decimal.hpp:18
Definition parse_location.hpp:18
Definition tokenizer.hpp:95
Definition tokenizer.hpp:219