Skip to main content

oak_actionscript/ast/
mod.rs

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