Skip to main content

Crate java_lang

Crate java_lang 

Source
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 Parse trait and ParseStream similar 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 Ident with interning for efficient comparison.

Re-exports§

pub use ast::compilation_unit::CompilationUnit;
pub use ast::Comment;
pub use ast::CommentKind;

Modules§

ast

Macros§

format_ident
Construct an identifier from a string literal.
opt
Optionally parse something. Returns None if 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.
ParseStream
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§

TokenKind
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 T without 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 T that implements Parse.

Type Aliases§

Result
A specialized Result type for Java parsing.