treesitter-types-java 0.1.1

Pre-generated strongly-typed AST types for Java (tree-sitter-java)
Documentation

Strongly-typed AST types for Java, auto-generated from tree-sitter-java's node-types.json.

This crate is generated by treesitter-types and is automatically kept up to date when a new version of the grammar crate is released.

These types have been tested by parsing the Spring Framework source code.

See the Tree-sitter project for more information about the underlying parser framework.

Example

use treesitter_types_java::*;

// A minimal Java hello-world program.
let src = b"\
class Hello {
    public static void main(String[] args) {
        System.out.println(\"Hello, World!\");
    }
}
";

// Parse the source with tree-sitter and convert into typed AST.
let mut parser = tree_sitter::Parser::new();
parser.set_language(&tree_sitter_java::LANGUAGE.into()).unwrap();
let tree = parser.parse(src, None).unwrap();
let program = Program::from_node(tree.root_node(), src).unwrap();

// The program has one top-level child: the `Hello` class.
assert_eq!(program.children.len(), 1);

// Unwrap the class declaration.
let ProgramChildren::Statement(stmt) = &program.children[0] else {
    panic!("expected a statement");
};
let Statement::Declaration(decl) = stmt.as_ref() else {
    panic!("expected a declaration");
};
let Declaration::ClassDeclaration(class) = decl.as_ref() else {
    panic!("expected a class declaration");
};
assert_eq!(class.name.text(), "Hello");
assert!(class.superclass.is_none());   // no `extends`
assert!(class.interfaces.is_none());   // no `implements`

// The class body contains one method.
assert_eq!(class.body.children.len(), 1);