Expand description
AST module.
§Java Abstract Syntax Tree (AST) Module
This module defines the Abstract Syntax Tree (AST) structure for the Java programming language. It provides a strongly-typed and comprehensive representation of Java source code, strictly adhering to the Java Language Specification (JLS).
§Purpose
The Java AST is the central data structure used by the compiler, static analysis tools, and IDE features to represent the semantic and structural information of a Java program. It captures the full range of Java constructs, from package declarations to complex expression trees.
§AST Node Types
§Core Structure
JavaRoot: The root node representing a complete Java compilation unit.PackageDeclaration: Represents thepackagestatement.ImportDeclaration: Representsimportandimport staticstatements.
§Type Declarations
ClassDeclaration: Class definition including modifiers, type parameters, superclass, interfaces, and body.InterfaceDeclaration: Interface definition.EnumDeclaration: Enum definition with its constants and members.RecordDeclaration: Java 14+ record definition.AnnotationDeclaration: Annotation type definition.
§Members and Elements
MethodDeclaration: Method definition with its signature and body.FieldDeclaration: Field definition with modifiers and initializers.ConstructorDeclaration: Class constructor definition.EnumConstant: Individual constant in an enum.Annotation: Represents an annotation usage (e.g.,↯Override).
§Statements and Expressions
Statement: Represents various Java statements (local variables, blocks, control flow, etc.).Expression: The base type for all Java expressions (literals, assignments, calls, lambda expressions, etc.).Block: A sequence of statements enclosed in braces.TryStatement: Try-catch-finally construct, including try-with-resources.
§Types and Parameters
Type: Representation of Java types (primitive, reference, array, wildcard, etc.).Parameter: Method or constructor parameter.TypeParameter: Generic type parameter definition.
§Usage Example
use oak_java::ast::*;
fn main() {
// Manually constructing a simple AST for a Java class
let java_ast = JavaRoot {
package: Some(PackageDeclaration { name: "com.example".to_string() }),
imports: vec![],
types: vec![
TypeDeclaration::Class(ClassDeclaration {
modifiers: vec![Modifier::Public],
name: "Main".to_string(),
members: vec![
ClassMember::Method(MethodDeclaration {
modifiers: vec![Modifier::Public, Modifier::Static],
return_type: Type::Void,
name: "main".to_string(),
params: vec![
Parameter {
ty: Type::Array(Box::new(Type::Reference("String".to_string()))),
name: "args".to_string(),
}
],
body: Some(Block {
statements: vec![
Statement::Expression(Expression::MethodCall(MethodCall {
target: Some(Box::new(Expression::Identifier("System.out".to_string()))),
name: "println".to_string(),
arguments: vec![Expression::StringLiteral("Hello, Java!".to_string())],
}))
],
}),
})
],
})
],
};
}§Design Principles
- JLS Fidelity: Accurately reflects the structural rules of the Java Language Specification.
- Type Safety: Uses Rust’s rich type system to ensure AST validity and integrity.
- Rich Metadata: Each node includes span information for precise source mapping.
- Extensibility: Designed to be easily extended with semantic information during compilation phases. Java AST definitions
Structs§
- Array
Access - 数组访问
- Array
Creation - 数组创建
- Catch
Clause - Catch 子句
- Class
Declaration - 类声明
- Constructor
Declaration - 构造函数声明
- Enum
Declaration - 枚举声明
- Field
Access - 字段访问
- Field
Declaration - 字段声明
- Import
Declaration - 导入声明
- Interface
Declaration - 接口声明
- Java
Root - Java 程序的根节点
- Method
Call - 方法调用
- Method
Declaration - 方法声明
- NewExpression
- New 表达式
- Package
Declaration - 包声明
- Parameter
- 参数
- Record
Declaration - 记录声明
- Struct
Declaration - 结构体声明
- Switch
Case - Switch 分支
- TryStatement
- Try 语句
Enums§
- Expression
- 表达式
- Item
- Java 程序中的顶级项目
- Literal
- 字面量
- Member
- 类成员
- Statement
- 语句