24#ifndef MYSQL_STRCONV_DECODE_FLUENT_PARSER_H
25#define MYSQL_STRCONV_DECODE_FLUENT_PARSER_H
115template <Is_format Format_t>
366 std::size_t
max,
auto &obj) {
415 const std::string_view &sv) {
421 const std::string_view &sv) {
447 const std::invocable
auto &
function) {
466 const std::invocable
auto &
function) {
473 const std::invocable
auto &
function) {
474 return do_call(repeat,
function);
507 auto &
object,
const std::string_view &
separator,
514 repeat, allow_repeated_separators, leading_separators,
515 trailing_separators);
521 const std::invocable
auto &
function,
const std::string_view &
separator,
526 std::size_t max_separators{1};
531 auto parse_leading_separator = [&] {
532 switch (leading_separators) {
549 parse_leading_separator();
558 parse_leading_separator();
578 const std::invocable
auto &
function) {
586 auto wrapped_function = [&] {
Fluent API for parsing strings.
Definition: fluent_parser.h:116
Self_t & call_any(const std::invocable auto &function)
Like read_any, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:440
std::ptrdiff_t m_before_last_token
Position before the last parsed token.
Definition: fluent_parser.h:636
Self_t & literal_at_least(std::size_t count, const std::string_view &sv)
Like read_at_least, but skips the given string literal.
Definition: fluent_parser.h:399
Self_t & literal_optional(const std::string_view &sv)
Like read_optional, but skips the given string literal.
Definition: fluent_parser.h:389
Self_t & read_with_format_optional(const auto &format, auto &obj)
If the state is not "closed" read into the given object once; if that fails with parse error,...
Definition: fluent_parser.h:326
Self_t & read_with_format_any(const auto &format, auto &obj)
If the state is not "closed" read repeatedly into the given object until it fails.
Definition: fluent_parser.h:333
std::ptrdiff_t m_backtrack_position
Previous position we parsed to at which it is OK to stop parsing in case a parse error occurs.
Definition: fluent_parser.h:632
Self_t & literal(const std::string_view &sv)
Like read, but skips the given string literal.
Definition: fluent_parser.h:384
Parser & m_parser
Underlying Parser that this Fluent_parser operates on.
Definition: fluent_parser.h:628
Self_t & call_repeated(const Is_repeat auto &repeat, const std::invocable auto &function)
Like read_repeated, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:472
Self_t & next_token_only_if(const std::invocable auto &condition)
If the states is not "closed", suppress the next token if condition evaluates to false.
Definition: fluent_parser.h:198
Self_t & read_repeated_with_separators(auto &object, const std::string_view &separator, const Is_repeat auto &repeat, Allow_repeated_separators allow_repeated_separators, Leading_separators leading_separators, Trailing_separators trailing_separators)
Reads repeatedly into the given object, using the given number of repetitions, and skipping string li...
Definition: fluent_parser.h:506
Self_t & call_at_least(std::size_t count, const std::invocable auto &function)
Like read_at_least, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:446
Fluent_state
Represents the execution state of the fluent parser.
Definition: fluent_parser.h:135
@ closed
All subsequent tokens are suppressed.
@ open
Ready to parse tokens.
@ next_suppressed
next_token_only_if has temporarily closed the token, and none of read/call/literal/end_optional/check...
@ last_suppressed
next_token_only_if has temporarily closed the token, and at least one of read/call/literal/ end_optio...
Self_t & read_with_format(const auto &format, auto &obj)
If the state is not "closed", read into the given object once.
Definition: fluent_parser.h:319
Self_t & read_any(auto &obj)
If the state is not "closed" read repeatedly into the given object until it fails.
Definition: fluent_parser.h:276
Self_t & read_optional(auto &obj)
If the state is not "closed" read into the given object once; if that fails with parse error,...
Definition: fluent_parser.h:269
Self_t & next_token_only_if(bool condition)
Suppress the next token if condition is false.
Definition: fluent_parser.h:177
Self_t & read_exact(std::size_t count, auto &obj)
If the state is not "closed", read into the given object count times.
Definition: fluent_parser.h:295
Self_t & call_exact(std::size_t count, const std::invocable auto &function)
Like read_exact, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:459
Self_t & read_with_format_at_most(const auto &format, std::size_t count, auto &obj)
If the state is not "closed" read repeatedly into the given object until count instances are found or...
Definition: fluent_parser.h:350
Self_t & read_with_format_exact(const auto &format, std::size_t count, auto &obj)
If the state is not "closed" read into the given object count times.
Definition: fluent_parser.h:356
Self_t & read_repeated(const Is_repeat auto &repeat, auto &object)
If the state is not "closed" read with a number of repetitions specified by the given Is_repeat objec...
Definition: fluent_parser.h:311
Fluent_parser(const Fluent_parser &)=delete
Self_t & do_call(const Is_repeat auto &repeat, const std::invocable auto &function)
Invoke the given function with the given number of repetitions.
Definition: fluent_parser.h:577
Self_t & literal_repeated(const Is_repeat auto &repeat, const std::string_view &sv)
Like read_repeated, but skips the given string literal.
Definition: fluent_parser.h:420
Self_t & call_repeated_with_separators(const std::invocable auto &function, const std::string_view &separator, const Is_repeat auto &repeat, Allow_repeated_separators allow_repeated_separators, Leading_separators leading_separators, Trailing_separators trailing_separators)
Like read_repeated_with_separators, but invokes a function instead of reads into an object.
Definition: fluent_parser.h:520
Self_t & literal_range(std::size_t min, std::size_t max, const std::string_view &sv)
Like read_range, but skips the given string literal.
Definition: fluent_parser.h:414
Self_t & call_range(std::size_t min, std::size_t max, const std::invocable auto &function)
Like read_range, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:465
Fluent_parser(Parser &parser, const Format_t &format)
Construct a new object, wrapping the given Parser, and using the given format when reading sub-object...
Definition: fluent_parser.h:160
Self_t & end_optional()
If the state is not "closed" save the current position, and if a subsequent invocation of a member fu...
Definition: fluent_parser.h:237
Self_t & check_prev_token(const std::invocable auto &checker)
If the state is not "closed", invoke the given invocable.
Definition: fluent_parser.h:207
Self_t & read_with_format_repeated(const auto &format, const Is_repeat auto &repeat, auto &object)
If the state is not "closed" read with a number of repetitions specified by the given Is_repeat objec...
Definition: fluent_parser.h:374
Fluent_state m_fluent_state
The open/closed state.
Definition: fluent_parser.h:639
Self_t & read_at_most(std::size_t count, auto &obj)
If the state is not "closed" read repeatedly into the given object until count instances are found or...
Definition: fluent_parser.h:290
Self_t & read_at_least(std::size_t count, auto &obj)
If the state is not "closed" read repeatedly into the given object until it fails.
Definition: fluent_parser.h:282
Self_t & call_unconditionally(const std::invocable auto &function)
Invoke the given invocable regardless o the open/closed state.
Definition: fluent_parser.h:256
Self_t & literal_any(const std::string_view &sv)
Like read_any, but skips the given string literal.
Definition: fluent_parser.h:394
Self_t & read_with_format_range(const auto &format, std::size_t min, std::size_t max, auto &obj)
If the state is not "closed" read repeatedly into the given object until max instances are found or i...
Definition: fluent_parser.h:365
Format_t m_format
Format used to parse objects.
Definition: fluent_parser.h:625
Self_t & read(auto &obj)
If the state is not "closed", read into the given object once.
Definition: fluent_parser.h:264
Self_t & read_with_format_at_least(const auto &format, std::size_t count, auto &obj)
If the state is not "closed" read repeatedly into the given object until it fails.
Definition: fluent_parser.h:341
static constexpr std::ptrdiff_t no_backtrack_position
Value used by m_backtrack_position, indicating that end_optional has not been invoked.
Definition: fluent_parser.h:155
Fluent_parser(Fluent_parser &&) noexcept=default
Self_t & call_optional(const std::invocable auto &function)
Like read_optional, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:434
Self_t & call_at_most(std::size_t count, const std::invocable auto &function)
Like read_at_most, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:453
Self_t & read_range(std::size_t min, std::size_t max, auto &obj)
If the state is not "closed" read repeatedly into the given object until max instances are found or i...
Definition: fluent_parser.h:303
Self_t & call(const std::invocable auto &function)
Like read, but invokes the given function instead of reading an object.
Definition: fluent_parser.h:428
Self_t & literal_exact(std::size_t count, const std::string_view &sv)
Like read_exact, but skips the given string literal.
Definition: fluent_parser.h:409
Self_t & literal_at_most(std::size_t count, const std::string_view &sv)
Like read_at_most, but skips the given string literal.
Definition: fluent_parser.h:404
Object used to parse strings.
Definition: parser.h:69
Return_status_t call(const std::invocable auto &func)
Parse using the given function, using empty parse options.
Definition: parser.h:377
Return_status_t read(const Is_parse_options auto &opt, Object_t &obj)
Parse into the given object.
Definition: parser.h:225
void skip(const Is_parse_options_optional auto &opt, const std::string_view &sv)
Skip occurrences of the literal string sv, if found.
Definition: parser.h:125
static Repeat range(Int_t min_arg, Int_t max_arg)
Return a Repeat object representing a range of at least min_arg and at most max_arg repetitions.
Definition: repeat.h:157
static Repeat exact(std::integral auto count)
Return a Repeat object representing exactly the given number of repetitions.
Definition: repeat.h:163
static Repeat at_least(Int_t min_arg)
Return a Repeat object representing min_arg or more repetitions (bounded only by std::numeric_limits)
Definition: repeat.h:145
static Repeat one()
Return a Repeat object representing exactly one repetition.
Definition: repeat.h:168
std::size_t int_pos() const
Return the current position as an integer.
Definition: parse_position.h:107
void set_int_pos(std::size_t int_pos_arg)
Set the position to the given one.
Definition: parse_position.h:101
void revert_parse_error_to_ok() noexcept
Definition: parse_result.h:47
void update_parse_error_pos(std::size_t position)
Update the position of a parse error to the given position.
Definition: parse_result.h:58
bool is_parse_error() const
Return true if a parse error occurred.
Definition: parse_result.h:126
bool is_ok() const
Return true if the last operation succeeded, i.e., either a full match was requested and an object wa...
Definition: parse_result.h:97
static Repeat_optional at_most(std::integral auto max_arg)
Return a Repeat object representing between 0 and min_arg repetitions.
Definition: repeat.h:58
static Repeat_optional any()
Return a Repeat_optional object representing any number of repetitions from 0 and up (bounded only by...
Definition: repeat.h:64
static Repeat_optional optional()
Return a Repeat_optional object representing zero or one instances.
Definition: repeat.h:52
True if Test is either Repeat or Repeat_optional.
Definition: repeat.h:180
static const char separator
Definition: item_func.cc:4532
static int count
Definition: myisam_ftdump.cc:45
ValueType max(X &&first)
Definition: gtid.h:103
Definition: gtid_binary_format.h:41
Allow_repeated_separators
Used in read_repeated_with_separators: if yes, the separator may be repeated more than once whenever ...
Definition: fluent_parser.h:46
Leading_separators
Used in read_repeated_with_separators: if yes, the string must start with a separator; if no,...
Definition: fluent_parser.h:50
Trailing_separators
Used in read_repeated_with_separators: if yes, the string must end with a separator; if no,...
Definition: fluent_parser.h:54
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76