orbit/parser/
ast.rs

1//! Abstract Syntax Tree definitions for .orbit files
2
3use std::collections::HashMap;
4
5/// Represents a parsed .orbit file
6#[derive(Debug, Clone)]
7pub struct OrbitAst {
8    pub template: TemplateNode,
9    pub style: StyleNode,
10    pub script: ScriptNode,
11}
12
13/// Represents a node in the template tree
14#[derive(Debug, Clone)]
15pub enum TemplateNode {
16    Element {
17        tag: String,
18        attributes: HashMap<String, AttributeValue>,
19        events: HashMap<String, String>,
20        children: Vec<TemplateNode>,
21    },
22    Expression(String),
23    Text(String),
24}
25
26/// Represents an attribute value that can be either static or dynamic
27#[derive(Debug, Clone)]
28pub enum AttributeValue {
29    Static(String),
30    Dynamic(String), // Expression inside {{ }}
31}
32
33/// Represents the style section
34#[derive(Debug, Clone)]
35pub struct StyleNode {
36    pub rules: Vec<StyleRule>,
37    pub scoped: bool,
38}
39
40/// Represents a CSS rule
41#[derive(Debug, Clone)]
42pub struct StyleRule {
43    pub selector: String,
44    pub declarations: HashMap<String, String>,
45}
46
47/// Represents the script section
48#[derive(Debug, Clone)]
49pub struct ScriptNode {
50    pub imports: Vec<String>,
51    pub component_name: String,
52    pub props: Vec<PropDefinition>,
53    pub state: Vec<StateDefinition>,
54    pub methods: Vec<MethodDefinition>,
55    pub lifecycle: Vec<LifecycleHook>,
56}
57
58/// Represents a component property definition
59#[derive(Debug, Clone)]
60pub struct PropDefinition {
61    pub name: String,
62    pub ty: String,
63    pub required: bool,
64    pub default: Option<String>,
65}
66
67/// Represents a state field definition
68#[derive(Debug, Clone)]
69pub struct StateDefinition {
70    pub name: String,
71    pub ty: String,
72    pub initial: Option<String>,
73}
74
75/// Represents a method definition
76#[derive(Debug, Clone)]
77pub struct MethodDefinition {
78    pub name: String,
79    pub args: Vec<(String, String)>, // (name, type)
80    pub return_type: Option<String>,
81    pub body: String,
82}
83
84/// Represents a lifecycle hook
85#[derive(Debug, Clone)]
86pub struct LifecycleHook {
87    pub hook_type: LifecycleType,
88    pub body: String,
89}
90
91/// Types of lifecycle hooks
92#[derive(Debug, Clone, PartialEq)]
93pub enum LifecycleType {
94    Created,
95    Mounted,
96    Updated,
97    Destroyed,
98}
99
100impl OrbitAst {
101    /// Create a new AST from parsed sections
102    pub fn new(template: TemplateNode, style: StyleNode, script: ScriptNode) -> Self {
103        Self {
104            template,
105            style,
106            script,
107        }
108    }
109}