Expand description
Java AST Parser — a syn-style API for Java 25.
This crate provides a complete Java Abstract Syntax Tree parser that closely
follows the Java Language Specification (JLS) SE 25. The API design is inspired
by Rust’s [syn] crate.
§Quick Start
use java_lang::ast::CompilationUnit;
use java_lang::parse_str;
let unit: CompilationUnit = parse_str(
"package com.example;\n\npublic class Hello {\n public static void main(String[] args) {\n System.out.println(\"Hello, World!\");\n }\n}\n"
).unwrap();
// Access parsed elements
if let Some(pkg) = &unit.package {
println!("Package: {:?}", pkg.name);
}
for item in &unit.type_decls {
println!("Type: {:?}", item);
}§Design Principles
- Syn-style API: Uses
Parsetrait andParseStreamsimilar to [syn]. - Complete coverage: Supports Java 25 features including records, sealed classes, pattern matching, switch expressions, text blocks, and unnamed variables.
- Span tracking: Every AST node carries span information for error reporting.
- Zero-copy identifiers: Uses
Identwith interning for efficient comparison.
Re-exports§
pub use ast::compilation_unit::CompilationUnit;pub use ast::Comment;pub use ast::CommentKind;
Modules§
Macros§
- format_
ident - Construct an identifier from a string literal.
- opt
- Optionally parse something. Returns
Noneif the next token doesn’t match. - peek
- Peek at the next token and check if it matches.
- peek_
ident - Peek at the next token and check if it is an identifier.
Structs§
- Error
- The error type for Java parsing.
- Ident
- A Java identifier.
- Parse
Stream - The parsing cursor, similar to syn’s
ParseBuffer. - Span
- A span of source code, represented as a range of byte offsets.
- Token
- A token produced by the lexer.
Enums§
- Token
Kind - The kind of token.
Traits§
- Parse
- A trait for types that can be parsed from a
ParseStream.
Functions§
- parse
- Parse a string into a value of type
Twithout checking for trailing tokens. - parse_
file - Parse a Java source file into a value of type
T. - parse_
str - Parse a string into a value of type
Tthat implementsParse.
Type Aliases§
- Result
- A specialized
Resulttype for Java parsing.