Skip to main content

oak_bash/parser/
element_type.rs

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