Header lexy/action/validate.hpp

Validate input.

Error callback

template <typename T>
concept error-callback = callback<T> || sink<T>;

The callback types used for error reporting.

If it is a sink, it must not return void. Its sink callback must have the signature

template <input Input, typename Tag>
void operator()(const lexy::error_context<Input>& context,
                const lexy::error_for<Input, Tag>& error);

It will be invoked for each lexy::error  object with the corresponding lexy::error_context ; errors with generic tag are type-erased prior to invocation. The final result of the sink, the error list, is then made available.

If it is a callback, it must return void with the same signature. lexy::collect  is used to turn into a sink that counts the invocations of the callback. As such, it’s final value is a std::size_t.

Tip
Use the extension lexy_ext::report_error of lexy_ext/report_error.hpp for a simple error callback that formats the error nicely and prints it to stderr.
Tip
Use the other overload of lexy::collect  to turn a non-void returning callback into a sink that collects all values into the specified container.

Class lexy::validate_result

lexy/action/validate.hpp
namespace lexy
{
    template <typename ErrorCallback>
    class validate_result
    {
    public:
        using error_callback = ErrorCallback;
        using error_type     = see-below;

        //=== status ===//
        constexpr explicit operator bool() const noexcept
        {
            return is_success();
        }

        constexpr bool is_success()         const noexcept;
        constexpr bool is_error()           const noexcept;
        constexpr bool is_recovered_error() const noexcept;
        constexpr bool is_fatal_error()     const noexcept;

        //=== error list ===//
        constexpr std::size_t error_count() const noexcept;

        constexpr const error_type& errors() const& noexcept;
        constexpr error_type&&      errors() &&     noexcept;
    };
}

It stores the status of the action and the final error list of the error callback, which has type error_type.

Status

lexy/action/validate.hpp
constexpr explicit operator bool() const noexcept
{
    return is_success();
}

constexpr bool is_success()         const noexcept; (1)
constexpr bool is_error()           const noexcept; (2)
constexpr bool is_recovered_error() const noexcept; (3)
constexpr bool is_fatal_error()     const noexcept; (4)
  1. Returns true if parsing succeeded without raising any errors, false otherwise.

  2. Returns true if at least one error occurred during parsing, false otherwise.

  3. Returns true if at least one error occurred during parsing but it could recover from all of them, false otherwise.

  4. Returns true if at least one error occurred during parsing that could not be recovered, false otherwise.

Note
is_error() is equivalent to is_recovered_error() || is_fatal_error(), and is_success() is !is_error().

Error list

lexy/action/validate.hpp
constexpr std::size_t error_count() const noexcept; (1)

constexpr const error_type& errors() const& noexcept; (2)
constexpr error_type&&      errors() &&     noexcept; (2)
  1. If error_type is std::size_t, returns errors(). Otherwise, returns errors().size().

  2. The final value of the ErrorCallback, unchanged.

If is_success() == true, error_count() == 0 and errors() returns the result of the sink callback that is finished without ever invoking it.

Action lexy::validate

lexy/action/validate.hpp
namespace lexy
{
    template <typename State, typename Input, typename ErrorCallback>
    struct validate_action;

    template <production Production>
    constexpr auto validate(const input auto& input,
                            error-callback auto error_callback)
      -> validate_result<decltype(error_callback)>;

    template <production Production, typename ParseState>
    constexpr auto validate(const input auto& input,
                            ParseState& state,
                            error-callback auto error_callback)
      -> validate_result<decltype(error_callback)>;
    template <production Production, typename ParseState>
    constexpr auto validate(const input auto& input,
                            const ParseState& state,
                            error-callback auto error_callback)
      -> validate_result<decltype(error_callback)>;
}

An action that validates input according to Production.

It parses Production on input. All values produced during parsing are discarded; all errors raised are forwarded to the error callback. Returns the lexy::validate_result  containing the result of the error callback.

Note
Production does not need to match the entire input to succeed. Use lexy::dsl::eof  if it should fail when it didn’t consume the entire input.

See also