oak_actionscript/parser/
element_type.rs1use crate::lexer::ActionScriptTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6pub type ActionScriptElement<'a> = Arc<GreenNode<'a, ActionScriptElementType>>;
8
9#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum ActionScriptElementType {
12 Program,
14 Root,
16 Statement,
18 Expression,
20 Block,
22 Class,
24 Interface,
26 Function,
28 Variable,
30 Import,
32 Package,
34 Namespace,
36 Assignment,
38 FunctionCall,
40 MethodCall,
42 PropertyAccess,
44 ArrayAccess,
46 ConditionalExpression,
48 BinaryExpression,
50 UnaryExpression,
52 IfStatement,
54 ForStatement,
56 WhileStatement,
58 DoWhileStatement,
60 SwitchStatement,
62 TryStatement,
64 ThrowStatement,
66 ReturnStatement,
68 BreakStatement,
70 ContinueStatement,
72
73 Error,
75
76 Identifier,
78 LiteralExpression,
80 IdentifierExpression,
82 ParenthesizedExpression,
84 SourceFile,
86 ParameterList,
88 BlockExpression,
90 UseItem,
92 ModuleItem,
94 StructItem,
96 EnumItem,
98 LetStatement,
100 IfExpression,
102 WhileExpression,
104 LoopExpression,
106 ForExpression,
108 CallExpression,
110 IndexExpression,
112 FieldExpression,
114}
115
116impl ElementType for ActionScriptElementType {
117 type Role = UniversalElementRole;
118
119 fn is_root(&self) -> bool {
120 matches!(self, Self::Program | Self::Root | Self::SourceFile)
121 }
122
123 fn is_error(&self) -> bool {
124 matches!(self, Self::Error)
125 }
126
127 fn role(&self) -> Self::Role {
128 use UniversalElementRole::*;
129 match self {
130 Self::Program | Self::Root | Self::SourceFile => Root,
131 Self::Class | Self::Interface | Self::Function | Self::Variable | Self::Package | Self::Namespace | Self::ModuleItem | Self::StructItem | Self::EnumItem => Definition,
132 Self::Block | Self::BlockExpression | Self::ParameterList | Self::ParenthesizedExpression => Container,
133 Self::Statement | Self::Import | Self::UseItem | Self::LetStatement | Self::ReturnStatement | Self::BreakStatement | Self::ContinueStatement | Self::ThrowStatement => Statement,
134 Self::Expression
135 | Self::Assignment
136 | Self::BinaryExpression
137 | Self::UnaryExpression
138 | Self::ConditionalExpression
139 | Self::IfExpression
140 | Self::WhileExpression
141 | Self::LoopExpression
142 | Self::ForExpression
143 | Self::IdentifierExpression
144 | Self::LiteralExpression
145 | Self::IndexExpression
146 | Self::FieldExpression
147 | Self::PropertyAccess
148 | Self::ArrayAccess => Expression,
149 Self::FunctionCall | Self::MethodCall | Self::CallExpression => Call,
150 Self::Identifier => Reference,
151 Self::Error => Error,
152 _ => None,
153 }
154 }
155}
156
157impl From<ActionScriptTokenType> for ActionScriptElementType {
158 fn from(token_type: ActionScriptTokenType) -> Self {
159 match token_type {
160 ActionScriptTokenType::Identifier => Self::Identifier,
161 ActionScriptTokenType::StringLiteral | ActionScriptTokenType::NumberLiteral | ActionScriptTokenType::BooleanLiteral | ActionScriptTokenType::NullLiteral => Self::LiteralExpression,
162 _ => Self::Error,
163 }
164 }
165}