oak-bash 0.0.11

High-performance incremental Bash parser for the oak ecosystem with flexible configuration, supporting shell scripting and automation workflows.
Documentation
use crate::lexer::BashTokenType;
use oak_core::{ElementType, UniversalElementRole};

/// Represents all possible element kinds in the Bash shell scripting language.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BashElementType {
    /// A wrapper for tokens
    Token(BashTokenType),
    /// Root node representing the entire source file
    Root,
    /// A single command or a pipeline
    CommandStatement,
    /// A variable assignment
    VariableAssignment,
    /// A pipeline of commands
    Pipeline,
    /// A redirection operation
    Redirection,
    /// An if statement
    IfStatement,
    /// A for loop
    ForStatement,
    /// A while loop
    WhileStatement,
    /// A function definition
    FunctionDefinition,
    /// Error node for syntax errors
    Error,
}

impl From<BashTokenType> for BashElementType {
    fn from(token: BashTokenType) -> Self {
        Self::Token(token)
    }
}

impl ElementType for BashElementType {
    type Role = UniversalElementRole;

    fn is_root(&self) -> bool {
        matches!(self, Self::Root)
    }

    fn is_error(&self) -> bool {
        matches!(self, Self::Error)
    }

    fn role(&self) -> Self::Role {
        match self {
            Self::Root => UniversalElementRole::Root,
            Self::FunctionDefinition => UniversalElementRole::Definition,
            Self::VariableAssignment => UniversalElementRole::Statement,
            Self::CommandStatement | Self::IfStatement | Self::ForStatement | Self::WhileStatement | Self::Pipeline | Self::Redirection => UniversalElementRole::Statement,
            Self::Error => UniversalElementRole::Error,
            _ => UniversalElementRole::None,
        }
    }
}