Skip to main content

oak_perl/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element type for the Perl language.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum PerlElementType {
7    /// Root node.
8    Root,
9    /// Program.
10    Program,
11    /// Statement.
12    Statement,
13    /// Expression.
14    Expression,
15    /// Block.
16    Block,
17    /// Subroutine declaration.
18    SubroutineDeclaration,
19    /// Package declaration.
20    PackageDeclaration,
21    /// Use statement.
22    UseStatement,
23    /// Variable declaration.
24    VariableDeclaration,
25    /// Assignment.
26    Assignment,
27    /// Function call.
28    FunctionCall,
29    /// Method call.
30    MethodCall,
31    /// Array access.
32    ArrayAccess,
33    /// Hash access.
34    HashAccess,
35    /// Reference.
36    Reference,
37    /// Dereference.
38    Dereference,
39    /// Conditional expression.
40    ConditionalExpression,
41    /// Loop statement.
42    LoopStatement,
43    /// If statement.
44    IfStatement,
45    /// Unless statement.
46    UnlessStatement,
47    /// While statement.
48    WhileStatement,
49    /// Until statement.
50    UntilStatement,
51    /// For statement.
52    ForStatement,
53    /// Foreach statement.
54    ForeachStatement,
55    /// Do statement.
56    DoStatement,
57    /// Eval statement.
58    EvalStatement,
59    /// Regex match.
60    RegexMatch,
61    /// Regex substitution.
62    RegexSubstitution,
63    /// Regex transliteration.
64    RegexTransliteration,
65    /// Error.
66    Error,
67}
68
69impl PerlElementType {
70    /// Returns `true` if this element type is a token.
71    pub fn is_token(&self) -> bool {
72        false
73    }
74
75    /// Returns `true` if this element type is an element.
76    pub fn is_element(&self) -> bool {
77        true
78    }
79}
80
81impl ElementType for PerlElementType {
82    type Role = UniversalElementRole;
83
84    fn role(&self) -> Self::Role {
85        use UniversalElementRole::*;
86        match self {
87            Self::Root | Self::Program => Root,
88            Self::Statement | Self::UseStatement | Self::LoopStatement | Self::IfStatement | Self::UnlessStatement | Self::WhileStatement | Self::UntilStatement | Self::ForStatement | Self::ForeachStatement | Self::DoStatement | Self::EvalStatement => {
89                Statement
90            }
91            Self::Expression
92            | Self::Assignment
93            | Self::FunctionCall
94            | Self::MethodCall
95            | Self::ArrayAccess
96            | Self::HashAccess
97            | Self::Reference
98            | Self::Dereference
99            | Self::ConditionalExpression
100            | Self::RegexMatch
101            | Self::RegexSubstitution
102            | Self::RegexTransliteration => Expression,
103            Self::Block => Statement,
104            Self::SubroutineDeclaration | Self::PackageDeclaration | Self::VariableDeclaration => Definition,
105            Self::Error => Error,
106        }
107    }
108}
109
110impl From<crate::lexer::token_type::PerlTokenType> for PerlElementType {
111    fn from(token: crate::lexer::token_type::PerlTokenType) -> Self {
112        use crate::lexer::token_type::PerlTokenType;
113        match token {
114            PerlTokenType::InternalProgram => PerlElementType::Program,
115            PerlTokenType::InternalStatement => PerlElementType::Statement,
116            PerlTokenType::InternalExpression => PerlElementType::Expression,
117            PerlTokenType::InternalBlock => PerlElementType::Block,
118            _ => PerlElementType::Error,
119        }
120    }
121}