Header lexy/dsl/alternative.hpp

Token rule lexy::dsl::operator/

lexy/dsl/alternative.hpp
namespace lexy
{
    struct exhausted_alternatives {};
}

namespace lexy::dsl
{
    constexpr token-rule auto operator/(token-rule auto lhs,
                                        token-rule auto rhs);
}

operator/ (alternative) is a token rule that matches one of the specified tokens.

Matching

Tries to match lhs and rhs; consuming the longest match.

Errors

lexy::exhausted_alternatives: if neither lhs or rhs matched; at the original reader position. The rule then fails without consuming anything.

Parse tree

Single token node covering everything consumed by the selected token. If the token kind of all individual token rules identical, that is the token kind. Otherwise, the kind is unknown.

As the result of operator/ is itself a token rule, it can be chained.

Example 1. Match one of the given literals.
struct production
{
    static constexpr auto rule = LEXY_LIT("a") / LEXY_LIT("abc") / LEXY_LIT("bc");
};
Caution
For literal token rules, the implementation uses a trie to match them efficiently. For complex tokens the alternative rule requires backtracking. Use choice with a branch condition as an optimization.
Note
Unlike choice, the ordering of rules in an alternative does not matter. It will always consume the longest match.

See also