Skip to main content

oak_java/ast/
mod.rs

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