Header lexy/dsl/capture.hpp

Rule lexy::dsl::capture

lexy/dsl/capture.hpp
namespace lexy::dsl
{
    constexpr rule        auto capture(rule        auto rule);
    constexpr branch-rule auto capture(branch-rule auto rule);
}

capture is a rule that parses rule capturing everything it has consumed as a value.

(Branch) Parsing

Parses rule unchanged.

Errors

All errors raised by rule. The rule then fails if rule has failed.

Values

A lexy::lexeme whose range covers everything consumed by rule, followed by all values produced by rule.

Example 1. Get a single code point
// The type of a lexy::lexeme depends on the input.
using lexeme = lexy_ext::compiler_explorer_lexeme;

struct production
{
    static constexpr auto rule = dsl::capture(dsl::code_point);

    // Same as `lexy::as_string<std::string>`.
    static constexpr auto value = lexy::callback<std::string>(
        [](lexeme lex) { return std::string(lex.begin(), lex.end()); });
};
Caution
If a token rule is captured, the lexy::lexeme includes the automatic whitespace skipped after the token. Use lexy::dsl::capture_token or lexy::dsl::identifier instead.
Tip
Use the callback lexy::as_string to convert the lexy::lexeme to a string.

Branch rule lexy::dsl::capture_token

lexy/dsl/capture.hpp
namespace lexy::dsl
{
    constexpr branch-rule auto capture_token(token-rule auto token);
}

capture_token is a branch rule that parses token capturing everything it has consumed but excluding whitespace as a value.

(Branch) Parsing

Parses token unchanged.

Errors

All errors raised by token. The rule then fails if token has failed.

Values

A lexy::lexeme whose range covers everything consumed by token except any trailing whitespace.

Tip
Use the callback lexy::as_string to convert the lexy::lexeme to a string.
Tip
In most cases, you should prefer lexy::dsl::identifier instead.

See also