10 #include <magic_enum/magic_enum.hpp> 12 #include "base/type_traits.h" 20 view(std::string_view input) : input_(input) {}
22 std::string_view substr()
const {
return input_.substr(pos_); }
24 std::string_view substr(std::size_t len)
const {
25 return input_.substr(pos_, len);
28 std::string_view substr(std::size_t
start, std::size_t len)
const {
29 return input_.substr(start, len);
32 std::string_view consume(std::size_t len) {
33 auto result = input_.substr(pos_, len);
38 std::string_view input_;
42 template <
class TokenType, ctll::fixed_
string Regex>
class tokenizer {
45 using Token = TokenType;
47 static constexpr
auto TokenTypeSize = magic_enum::enum_count<TokenType>();
51 tokenizer(
view &&input) : view_(input) {}
54 std::optional<TokenType> next() {
58 if (
auto match = ctre::multiline_starts_with<Regex>(view_.substr())) {
60 mpl::for_sequence(std::make_index_sequence<TokenTypeSize>{}, [&](
auto i) {
61 if (match.template get<i + 1>()) {
62 result = magic_enum::enum_value<TokenType>(i);
73 bool isToken(TokenType type) {
75 if (
auto match = ctre::multiline_starts_with<Regex>(view_.substr())) {
76 auto type_integer = magic_enum::enum_integer(type);
77 mpl::for_sequence(std::make_index_sequence<TokenTypeSize>{}, [&](
auto i) {
78 if (i == type_integer) {
79 if (match.template get<i + 1>()) {
88 [[deprecated(
"Don't use this function anymore")]] std::string_view consume() {
89 if (
auto match = ctre::multiline_starts_with<Regex>(view_.substr())) {
90 view_.consume(match.size());
91 return view_.consume(match.size());
96 std::optional<std::string_view> consume(TokenType type) {
99 std::string_view str =
""sv;
101 if (
auto match = ctre::multiline_starts_with<Regex>(view_.substr())) {
103 auto type_integer = magic_enum::enum_integer(type);
105 mpl::for_sequence(std::make_index_sequence<TokenTypeSize>{}, [&](
auto i) {
106 if (i == type_integer) {
107 if (
auto capture = match.template get<i + 1>()) {
108 str = view_.consume(capture.size());
120 const view &getView()
const {
return view_; }
127 constexpr
auto jsonTokenRegex = ctll::fixed_string{
128 "(\\s+)|(\\u007b)|(\\u007d)|(\\u005b)|(\\u005d)|(:)|(,)|(\")|" 129 "(true)|(false)|(null)"};
131 enum class jsonTokenType {
148 constexpr
auto stringTokenRegex =
149 ctll::fixed_string{
"(\")" 150 "|(\\u005Cu[0-9a-fA-F]{4})" 151 "|([^\"\\u005C\\u0000-\\u001f\\u007F]+)" 152 "|(\\u005C[bfnrt/\\\"])"};
154 enum class stringTokenType { DOUBLE_QUOTE, HEX, CHARS, ESCAPE };
158 constexpr
auto numberTokenRegex = ctll::fixed_string{
"([+\\-])" 163 enum class numberTokenType { SIGN, DIGIT, DOT, EXP };
Definition: tokenizer.h:18
Definition: tokenizer.h:42
Definition: simple_fsm.cpp:15