mod nodes;
mod support;
pub use nodes::*;
use crate::SyntaxNode;
pub trait AstNode: Sized {
fn can_cast(kind: crate::SyntaxKind) -> bool;
fn cast(node: SyntaxNode) -> Option<Self>;
fn syntax(&self) -> &SyntaxNode;
}
macro_rules! ast_node {
($name:ident, $kind:ident) => {
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct $name {
syntax: $crate::SyntaxNode,
}
impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.syntax, f)
}
}
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.syntax.text(), f)
}
}
impl $crate::ast::AstNode for $name {
fn can_cast(kind: $crate::SyntaxKind) -> bool {
kind == $crate::SyntaxKind::$kind
}
fn cast(node: $crate::SyntaxNode) -> Option<Self> {
if Self::can_cast(node.kind()) {
Some(Self { syntax: node })
} else {
None
}
}
fn syntax(&self) -> &$crate::SyntaxNode {
&self.syntax
}
}
};
}
pub(crate) use ast_node;
#[cfg(test)]
mod tests;