brink_syntax_native/ast/
mod.rs1mod nodes;
10mod support;
11
12pub use nodes::*;
13
14use crate::SyntaxNode;
15
16pub trait AstNode: Sized {
18 fn can_cast(kind: crate::SyntaxKind) -> bool;
20
21 fn cast(node: SyntaxNode) -> Option<Self>;
23
24 fn syntax(&self) -> &SyntaxNode;
26}
27
28macro_rules! ast_node {
30 ($name:ident, $kind:ident) => {
31 #[derive(Clone, PartialEq, Eq, Hash)]
32 pub struct $name {
33 syntax: $crate::SyntaxNode,
34 }
35
36 impl std::fmt::Debug for $name {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 std::fmt::Debug::fmt(&self.syntax, f)
39 }
40 }
41
42 impl std::fmt::Display for $name {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 std::fmt::Display::fmt(&self.syntax.text(), f)
45 }
46 }
47
48 impl $crate::ast::AstNode for $name {
49 fn can_cast(kind: $crate::SyntaxKind) -> bool {
50 kind == $crate::SyntaxKind::$kind
51 }
52
53 fn cast(node: $crate::SyntaxNode) -> Option<Self> {
54 if Self::can_cast(node.kind()) {
55 Some(Self { syntax: node })
56 } else {
57 None
58 }
59 }
60
61 fn syntax(&self) -> &$crate::SyntaxNode {
62 &self.syntax
63 }
64 }
65 };
66}
67
68pub(crate) use ast_node;
69
70#[cfg(test)]
71mod tests;