oak_perl/parser/
element_type.rs1use oak_core::{ElementType, UniversalElementRole};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum PerlElementType {
7 Root,
9 Program,
11 Statement,
13 Expression,
15 Block,
17 SubroutineDeclaration,
19 PackageDeclaration,
21 UseStatement,
23 VariableDeclaration,
25 Assignment,
27 FunctionCall,
29 MethodCall,
31 ArrayAccess,
33 HashAccess,
35 Reference,
37 Dereference,
39 ConditionalExpression,
41 LoopStatement,
43 IfStatement,
45 UnlessStatement,
47 WhileStatement,
49 UntilStatement,
51 ForStatement,
53 ForeachStatement,
55 DoStatement,
57 EvalStatement,
59 RegexMatch,
61 RegexSubstitution,
63 RegexTransliteration,
65 Error,
67}
68
69impl PerlElementType {
70 pub fn is_token(&self) -> bool {
72 false
73 }
74
75 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}