skullbrain-parser 0.1.0

Parses skullbrain tokens into a concrete syntax tree (CST).
Documentation
mod expr;
mod nodes;
mod tokens;
mod utils;

use crate::SyntaxKind;
use utils::{define_cst_node, define_cst_token};

pub use expr::*;
pub use nodes::*;
pub use tokens::*;

/// A utility type alias
pub type SyntaxNode = cstree::syntax::SyntaxNode<SyntaxKind>;
/// A utility type alias
pub type SyntaxToken = cstree::syntax::SyntaxToken<SyntaxKind>;
/// A utility type alias
pub type SyntaxNodeChildren<'a> = cstree::syntax::SyntaxNodeChildren<'a, SyntaxKind>;
/// A utility type alias
pub type SyntaxElementRef<'a> = cstree::syntax::SyntaxElementRef<'a, SyntaxKind>;

/// Core abstraction for typed CST node wrappers
pub trait CstNode: Sized {
    /// Returns true if the given syntax kind can be cast to this node type
    fn can_cast(kind: SyntaxKind) -> bool;

    /// Attempts to cast a raw syntax node to this typed node
    ///
    /// Returns `Some(Self)` if the cast is valid, `None` otherwise
    fn cast(node: SyntaxNode) -> Option<Self>;

    /// Returns a reference to the underlying syntax node
    fn syntax(&self) -> &SyntaxNode;
}

/// Core abstraction for typed CST token wrappers
pub trait CstToken: Sized {
    /// Returns true if the given syntax kind can be cast to this token type
    fn can_cast(kind: SyntaxKind) -> bool;

    /// Attempts to cast a raw syntax token to this typed token
    ///
    /// Returns `Some(Self)` if the cast is valid, `None` otherwise
    fn cast(token: SyntaxToken) -> Option<Self>;

    /// Returns a reference to the underlying syntax token
    fn token(&self) -> &SyntaxToken;
}