Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is just some additional design and polish atop @kureuil's work in #480. Copied from documentation:
Define a type that supports parsing and printing a given identifier as if it were a keyword.
Usage
As a convention, it is recommended that this macro be invoked within a module called
kw
and that the resulting parser be invoked with akw::
prefix.The generated syntax tree node supports the following operations just like any built-in keyword token.
Peeking —
input.peek(kw::whatever)
Parsing —
input.parse::<kw::whatever>()?
Printing —
quote!( ... #whatever_token ... )
Construction from a
Span
—let whatever_token = kw::whatever(sp)
Field access to its span —
let sp = whatever_token.span
Example
This example parses input that looks like
bool = true
orstr = "value"
. The key must be either the identifierbool
or the identifierstr
. Ifbool
, the value may be eithertrue
orfalse
. Ifstr
, the value may be any string literal.The symbols
bool
andstr
are not reserved keywords in Rust so these are not considered keywords in thesyn::token
module. Like any other identifier that is not a keyword, these can be declared as custom keywords by crates that need to use them as such.