Header lexy/dsl/position.hpp

Rule lexy::dsl::position

lexy/dsl/position.hpp
namespace lexy::dsl
{
    constexpr rule auto position;                                  (1)

    constexpr rule auto position(rule auto rule);                (2)
    constexpr branch-rule auto position(branch-rule auto rule);
}

position is a rule that produces the current input position without consuming anything.

(Branch) Parsing
  1. Always succeeds without consuming anything.

  2. (Branch) Parses rule.

Errors
  1. None.

  2. All errors raised by (branch) parsing rule. The rule then fails if rule has failed.

Values

An iterator to the current position of the reader, followed by all values produced by rule.

Parse tree

A single token node, whose range is begins and ends at its position, with the lexy::predefined_token_kind  lexy::position_token_node, followed by all nodes produced by rule.

Example 1. Determine the position of a function declaration
struct production
{
    static constexpr auto whitespace = dsl::ascii::space;

    static constexpr auto rule = [] {
        auto id          = dsl::identifier(dsl::ascii::alpha);
        auto kw_function = LEXY_KEYWORD("function", id);

        auto arguments = dsl::parenthesized(LEXY_LIT("..."));
        auto body      = dsl::curly_bracketed(LEXY_LIT("..."));

        // The position of a function is the first character of the name.
        return kw_function + dsl::position + id + arguments + body;
    }();
};
Note
As a rule, position does not do whitespace  skipping. If used immediately before a token rule it will produce the position that is the beginning of this token, because whitespace skipping is done after token rules.

See also