Header lexy/dsl/sign.hpp

The plus_sign, minus_sign and sign rules.

Tag types

lexy/dsl/sign.hpp
namespace lexy
{
    struct plus_sign
    {
        constexpr operator int() const
        {
            return +1;
        }
    };

    struct minus_sign
    {
        constexpr operator int() const
        {
            return -1;
        }
    };
}

The tag types plus_sign and minus_sign indicate a plus/minus sign.

They are implicitly convertible to the integer +1/-1.

Tip
The callback lexy::as_integer  accepts an optional plus_sign/minus_sign object followed by an integer and returns the correctly signed integer.

Rule lexy::dsl::plus_sign

lexy/dsl/sign.hpp
namespace lexy::dsl
{
    constexpr rule auto plus_sign;
}

plus_sign is a rule that parses an optional + sign.

Parsing

Tries to match and consume lexy::dsl::lit_c <'+'>. If that fails, succeeds without consuming anything.

Errors

None.

Values

If it matched a +, an object of type lexy::plus_sign. Otherwise, no values.

Example 1. A number with an optional plus sign
struct production
{
    static constexpr auto rule
        // Plus sign followed by a decimal integer.
        = dsl::plus_sign + dsl::integer<int>;
};

Rule lexy::dsl::minus_sign

lexy/dsl/sign.hpp
namespace lexy::dsl
{
    constexpr rule auto minus_sign;
}

minus_sign is a rule that parses an optional - sign.

Parsing

Tries to match and consume lexy::dsl::lit_c <'-'>. If that fails, succeeds without consuming anything.

Errors

None.

Values

If it matched a -, an object of type lexy::minus_sign. Otherwise, no values.

Example 2. A number with an optional minus sign
struct production
{
    static constexpr auto rule
        // Minus sign followed by a decimal integer.
        = dsl::minus_sign + dsl::integer<int>;
};

Rule lexy::dsl::sign

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

sign is a rule that parses an optional sign.

Parsing

Tries to match and consume lexy::dsl::lit_c <'+'> or lexy::dsl::lit_c <'-'>. If both fail, succeeds without consuming anything.

Errors

None.

Values

If it matched a +, an object of type lexy::plus_sign. If it matched a -, an object of type lexy::minus_sign. Otherwise, no values.

Example 3. A number with an optional plus/minus sign
struct production
{
    // Sign followed by a decimal integer.
    static constexpr auto rule  = dsl::sign + dsl::integer<int>;
    static constexpr auto value = lexy::as_integer<int>;
};

See also