Skip to main content

oak_bash/parser/
element_type.rs

1use crate::lexer::BashTokenType;
2use oak_core::{ElementType, UniversalElementRole};
3
4/// Represents all possible element kinds in the Bash shell scripting language.
5#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum BashElementType {
8    /// A wrapper for tokens
9    Token(BashTokenType),
10    /// Root node representing the entire source file
11    Root,
12    /// A single command or a pipeline
13    CommandStatement,
14    /// A variable assignment
15    VariableAssignment,
16    /// A pipeline of commands
17    Pipeline,
18    /// A redirection operation
19    Redirection,
20    /// An if statement
21    IfStatement,
22    /// A for loop
23    ForStatement,
24    /// A while loop
25    WhileStatement,
26    /// A function definition
27    FunctionDefinition,
28    /// Error node for syntax errors
29    Error,
30}
31
32impl From<BashTokenType> for BashElementType {
33    fn from(token: BashTokenType) -> Self {
34        Self::Token(token)
35    }
36}
37
38impl ElementType for BashElementType {
39    type Role = UniversalElementRole;
40
41    fn is_root(&self) -> bool {
42        matches!(self, Self::Root)
43    }
44
45    fn is_error(&self) -> bool {
46        matches!(self, Self::Error)
47    }
48
49    fn role(&self) -> Self::Role {
50        match self {
51            Self::Root => UniversalElementRole::Root,
52            Self::FunctionDefinition => UniversalElementRole::Definition,
53            Self::VariableAssignment => UniversalElementRole::Statement,
54            Self::CommandStatement | Self::IfStatement | Self::ForStatement | Self::WhileStatement | Self::Pipeline | Self::Redirection => UniversalElementRole::Statement,
55            Self::Error => UniversalElementRole::Error,
56            _ => UniversalElementRole::None,
57        }
58    }
59}