Skip to main content

oak_java/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2//! Java AST definitions
3
4use core::range::Range;
5use serde::{Deserialize, Serialize};
6
7/// Root node of a Java program
8#[derive(Debug, Clone, PartialEq)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub struct JavaRoot {
11    /// Items in the compilation unit
12    pub items: Vec<Item>,
13}
14
15/// Annotation/Attribute
16#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
18pub struct Annotation {
19    /// Annotation name
20    pub name: String,
21    /// Arguments (if any)
22    pub arguments: Vec<Expression>,
23    /// Source span
24    #[serde(with = "oak_core::serde_range")]
25    pub span: Range<usize>,
26}
27
28/// Top-level items in a Java program
29#[derive(Debug, Clone, PartialEq)]
30#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31pub enum Item {
32    /// Class declaration
33    Class(ClassDeclaration),
34    /// Interface declaration
35    Interface(InterfaceDeclaration),
36    /// Struct declaration
37    Struct(StructDeclaration),
38    /// Enum declaration
39    Enum(EnumDeclaration),
40    /// Record declaration
41    Record(RecordDeclaration),
42    /// Package declaration
43    Package(PackageDeclaration),
44    /// Import declaration
45    Import(ImportDeclaration),
46}
47
48/// Class declaration
49#[derive(Debug, Clone, PartialEq)]
50#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
51pub struct ClassDeclaration {
52    /// Class name
53    pub name: String,
54    /// Modifiers
55    pub modifiers: Vec<String>,
56    /// Annotations
57    pub annotations: Vec<Annotation>,
58    /// Superclass
59    pub extends: Option<String>,
60    /// Implemented interfaces
61    pub implements: Vec<String>,
62    /// Members
63    pub members: Vec<Member>,
64    /// Source span
65    #[serde(with = "oak_core::serde_range")]
66    pub span: Range<usize>,
67}
68
69/// Class members
70#[derive(Debug, Clone, PartialEq)]
71#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
72pub enum Member {
73    /// Method declaration
74    Method(MethodDeclaration),
75    /// Field declaration
76    Field(FieldDeclaration),
77    /// Constructor declaration
78    Constructor(ConstructorDeclaration),
79}
80
81/// Constructor declaration
82#[derive(Debug, Clone, PartialEq)]
83#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
84pub struct ConstructorDeclaration {
85    /// Modifiers
86    pub modifiers: Vec<String>,
87    /// Annotations
88    pub annotations: Vec<Annotation>,
89    /// Name (should match class name)
90    pub name: String,
91    /// Parameter list
92    pub parameters: Vec<Parameter>,
93    /// Method body
94    pub body: Vec<Statement>,
95    /// Source span
96    #[serde(with = "oak_core::serde_range")]
97    pub span: Range<usize>,
98}
99
100/// Method declaration
101#[derive(Debug, Clone, PartialEq)]
102#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
103pub struct MethodDeclaration {
104    /// Method name
105    pub name: String,
106    /// Modifiers
107    pub modifiers: Vec<String>,
108    /// Annotations
109    pub annotations: Vec<Annotation>,
110    /// Return type
111    pub return_type: String,
112    /// Parameter list
113    pub parameters: Vec<Parameter>,
114    /// Method body
115    pub body: Vec<Statement>,
116    /// Thrown exceptions
117    pub throws: Vec<String>,
118    /// Whether it is static
119    pub is_static: bool,
120    /// Source span
121    #[serde(with = "oak_core::serde_range")]
122    pub span: Range<usize>,
123}
124
125/// Parameter
126#[derive(Debug, Clone, PartialEq)]
127#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
128pub struct Parameter {
129    /// Parameter name
130    pub name: String,
131    /// Parameter type
132    pub r#type: String,
133}
134
135/// Field declaration.
136#[derive(Debug, Clone, PartialEq)]
137#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
138pub struct FieldDeclaration {
139    /// Field name.
140    pub name: String,
141    /// Field type.
142    pub r#type: String,
143    /// Field modifiers (e.g., public, static).
144    pub modifiers: Vec<String>,
145    /// Annotations
146    pub annotations: Vec<Annotation>,
147    /// The span of the field declaration in the source file.
148    #[serde(with = "oak_core::serde_range")]
149    pub span: Range<usize>,
150}
151
152/// Statement.
153#[derive(Debug, Clone, PartialEq)]
154#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
155pub enum Statement {
156    /// Expression statement.
157    Expression(Expression),
158    /// Return statement.
159    Return(Option<Expression>),
160    /// Block statement.
161    Block(Vec<Statement>),
162    /// Try statement.
163    Try(TryStatement),
164    /// Throw statement.
165    Throw(Expression),
166    /// If statement.
167    If {
168        /// Condition expression.
169        condition: Expression,
170        /// Then branch statement.
171        then_branch: Box<Statement>,
172        /// Optional else branch statement.
173        else_branch: Option<Box<Statement>>,
174    },
175    /// While loop.
176    While {
177        /// Condition expression.
178        condition: Expression,
179        /// Loop body.
180        body: Box<Statement>,
181    },
182    /// Do-while loop.
183    DoWhile {
184        /// Condition expression.
185        condition: Expression,
186        /// Loop body.
187        body: Box<Statement>,
188    },
189    /// For loop.
190    For {
191        /// Initializer statement.
192        init: Option<Box<Statement>>,
193        /// Condition expression.
194        condition: Option<Expression>,
195        /// Update expression.
196        update: Option<Expression>,
197        /// Loop body.
198        body: Box<Statement>,
199    },
200    /// For-each loop.
201    ForEach {
202        /// Item type.
203        item_type: String,
204        /// Item name.
205        item_name: String,
206        /// Iterable expression.
207        iterable: Expression,
208        /// Loop body.
209        body: Box<Statement>,
210    },
211    /// Switch statement.
212    Switch {
213        /// Selector expression.
214        selector: Expression,
215        /// Switch cases.
216        cases: Vec<SwitchCase>,
217        /// Optional default case statements.
218        default: Option<Vec<Statement>>,
219    },
220    /// Break statement.
221    Break,
222    /// Continue statement.
223    Continue,
224    /// Local variable declaration statement.
225    LocalVariable {
226        /// Variable type.
227        r#type: String,
228        /// Variable name.
229        name: String,
230        /// Optional initializer expression.
231        initializer: Option<Expression>,
232    },
233}
234
235/// Switch case.
236#[derive(Debug, Clone, PartialEq)]
237#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
238pub struct SwitchCase {
239    /// Case label expression.
240    pub label: Expression,
241    /// Case body statements.
242    pub body: Vec<Statement>,
243}
244
245/// Try statement.
246#[derive(Debug, Clone, PartialEq)]
247#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
248pub struct TryStatement {
249    /// Try block.
250    pub block: Vec<Statement>,
251    /// Catch clauses.
252    pub catches: Vec<CatchClause>,
253    /// Optional finally block.
254    pub finally: Option<Vec<Statement>>,
255}
256
257/// Catch clause.
258#[derive(Debug, Clone, PartialEq)]
259#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
260pub struct CatchClause {
261    /// Catch parameter.
262    pub parameter: Parameter,
263    /// Catch block.
264    pub block: Vec<Statement>,
265}
266
267/// Expression.
268#[derive(Debug, Clone, PartialEq)]
269#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
270pub enum Expression {
271    /// Literal expression.
272    Literal(Literal),
273    /// Identifier expression (e.g., variable reference).
274    Identifier(String),
275    /// Method call expression.
276    MethodCall(MethodCall),
277    /// Field access expression.
278    FieldAccess(FieldAccess),
279    /// Array access expression.
280    ArrayAccess(ArrayAccess),
281    /// Array creation expression.
282    ArrayCreation(ArrayCreation),
283    /// New expression (object creation).
284    New(NewExpression),
285    /// This expression.
286    This,
287    /// Super expression.
288    Super,
289    /// Binary operation.
290    Binary {
291        /// Left operand.
292        left: Box<Expression>,
293        /// Operator.
294        op: String,
295        /// Right operand.
296        right: Box<Expression>,
297    },
298    /// Unary operation.
299    Unary {
300        /// Operator.
301        op: String,
302        /// Operand expression.
303        expression: Box<Expression>,
304    },
305    /// Assignment operation.
306    Assignment {
307        /// Left operand.
308        left: Box<Expression>,
309        /// Operator.
310        op: String,
311        /// Right operand.
312        right: Box<Expression>,
313    },
314    /// Update operation (increment/decrement).
315    Update {
316        /// Operand expression.
317        expression: Box<Expression>,
318        /// Operator.
319        op: String,
320        /// Whether the operator is a prefix.
321        is_prefix: bool,
322    },
323    /// Ternary operation.
324    Ternary {
325        /// Condition expression.
326        condition: Box<Expression>,
327        /// Then branch expression.
328        then_branch: Box<Expression>,
329        /// Else branch expression.
330        else_branch: Box<Expression>,
331    },
332    /// Cast operation.
333    Cast {
334        /// Target type.
335        target_type: String,
336        /// Operand expression.
337        expression: Box<Expression>,
338    },
339}
340
341/// New expression.
342#[derive(Debug, Clone, PartialEq)]
343#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
344pub struct NewExpression {
345    /// Object type.
346    pub r#type: String,
347    /// Constructor arguments.
348    pub arguments: Vec<Expression>,
349}
350
351/// Literal.
352#[derive(Debug, Clone, PartialEq)]
353#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
354pub enum Literal {
355    /// Integer literal.
356    Integer(i64),
357    /// Float literal.
358    Float(f64),
359    /// String literal.
360    String(String),
361    /// Boolean literal.
362    Boolean(bool),
363}
364
365/// Field access.
366#[derive(Debug, Clone, PartialEq)]
367#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
368pub struct FieldAccess {
369    /// Target expression.
370    pub target: Box<Expression>,
371    /// Field name.
372    pub name: String,
373}
374
375/// Method call.
376#[derive(Debug, Clone, PartialEq)]
377#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
378pub struct MethodCall {
379    /// Call target (optional, e.g., System.out).
380    pub target: Option<Box<Expression>>,
381    /// Method name.
382    pub name: String,
383    /// Arguments.
384    pub arguments: Vec<Expression>,
385}
386
387/// Array access.
388#[derive(Debug, Clone, PartialEq)]
389#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
390pub struct ArrayAccess {
391    /// Target array expression.
392    pub target: Box<Expression>,
393    /// Index expression.
394    pub index: Box<Expression>,
395}
396
397/// Array creation.
398#[derive(Debug, Clone, PartialEq)]
399#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
400pub struct ArrayCreation {
401    /// Element type.
402    pub element_type: String,
403    /// Array dimensions.
404    pub dimensions: Vec<Expression>,
405}
406
407/// Interface declaration.
408#[derive(Debug, Clone, PartialEq)]
409#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
410pub struct InterfaceDeclaration {
411    /// Interface name.
412    pub name: String,
413    /// Modifiers.
414    pub modifiers: Vec<String>,
415    /// Annotations
416    pub annotations: Vec<Annotation>,
417    /// Extended interfaces.
418    pub extends: Vec<String>,
419    /// Interface members.
420    pub members: Vec<Member>,
421    /// The span of the interface declaration in the source file.
422    #[serde(with = "oak_core::serde_range")]
423    pub span: Range<usize>,
424}
425
426/// Struct declaration.
427#[derive(Debug, Clone, PartialEq)]
428#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
429pub struct StructDeclaration {
430    /// Struct name.
431    pub name: String,
432    /// Modifiers.
433    pub modifiers: Vec<String>,
434    /// Annotations
435    pub annotations: Vec<Annotation>,
436    /// Implemented interfaces.
437    pub implements: Vec<String>,
438    /// Struct members.
439    pub members: Vec<Member>,
440    /// The span of the struct declaration in the source file.
441    #[serde(with = "oak_core::serde_range")]
442    pub span: Range<usize>,
443}
444
445/// Enum declaration
446#[derive(Debug, Clone, PartialEq)]
447#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
448pub struct EnumDeclaration {
449    /// Enum name
450    pub name: String,
451    /// Modifiers
452    pub modifiers: Vec<String>,
453    /// Annotations
454    pub annotations: Vec<Annotation>,
455    /// Implemented interfaces
456    pub implements: Vec<String>,
457    /// Enum variants
458    pub variants: Vec<String>,
459    /// Members
460    pub members: Vec<Member>,
461    /// Source span
462    #[serde(with = "oak_core::serde_range")]
463    pub span: Range<usize>,
464}
465
466/// Record declaration.
467#[derive(Debug, Clone, PartialEq)]
468#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
469pub struct RecordDeclaration {
470    /// Record name.
471    pub name: String,
472    /// Modifiers.
473    pub modifiers: Vec<String>,
474    /// Annotations
475    pub annotations: Vec<Annotation>,
476    /// Record parameters (e.g., primary constructor parameters).
477    pub parameters: Vec<Parameter>,
478    /// Implemented interfaces.
479    pub implements: Vec<String>,
480    /// Record members.
481    pub members: Vec<Member>,
482    /// The span of the record declaration in the source file.
483    #[serde(with = "oak_core::serde_range")]
484    pub span: Range<usize>,
485}
486
487/// Package declaration.
488#[derive(Debug, Clone, PartialEq)]
489#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
490pub struct PackageDeclaration {
491    /// Package name.
492    pub name: String,
493    /// The span of the package declaration in the source file.
494    #[serde(with = "oak_core::serde_range")]
495    pub span: Range<usize>,
496}
497
498/// Import declaration.
499#[derive(Debug, Clone, PartialEq)]
500#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
501pub struct ImportDeclaration {
502    /// Import path.
503    pub path: String,
504    /// Whether it's a static import.
505    pub is_static: bool,
506    /// The span of the import declaration in the source file.
507    #[serde(with = "oak_core::serde_range")]
508    pub span: Range<usize>,
509}