tree-sitter-rust Documentation

repository·master·Indexed 19 days ago

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

A high-performance Rust grammar for the tree-sitter incremental parsing engine. It provides a concrete syntax tree (CST) parser for the Rust programming language, supporting fast initial parsing and sub-millisecond incremental updates. Includes Node.js bindings and node type information for programmatic grammar inspection.

Tokens
581
Snippets
3
Records
4
Agent score
18%

What's inside tree-sitter-rust

  1. Performance characteristics of tree-sitter-rust

    master

    The parser is optimized for both initial parsing and incremental updates:

    • Initial Parsing: When first parsing a file, tree-sitter-rust is approximately two to three times slower than rustc's hand-written parser.
    • Incremental Parsing: After an initial parse, subsequent edits to the file can be reflected in the syntax tree in less than a millisecond due to tree-sitter's incremental parsing capabilities.
    # Example comparison of initial parse time
    $ tree-sitter parse examples/ast.rs --quiet --time
    treesitter-rust    6.48 ms        9908 bytes/ms
  2. Import the tree-sitter-rust Node.js bindings

    master

    To use the Rust grammar in a Node.js environment, require the tree-sitter-rust package. The package automatically selects the correct prebuilt binary based on your platform and architecture using node-gyp-build. It also provides support for Bun via static analysis of the .node file path.

    Note: This module exports the compiled grammar object which can be used with the main tree-sitter library to parse Rust code.

    const Parser = require('tree-sitter');
    const Rust = require('tree-sitter-rust');
    
    const parser = new Parser();
    parser.setLanguage(Rust);
  3. Access Rust node type information via nodeTypeInfo

    master

    The tree-sitter-rust bindings export a nodeTypeInfo property which contains metadata about the different node types defined in the Rust grammar. This information is loaded from src/node-types.json and can be used to programmatically inspect the grammar's structure.

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