Skip to main content

skullbrain_parser/cst/
mod.rs

1mod expr;
2mod nodes;
3mod tokens;
4mod utils;
5
6use crate::SyntaxKind;
7use utils::{define_cst_node, define_cst_token};
8
9pub use expr::*;
10pub use nodes::*;
11pub use tokens::*;
12
13/// A utility type alias
14pub type SyntaxNode = cstree::syntax::SyntaxNode<SyntaxKind>;
15/// A utility type alias
16pub type SyntaxToken = cstree::syntax::SyntaxToken<SyntaxKind>;
17/// A utility type alias
18pub type SyntaxNodeChildren<'a> = cstree::syntax::SyntaxNodeChildren<'a, SyntaxKind>;
19/// A utility type alias
20pub type SyntaxElementRef<'a> = cstree::syntax::SyntaxElementRef<'a, SyntaxKind>;
21
22/// Core abstraction for typed CST node wrappers
23pub trait CstNode: Sized {
24    /// Returns true if the given syntax kind can be cast to this node type
25    fn can_cast(kind: SyntaxKind) -> bool;
26
27    /// Attempts to cast a raw syntax node to this typed node
28    ///
29    /// Returns `Some(Self)` if the cast is valid, `None` otherwise
30    fn cast(node: SyntaxNode) -> Option<Self>;
31
32    /// Returns a reference to the underlying syntax node
33    fn syntax(&self) -> &SyntaxNode;
34}
35
36/// Core abstraction for typed CST token wrappers
37pub trait CstToken: Sized {
38    /// Returns true if the given syntax kind can be cast to this token type
39    fn can_cast(kind: SyntaxKind) -> bool;
40
41    /// Attempts to cast a raw syntax token to this typed token
42    ///
43    /// Returns `Some(Self)` if the cast is valid, `None` otherwise
44    fn cast(token: SyntaxToken) -> Option<Self>;
45
46    /// Returns a reference to the underlying syntax token
47    fn token(&self) -> &SyntaxToken;
48}