Header lexy/dsl/token.hpp
The basic token interface as well as the token
token rule.
lexy/dsl/token.hpp
template <typename T>
concept token-rule = rule<T> && …;
A token is an atomic element of the input, which can be parsed by a token rule. Token rules are special rules that do not produce any values and can be matched efficiently. As such, every token rule is also a branch rule.
In a parse tree, every token rule produces a token node which directly captures a substring of the input, the part consumed by the rule. The token node also has a token kind, which identifies a token rule.
As token rules are the atomic elements, they also serve the basis for whitespace
handling:
if automatic whitespace skipping has been enabled, it is skipped after every token rule.
Token interface .error<Tag>
lexy/dsl/token.hpp
class token-rule
{
public:
template <typename Tag>
static constexpr token-rule auto error;
…
};
Every token rule token
has a .error<Tag>
member that overrides the error raised when token
does not match.
- Matching
Matches and consumes
token
.- Errors
Tag
: if matchingtoken
fails. Its range covers everything already consumed bytoken
. The rule then fails as well.
Caution | .error<Tag> overrides all errors raised by a token rule to the same error.
If a token can fail with different kinds of errors, this information is lost. |
Token interface .kind<Tag>
lexy/dsl/token.hpp
class token-rule
{
public:
template <auto Kind>
static constexpr token-rule auto kind;
…
};
Every token rule token
has a .kind<Kind>
member that overrides the token kind of its node in the parse tree.
- Matching
Matches and consumes
token
.- Errors
All errors raised by
token
. The rule then fails.- Parse tree
A single token whose range covers everything consumed by
token
. However, its kind is nowKind
.
Token rule lexy::dsl::token
lexy/dsl/token.hpp
namespace lexy
{
struct missing_token {};
}
namespace lexy::dsl
{
constexpr token-rule auto token(rule auto rule);
}
token
is a rule that converts an arbitrary rule
into a token rule.
- Matching
Matches and consumes
rule
in a new context.- Errors
lexy::missing_token
: if matching ofrule
has failed for any reason; its range covers everythingrule
has already consumed. The rule then fails.
Note | Whitespace skipping is disabled while parsing rule ,
but it is skipped at the end, as it is a token rule. |
Caution | While token is optimized itself, parsing rule can be arbitrarily expensive.
As such, it should only be used when absolutely required. |
Warning | rule does not have access to any context variables created by the context-sensitive parsing facilities and it can’t use recursion. |