Header lexy/dsl/ascii.hpp

Token rules for matching the ASCII character classes.

lexy/dsl/ascii.hpp
namespace lexy::dsl
{
    namespace ascii
    {
        constexpr token-rule auto control;

        constexpr token-rule auto blank;
        constexpr token-rule auto newline;
        constexpr token-rule auto other_space;
        constexpr token-rule auto space;

        constexpr token-rule auto digit;

        constexpr token-rule auto lower;
        constexpr token-rule auto upper;
        constexpr token-rule auto alpha;
        constexpr token-rule auto alpha_underscore;

        constexpr token-rule auto alpha_digit;
        constexpr token-rule auto alpha_digit_underscore;
        constexpr token-rule auto alnum = alpha_digit;

        constexpr token-rule auto punct;

        constexpr token-rule auto graph;
        constexpr token-rule auto print;

        constexpr token-rule auto character;

    }
}

Every token rule in the namespace lexy::dsl::ascii matches one ASCII character from a character class.

Matching

Matches and consumes one character from the character class given in the table below. Any transcoding from ASCII to the input encoding for the comparison is done by a simple static_cast.

Errors

lexy::expected_char_class ("ASCII.<name>"): at the unchanged reader position. The rule then fails.

The character classes
Token Rule Character Class <cctype> function (C locale)

control

0x00-0x1F, \x7F

std::iscntrl()

blank

' ' (space) or '\t'

std::isblank()

newline

'\n' or '\r'

n/a

other_space

'\f' or '\v\

n/a

space

blank, newline, other_space

std::isspace()

digit

0123456789

std::isdigit()

lower

abcdefghijklmnopqrstuvwxyz

std::islower()

upper

ABCDEFGHIJKLMNOPQRSTUVWXYZ

std::isupper()

alpha

lower, upper

std::isalpha()

alpha_underscore

lower, upper, '_'

n/a

alpha_digit, alnum

lower, upper, digit

std::isalnum()

alpha_digit_underscore

lower, upper, digit, '_'

n/a

punct

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

std::ispunct()

graph

alpha_digit, punct

std::isgraph()

print

alpha_digit, punct, ' ' (space)

std::ispunct()

character

any ASCII character

n/a

Example 1. A C-like identifier
struct production
{
    static constexpr auto rule = [] {
        auto head = dsl::ascii::alpha_underscore;
        auto tail = dsl::ascii::alpha_digit_underscore;
        return dsl::identifier(head, tail);
    }();
};
Example 2. Allow ASCII whitespace between tokens
struct production
{
    static constexpr auto whitespace = dsl::ascii::space;
    static constexpr auto rule                  //
        = LEXY_LIT("Hello") + LEXY_LIT("World") //
          + dsl::exclamation_mark + dsl::eof;
};
Note
The only difference between lexy::dsl::ascii::digit and lexy::dsl::digit<lexy::dsl::decimal> is the name of the character class in the error.
Caution
Differentiate between lexy::dsl::ascii::newline, which matches \r or \n, and lexy::dsl::newline, which matches \r\n or \n!
Caution
As token rules, they match whitespace immediately following the character. As such, the rule is best used in contexts where automatic whitespace skipping is disabled. They can safely be used as part of the whitespace definition.
Tip
The equivalent of std::isxdigit() is lexy::dsl::digit<lexy::dsl::hex>.

See also