Header lexy/dsl/capture.hpp

Branch rule lexy::dsl::capture

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

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

Requires

rule is either a token rule or lexy::dsl::p where the production is a token production.

(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 token except any trailing whitespace. Then 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()); });
};
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