7#include "unicode/UTF.hpp"
9#include "small_vector.hpp"
12#include "exception.hpp"
13#include "parse_location.hpp"
14#include "charconv.hpp"
22namespace hi::inline v1 {
24enum class tokenizer_name_t : uint8_t {
26 ErrorInvalidCharacter,
27 ErrorEOTInBlockComment,
41constexpr char const *to_const_string(tokenizer_name_t name)
noexcept
44 case tokenizer_name_t::NotAssigned:
return "NotAssigned";
45 case tokenizer_name_t::ErrorInvalidCharacter:
return "ErrorInvalidCharacter";
46 case tokenizer_name_t::ErrorEOTInBlockComment:
return "ErrorEOTInBlockComment";
47 case tokenizer_name_t::ErrorEOTInString:
return "ErrorEOTInString";
48 case tokenizer_name_t::ErrorLFInString:
return "ErrorLFInString";
49 case tokenizer_name_t::Name:
return "Name";
50 case tokenizer_name_t::StringLiteral:
return "StringLiteral";
51 case tokenizer_name_t::IntegerLiteral:
return "IntegerLiteral";
52 case tokenizer_name_t::DateLiteral:
return "DateLiteral";
53 case tokenizer_name_t::TimeLiteral:
return "TimeLiteral";
54 case tokenizer_name_t::FloatLiteral:
return "FloatLiteral";
55 case tokenizer_name_t::Operator:
return "Operator";
56 case tokenizer_name_t::End:
return "End";
57 default: hi_no_default();
63 return lhs << to_const_string(rhs);
66template<
typename CharT>
67struct std::formatter<hi::tokenizer_name_t, CharT> : std::formatter<char const *, CharT> {
68 auto format(hi::tokenizer_name_t
const &t,
auto &fc)
70 return std::formatter<char const *, CharT>::format(hi::to_const_string(t), fc);
75 tokenizer_name_t name;
81 token_t() noexcept : name(tokenizer_name_t::NotAssigned), value(), location(), is_binary(
false), precedence(0) {}
84 name(name), value(
std::move(value)), location(), is_binary(
false), precedence(0)
89 name(other.name), value(other.value), location(other.location), is_binary(other.is_binary), precedence(other.precedence)
91 hi_axiom(&other !=
this);
98 is_binary(other.is_binary),
99 precedence(other.precedence)
101 hi_axiom(&other !=
this);
106 hi_return_on_self_assignment(other);
109 location = other.location;
110 is_binary = other.is_binary;
111 precedence = other.precedence;
119 name = move(other.name);
120 value = move(other.value);
121 location = move(other.location);
122 is_binary = move(other.is_binary);
123 precedence = move(other.precedence);
127 operator bool()
const noexcept
129 return name != tokenizer_name_t::NotAssigned;
132 explicit operator long double()
const
138 throw parse_error(std::format(
"Could not convert token {} to long double", *
this));
142 explicit operator double()
const
148 throw parse_error(std::format(
"Could not convert token {} to double", *
this));
152 explicit operator float()
const
158 throw parse_error(std::format(
"Could not convert token {} to float", *
this));
162 template<std::
integral T>
163 explicit operator T()
const
166 return hi::from_string<T>(value);
169 throw parse_error(std::format(
"Could not convert token {} to {}", *
this,
typeid(T).name()));
178 explicit operator decimal()
const
183 explicit operator std::chrono::year_month_day()
const
185 hilet parts = split(value,
'-');
186 if (parts.size() != 3) {
187 throw parse_error(
"Expect date to be in the format YYYY-MM-DD");
190 hilet year = std::chrono::year{stoi(parts[0])};
191 hilet month = std::chrono::month{narrow_cast<unsigned int>(stoi(parts[1]))};
192 hilet day = std::chrono::day{narrow_cast<unsigned int>(stoi(parts[2]))};
193 return {year, month, day};
199 if (value.
size() > 0) {
209 return lhs << rhs.repr();
212 [[nodiscard]]
friend bool operator==(
token_t const &lhs,
token_t const &rhs)
noexcept
214 return (lhs.name == rhs.name) && (lhs.value == rhs.value);
217 [[nodiscard]]
friend bool operator==(
token_t const &lhs, tokenizer_name_t
const &rhs)
noexcept
219 return lhs.name == rhs;
222 [[nodiscard]]
friend bool operator==(
token_t const &lhs,
const char *rhs)
noexcept
224 return lhs.value == rhs;
229using token_iterator =
typename token_vector::iterator;
235 token_iterator next_token;
237 parse_result() noexcept : found(
false), value(), next_token() {}
239 parse_result(T
const &value, token_iterator next_token) : found(
true), value(value), next_token(next_token) {}
241 operator bool()
const noexcept
246 T
const &operator*()
const noexcept
272parseTokens(std::string_view::const_iterator first, std::string_view::const_iterator last)
noexcept;
276template<
typename CharT>
277struct std::formatter<hi::token_t, CharT> : std::formatter<std::string_view, CharT> {
278 auto format(hi::token_t
const &t,
auto &fc)
280 return std::formatter<std::string_view, CharT>::format(t.repr(), fc);
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Definition decimal.hpp:18
Exception thrown during parsing on an error.
Definition exception.hpp:25
Definition parse_location.hpp:17
Definition tokenizer.hpp:74
Definition tokenizer.hpp:232