Generate a method for static retrieval of the SyntaxKind of a node, where possible

This will help for the quote macro for `ast::make`.
This commit is contained in:
Chayim Refael Friedman 2024-12-30 03:13:17 +02:00
parent 59bc7b49d0
commit 737500137f
3 changed files with 1093 additions and 0 deletions

View file

@ -42,6 +42,14 @@ pub use self::{
/// the same representation: a pointer to the tree root and a pointer to the
/// node itself.
pub trait AstNode {
/// This panics if the `SyntaxKind` is not statically known.
fn kind() -> SyntaxKind
where
Self: Sized,
{
panic!("dynamic `SyntaxKind` for `AstNode::kind()`")
}
fn can_cast(kind: SyntaxKind) -> bool
where
Self: Sized;

File diff suppressed because it is too large Load diff

View file

@ -162,6 +162,13 @@ fn generate_nodes(kinds: KindsSrc, grammar: &AstSrc) -> String {
},
quote! {
impl AstNode for #name {
#[inline]
fn kind() -> SyntaxKind
where
Self: Sized
{
#kind
}
#[inline]
fn can_cast(kind: SyntaxKind) -> bool {
kind == #kind