132 enum class state_type : uint8_t {
140 dec_integer_found_e_id,
141 dec_integer_found_E_id,
146 dec_float_found_e_id,
147 dec_float_found_E_id,
157 sqstring_literal_quote,
158 sqstring_literal_escape,
160 dqstring_literal_quote,
161 dqstring_literal_escape,
163 bqstring_literal_quote,
164 bqstring_literal_escape,
167 block_comment_found_star,
168 block_comment_found_dash,
169 block_comment_found_dash_dash,
170 block_comment_found_dash_dash_fin0,
189 struct command_type {
192 state_type next_state = state_type::idle;
196 token::kind_type emit_token = token::none;
200 char char_to_capture =
'\0';
204 uint8_t clear : 1 = 0;
208 uint8_t advance : 1 = 0;
212 uint8_t assigned : 1 = 0;
216 uint8_t advance_line : 1 = 0;
220 uint8_t advance_tab : 1 = 0;
225 struct advance_tag {};
226 struct capture_tag {};
228 class excluding_tag {
230 constexpr excluding_tag(
std::string exclusions) noexcept : _exclusions(
std::move(exclusions)) {}
232 [[nodiscard]]
constexpr bool contains(
char c)
const noexcept
234 return _exclusions.
find(c) != _exclusions.npos;
243 constexpr static auto capture = capture_tag{};
247 constexpr static auto advance = advance_tag{};
251 constexpr static auto clear = clear_tag{};
255 constexpr static auto any = any_tag{};
258 [[nodiscard]]
constexpr excluding_tag excluding(
char const (&exclusions)[N])
noexcept
260 return excluding_tag{
std::string(exclusions, N - 1)};
263 template<
typename First,
typename... Args>
264 [[nodiscard]]
constexpr static bool _has_advance_tag_argument()
noexcept
266 if constexpr (std::is_same_v<First, advance_tag>) {
268 }
else if constexpr (
sizeof...(Args) == 0) {
271 return _has_advance_tag_argument<Args...>();
275 template<
typename... Args>
276 [[nodiscard]]
constexpr static bool has_advance_tag_argument()
noexcept
278 if constexpr (
sizeof...(Args) == 0) {
281 return _has_advance_tag_argument<Args...>();
286 constexpr lexer() noexcept : _transition_table()
288 using enum state_type;
290 add(idle,
'/', found_slash, advance, capture);
291 add(idle,
'<', found_lt, advance, capture);
292 add(idle,
'#', found_hash, advance, capture);
293 add(idle,
'.', found_dot, advance, capture);
294 add(idle,
'=', found_eq, advance, capture);
295 add(idle,
':', found_colon, advance, capture);
297 add(found_slash, any, idle, token::other);
298 add(found_lt, any, idle, token::other);
299 add(found_hash, any, idle, token::other);
300 add(found_dot, any, idle, token::other);
301 add(found_eq, any, idle, token::other);
302 add(found_colon, any, idle, token::other);
305 add_string_literals();
308 add_number_literals();
314 add_ini_assignment();
316 add(idle,
"~!@$%^&*()-+[]{}\\|,>?", idle, token::other, capture, advance);
319 for (uint8_t i = 0; i != 128; ++i) {
320 auto& command = get_command(idle, char_cast<char>(i));
321 if (not command.assigned) {
322 command.assigned = 1;
325 command.char_to_capture = char_cast<char>(i);
326 command.emit_token = token::error_unexepected_character;
327 command.next_state = idle;
332 [[nodiscard]]
constexpr command_type& get_command(state_type from,
char c)
noexcept
334 return _transition_table[to_underlying(from) * 128_uz + char_cast<size_t>(c)];
337 [[nodiscard]]
constexpr command_type
const& get_command(state_type from,
char c)
const noexcept
339 return _transition_table[to_underlying(from) * 128_uz + char_cast<size_t>(c)];
342 template<
typename It, std::sentinel_for<It> ItEnd>
352 _lexer(
lexer), _first(first), _last(last), _it(first)
356 _token.kind = parse_token();
357 }
while (Config.filter_white_space and _token.kind == token::ws);
360 [[nodiscard]]
constexpr reference operator*()
const noexcept
365 [[nodiscard]]
constexpr pointer operator&()
const noexcept
370 constexpr iterator& operator++()
noexcept
372 hi_axiom(*
this != std::default_sentinel);
374 _token.kind = parse_token();
375 }
while (Config.filter_white_space and _token.kind == token::ws);
379 constexpr void operator++(
int)
noexcept
384 [[nodiscard]]
constexpr bool operator==(std::default_sentinel_t)
const noexcept
386 return _token.kind == token::none;
396 state_type _state = state_type::idle;
398 size_t _column_nr = 0;
404 constexpr void clear()
noexcept
406 _token.capture.
clear();
413 constexpr void capture(
char code_point)
noexcept
422 constexpr void capture(
char32_t code_point)
noexcept
427 char_map<
"utf-8">{}.write(code_point, out_it);
430 constexpr void advance_counters()
noexcept
432 if (_cp ==
'\n' or _cp ==
'\v' or _cp ==
'\f' or _cp ==
'\x85' or _cp == U
'\u2028' or _cp == U
'\u2029') {
434 }
else if (_cp ==
'\t') {
447 [[nodiscard]]
constexpr char32_t advance()
noexcept
453 hilet[code_point, valid] =
char_map<
"utf-8">{}.read(_it, _last);
457 [[nodiscard]]
constexpr token::kind_type parse_token_unicode_identifier()
noexcept
459 switch (ucd_get_lexical_class(_cp & 0x1f'ffff)) {
460 case unicode_lexical_class::id_start:
461 case unicode_lexical_class::id_continue:
468 if (Config.minus_in_identifier and _cp ==
'-') {
475 _state = state_type::idle;
481 [[nodiscard]]
constexpr token::kind_type parse_token_unicode_line_comment()
noexcept
483 hilet cp_ = _cp & 0x1f'ffff;
484 if (cp_ == U
'\u0085' or cp_ == U
'\u2028' or cp_ == U
'\u2029') {
485 _state = state_type::idle;
488 return token::lcomment;
498 [[nodiscard]]
constexpr token::kind_type parse_token_unicode_white_space()
noexcept
500 if (ucd_get_lexical_class(_cp & 0x1f'ffff) == unicode_lexical_class::white_space) {
507 _state = state_type::idle;
512 [[nodiscard]]
constexpr token::kind_type parse_token_unicode_idle()
noexcept
514 switch (ucd_get_lexical_class(_cp & 0x1f'ffff)) {
515 case unicode_lexical_class::id_start:
516 _state = state_type::identifier;
522 case unicode_lexical_class::white_space:
523 _state = state_type::white_space;
529 case unicode_lexical_class::syntax:
530 _state = state_type::idle;
540 return token::error_unexepected_character;
544 [[nodiscard]] hi_no_inline
constexpr token::kind_type parse_token_unicode()
noexcept
546 using enum state_type;
551 return parse_token_unicode_idle();
554 return parse_token_unicode_white_space();
557 return parse_token_unicode_line_comment();
560 return parse_token_unicode_identifier();
562 case dqstring_literal:
563 case sqstring_literal:
564 case bqstring_literal:
573 if (_cp == U
'\u0085' or _cp == U
'\u2028' or _cp == U
'\u2029') {
585 return process_command();
589 [[nodiscard]]
constexpr token::kind_type process_command(
char c =
'\0')
noexcept
591 hilet command = _lexer->get_command(_state, c);
592 _state = command.next_state;
598 if (command.char_to_capture !=
'\0') {
599 capture(command.char_to_capture);
602 if (command.advance) {
603 if (command.advance_line) {
606 }
else if (command.advance_tab) {
616 return command.emit_token;
619 [[nodiscard]]
constexpr token::kind_type parse_token()
noexcept
621 _token.line_nr = _line_nr;
622 _token.column_nr = _column_nr;
625 while (_cp <= 0x7fff'ffff) {
627 if (
auto token_kind = process_command(char_cast<char>(_cp)); token_kind != token::none) {
632 auto emit_token = parse_token_unicode();
633 if (emit_token != token::none) {
640 while (_state != state_type::idle) {
641 if (
auto token_kind = process_command(); token_kind != token::none) {
652 static_assert(std::movable<iterator<std::string::iterator, std::string::iterator>>);
653 static_assert(std::is_same_v<std::iterator_traits<iterator<std::string::iterator, std::string::iterator>>::value_type,
token>);
654 static_assert(std::input_or_output_iterator<iterator<std::string::iterator, std::string::iterator>>);
655 static_assert(std::weakly_incrementable<iterator<std::string::iterator, std::string::iterator>>);
663 template<
typename It, std::sentinel_for<It> ItEnd>
674 [[nodiscard]]
constexpr auto parse(std::string_view str)
const noexcept
676 return parse(str.begin(), str.end());
683 using transition_table_type =
std::array<command_type, to_underlying(state_type::_size) * 128>;
685 transition_table_type _transition_table;
687 constexpr void add_string_literal(
689 token::kind_type string_token,
690 state_type string_literal,
691 state_type string_literal_quote,
692 state_type string_literal_escape)
noexcept
694 using enum state_type;
696 add(idle, c, string_literal, advance);
697 add(string_literal, any, idle, token::error_incomplete_string);
698 for (uint8_t i = 1; i != 128; ++i) {
699 if (char_cast<char>(i) != c and char_cast<char>(i) !=
'\\') {
700 add(string_literal, char_cast<char>(i), string_literal, advance, capture);
704 if constexpr (Config.escape_by_quote_doubling) {
706 add(string_literal, c, string_literal_quote, advance);
708 add(string_literal_quote, any, idle, string_token);
710 add(string_literal_quote, c, string_literal, advance, capture);
713 add(string_literal, c, idle, advance, string_token);
717 add(string_literal,
'\\', string_literal_escape, advance, capture);
718 add(string_literal_escape, any, idle, token::error_incomplete_string);
719 for (uint8_t i = 1; i != 128; ++i) {
720 add(string_literal_escape, char_cast<char>(i), string_literal, advance, capture);
724 constexpr void add_string_literals() noexcept
726 using enum state_type;
728 if constexpr (Config.has_single_quote_string_literal) {
729 add_string_literal(
'\'', token::sstr, sqstring_literal, sqstring_literal_quote, sqstring_literal_escape);
731 add(idle,
'\'', idle, token::other, advance, capture);
734 if constexpr (Config.has_double_quote_string_literal) {
735 add_string_literal(
'"', token::dstr, dqstring_literal, dqstring_literal_quote, dqstring_literal_escape);
737 add(idle,
'"', idle, token::other, advance, capture);
740 if constexpr (Config.has_back_quote_string_literal) {
741 add_string_literal(
'`', token::bstr, bqstring_literal, bqstring_literal_quote, bqstring_literal_escape);
743 add(idle,
'`', idle, token::other, advance, capture);
747 constexpr void add_number_literals() noexcept
749 using enum state_type;
751 add(idle,
"0", zero, advance, capture);
752 add(idle,
"123456789", dec_integer, advance, capture);
754 add(zero, any, idle, token::integer);
755 add(zero,
".", dec_float, advance, capture);
756 add(zero,
"bB", bin_integer, advance, capture);
757 add(zero,
"oO", oct_integer, advance, capture);
758 add(zero,
"dD", dec_integer, advance, capture);
759 add(zero,
"xX", hex_integer, advance, capture);
761 if constexpr (Config.zero_starts_octal) {
762 add(zero,
"01234567", oct_integer, advance, capture);
763 add(zero,
"89", idle, token::error_invalid_digit);
765 add(zero,
"0123456789", dec_integer, advance, capture);
769 add(bin_integer, any, idle, token::integer);
770 add(bin_integer,
"01", bin_integer, advance, capture);
771 add(bin_integer,
"23456789", idle, token::error_invalid_digit);
774 add(oct_integer, any, idle, token::integer);
775 add(oct_integer,
"01234567", oct_integer, advance, capture);
776 add(oct_integer,
"89", idle, token::error_invalid_digit);
779 add(dec_integer, any, idle, token::integer);
780 add(dec_integer,
"0123456789", dec_integer, advance, capture);
781 add(dec_integer,
".", dec_float, advance, capture);
782 add(dec_integer,
"e", dec_integer_found_e, advance);
783 add(dec_integer,
"E", dec_integer_found_E, advance);
784 add(dec_integer_found_e, any, dec_integer_found_e_id, token::integer);
785 add(dec_integer_found_E, any, dec_integer_found_E_id, token::integer);
786 add(dec_integer_found_e_id, any, identifier,
'e');
787 add(dec_integer_found_E_id, any, identifier,
'E');
788 add(dec_integer_found_e,
"+-0123456789", dec_sign_exponent,
'e');
789 add(dec_integer_found_E,
"+-0123456789", dec_sign_exponent,
'E');
792 add(hex_integer, any, idle, token::integer);
793 add(hex_integer,
"0123456789abcdefABCDEF", hex_integer, advance, capture);
794 add(hex_integer,
".", hex_float, advance, capture);
795 add(hex_integer,
"pP", hex_sign_exponent, advance, capture);
798 add(found_dot,
"0123456789eE", dec_float);
799 add(dec_float, any, idle, token::real);
800 add(dec_float,
"0123456789", dec_float, advance, capture);
801 add(dec_float,
"e", dec_float_found_e, advance);
802 add(dec_float,
"E", dec_float_found_E, advance);
803 add(dec_float_found_e, any, dec_float_found_e_id, token::real);
804 add(dec_float_found_E, any, dec_float_found_E_id, token::real);
805 add(dec_float_found_e_id, any, identifier,
'e');
806 add(dec_float_found_E_id, any, identifier,
'E');
807 add(dec_float_found_e,
"+-0123456789", dec_sign_exponent,
'e');
808 add(dec_float_found_E,
"+-0123456789", dec_sign_exponent,
'E');
810 add(dec_sign_exponent, any, idle, token::error_incomplete_exponent);
811 add(dec_sign_exponent,
"0123456789", dec_exponent_more, advance, capture);
812 add(dec_sign_exponent,
"+-", dec_exponent, advance, capture);
813 add(dec_exponent, any, idle, token::error_incomplete_exponent);
814 add(dec_exponent,
"0123456789", dec_exponent_more, advance, capture);
815 add(dec_exponent_more, any, idle, token::real);
816 add(dec_exponent_more,
"0123456789", dec_exponent_more, advance, capture);
819 add(hex_float, any, idle, token::real);
820 add(hex_float,
"0123456789abcdefABCDEF", hex_float, advance, capture);
821 add(hex_float,
"pP", hex_sign_exponent, advance, capture);
822 add(hex_sign_exponent, any, idle, token::error_incomplete_exponent);
823 add(hex_sign_exponent,
"0123456789abcdefABCDEF", hex_exponent_more, advance, capture);
824 add(hex_sign_exponent,
"+-", hex_exponent, advance, capture);
825 add(hex_exponent, any, idle, token::error_incomplete_exponent);
826 add(hex_exponent,
"0123456789abcdefABCDEF", hex_exponent_more, advance, capture);
827 add(hex_exponent_more, any, idle, token::real);
828 add(hex_exponent_more,
"0123456789abcdefABCDEF", hex_exponent_more, advance, capture);
830 if constexpr (Config.digit_separator !=
'\0') {
831 if constexpr (Config.zero_starts_octal) {
832 add(zero, Config.digit_separator, oct_integer, advance);
834 add(zero, Config.digit_separator, dec_integer, advance);
836 add(bin_integer, Config.digit_separator, bin_integer, advance);
837 add(oct_integer, Config.digit_separator, oct_integer, advance);
838 add(dec_integer, Config.digit_separator, dec_integer, advance);
839 add(hex_integer, Config.digit_separator, hex_integer, advance);
840 add(dec_float, Config.digit_separator, dec_integer, advance);
841 add(hex_float, Config.digit_separator, dec_integer, advance);
842 add(dec_exponent, Config.digit_separator, dec_integer, advance);
843 add(hex_exponent, Config.digit_separator, dec_integer, advance);
847 constexpr void add_color_literal() noexcept
849 using enum state_type;
851 if constexpr (Config.has_color_literal) {
852 add(found_hash,
"0123456789abcdefABCDEF", color_literal, clear, capture, advance);
853 add(color_literal, any, idle, token::color);
854 add(color_literal,
"0123456789abcdefABCDEF", color_literal, advance, capture);
858 constexpr void add_ini_assignment() noexcept
860 using enum state_type;
862 if constexpr (Config.equal_is_ini_assignment) {
864 add(found_eq,
" \t", found_eq, advance);
865 add(found_eq,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", ini_string, token::other);
868 if constexpr (Config.colon_is_ini_assignment) {
870 add(found_colon,
" \t", found_colon, advance);
871 add(found_colon,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", ini_string, token::other);
874 add(ini_string, any, idle, token::istr);
875 add(ini_string, excluding(
"\n\v\f\r\0"), ini_string, advance, capture);
876 add(ini_string,
'\r', ini_string, advance);
879 constexpr void add_comments() noexcept
881 using enum state_type;
883 if constexpr (Config.has_double_slash_line_comment) {
884 add(found_slash,
'/', line_comment, clear, advance);
887 if constexpr (Config.has_semicolon_line_comment) {
888 add(idle,
';', line_comment, advance);
890 add(idle,
';', idle, token::other, capture, advance);
893 if constexpr (Config.has_hash_line_comment) {
894 add(found_hash, excluding(
"\0"), line_comment, clear, advance, capture);
897 if constexpr (Config.has_c_block_comment) {
898 add(found_slash,
'*', block_comment, advance, clear);
901 if constexpr (Config.has_sgml_block_comment) {
902 add(found_lt,
'!', found_lt_bang, advance);
903 add(found_lt_bang, any, idle, token::error_after_lt_bang);
904 add(found_lt_bang,
'-', found_lt_bang_dash, advance);
905 add(found_lt_bang_dash, any, idle, token::error_after_lt_bang);
906 add(found_lt_bang_dash,
'-', block_comment, advance);
909 add(line_comment, any, idle, token::lcomment);
910 add(line_comment, excluding(
"\r\n\f\v\0"), line_comment, advance, capture);
912 add(line_comment,
'\r', line_comment, advance);
913 add(line_comment,
"\n\f\v", idle, advance, token::lcomment);
915 add(block_comment, any, idle, token::error_incomplete_comment);
917 static_assert(Config.has_c_block_comment == 0 or Config.has_sgml_block_comment == 0);
919 if constexpr (Config.has_c_block_comment) {
920 add(block_comment, excluding(
"*\0"), block_comment, advance, capture);
921 add(block_comment,
'*', block_comment_found_star, advance);
922 add(block_comment_found_star, any, block_comment,
'*');
923 add(block_comment_found_star,
'/', idle, advance, token::bcomment);
925 }
else if constexpr (Config.has_sgml_block_comment) {
926 add(block_comment, excluding(
"-\0"), block_comment, advance, capture);
927 add(block_comment,
'-', block_comment_found_dash, advance);
928 add(block_comment_found_dash, any, block_comment,
'-');
929 add(block_comment_found_dash,
'-', block_comment_found_dash_dash, advance);
930 add(block_comment_found_dash_dash, any, block_comment_found_dash_dash_fin0,
'-');
931 add(block_comment_found_dash_dash_fin0, any, block_comment,
'-');
932 add(block_comment_found_dash_dash,
'>', idle, advance, token::bcomment);
936 constexpr void add_white_space() noexcept
938 using enum state_type;
940 add(idle,
'\r', white_space, advance);
941 add(idle,
" \n\t\v\f", white_space, advance, capture);
942 add(white_space, any, idle, token::ws);
943 add(white_space,
'\r', white_space, advance);
944 add(white_space,
" \n\t\v\f", white_space, advance, capture);
947 constexpr void add_identifier() noexcept
949 using enum state_type;
951 add(idle,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_", identifier, advance, capture);
952 add(identifier, any, idle, token::id);
953 add(identifier,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789", identifier, advance, capture);
954 if constexpr (Config.minus_in_identifier) {
955 add(identifier,
'-', identifier, advance, capture);
959 constexpr command_type& _add(state_type from,
char c, state_type to)
noexcept
961 auto& command = get_command(from, c);
962 command.next_state = to;
963 command.char_to_capture =
'\0';
965 command.advance_line = 0;
966 command.advance_tab = 0;
968 command.emit_token = token::none;
986 template<
typename First,
typename... Args>
987 constexpr command_type& _add(state_type from,
char c, state_type to, First first, Args
const&...args)
noexcept
989 auto& command = _add(from, c, to, args...);
990 if constexpr (std::is_same_v<First, token::kind_type>) {
991 command.emit_token = first;
993 }
else if constexpr (std::is_same_v<First, advance_tag>) {
995 if (c ==
'\n' or c ==
'\v' or c ==
'\f') {
996 command.advance_line = 1;
997 }
else if (c ==
'\t') {
998 command.advance_tab = 1;
1001 }
else if constexpr (std::is_same_v<First, clear_tag>) {
1004 }
else if constexpr (std::is_same_v<First, capture_tag>) {
1005 command.char_to_capture = c;
1007 }
else if constexpr (std::is_same_v<First, char>) {
1008 command.char_to_capture = first;
1017 template<
typename... Args>
1018 constexpr void add(state_type from,
char c, state_type to, Args
const&...args)
noexcept
1020 auto& command = _add(from, c, to, args...);
1021 hi_assert(not command.assigned,
"Overwriting an already assigned state:char combination.");
1022 command.assigned =
true;
1025 template<
typename... Args>
1026 constexpr void add(state_type from, std::string_view str, state_type to, Args
const&...args)
noexcept
1028 for (
auto c : str) {
1029 auto& command = _add(from, c, to, args...);
1030 hi_assert(not command.assigned,
"Overwriting an already assigned state:char combination.");
1031 command.assigned =
true;
1035 template<
typename... Args>
1036 constexpr void add(state_type from, any_tag, state_type to, Args
const&...args)
noexcept
1038 static_assert(not has_advance_tag_argument<Args...>(),
"any should not advance");
1040 for (uint8_t c = 0; c != 128; ++c) {
1041 hilet& command = _add(from, char_cast<char>(c), to, args...);
1042 hi_assert(not command.assigned,
"any should be added first to a state");
1046 template<
typename... Args>
1047 constexpr void add(state_type from, excluding_tag
const& exclusions, state_type to, Args
const&...args)
noexcept
1049 for (uint8_t c = 0; c != 128; ++c) {
1050 if (not exclusions.contains(char_cast<char>(c))) {
1051 auto& command = _add(from, char_cast<char>(c), to, args...);
1052 hi_assert(not command.assigned,
"Overwriting an already assigned state:char combination.");
1053 command.assigned =
true;