Header lexy/dsl/return.hpp

Rule lexy::dsl::return_

lexy/dsl/return.hpp
namespace lexy::dsl
{
    constexpr rule auto return_;
}

return_ is a rule that terminates parsing of the current production early.

Requires

It is not used inside loops (e.g. lexy::dsl::loop , lexy::dsl::while_ , lexy::dsl::list ).

Parsing

Always succeeds without consuming any input. Parsing of the current production is then finished.

Errors

None.

Values

None; but all existing values are immediately parsed to the context, e.g. the callback is immediately invoked if lexy::parse() is used.

Example 1. Parse a (simplified) XML element, which can be empty
struct production
{
    static constexpr auto rule = [] {
        auto tag_name = dsl::identifier(dsl::ascii::alpha);

        // If we're having tag name followed by a `/`,
        // it is an empty element without content.
        // Immediately return in that case.
        auto if_empty  = dsl::if_(dsl::lit_c<'/'> >> dsl::return_);
        auto open_tag  = dsl::angle_bracketed(tag_name + if_empty);
        auto close_tag = dsl::angle_bracketed(dsl::lit_c<'/'> + tag_name);

        // Placeholder content.
        auto content = LEXY_LIT("content");

        return open_tag + content + close_tag;
    }();
};
Note
See xml.cpp for a more complete XML parser.

See also