tree-sitter-java

repository·master·Indexed 42 days ago

https://github.com/tree-sitter/tree-sitter-java

A Java grammar implementation for the tree-sitter incremental parsing library, version 0.23.5. It enables the generation of concrete syntax trees for Java source code to support syntax highlighting, code navigation, and structural analysis.

Tokens
408
Snippets
2
Records
3
Agent score
5%

What's inside tree-sitter-java

  1. Import the tree-sitter-java Node.js bindings

    master

    To use the Java grammar in a Node.js application, require the tree-sitter-java package. The module exports the compiled tree-sitter grammar object, which can be used with the main tree-sitter library to parse Java source code.

    Note: The package includes support for Bun via static analysis for bun build --compile and uses node-gyp-build for standard Node.js environments.

    const Parser = require('tree-sitter');
    const Java = require('tree-sitter-java');
    
    const parser = new Parser();
    parser.setLanguage(Java);
    
    const tree = parser.parse('class Test { int x = 1; }');
  2. Access Java node type information via nodeTypeInfo

    master

    The tree-sitter-java module optionally exports a nodeTypeInfo property. This property contains a JSON object mapping node types to their metadata (defined in src/node-types.json). This is useful for programmatic inspection of the grammar's node structure.

    const Java = require('tree-sitter-java');
    
    if (Java.nodeTypeInfo) {
      console.log(Java.nodeTypeInfo);
    }