Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'xtask/src/codegen/grammar.rs')
-rw-r--r--xtask/src/codegen/grammar.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/xtask/src/codegen/grammar.rs b/xtask/src/codegen/grammar.rs
index d39231f1c2..035891dcbe 100644
--- a/xtask/src/codegen/grammar.rs
+++ b/xtask/src/codegen/grammar.rs
@@ -404,6 +404,7 @@ fn generate_syntax_kinds(grammar: KindsSrc) -> String {
});
let punctuation =
grammar.punct.iter().map(|(_token, name)| format_ident!("{}", name)).collect::<Vec<_>>();
+ let punctuation_texts = grammar.punct.iter().map(|&(text, _name)| text);
let fmt_kw_as_variant = |&name| match name {
"Self" => format_ident!("SELF_TYPE_KW"),
@@ -429,6 +430,7 @@ fn generate_syntax_kinds(grammar: KindsSrc) -> String {
quote! { #kw if #ed <= edition }
})
.collect::<Vec<_>>();
+ let edition_dependent_keywords = grammar.edition_dependent_keywords.iter().map(|&(it, _)| it);
let edition_dependent_keywords_variants = grammar
.edition_dependent_keywords
.iter()
@@ -502,6 +504,20 @@ fn generate_syntax_kinds(grammar: KindsSrc) -> String {
use self::SyntaxKind::*;
impl SyntaxKind {
+ #[allow(unreachable_patterns)]
+ pub const fn text(self) -> &'static str {
+ match self {
+ TOMBSTONE | EOF | __LAST
+ #( | #literals )*
+ #( | #nodes )*
+ #( | #tokens )* => panic!("no text for these `SyntaxKind`s"),
+ #( #punctuation => #punctuation_texts ,)*
+ #( #strict_keywords_variants => #strict_keywords ,)*
+ #( #contextual_keywords_variants => #contextual_keywords ,)*
+ #( #edition_dependent_keywords_variants => #edition_dependent_keywords ,)*
+ }
+ }
+
/// Checks whether this syntax kind is a strict keyword for the given edition.
/// Strict keywords are identifiers that are always considered keywords.
pub fn is_strict_keyword(self, edition: Edition) -> bool {