oak_actionscript/parser/
element_type.rs

1use crate::lexer::ActionScriptTokenType;
2use oak_core::{ElementType, GreenNode, UniversalElementRole};
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6/// ActionScript 语法树元素的类型别名
7pub type ActionScriptElement<'a> = Arc<GreenNode<'a, ActionScriptElementType>>;
8
9/// ActionScript 语法树中所有可能的元素类型。
10#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum ActionScriptElementType {
12    /// Root node of the program
13    Program,
14    /// Root node
15    Root,
16    /// Generic statement node
17    Statement,
18    /// Generic expression node
19    Expression,
20    /// Block of statements
21    Block,
22    /// Class declaration
23    Class,
24    /// Interface declaration
25    Interface,
26    /// Function declaration
27    Function,
28    /// Variable declaration
29    Variable,
30    /// Import statement
31    Import,
32    /// Package declaration
33    Package,
34    /// Namespace declaration
35    Namespace,
36    /// Assignment expression
37    Assignment,
38    /// Function call
39    FunctionCall,
40    /// Method call
41    MethodCall,
42    /// Property access
43    PropertyAccess,
44    /// Array access
45    ArrayAccess,
46    /// Conditional expression (ternary)
47    ConditionalExpression,
48    /// Binary expression
49    BinaryExpression,
50    /// Unary expression
51    UnaryExpression,
52    /// If statement
53    IfStatement,
54    /// For statement
55    ForStatement,
56    /// While statement
57    WhileStatement,
58    /// Do-while statement
59    DoWhileStatement,
60    /// Switch statement
61    SwitchStatement,
62    /// Try statement
63    TryStatement,
64    /// Throw statement
65    ThrowStatement,
66    /// Return statement
67    ReturnStatement,
68    /// Break statement
69    BreakStatement,
70    /// Continue statement
71    ContinueStatement,
72
73    /// Error token
74    Error,
75
76    /// Identifier
77    Identifier,
78    /// Literal
79    LiteralExpression,
80    /// Identifier expression
81    IdentifierExpression,
82    /// Parenthesized expression
83    ParenthesizedExpression,
84    /// Source file
85    SourceFile,
86    /// Parameter list
87    ParameterList,
88    /// Block expression
89    BlockExpression,
90    /// Use item
91    UseItem,
92    /// Module item
93    ModuleItem,
94    /// Struct item
95    StructItem,
96    /// Enum item
97    EnumItem,
98    /// Let statement
99    LetStatement,
100    /// If expression
101    IfExpression,
102    /// While expression
103    WhileExpression,
104    /// Loop expression
105    LoopExpression,
106    /// For expression
107    ForExpression,
108    /// Call expression
109    CallExpression,
110    /// Index expression
111    IndexExpression,
112    /// Field expression
113    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}