Header lexy/dsl/newline.hpp

The newline and eol token rules.

Token rule lexy::dsl::newline

lexy/dsl/newline.hpp
namespace lexy::dsl
{
    constexpr token-rule auto newline;
}

newline is a token rule that matches a newline.

Matching

Tries to match and consume lexy::dsl::lit<"\r\n"> or lexy::dsl::lit<"\n">. Succeeds if one of them matched.

Errors

lexy::expected_char_class ("newline"): if they both did not match; at the starting reader position.

Example 1. Match a newline at the end
struct production
{
    static constexpr auto rule = LEXY_LIT("Hello") + dsl::newline;
};
Caution
As a token rule, it matches whitespace immediately following the newline. As such, the rule is best used in contexts where automatic whitespace skipping is disabled, or where it doesn’t include newline characters.
Caution
Differentiate between lexy::dsl::ascii::newline, which matches \r or \n, and lexy::dsl::newline, which matches \r\n or \n!

Token rule lexy::dsl::eol

lexy/dsl/newline.hpp
namespace lexy::dsl
{
    constexpr token-rule auto eol;
}

eol is a token rule that matches the end of a line.

Matching

Tries to match and consume lexy::dsl::lit<"\r\n">, lexy::dsl::lit<"\n">, or lexy::dsl::eof. Succeeds if one of them matched.

Errors

lexy::expected_char_class ("EOF"): if they all did not match; at the starting reader position.

Parse tree

Single token type with the lexy::predefined_token_kind lexy::eol_token_kind.

Example 2. Match a newline or EOF at the end
struct production
{
    static constexpr auto rule = LEXY_LIT("Hello") + dsl::eol;
};
Caution
The same caveat about whitespace as for newline applies here as well.

See also