tree-sitter-java

A fast, incremental Java grammar for the tree-sitter parsing library

Library
Cargo
v0.23.5
275stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
44/100Fair
Development Activity0
Maintenance20
Community76
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
71/100Good
Architecture76
Code Quality73
Innovation70
Learning Curve65

tree-sitter-java is the official Java language grammar for tree-sitter, the incremental parsing library used by editors and developer tools to build concrete syntax trees. It defines Java’s syntax (classes, interfaces, generics, annotations, records) in a declarative grammar that compiles to a generated C parser plus bindings for Rust, Node.js, Python, Go, and Swift.

Editors, linters, and static-analysis tools consume this grammar to parse Java source incrementally and error-tolerantly, enabling syntax highlighting, code folding, and structural navigation without a full javac front-end.

What You Get

  • A complete, actively maintained Java grammar covering modern syntax including records, sealed classes, and annotations
  • A generated, dependency-free C parser (parser.c) compiled from the grammar for use in any host language
  • Official bindings for Rust, Go, Node.js, Python, and Swift for embedding the grammar directly in tooling
  • Highlight and tag queries (queries/highlights.scm, queries/tags.scm) mapping syntax nodes to editor categories
  • A corpus of test fixtures (test/corpus, test/highlight) validating grammar correctness against real Java code

Common Use Cases

  • Editor syntax highlighting - power tree-sitter-based highlighting for Java files in editors like Neovim, Helix, and Zed
  • Code navigation tooling - build ‘jump to definition’ and structural outlines by querying the parsed Java syntax tree
  • Static analysis and linters - traverse the concrete syntax tree to implement custom Java lint rules without invoking javac
  • Language server implementations - use the grammar as the incremental parsing layer underneath a Java LSP server

Under The Hood

Architecture - The grammar is authored in grammar.js as a JavaScript DSL describing Java’s syntax rules (declarations, statements, expressions, generics, annotations). The tree-sitter CLI compiles this into a generated, table-driven src/parser.c, with src/node-types.json describing the resulting AST node schema.

Tech Stack - The grammar source is JavaScript, but the shipped artifact is generated C code with zero runtime dependencies beyond tree-sitter-language; the repo ships first-class bindings for Rust (Cargo.toml), Go (go.mod), Node.js (package.json, binding.gyp), Python (pyproject.toml, setup.py), and Swift (Package.swift).

Code Quality - Correctness is validated through test/corpus (parse-tree fixtures) and a separate test/highlight suite exercising the highlight queries; CI runs across the various language bindings before each Cargo/npm/PyPI release, keeping the grammar current as Java language features evolve.

API Design - Consumers interact through a minimal surface: a single exported language constant handed to the generic tree-sitter runtime API, so there’s effectively no Java-specific API to learn beyond tree-sitter’s own parser/query interface, keeping integration overhead low.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search