3 #include "fly/types/string/concepts.hpp"
4 #include "fly/types/string/detail/classifier.hpp"
5 #include "fly/types/string/detail/traits.hpp"
6 #include "fly/types/string/literals.hpp"
13 template <StandardCharacter CharType>
16 using Lexer = BasicLexer<char>;
17 using WLexer = BasicLexer<wchar_t>;
18 using Lexer8 = BasicLexer<char8_t>;
19 using Lexer16 = BasicLexer<char16_t>;
20 using Lexer32 = BasicLexer<char32_t>;
29 template <StandardCharacter CharType>
34 using view_type =
typename traits::view_type;
44 template <std::
size_t N>
45 constexpr
explicit BasicLexer(
const CharType (&literals)[N]) noexcept;
57 constexpr view_type
view()
const;
62 constexpr std::size_t
position()
const;
84 constexpr std::optional<CharType>
peek(std::size_t offset = 0);
92 constexpr std::optional<CharType>
consume();
135 template <
typename Condition>
136 constexpr std::optional<CharType>
consume_if(Condition condition);
138 static constexpr
const auto s_zero = FLY_CHR(CharType,
'0');
139 static constexpr
const auto s_upper_a = FLY_CHR(CharType,
'A');
140 static constexpr
const auto s_upper_f = FLY_CHR(CharType,
'F');
141 static constexpr
const auto s_lower_a = FLY_CHR(CharType,
'a');
142 static constexpr
const auto s_lower_f = FLY_CHR(CharType,
'f');
144 const std::size_t m_size;
145 const view_type m_view;
147 std::size_t m_index {0};
151 template <StandardCharacter CharType>
152 template <std::
size_t N>
154 m_size(classifier::size(literals)),
155 m_view(literals, m_size)
160 template <StandardCharacter CharType>
163 m_view(std::move(view))
168 template <StandardCharacter CharType>
175 template <StandardCharacter CharType>
182 template <StandardCharacter CharType>
189 template <StandardCharacter CharType>
192 if ((m_index + offset) >= m_size)
197 return *(m_view.data() + m_index + offset);
201 template <StandardCharacter CharType>
204 if (m_index >= m_size)
209 return *(m_view.data() + m_index++);
213 template <StandardCharacter CharType>
216 if (
auto next = peek(); next && (next.value() == ch))
226 template <StandardCharacter CharType>
229 bool parsed_number =
false;
230 std::uintmax_t number = 0;
232 while (
auto ch = consume_if(classifier::is_digit))
234 parsed_number =
true;
237 number +=
static_cast<std::uintmax_t
>(ch.value() - s_zero);
240 return parsed_number ? std::optional<std::uintmax_t>(number) : std::nullopt;
244 template <StandardCharacter CharType>
247 bool parsed_number =
false;
248 std::uintmax_t number = 0;
250 while (
auto ch = consume_if(classifier::is_x_digit))
252 parsed_number =
true;
255 if ((ch.value() >= s_upper_a) && (ch.value() <= s_upper_f))
257 number +=
static_cast<std::uintmax_t
>(ch.value()) - s_upper_a + 0xA;
259 else if ((ch.value() >= s_lower_a) && (ch.value() <= s_lower_f))
261 number +=
static_cast<std::uintmax_t
>(ch.value()) - s_lower_a + 0xa;
265 number +=
static_cast<std::uintmax_t
>(ch.value()) - s_zero;
269 return parsed_number ? std::optional<std::uintmax_t>(number) : std::nullopt;
273 template <StandardCharacter CharType>
274 template <
typename Condition>
277 if (
auto next = peek(); next && condition(next.value()))
constexpr view_type view() const
Definition: lexer.hpp:169
constexpr BasicLexer(const CharType(&literals)[N]) noexcept
Definition: lexer.hpp:153
constexpr std::optional< std::uintmax_t > consume_number()
Definition: lexer.hpp:227
constexpr void set_position(std::size_t position)
Definition: lexer.hpp:183
constexpr std::optional< std::uintmax_t > consume_hex_number()
Definition: lexer.hpp:245
constexpr std::size_t position() const
Definition: lexer.hpp:176
constexpr std::optional< CharType > consume()
Definition: lexer.hpp:202
constexpr bool consume_if(CharType ch)
Definition: lexer.hpp:214
constexpr std::optional< CharType > peek(std::size_t offset=0)
Definition: lexer.hpp:190
Definition: classifier.hpp:19
Definition: traits.hpp:18