Header lexy/dsl/if.hpp

Rule lexy::dsl::if_

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

if_ is a rule that tries to parse a branch rule.

Parsing

Tries to parse branch. If that backtracks, succeeds without consuming anything.

Errors

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

Values

All values produced by branch.

Example 1. Only parse a fraction if preceded by a dot
struct production
{
    static constexpr auto rule = [] {
        auto integer  = dsl::digits<>.no_leading_zero();
        auto fraction = dsl::digits<>;

        return integer + dsl::if_(dsl::period >> fraction);
    }();
};
Note
if_(branch) is functionally equivalent to the choice  branch | lexy::dsl::else_.
Tip
Use lexy::dsl::opt  if you need a value to indicate that branch was not parsed.

See also