Skip to main content

oak_actionscript/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2/// ActionScript AST definitions
3use core::range::Range;
4
5/// Identifier
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Identifier {
9    /// Identifier name
10    pub name: String,
11    /// Range in the source code
12    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
13    pub span: Range<usize>,
14}
15
16/// Root node of the ActionScript AST
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[derive(Debug, Clone, PartialEq)]
19pub struct ActionScriptRoot {
20    /// All top-level items in the source file
21    pub items: Vec<ActionScriptItem>,
22}
23
24/// ActionScript top-level items
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[derive(Debug, Clone, PartialEq)]
27pub enum ActionScriptItem {
28    /// Package definition
29    Package(PackageDeclaration),
30    /// Class definition
31    Class(ClassDeclaration),
32    /// Interface definition
33    Interface(InterfaceDeclaration),
34    /// Function definition
35    Function(FunctionDeclaration),
36    /// Variable declaration
37    Variable(VariableDeclaration),
38    /// Import statement
39    Import(ImportDeclaration),
40}
41
42/// Package declaration
43#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
44#[derive(Debug, Clone, PartialEq)]
45pub struct PackageDeclaration {
46    /// Package name
47    pub name: Option<Identifier>,
48    /// Items in the package body
49    pub items: Vec<ActionScriptItem>,
50    /// Span
51    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
52    pub span: Range<usize>,
53}
54
55/// Class declaration
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57#[derive(Debug, Clone, PartialEq)]
58pub struct ClassDeclaration {
59    /// Class name
60    pub name: Identifier,
61    /// Modifiers (public, private, internal, etc.)
62    pub modifiers: Vec<String>,
63    /// Base class
64    pub extends: Option<Identifier>,
65    /// Implemented interfaces
66    pub implements: Vec<Identifier>,
67    /// Items in the class body
68    pub items: Vec<ActionScriptItem>,
69    /// Span
70    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
71    pub span: Range<usize>,
72}
73
74/// Interface declaration
75#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76#[derive(Debug, Clone, PartialEq)]
77pub struct InterfaceDeclaration {
78    /// Interface name
79    pub name: Identifier,
80    /// Base interfaces
81    pub extends: Vec<Identifier>,
82    /// Items in the interface body
83    pub items: Vec<ActionScriptItem>,
84    /// Span
85    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
86    pub span: Range<usize>,
87}
88
89/// Function declaration
90#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
91#[derive(Debug, Clone, PartialEq)]
92pub struct FunctionDeclaration {
93    /// Function name
94    pub name: Identifier,
95    /// Parameter list
96    pub parameters: Vec<Parameter>,
97    /// Return type
98    pub return_type: Option<Identifier>,
99    /// Span
100    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
101    pub span: Range<usize>,
102}
103
104/// Parameter
105#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106#[derive(Debug, Clone, PartialEq)]
107pub struct Parameter {
108    /// Parameter name
109    pub name: Identifier,
110    /// Parameter type
111    pub type_annotation: Option<Identifier>,
112}
113
114/// Variable declaration
115#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
116#[derive(Debug, Clone, PartialEq)]
117pub struct VariableDeclaration {
118    /// Variable name
119    pub name: Identifier,
120    /// Variable type
121    pub type_annotation: Option<Identifier>,
122    /// Whether it is a constant
123    pub is_const: bool,
124    /// Span
125    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
126    pub span: Range<usize>,
127}
128
129/// Import declaration
130#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
131#[derive(Debug, Clone, PartialEq)]
132pub struct ImportDeclaration {
133    /// Import path
134    pub path: String,
135    /// Span
136    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
137    pub span: Range<usize>,
138}
139
140impl Default for ActionScriptRoot {
141    fn default() -> Self {
142        Self { items: Vec::new() }
143    }
144}