Header lexy/dsl/position.hpp

Rule lexy::dsl::position

namespace lexy::dsl
    constexpr rule auto position;

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


Always succeeds without consuming anything.




An iterator to the current position of the reader.

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.

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;
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